KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > OptionSupport


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.editor.options;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.netbeans.editor.Settings;
25 import org.netbeans.modules.editor.NbEditorUtilities;
26 import org.openide.options.SystemOption;
27 import org.openide.util.NbBundle;
28 import org.netbeans.modules.editor.NbEditorSettingsInitializer;
29
30 /**
31 * Options for the base editor kit
32 *
33 * @author Miloslav Metelka
34 * @version 1.00
35 */

36 public class OptionSupport extends SystemOption {
37
38     static final long serialVersionUID = 2002899758839584077L;
39
40     static final String JavaDoc OPTIONS_PREFIX = "OPTIONS_"; // NOI18N
41

42     private Class JavaDoc kitClass;
43
44     private String JavaDoc typeName;
45
46     
47     private HashMap JavaDoc initializerValuesMap;
48     
49     private transient SettingsInitializer settingsInitializer;
50     
51     private static final HashMap JavaDoc kitClass2Type = new HashMap JavaDoc();
52     
53
54
55     /** Construct new option support. The pair [kitClass, typeName]
56     * is put into a map so it's possible to find a typeName when kitClass is known
57     * through <tt>getTypeName()</tt> static method.
58     * @param kitClass class of the editorr kit for which this support is constructed.
59     * @param typeName name
60     */

61     public OptionSupport(Class JavaDoc kitClass, String JavaDoc typeName) {
62         this.kitClass = kitClass;
63         this.typeName = typeName;
64         initializerValuesMap = new HashMap JavaDoc();
65         kitClass2Type.put(kitClass, typeName);
66         
67         // Hook up the settings initializer
68
Settings.Initializer si = getSettingsInitializer();
69         Settings.removeInitializer(si.getName());
70         Settings.addInitializer(si, Settings.OPTION_LEVEL);
71         Settings.reset();
72     }
73
74     public Class JavaDoc getKitClass() {
75         return kitClass;
76     }
77
78     public String JavaDoc getTypeName() {
79         return typeName;
80     }
81     
82     public static String JavaDoc getTypeName(Class JavaDoc kitClass) {
83         return (String JavaDoc)kitClass2Type.get(kitClass);
84     }
85
86     public String JavaDoc displayName() {
87         return getString(OPTIONS_PREFIX + typeName);
88     }
89
90     Settings.KitAndValue[] getSettingValueHierarchy(String JavaDoc settingName) {
91         return Settings.getValueHierarchy(kitClass, settingName);
92     }
93
94     /** Get the value of the setting from the <code>Settings</code>
95      * @param settingName name of the setting to get.
96      */

97     public Object JavaDoc getSettingValue(String JavaDoc settingName) {
98         NbEditorSettingsInitializer.init();
99         return Settings.getValue(kitClass, settingName);
100     }
101
102     /** Get the value of the boolean setting from the <code>Settings</code>
103      * @param settingName name of the setting to get.
104      */

105     protected final boolean getSettingBoolean(String JavaDoc settingName) {
106         Boolean JavaDoc val = (Boolean JavaDoc)getSettingValue(settingName);
107         return (val != null) ? val.booleanValue() : false;
108     }
109
110     /** Get the value of the integer setting from the <code>Settings</code>
111      * @param settingName name of the setting to get.
112      */

113     protected final int getSettingInteger(String JavaDoc settingName) {
114         Integer JavaDoc val = (Integer JavaDoc)getSettingValue(settingName);
115         return (val != null) ? val.intValue() : 0;
116     }
117
118     /** Can be used when the settingName is the same as the propertyName */
119     public void setSettingValue(String JavaDoc settingName, Object JavaDoc newValue) {
120         setSettingValue(settingName, newValue, settingName);
121     }
122     
123     /** Set the value into the <code>Settings</code> and optionally
124      * fire the property change.
125      * @param settingName name of the setting to change
126      * @param newValue new value of the setting
127      * @param propertyName if non-null it means that the property change
128      * should be fired if the newValue is differernt from the old one.
129      * Firing is performed using the given property name. Nothing is fired
130      * when it's set to null.
131      */

132     public void setSettingValue(String JavaDoc settingName, Object JavaDoc newValue,
133     String JavaDoc propertyName) {
134         
135         initializerValuesMap.put(settingName, newValue);
136
137         Object JavaDoc oldValue = getSettingValue(settingName);
138         if ((oldValue == null && newValue == null)
139                 || (oldValue != null && oldValue.equals(newValue))
140            ) {
141             return; // no change
142
}
143
144         Settings.setValue(kitClass, settingName, newValue);
145
146 // if (propertyName != null) {
147
//firePropertyChange(propertyName, oldValue, newValue);[PENDING]
148
// }
149
}
150
151     
152     public void doSetSettingValue(String JavaDoc settingName, Object JavaDoc newValue,
153     String JavaDoc propertyName) {
154         initializerValuesMap.put(settingName, newValue);
155         Settings.setValue(kitClass, settingName, newValue);
156     }
157     
158     
159     /** Enables easier handling of the boolean settings.
160      * @param settingName name of the setting to change
161      * @param newValue new boolean value of the setting
162      * @param propertyName if non-null it means that the property change
163      * should be fired if the newValue is differernt from the old one.
164      * Firing is performed using the given property name. Nothing is fired
165      * when it's set to null.
166      */

167     protected void setSettingBoolean(String JavaDoc settingName, boolean newValue, String JavaDoc propertyName) {
168         setSettingValue(settingName, newValue ? Boolean.TRUE : Boolean.FALSE, propertyName);
169     }
170
171     /** Enables easier handling of the integer settings.
172      * @param settingName name of the setting to change
173      * @param newValue new integer value of the setting
174      * @param propertyName if non-null it means that the property change
175      * should be fired if the newValue is differernt from the old one.
176      * Firing is performed using the given property name. Nothing is fired
177      * when it's set to null.
178      */

179     protected void setSettingInteger(String JavaDoc settingName, int newValue, String JavaDoc propertyName) {
180         setSettingValue(settingName, new Integer JavaDoc(newValue));
181     }
182
183     /** @return localized string */
184     protected String JavaDoc getString(String JavaDoc s) {
185         return NbBundle.getMessage(OptionSupport.class, s);
186     }
187     
188     /** Helper method for merging string arrays without searching
189      * for the same strings.
190      * @param a1 array that will be at the begining of the resulting array
191      * @param a1 array that will be at the end of the resulting array
192      */

193     public static String JavaDoc[] mergeStringArrays(String JavaDoc[] a1, String JavaDoc[] a2) {
194         return NbEditorUtilities.mergeStringArrays(a1, a2);
195     }
196
197     /** Get the name of the <code>Settings.Initializer</code> related
198      * to these options.
199      */

200     protected String JavaDoc getSettingsInitializerName() {
201         return getTypeName() + "-options-initalizer"; // NOI18N
202
}
203
204     protected void updateSettingsMap(Class JavaDoc kitClass, Map JavaDoc settingsMap) {
205         if (kitClass == getKitClass()) {
206             settingsMap.putAll(initializerValuesMap);
207         }
208     }
209     
210     Settings.Initializer getSettingsInitializer() {
211         if (settingsInitializer == null) {
212             settingsInitializer = new SettingsInitializer();
213         }
214         return settingsInitializer;
215     }
216     
217     
218     class SettingsInitializer implements Settings.Initializer {
219         
220         String JavaDoc name;
221         private volatile boolean updating = false;
222         
223         public String JavaDoc getName() {
224             if (name == null) {
225                 name = getSettingsInitializerName();
226             }
227             
228             return name;
229         }
230         
231         public void updateSettingsMap(Class JavaDoc kitClass, Map JavaDoc settingsMap) {
232             if (!updating) {
233                 updating = true;
234                 try {
235                     OptionSupport.this.updateSettingsMap(kitClass, settingsMap);
236                 } finally {
237                     updating = false;
238                 }
239             }
240         }
241         
242     }
243
244
245 }
246
Popular Tags