KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
23 import java.awt.Image JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import org.openide.util.NbBundle;
28 import org.openide.util.SharedClassObject;
29
30 import org.netbeans.editor.BaseCaret;
31
32 /** BeanInfo for base options
33 *
34 * @author Miloslav Metelka, Ales Novak
35 */

36 public class BaseOptionsBeanInfo extends SimpleBeanInfo {
37
38     /** Prefix of the icon location. */
39     private String JavaDoc iconPrefix;
40
41     /** Prefix for getting localized strings for property name and hint */
42     private String JavaDoc bundlePrefix;
43
44     /** Icons for compiler settings objects. */
45     private Image JavaDoc icon;
46     private Image JavaDoc icon32;
47
48     private HashMap JavaDoc names2PD;
49
50     /** Propertydescriptors */
51     PropertyDescriptor[] descriptors;
52
53     private static final String JavaDoc[] EXPERT_PROP_NAMES = new String JavaDoc[] {
54                 BaseOptions.CARET_BLINK_RATE_PROP,
55                 BaseOptions.CARET_TYPE_INSERT_MODE_PROP,
56                 BaseOptions.CARET_TYPE_OVERWRITE_MODE_PROP,
57                 BaseOptions.CARET_COLOR_INSERT_MODE_PROP,
58                 BaseOptions.CARET_COLOR_OVERWRITE_MODE_PROP,
59                 BaseOptions.HIGHLIGHT_CARET_ROW_PROP,
60                 BaseOptions.HIGHLIGHT_MATCHING_BRACKET_PROP,
61                 BaseOptions.LINE_HEIGHT_CORRECTION_PROP,
62                 BaseOptions.MARGIN_PROP,
63                 BaseOptions.SCROLL_JUMP_INSETS_PROP,
64                 BaseOptions.SCROLL_FIND_INSETS_PROP,
65                 BaseOptions.STATUS_BAR_CARET_DELAY_PROP,
66                 BaseOptions.STATUS_BAR_VISIBLE_PROP,
67                 BaseOptions.TEXT_LIMIT_LINE_COLOR_PROP,
68                 BaseOptions.TEXT_LIMIT_LINE_VISIBLE_PROP,
69                 BaseOptions.TEXT_LIMIT_WIDTH_PROP,
70                 BaseOptions.TEXT_ANTIALIASING_PROP
71             };
72
73
74     public BaseOptionsBeanInfo() {
75         this("/org/netbeans/modules/editor/resources/baseOptions"); // NOI18N
76
}
77
78     public BaseOptionsBeanInfo(String JavaDoc iconPrefix) {
79         this(iconPrefix, ""); // NOI18N
80
}
81
82     public BaseOptionsBeanInfo(String JavaDoc iconPrefix, String JavaDoc bundlePrefix) {
83         this.iconPrefix = iconPrefix;
84         this.bundlePrefix = bundlePrefix;
85     }
86
87     /*
88     * @return Returns an array of PropertyDescriptors
89     * describing the editable properties supported by this bean.
90     */

91     public PropertyDescriptor[] getPropertyDescriptors () {
92         if (descriptors == null) {
93             String JavaDoc[] propNames = getPropNames();
94             PropertyDescriptor[] pds = new PropertyDescriptor[propNames.length];
95
96             for (int i = 0; i < propNames.length; i++) {
97                 pds[i] = createPropertyDescriptor(propNames[i]);
98                 // Set display-name and short-description
99
pds[i].setDisplayName(getString("PROP_" + bundlePrefix + propNames[i])); // NOI18N
100
pds[i].setShortDescription(getString("HINT_" + bundlePrefix + propNames[i])); // NOI18N
101
}
102
103             descriptors = pds; // now the array are inited
104

105             // Now various properties of the descriptors can be updated
106
updatePropertyDescriptors();
107         }
108         return descriptors;
109     }
110
111     /** Create property descriptor for a particular property-name. */
112     protected PropertyDescriptor createPropertyDescriptor(String JavaDoc propName) {
113         PropertyDescriptor pd;
114         try {
115             pd = new PropertyDescriptor(propName, getBeanClass());
116
117         } catch (IntrospectionException e) {
118             try {
119                 // Create property without read/write methods
120
pd = new PropertyDescriptor(propName, null, null);
121             } catch (IntrospectionException e2) {
122                 throw new IllegalStateException JavaDoc("Invalid property name=" + propName); // NOI18N
123
}
124
125             // Try a simple search for get/set methods - just by name
126
// Successor can customize it if necessary
127
String JavaDoc cap = capitalize(propName);
128             Method JavaDoc m = findMethod("get" + cap); // NOI18N
129
if (m != null) {
130                 try {
131                     pd.setReadMethod(m);
132                 } catch (IntrospectionException e2) {
133                 }
134             }
135             m = findMethod("set" + cap); // NOI18N
136
if (m != null) {
137                 try {
138                     pd.setWriteMethod(m);
139                 } catch (IntrospectionException e2) {
140                 }
141             }
142         }
143
144         return pd;
145     }
146
147     private Method JavaDoc findMethod(String JavaDoc name) {
148         try {
149             Method JavaDoc[] ma = getBeanClass().getDeclaredMethods();
150             for (int i = 0; i < ma.length; i++) {
151                 if (name.equals(ma[i].getName())) {
152                     return ma[i];
153                 }
154             }
155         } catch (SecurityException JavaDoc e) {
156         }
157         return null;
158     }
159
160     private static String JavaDoc capitalize(String JavaDoc s) {
161     if (s.length() == 0) {
162         return s;
163     }
164     char chars[] = s.toCharArray();
165     chars[0] = Character.toUpperCase(chars[0]);
166     return new String JavaDoc(chars);
167     }
168
169     /** Update various properties of the property descriptors. */
170     protected void updatePropertyDescriptors() {
171         setPropertyEditor(BaseOptions.ABBREV_MAP_PROP, AbbrevsEditor.class, false);
172         setPropertyEditor(BaseOptions.CARET_TYPE_INSERT_MODE_PROP, CaretTypeEditor.class);
173         setPropertyEditor(BaseOptions.CARET_TYPE_OVERWRITE_MODE_PROP, CaretTypeEditor.class);
174         setPropertyEditor(BaseOptions.KEY_BINDING_LIST_PROP, KeyBindingsEditor.class, false);
175         setPropertyEditor(BaseOptions.COLORING_MAP_PROP, ColoringArrayEditor.class, false);
176         setPropertyEditor(BaseOptions.SCROLL_JUMP_INSETS_PROP, ScrollInsetsEditor.class);
177         setPropertyEditor(BaseOptions.SCROLL_FIND_INSETS_PROP, ScrollInsetsEditor.class);
178         setPropertyEditor(BaseOptions.MACRO_MAP_PROP, MacrosEditor.class, false);
179         
180         setExpert(EXPERT_PROP_NAMES);
181         boolean usesNewOptions = usesNewOptions();
182         
183         String JavaDoc hidden[] = (usesNewOptions) ?
184                 new String JavaDoc[] {
185                     BaseOptions.EXPAND_TABS_PROP,
186                     BaseOptions.SPACES_PER_TAB_PROP,
187                     BaseOptions.OPTIONS_VERSION_PROP,
188                     BaseOptions.CARET_ITALIC_INSERT_MODE_PROP,
189                     BaseOptions.CARET_ITALIC_OVERWRITE_MODE_PROP,
190                     BaseOptions.COLORING_MAP_PROP,
191                     BaseOptions.FONT_SIZE_PROP,
192                     BaseOptions.KEY_BINDING_LIST_PROP,
193                     BaseOptions.TEXT_LIMIT_LINE_COLOR_PROP,
194                     BaseOptions.CARET_COLOR_INSERT_MODE_PROP,
195                     BaseOptions.CARET_COLOR_OVERWRITE_MODE_PROP,
196                 } :
197                 new String JavaDoc[] {
198                     BaseOptions.EXPAND_TABS_PROP,
199                     BaseOptions.SPACES_PER_TAB_PROP,
200                     BaseOptions.OPTIONS_VERSION_PROP,
201                     BaseOptions.CARET_ITALIC_INSERT_MODE_PROP,
202                     BaseOptions.CARET_ITALIC_OVERWRITE_MODE_PROP
203                 } ;
204         
205         setHidden(hidden);
206
207     }
208
209     protected boolean usesNewOptions() {
210
211         boolean usesNewOptions = false;
212         BaseOptions base = (BaseOptions) SharedClassObject.findObject(getBeanClass());
213         if (base != null){
214             usesNewOptions = base.usesNewOptionsDialog();
215         }
216         return usesNewOptions;
217     }
218
219     protected Class JavaDoc getBeanClass() {
220         return BaseOptions.class;
221     }
222
223     protected String JavaDoc[] getPropNames() {
224         return BaseOptions.BASE_PROP_NAMES;
225     }
226
227     protected synchronized PropertyDescriptor getPD(String JavaDoc propName) {
228         if (names2PD == null) {
229             names2PD = new HashMap JavaDoc(37);
230             PropertyDescriptor[] pds = getPropertyDescriptors();
231             for (int i = pds.length - 1; i >= 0; i--) {
232                 names2PD.put(pds[i].getName(), pds[i]);
233             }
234         }
235         return (PropertyDescriptor)names2PD.get(propName);
236     }
237     
238     protected void setPropertyEditor(String JavaDoc propName, Class JavaDoc propEditor, boolean canEditAsText) {
239         PropertyDescriptor pd = getPD(propName);
240         if (pd != null) {
241             pd.setPropertyEditorClass(propEditor);
242             pd.setValue("canEditAsText", canEditAsText ? Boolean.TRUE : Boolean.FALSE); //NOI18N
243
}
244     }
245
246     protected void setPropertyEditor(String JavaDoc propName, Class JavaDoc propEditor) {
247         setPropertyEditor(propName, propEditor, true);
248     }
249
250     protected void setExpert(String JavaDoc[] propNames) {
251         for (int i = 0; i < propNames.length; i++) {
252             PropertyDescriptor pd = getPD(propNames[i]);
253             if (pd != null) {
254                 pd.setExpert(true);
255             }
256         }
257     }
258
259     protected void setHidden(String JavaDoc[] propNames) {
260         for (int i = 0; i < propNames.length; i++) {
261             PropertyDescriptor pd = getPD(propNames[i]);
262             if (pd != null) {
263                 pd.setHidden(true);
264             }
265         }
266     }
267
268     /* @param type Desired type of the icon
269     * @return returns the Java loader's icon
270     */

271     public Image JavaDoc getIcon(final int type) {
272         if ((type == BeanInfo.ICON_COLOR_16x16) || (type == BeanInfo.ICON_MONO_16x16)) {
273             if (icon == null)
274                 icon = loadImage(iconPrefix + ".gif"); // NOI18N
275
return icon;
276         }
277         else {
278             if (icon32 == null)
279                 icon32 = loadImage(iconPrefix + "32.gif"); // NOI18N
280
return icon32;
281         }
282     }
283
284     /**
285      * Get localized string for the given key.
286      *
287      * @param key the key string for which the localized
288      * text is being retrieved. The localized string
289      * for the key must exist otherwise
290      * {@link java.util.MissingResourceException}
291      * gets thrown.
292      * @return localized string
293      */

294     protected String JavaDoc getString(String JavaDoc key) {
295         return NbBundle.getMessage(BaseOptionsBeanInfo.class, key);
296     }
297
298     // ------------------------ carets --------------------------------
299

300     public static class CaretTypeEditor extends PropertyEditorSupport {
301
302         private static String JavaDoc[] tags = new String JavaDoc[] {
303                                            BaseCaret.LINE_CARET,
304                                            BaseCaret.THIN_LINE_CARET,
305                                            BaseCaret.BLOCK_CARET
306                                        };
307
308         private static String JavaDoc[] locTags = new String JavaDoc[] {
309                                               getString("LINE_CARET"), // NOI18N
310
getString("THIN_LINE_CARET"), // NOI18N
311
getString("BLOCK_CARET") // NOI18N
312
};
313
314         public String JavaDoc[] getTags() {
315             return locTags;
316         }
317
318         public void setAsText(String JavaDoc txt) {
319             for (int i = 0; i < locTags.length; i++) {
320                 if (locTags[i].equals(txt)) {
321                     setValue(tags[i]);
322                     break;
323                 }
324             }
325         }
326
327         public String JavaDoc getAsText() {
328             String JavaDoc val = (String JavaDoc) getValue();
329             for (int i = 0; i < tags.length; i++) {
330                 if (tags[i].equals(val)) {
331                     return locTags[i];
332                 }
333             }
334             throw new IllegalStateException JavaDoc();
335         }
336
337         static String JavaDoc getString(String JavaDoc s) {
338             return NbBundle.getMessage(BaseOptionsBeanInfo.class, s);
339         }
340
341     }
342 }
343
Popular Tags