~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to apisupport/beanbrowser/src/org/netbeans/modules/apisupport/beanbrowser/PreferencesNode.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.apisupport.beanbrowser;
 
43
 
 
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;
 
66
 
 
67
/**
 
68
 *
 
69
 * @author Radek Matous
 
70
 */
 
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;
 
75
    
 
76
    public PreferencesNode() {
 
77
        super(new RootChildren());
 
78
        setDisplayName("Preferences");
 
79
    }
 
80
    
 
81
    private static class RootChildren extends Children.Array {
 
82
        RootChildren() {
 
83
            super();
 
84
        }
 
85
        
 
86
        protected void addNotify() {
 
87
            add(new Node[] {
 
88
                new PreferencesNodeImpl(PreferencesNode.NB_USER),
 
89
                new PreferencesNodeImpl(PreferencesNode.DEFAULT_USER),
 
90
                new PreferencesNodeImpl(PreferencesNode.DEFAULT_SYSTEM)
 
91
            });
 
92
        }
 
93
    }
 
94
    
 
95
    private static class PreferencesNodeImpl extends AbstractNode {
 
96
        /** Creates a new instance of PreferencesNode */
 
97
        public PreferencesNodeImpl(int type) {
 
98
            this(type, "/");
 
99
            switch(type) {
 
100
                case PreferencesNode.NB_USER:
 
101
                    setDisplayName("NetBeans Preferences");
 
102
                    break;
 
103
                case PreferencesNode.DEFAULT_USER:
 
104
                    setDisplayName("User Preferences");
 
105
                    break;
 
106
                case PreferencesNode.DEFAULT_SYSTEM:
 
107
                    setDisplayName("System Preferences");
 
108
                    break;
 
109
                default:
 
110
                    assert false;
 
111
            }
 
112
        }
 
113
        
 
114
        private  PreferencesNodeImpl(int type, String absolutePath) {
 
115
            super(new PreferencesChildren(type, absolutePath));
 
116
            setDisplayName(getPreferences().name());
 
117
        }
 
118
        
 
119
        protected Sheet createSheet() {
 
120
            Sheet sh = super.createSheet();
 
121
            populatePropertiesSet(sh);
 
122
            return sh;
 
123
        }
 
124
        
 
125
        private Preferences getPreferences() {
 
126
            return ((PreferencesChildren)getChildren()).getPreferences();
 
127
        }
 
128
        
 
129
        private void populatePropertiesSet(final Sheet sheet) {
 
130
            Sheet.Set set = getPropertySet(sheet);
 
131
            final String[] keys;
 
132
            try {
 
133
                keys = getPreferences().keys();
 
134
                for (int i = 0; i < keys.length; i++) {
 
135
                    set.put(new PreferencesProperty(keys[i]));
 
136
                }
 
137
            } catch (BackingStoreException ex) {
 
138
                Logger.getLogger("org.netbeans.modules.apisupport.beanbrowser.PreferencesNode").//NOI18N
 
139
                        log(Level.WARNING, null, ex);
 
140
            }
 
141
        }
 
142
        
 
143
        private Sheet.Set getPropertySet(final Sheet sheet) {
 
144
            Sheet.Set set = sheet.get(Sheet.PROPERTIES);
 
145
            if (set == null) {
 
146
                final Sheet.Set set2 = Sheet.createPropertiesSet();
 
147
                set = set2;
 
148
                sheet.put(set);
 
149
                PreferenceChangeListener pL = new PreferenceChangeListener() {
 
150
                    public void preferenceChange(PreferenceChangeEvent evt) {
 
151
                        if (!evt.getNode().equals(getPreferences())) {
 
152
                            return;
 
153
                        }
 
154
                        Property p = set2.get(evt.getKey());
 
155
                        Object newValue = evt.getNewValue();
 
156
                        if (p == null) {
 
157
                            set2.put(new PreferencesProperty(evt.getKey()));
 
158
                        } else if (newValue == null) {
 
159
                            set2.remove(evt.getKey());
 
160
                        } else {
 
161
                            try {
 
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);                        }
 
173
                        }
 
174
                    }
 
175
                };
 
176
                getPreferences().addPreferenceChangeListener(pL);
 
177
            }
 
178
            return set;
 
179
        }
 
180
        
 
181
        private class PreferencesProperty extends PropertySupport.ReadWrite  {
 
182
            PreferencesProperty(String key) {
 
183
                super(key, String.class, key, key);
 
184
            }
 
185
            
 
186
            public Object getValue() throws IllegalAccessException, InvocationTargetException {
 
187
                return getPreferences().get(getName(), "");//NOI18N;
 
188
            }
 
189
            
 
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);
 
196
            }
 
197
        }
 
198
        
 
199
        private static class PreferencesChildren extends Children.Keys {
 
200
            private final String absolutePath;
 
201
            private int type;
 
202
            PreferencesChildren(int type, final String absolutePath) {
 
203
                super();
 
204
                this.type = type;
 
205
                this.absolutePath = absolutePath;
 
206
            }
 
207
            
 
208
            protected void addNotify() {
 
209
                refreshKeys();
 
210
                NodeChangeListener nL = new NodeChangeListener() {
 
211
                    public void childAdded(NodeChangeEvent evt) {
 
212
                        if (!evt.getParent().equals(getPreferences())) {
 
213
                            return;
 
214
                        }
 
215
                        refreshKeys();
 
216
                    }
 
217
                    public void childRemoved(NodeChangeEvent evt) {
 
218
                        childAdded(evt);
 
219
                    }
 
220
                };
 
221
                getPreferences().addNodeChangeListener(nL);
 
222
            }
 
223
            
 
224
            private void refreshKeys() {
 
225
                Preferences parent = getPreferences();
 
226
                try {
 
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());
 
231
                    }
 
232
                    setKeys(children);
 
233
                } catch (BackingStoreException ex) {
 
234
                    setKeys(new String[0]);
 
235
                }
 
236
            }
 
237
            
 
238
            
 
239
            protected Node[] createNodes(Object key) {
 
240
                assert key != null;
 
241
                if (key instanceof String) {
 
242
                    return new Node[] {new PreferencesNodeImpl(type,(String)key)};
 
243
                } else {
 
244
                    throw new IllegalStateException(key.getClass().getName());
 
245
                }
 
246
            }
 
247
            
 
248
            protected Preferences getPreferences() {
 
249
                Preferences root = null;
 
250
                switch(type) {
 
251
                    case PreferencesNode.NB_USER:
 
252
                        root = NbPreferences.root();
 
253
                        break;
 
254
                    case PreferencesNode.DEFAULT_USER:
 
255
                        root = Preferences.userRoot();
 
256
                        break;
 
257
                    case PreferencesNode.DEFAULT_SYSTEM:
 
258
                        root = Preferences.systemRoot();
 
259
                        break;
 
260
                    default:
 
261
                        assert false;
 
262
                }
 
263
                return root.node(absolutePath);
 
264
            }
 
265
        }
 
266
    }
 
267
}