2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.apisupport.beanbrowser;
44
import java.awt.event.ActionEvent;
45
import java.lang.reflect.InvocationTargetException;
46
import java.util.ArrayList;
47
import java.util.Date;
48
import java.util.List;
49
import java.util.logging.Level;
50
import java.util.logging.Logger;
51
import java.util.prefs.BackingStoreException;
52
import java.util.prefs.NodeChangeEvent;
53
import java.util.prefs.NodeChangeListener;
54
import java.util.prefs.PreferenceChangeEvent;
55
import java.util.prefs.PreferenceChangeListener;
56
import java.util.prefs.Preferences;
57
import org.openide.nodes.AbstractNode;
58
import org.openide.nodes.Children;
59
import org.openide.nodes.Node;
60
import org.openide.nodes.Node.Property;
61
import org.openide.nodes.PropertySupport;
62
import org.openide.nodes.Sheet;
63
import org.openide.util.HelpCtx;
64
import org.openide.util.NbPreferences;
65
import org.openide.util.actions.SystemAction;
69
* @author Radek Matous
71
public class PreferencesNode extends AbstractNode {
72
private static final int NB_USER = 0;
73
private static final int DEFAULT_USER = 1;
74
private static final int DEFAULT_SYSTEM = 2;
76
public PreferencesNode() {
77
super(new RootChildren());
78
setDisplayName("Preferences");
81
private static class RootChildren extends Children.Array {
86
protected void addNotify() {
88
new PreferencesNodeImpl(PreferencesNode.NB_USER),
89
new PreferencesNodeImpl(PreferencesNode.DEFAULT_USER),
90
new PreferencesNodeImpl(PreferencesNode.DEFAULT_SYSTEM)
95
private static class PreferencesNodeImpl extends AbstractNode {
96
/** Creates a new instance of PreferencesNode */
97
public PreferencesNodeImpl(int type) {
100
case PreferencesNode.NB_USER:
101
setDisplayName("NetBeans Preferences");
103
case PreferencesNode.DEFAULT_USER:
104
setDisplayName("User Preferences");
106
case PreferencesNode.DEFAULT_SYSTEM:
107
setDisplayName("System Preferences");
114
private PreferencesNodeImpl(int type, String absolutePath) {
115
super(new PreferencesChildren(type, absolutePath));
116
setDisplayName(getPreferences().name());
119
protected Sheet createSheet() {
120
Sheet sh = super.createSheet();
121
populatePropertiesSet(sh);
125
private Preferences getPreferences() {
126
return ((PreferencesChildren)getChildren()).getPreferences();
129
private void populatePropertiesSet(final Sheet sheet) {
130
Sheet.Set set = getPropertySet(sheet);
133
keys = getPreferences().keys();
134
for (int i = 0; i < keys.length; i++) {
135
set.put(new PreferencesProperty(keys[i]));
137
} catch (BackingStoreException ex) {
138
Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
139
log(Level.WARNING, null, ex);
143
private Sheet.Set getPropertySet(final Sheet sheet) {
144
Sheet.Set set = sheet.get(Sheet.PROPERTIES);
146
final Sheet.Set set2 = Sheet.createPropertiesSet();
149
PreferenceChangeListener pL = new PreferenceChangeListener() {
150
public void preferenceChange(PreferenceChangeEvent evt) {
151
if (!evt.getNode().equals(getPreferences())) {
154
Property p = set2.get(evt.getKey());
155
Object newValue = evt.getNewValue();
157
set2.put(new PreferencesProperty(evt.getKey()));
158
} else if (newValue == null) {
159
set2.remove(evt.getKey());
162
if (p.getValue().equals(evt.getNewValue())) return;
163
p.setValue(evt.getNewValue());
164
} catch (IllegalArgumentException ex) {
165
Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
166
log(Level.WARNING, null, ex);
167
} catch (InvocationTargetException ex) {
168
Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
169
log(Level.WARNING, null, ex);
170
} catch (IllegalAccessException ex) {
171
Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
172
log(Level.WARNING, null, ex); }
176
getPreferences().addPreferenceChangeListener(pL);
181
private class PreferencesProperty extends PropertySupport.ReadWrite {
182
PreferencesProperty(String key) {
183
super(key, String.class, key, key);
186
public Object getValue() throws IllegalAccessException, InvocationTargetException {
187
return getPreferences().get(getName(), "");//NOI18N;
190
public void setValue(Object val) throws IllegalAccessException, IllegalArgumentException,
191
InvocationTargetException {
192
Object oldValue = getValue();
193
if (oldValue.equals(val)) return;
194
getPreferences().put(getName(), val.toString());//NOI18N
195
firePropertyChange(getName(), oldValue,val);
199
private static class PreferencesChildren extends Children.Keys {
200
private final String absolutePath;
202
PreferencesChildren(int type, final String absolutePath) {
205
this.absolutePath = absolutePath;
208
protected void addNotify() {
210
NodeChangeListener nL = new NodeChangeListener() {
211
public void childAdded(NodeChangeEvent evt) {
212
if (!evt.getParent().equals(getPreferences())) {
217
public void childRemoved(NodeChangeEvent evt) {
221
getPreferences().addNodeChangeListener(nL);
224
private void refreshKeys() {
225
Preferences parent = getPreferences();
227
String[] names = parent.childrenNames();
228
List children = new ArrayList();
229
for (int i = 0; i < names.length; i++) {
230
children.add(parent.node(names[i]).absolutePath());
233
} catch (BackingStoreException ex) {
234
setKeys(new String[0]);
239
protected Node[] createNodes(Object key) {
241
if (key instanceof String) {
242
return new Node[] {new PreferencesNodeImpl(type,(String)key)};
244
throw new IllegalStateException(key.getClass().getName());
248
protected Preferences getPreferences() {
249
Preferences root = null;
251
case PreferencesNode.NB_USER:
252
root = NbPreferences.root();
254
case PreferencesNode.DEFAULT_USER:
255
root = Preferences.userRoot();
257
case PreferencesNode.DEFAULT_SYSTEM:
258
root = Preferences.systemRoot();
263
return root.node(absolutePath);