1 19 20 package org.netbeans.modules.apisupport.beanbrowser; 21 22 import java.awt.event.ActionEvent ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.util.ArrayList ; 25 import java.util.Date ; 26 import java.util.List ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import java.util.prefs.BackingStoreException ; 30 import java.util.prefs.NodeChangeEvent ; 31 import java.util.prefs.NodeChangeListener ; 32 import java.util.prefs.PreferenceChangeEvent ; 33 import java.util.prefs.PreferenceChangeListener ; 34 import java.util.prefs.Preferences ; 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 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 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 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 getPreferences() { 104 return ((PreferencesChildren)getChildren()).getPreferences(); 105 } 106 107 private void populatePropertiesSet(final Sheet sheet) { 108 Sheet.Set set = getPropertySet(sheet); 109 final String [] 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 ex) { 116 Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode"). 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 pL = new PreferenceChangeListener () { 128 public void preferenceChange(PreferenceChangeEvent evt) { 129 if (!evt.getNode().equals(getPreferences())) { 130 return; 131 } 132 Property p = set2.get(evt.getKey()); 133 Object 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 ex) { 143 Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode"). log(Level.WARNING, null, ex); 145 } catch (InvocationTargetException ex) { 146 Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode"). log(Level.WARNING, null, ex); 148 } catch (IllegalAccessException ex) { 149 Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode"). 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 key) { 161 super(key, String .class, key, key); 162 } 163 164 public Object getValue() throws IllegalAccessException , InvocationTargetException { 165 return getPreferences().get(getName(), ""); } 167 168 public void setValue(Object val) throws IllegalAccessException , IllegalArgumentException , 169 InvocationTargetException { 170 Object oldValue = getValue(); 171 if (oldValue.equals(val)) return; 172 getPreferences().put(getName(), val.toString()); firePropertyChange(getName(), oldValue,val); 174 } 175 } 176 177 private static class PreferencesChildren extends Children.Keys { 178 private final String absolutePath; 179 private int type; 180 PreferencesChildren(int type, final String absolutePath) { 181 super(); 182 this.type = type; 183 this.absolutePath = absolutePath; 184 } 185 186 protected void addNotify() { 187 refreshKeys(); 188 NodeChangeListener nL = new NodeChangeListener () { 189 public void childAdded(NodeChangeEvent evt) { 190 if (!evt.getParent().equals(getPreferences())) { 191 return; 192 } 193 refreshKeys(); 194 } 195 public void childRemoved(NodeChangeEvent evt) { 196 childAdded(evt); 197 } 198 }; 199 getPreferences().addNodeChangeListener(nL); 200 } 201 202 private void refreshKeys() { 203 Preferences parent = getPreferences(); 204 try { 205 String [] names = parent.childrenNames(); 206 List children = new ArrayList (); 207 for (int i = 0; i < names.length; i++) { 208 children.add(parent.node(names[i]).absolutePath()); 209 } 210 setKeys(children); 211 } catch (BackingStoreException ex) { 212 setKeys(new String [0]); 213 } 214 } 215 216 217 protected Node[] createNodes(Object key) { 218 assert key != null; 219 if (key instanceof String ) { 220 return new Node[] {new PreferencesNodeImpl(type,(String )key)}; 221 } else { 222 throw new IllegalStateException (key.getClass().getName()); 223 } 224 } 225 226 protected Preferences getPreferences() { 227 Preferences 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 |