KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > FormatterIndentEngineBeanInfo


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;
21
22 import java.beans.*;
23 import java.awt.Image JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import org.netbeans.editor.LocaleSupport;
26 import org.openide.util.Utilities;
27
28 /** BeanInfo for the FormatterIndentEngine class
29 *
30 * @author Miloslav Metelka
31 */

32 public abstract class FormatterIndentEngineBeanInfo extends SimpleBeanInfo {
33
34     /** Prefix of the icon location. */
35     private String JavaDoc iconPrefix;
36
37     /** Icons for compiler settings objects. */
38     private Image JavaDoc icon;
39     private Image JavaDoc icon32;
40
41     private PropertyDescriptor[] propertyDescriptors;
42
43     private String JavaDoc[] propertyNames;
44
45     public FormatterIndentEngineBeanInfo() {
46         this(null);
47     }
48
49     public FormatterIndentEngineBeanInfo(String JavaDoc iconPrefix) {
50         this.iconPrefix = iconPrefix;
51     }
52
53     public PropertyDescriptor[] getPropertyDescriptors() {
54         if (propertyDescriptors == null) {
55             String JavaDoc[] propNames = getPropertyNames();
56             PropertyDescriptor[] pds = new PropertyDescriptor[propNames.length];
57
58             for (int i = 0; i < propNames.length; i++) {
59                 pds[i] = createPropertyDescriptor(propNames[i]);
60                 // Set display-name and short-description
61
pds[i].setDisplayName(getString("PROP_indentEngine_" + propNames[i])); // NOI18N
62
pds[i].setShortDescription(getString("HINT_indentEngine_" + propNames[i])); // NOI18N
63

64             }
65
66             propertyDescriptors = pds; // now the array are inited
67

68             // Now various properties of the descriptors can be updated
69
updatePropertyDescriptors();
70         }
71
72         return propertyDescriptors;
73     }
74
75     /** Create property descriptor for a particular property-name. */
76     protected PropertyDescriptor createPropertyDescriptor(String JavaDoc propName) {
77         PropertyDescriptor pd;
78         try {
79             pd = new PropertyDescriptor(propName, getBeanClass());
80
81         } catch (IntrospectionException e) {
82             try {
83                 // Create property without read/write methods
84
pd = new PropertyDescriptor(propName, null, null);
85             } catch (IntrospectionException e2) {
86                 throw new IllegalStateException JavaDoc("Invalid property name=" + propName); // NOI18N
87
}
88
89             // Try a simple search for get/set methods - just by name
90
// Successor can customize it if necessary
91
String JavaDoc cap = capitalize(propName);
92             Method JavaDoc m = findMethod("get" + cap); // NOI18N
93
if (m != null) {
94                 try {
95                     pd.setReadMethod(m);
96                 } catch (IntrospectionException e2) {
97                 }
98             }
99             m = findMethod("set" + cap); // NOI18N
100
if (m != null) {
101                 try {
102                     pd.setWriteMethod(m);
103                 } catch (IntrospectionException e2) {
104                 }
105             }
106         }
107
108         return pd;
109     }
110
111     protected void updatePropertyDescriptors() {
112     }
113
114     private Method JavaDoc findMethod(String JavaDoc name) {
115         try {
116             Method JavaDoc[] ma = getBeanClass().getDeclaredMethods();
117             for (int i = 0; i < ma.length; i++) {
118                 if (name.equals(ma[i].getName())) {
119                     return ma[i];
120                 }
121             }
122         } catch (SecurityException JavaDoc e) {
123         }
124         return null;
125     }
126
127     private static String JavaDoc capitalize(String JavaDoc s) {
128     if (s.length() == 0) {
129         return s;
130     }
131     char chars[] = s.toCharArray();
132     chars[0] = Character.toUpperCase(chars[0]);
133     return new String JavaDoc(chars);
134     }
135
136     protected abstract Class JavaDoc getBeanClass();
137
138     protected String JavaDoc[] getPropertyNames() {
139         if (propertyNames == null) {
140             propertyNames = createPropertyNames();
141         }
142         return propertyNames;
143     }
144
145     protected String JavaDoc[] createPropertyNames() {
146         return new String JavaDoc[] {
147             FormatterIndentEngine.EXPAND_TABS_PROP,
148             FormatterIndentEngine.SPACES_PER_TAB_PROP
149         };
150     }
151
152     protected PropertyDescriptor getPropertyDescriptor(String JavaDoc propertyName) {
153         String JavaDoc[] propNames = getPropertyNames();
154         for (int i = 0; i < propNames.length; i++) {
155             if (propertyName.equals(propNames[i])) {
156                 return getPropertyDescriptors()[i];
157             }
158         }
159         return null;
160     }
161
162     protected void setPropertyEditor(String JavaDoc propertyName, Class JavaDoc propertyEditor) {
163         PropertyDescriptor pd = getPropertyDescriptor(propertyName);
164         if (pd != null) {
165             pd.setPropertyEditorClass(propertyEditor);
166         }
167     }
168
169     protected void setExpert(String JavaDoc[] expertPropertyNames) {
170         for (int i = 0; i < expertPropertyNames.length; i++) {
171             PropertyDescriptor pd = getPropertyDescriptor(expertPropertyNames[i]);
172             if (pd != null) {
173                 pd.setExpert(true);
174             }
175         }
176     }
177
178     protected void setHidden(String JavaDoc[] hiddenPropertyNames) {
179         for (int i = 0; i < hiddenPropertyNames.length; i++) {
180             PropertyDescriptor pd = getPropertyDescriptor(hiddenPropertyNames[i]);
181             if (pd != null) {
182                 pd.setHidden(true);
183             }
184         }
185     }
186     
187     private String JavaDoc getValidIconPrefix() {
188         return (iconPrefix != null) ? iconPrefix
189             : "org/netbeans/modules/editor/resources/indentEngine"; // NOI18N
190
}
191     
192     private Image JavaDoc getDefaultIcon(String JavaDoc iconResource){
193         return Utilities.loadImage(iconResource);
194     }
195
196     public Image JavaDoc getIcon(int type) {
197         if ((type == BeanInfo.ICON_COLOR_16x16) || (type == BeanInfo.ICON_MONO_16x16)) {
198             if (icon == null) {
199                 icon = loadImage(getValidIconPrefix() + ".gif"); // NOI18N
200
}
201             return (icon != null) ? icon : getDefaultIcon(getValidIconPrefix() + ".gif"); // NOI18N
202

203         } else {
204             if (icon32 == null) {
205                 icon32 = loadImage(getValidIconPrefix() + "32.gif"); // NOI18N
206
}
207             return (icon32 != null) ? icon32 : getDefaultIcon(getValidIconPrefix() + "32.gif"); // NOI18N
208
}
209     }
210
211     /**
212      * Get the localized string. This method must be overriden
213      * in children if they add new properties or other stuff
214      * that needs to be localized.
215      * @param key key to find in a bundle
216      * @return localized string
217      */

218     protected String JavaDoc getString(String JavaDoc key) {
219         return LocaleSupport.getString( key );
220     }
221
222 }
223
224
Popular Tags