KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > beanbrowser > PreferencesNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.beanbrowser;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.prefs.BackingStoreException JavaDoc;
30 import java.util.prefs.NodeChangeEvent JavaDoc;
31 import java.util.prefs.NodeChangeListener JavaDoc;
32 import java.util.prefs.PreferenceChangeEvent JavaDoc;
33 import java.util.prefs.PreferenceChangeListener JavaDoc;
34 import java.util.prefs.Preferences JavaDoc;
35 import org.openide.nodes.AbstractNode;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.nodes.Node.Property;
39 import org.openide.nodes.PropertySupport;
40 import org.openide.nodes.Sheet;
41 import org.openide.util.HelpCtx;
42 import org.openide.util.NbPreferences;
43 import org.openide.util.actions.SystemAction;
44
45 /**
46  *
47  * @author Radek Matous
48  */

49 public class PreferencesNode extends AbstractNode {
50     private static final int NB_USER = 0;
51     private static final int DEFAULT_USER = 1;
52     private static final int DEFAULT_SYSTEM = 2;
53     
54     public PreferencesNode() {
55         super(new RootChildren());
56         setDisplayName("Preferences");
57     }
58     
59     private static class RootChildren extends Children.Array {
60         RootChildren() {
61             super();
62         }
63         
64         protected void addNotify() {
65             add(new Node[] {
66                 new PreferencesNodeImpl(PreferencesNode.NB_USER),
67                 new PreferencesNodeImpl(PreferencesNode.DEFAULT_USER),
68                 new PreferencesNodeImpl(PreferencesNode.DEFAULT_SYSTEM)
69             });
70         }
71     }
72     
73     private static class PreferencesNodeImpl extends AbstractNode {
74         /** Creates a new instance of PreferencesNode */
75         public PreferencesNodeImpl(int type) {
76             this(type, "/");
77             switch(type) {
78                 case PreferencesNode.NB_USER:
79                     setDisplayName("NetBeans Preferences");
80                     break;
81                 case PreferencesNode.DEFAULT_USER:
82                     setDisplayName("User Preferences");
83                     break;
84                 case PreferencesNode.DEFAULT_SYSTEM:
85                     setDisplayName("System Preferences");
86                     break;
87                 default:
88                     assert false;
89             }
90         }
91         
92         private PreferencesNodeImpl(int type, String JavaDoc absolutePath) {
93             super(new PreferencesChildren(type, absolutePath));
94             setDisplayName(getPreferences().name());
95         }
96         
97         protected Sheet createSheet() {
98             Sheet sh = super.createSheet();
99             populatePropertiesSet(sh);
100             return sh;
101         }
102         
103         private Preferences JavaDoc getPreferences() {
104             return ((PreferencesChildren)getChildren()).getPreferences();
105         }
106         
107         private void populatePropertiesSet(final Sheet sheet) {
108             Sheet.Set set = getPropertySet(sheet);
109             final String JavaDoc[] keys;
110             try {
111                 keys = getPreferences().keys();
112                 for (int i = 0; i < keys.length; i++) {
113                     set.put(new PreferencesProperty(keys[i]));
114                 }
115             } catch (BackingStoreException JavaDoc ex) {
116                 Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
117
log(Level.WARNING, null, ex);
118             }
119         }
120         
121         private Sheet.Set getPropertySet(final Sheet sheet) {
122             Sheet.Set set = sheet.get(Sheet.PROPERTIES);
123             if (set == null) {
124                 final Sheet.Set set2 = Sheet.createPropertiesSet();
125                 set = set2;
126                 sheet.put(set);
127                 PreferenceChangeListener JavaDoc pL = new PreferenceChangeListener JavaDoc() {
128                     public void preferenceChange(PreferenceChangeEvent JavaDoc evt) {
129                         if (!evt.getNode().equals(getPreferences())) {
130                             return;
131                         }
132                         Property p = set2.get(evt.getKey());
133                         Object JavaDoc newValue = evt.getNewValue();
134                         if (p == null) {
135                             set2.put(new PreferencesProperty(evt.getKey()));
136                         } else if (newValue == null) {
137                             set2.remove(evt.getKey());
138                         } else {
139                             try {
140                                 if (p.getValue().equals(evt.getNewValue())) return;
141                                 p.setValue(evt.getNewValue());
142                             } catch (IllegalArgumentException JavaDoc ex) {
143                                 Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
144
log(Level.WARNING, null, ex);
145                             } catch (InvocationTargetException JavaDoc ex) {
146                                 Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
147
log(Level.WARNING, null, ex);
148                             } catch (IllegalAccessException JavaDoc ex) {
149                                 Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
150
log(Level.WARNING, null, ex); }
151                         }
152                     }
153                 };
154                 getPreferences().addPreferenceChangeListener(pL);
155             }
156             return set;
157         }
158         
159         private class PreferencesProperty extends PropertySupport.ReadWrite {
160             PreferencesProperty(String JavaDoc key) {
161                 super(key, String JavaDoc.class, key, key);
162             }
163             
164             public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
165                 return getPreferences().get(getName(), "");//NOI18N;
166
}
167             
168             public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc,
169                     InvocationTargetException JavaDoc {
170                 Object JavaDoc oldValue = getValue();
171                 if (oldValue.equals(val)) return;
172                 getPreferences().put(getName(), val.toString());//NOI18N
173
firePropertyChange(getName(), oldValue,val);
174             }
175         }
176         
177         private static class PreferencesChildren extends Children.Keys {
178             private final String JavaDoc absolutePath;
179             private int type;
180             PreferencesChildren(int type, final String JavaDoc absolutePath) {
181                 super();
182                 this.type = type;
183                 this.absolutePath = absolutePath;
184             }
185             
186             protected void addNotify() {
187                 refreshKeys();
188                 NodeChangeListener JavaDoc nL = new NodeChangeListener JavaDoc() {
189                     public void childAdded(NodeChangeEvent JavaDoc evt) {
190                         if (!evt.getParent().equals(getPreferences())) {
191                             return;
192                         }
193                         refreshKeys();
194                     }
195                     public void childRemoved(NodeChangeEvent JavaDoc evt) {
196                         childAdded(evt);
197                     }
198                 };
199                 getPreferences().addNodeChangeListener(nL);
200             }
201             
202             private void refreshKeys() {
203                 Preferences JavaDoc parent = getPreferences();
204                 try {
205                     String JavaDoc[] names = parent.childrenNames();
206                     List JavaDoc children = new ArrayList JavaDoc();
207                     for (int i = 0; i < names.length; i++) {
208                         children.add(parent.node(names[i]).absolutePath());
209                     }
210                     setKeys(children);
211                 } catch (BackingStoreException JavaDoc ex) {
212                     setKeys(new String JavaDoc[0]);
213                 }
214             }
215             
216             
217             protected Node[] createNodes(Object JavaDoc key) {
218                 assert key != null;
219                 if (key instanceof String JavaDoc) {
220                     return new Node[] {new PreferencesNodeImpl(type,(String JavaDoc)key)};
221                 } else {
222                     throw new IllegalStateException JavaDoc(key.getClass().getName());
223                 }
224             }
225             
226             protected Preferences JavaDoc getPreferences() {
227                 Preferences JavaDoc root = null;
228                 switch(type) {
229                     case PreferencesNode.NB_USER:
230                         root = NbPreferences.root();
231                         break;
232                     case PreferencesNode.DEFAULT_USER:
233                         root = Preferences.userRoot();
234                         break;
235                     case PreferencesNode.DEFAULT_SYSTEM:
236                         root = Preferences.systemRoot();
237                         break;
238                     default:
239                         assert false;
240                 }
241                 return root.node(absolutePath);
242             }
243         }
244     }
245 }
246
Popular Tags