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.editor;
45
import java.awt.Image;
46
import java.lang.reflect.Method;
47
import org.netbeans.editor.LocaleSupport;
48
import org.openide.util.Utilities;
50
/** BeanInfo for the FormatterIndentEngine class
52
* @author Miloslav Metelka
54
public abstract class FormatterIndentEngineBeanInfo extends SimpleBeanInfo {
56
/** Prefix of the icon location. */
57
private String iconPrefix;
59
/** Icons for compiler settings objects. */
63
private PropertyDescriptor[] propertyDescriptors;
65
private String[] propertyNames;
67
public FormatterIndentEngineBeanInfo() {
71
public FormatterIndentEngineBeanInfo(String iconPrefix) {
72
this.iconPrefix = iconPrefix;
75
public PropertyDescriptor[] getPropertyDescriptors() {
76
if (propertyDescriptors == null) {
77
String[] propNames = getPropertyNames();
78
PropertyDescriptor[] pds = new PropertyDescriptor[propNames.length];
80
for (int i = 0; i < propNames.length; i++) {
81
pds[i] = createPropertyDescriptor(propNames[i]);
82
// Set display-name and short-description
83
pds[i].setDisplayName(getString("PROP_indentEngine_" + propNames[i])); // NOI18N
84
pds[i].setShortDescription(getString("HINT_indentEngine_" + propNames[i])); // NOI18N
88
propertyDescriptors = pds; // now the array are inited
90
// Now various properties of the descriptors can be updated
91
updatePropertyDescriptors();
94
return propertyDescriptors;
97
/** Create property descriptor for a particular property-name. */
98
protected PropertyDescriptor createPropertyDescriptor(String propName) {
99
PropertyDescriptor pd;
101
pd = new PropertyDescriptor(propName, getBeanClass());
103
} catch (IntrospectionException e) {
105
// Create property without read/write methods
106
pd = new PropertyDescriptor(propName, null, null);
107
} catch (IntrospectionException e2) {
108
throw new IllegalStateException("Invalid property name=" + propName); // NOI18N
111
// Try a simple search for get/set methods - just by name
112
// Successor can customize it if necessary
113
String cap = capitalize(propName);
114
Method m = findMethod("get" + cap); // NOI18N
118
} catch (IntrospectionException e2) {
121
m = findMethod("set" + cap); // NOI18N
124
pd.setWriteMethod(m);
125
} catch (IntrospectionException e2) {
133
protected void updatePropertyDescriptors() {
136
private Method findMethod(String name) {
138
Method[] ma = getBeanClass().getDeclaredMethods();
139
for (int i = 0; i < ma.length; i++) {
140
if (name.equals(ma[i].getName())) {
144
} catch (SecurityException e) {
149
private static String capitalize(String s) {
150
if (s.length() == 0) {
153
char chars[] = s.toCharArray();
154
chars[0] = Character.toUpperCase(chars[0]);
155
return new String(chars);
158
protected abstract Class getBeanClass();
160
protected String[] getPropertyNames() {
161
if (propertyNames == null) {
162
propertyNames = createPropertyNames();
164
return propertyNames;
167
protected String[] createPropertyNames() {
168
return new String[] {
169
FormatterIndentEngine.EXPAND_TABS_PROP,
170
FormatterIndentEngine.SPACES_PER_TAB_PROP
174
protected PropertyDescriptor getPropertyDescriptor(String propertyName) {
175
String[] propNames = getPropertyNames();
176
for (int i = 0; i < propNames.length; i++) {
177
if (propertyName.equals(propNames[i])) {
178
return getPropertyDescriptors()[i];
184
protected void setPropertyEditor(String propertyName, Class propertyEditor) {
185
PropertyDescriptor pd = getPropertyDescriptor(propertyName);
187
pd.setPropertyEditorClass(propertyEditor);
191
protected void setExpert(String[] expertPropertyNames) {
192
for (int i = 0; i < expertPropertyNames.length; i++) {
193
PropertyDescriptor pd = getPropertyDescriptor(expertPropertyNames[i]);
200
protected void setHidden(String[] hiddenPropertyNames) {
201
for (int i = 0; i < hiddenPropertyNames.length; i++) {
202
PropertyDescriptor pd = getPropertyDescriptor(hiddenPropertyNames[i]);
209
private String getValidIconPrefix() {
210
return (iconPrefix != null) ? iconPrefix
211
: "org/netbeans/modules/editor/resources/indentEngine"; // NOI18N
214
private Image getDefaultIcon(String iconResource){
215
return Utilities.loadImage(iconResource);
218
public Image getIcon(int type) {
219
if ((type == BeanInfo.ICON_COLOR_16x16) || (type == BeanInfo.ICON_MONO_16x16)) {
221
icon = loadImage(getValidIconPrefix() + ".gif"); // NOI18N
223
return (icon != null) ? icon : getDefaultIcon(getValidIconPrefix() + ".gif"); // NOI18N
226
if (icon32 == null) {
227
icon32 = loadImage(getValidIconPrefix() + "32.gif"); // NOI18N
229
return (icon32 != null) ? icon32 : getDefaultIcon(getValidIconPrefix() + "32.gif"); // NOI18N
234
* Get the localized string. This method must be overriden
235
* in children if they add new properties or other stuff
236
* that needs to be localized.
237
* @param key key to find in a bundle
238
* @return localized string
240
protected String getString(String key) {
241
return LocaleSupport.getString( key );