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

« back to all changes in this revision

Viewing changes to editor/src/org/netbeans/modules/editor/FormatterIndentEngineBeanInfo.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.editor;
 
43
 
 
44
import java.beans.*;
 
45
import java.awt.Image;
 
46
import java.lang.reflect.Method;
 
47
import org.netbeans.editor.LocaleSupport;
 
48
import org.openide.util.Utilities;
 
49
 
 
50
/** BeanInfo for the FormatterIndentEngine class
 
51
*
 
52
* @author Miloslav Metelka
 
53
*/
 
54
public abstract class FormatterIndentEngineBeanInfo extends SimpleBeanInfo {
 
55
 
 
56
    /** Prefix of the icon location. */
 
57
    private String iconPrefix;
 
58
 
 
59
    /** Icons for compiler settings objects. */
 
60
    private Image icon;
 
61
    private Image icon32;
 
62
 
 
63
    private PropertyDescriptor[] propertyDescriptors;
 
64
 
 
65
    private String[] propertyNames;
 
66
 
 
67
    public FormatterIndentEngineBeanInfo() {
 
68
        this(null);
 
69
    }
 
70
 
 
71
    public FormatterIndentEngineBeanInfo(String iconPrefix) {
 
72
        this.iconPrefix = iconPrefix;
 
73
    }
 
74
 
 
75
    public PropertyDescriptor[] getPropertyDescriptors() {
 
76
        if (propertyDescriptors == null) {
 
77
            String[] propNames = getPropertyNames();
 
78
            PropertyDescriptor[] pds = new PropertyDescriptor[propNames.length];
 
79
 
 
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
 
85
 
 
86
            }
 
87
 
 
88
            propertyDescriptors = pds; // now the array are inited
 
89
 
 
90
            // Now various properties of the descriptors can be updated
 
91
            updatePropertyDescriptors();
 
92
        }
 
93
 
 
94
        return propertyDescriptors;
 
95
    }
 
96
 
 
97
    /** Create property descriptor for a particular property-name. */
 
98
    protected PropertyDescriptor createPropertyDescriptor(String propName) {
 
99
        PropertyDescriptor pd;
 
100
        try {
 
101
            pd = new PropertyDescriptor(propName, getBeanClass());
 
102
 
 
103
        } catch (IntrospectionException e) {
 
104
            try {
 
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
 
109
            }
 
110
 
 
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
 
115
            if (m != null) {
 
116
                try {
 
117
                    pd.setReadMethod(m);
 
118
                } catch (IntrospectionException e2) {
 
119
                }
 
120
            }
 
121
            m = findMethod("set" + cap); // NOI18N
 
122
            if (m != null) {
 
123
                try {
 
124
                    pd.setWriteMethod(m);
 
125
                } catch (IntrospectionException e2) {
 
126
                }
 
127
            }
 
128
        }
 
129
 
 
130
        return pd;
 
131
    }
 
132
 
 
133
    protected void updatePropertyDescriptors() {
 
134
    }
 
135
 
 
136
    private Method findMethod(String name) {
 
137
        try {
 
138
            Method[] ma = getBeanClass().getDeclaredMethods();
 
139
            for (int i = 0; i < ma.length; i++) {
 
140
                if (name.equals(ma[i].getName())) {
 
141
                    return ma[i];
 
142
                }
 
143
            }
 
144
        } catch (SecurityException e) {
 
145
        }
 
146
        return null;
 
147
    }
 
148
 
 
149
    private static String capitalize(String s) {
 
150
        if (s.length() == 0) {
 
151
            return s;
 
152
        }
 
153
        char chars[] = s.toCharArray();
 
154
        chars[0] = Character.toUpperCase(chars[0]);
 
155
        return new String(chars);
 
156
    }
 
157
 
 
158
    protected abstract Class getBeanClass();
 
159
 
 
160
    protected String[] getPropertyNames() {
 
161
        if (propertyNames == null) {
 
162
            propertyNames = createPropertyNames();
 
163
        }
 
164
        return propertyNames;
 
165
    }
 
166
 
 
167
    protected String[] createPropertyNames() {
 
168
        return new String[] {
 
169
            FormatterIndentEngine.EXPAND_TABS_PROP,
 
170
            FormatterIndentEngine.SPACES_PER_TAB_PROP
 
171
        };
 
172
    }
 
173
 
 
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];
 
179
            }
 
180
        }
 
181
        return null;
 
182
    }
 
183
 
 
184
    protected void setPropertyEditor(String propertyName, Class propertyEditor) {
 
185
        PropertyDescriptor pd = getPropertyDescriptor(propertyName);
 
186
        if (pd != null) {
 
187
            pd.setPropertyEditorClass(propertyEditor);
 
188
        }
 
189
    }
 
190
 
 
191
    protected void setExpert(String[] expertPropertyNames) {
 
192
        for (int i = 0; i < expertPropertyNames.length; i++) {
 
193
            PropertyDescriptor pd = getPropertyDescriptor(expertPropertyNames[i]);
 
194
            if (pd != null) {
 
195
                pd.setExpert(true);
 
196
            }
 
197
        }
 
198
    }
 
199
 
 
200
    protected void setHidden(String[] hiddenPropertyNames) {
 
201
        for (int i = 0; i < hiddenPropertyNames.length; i++) {
 
202
            PropertyDescriptor pd = getPropertyDescriptor(hiddenPropertyNames[i]);
 
203
            if (pd != null) {
 
204
                pd.setHidden(true);
 
205
            }
 
206
        }
 
207
    }
 
208
    
 
209
    private String getValidIconPrefix() {
 
210
        return (iconPrefix != null) ? iconPrefix
 
211
            : "org/netbeans/modules/editor/resources/indentEngine"; // NOI18N
 
212
    }
 
213
    
 
214
    private Image getDefaultIcon(String iconResource){
 
215
        return Utilities.loadImage(iconResource);
 
216
    }
 
217
 
 
218
    public Image getIcon(int type) {
 
219
        if ((type == BeanInfo.ICON_COLOR_16x16) || (type == BeanInfo.ICON_MONO_16x16)) {
 
220
            if (icon == null) {
 
221
                icon = loadImage(getValidIconPrefix() + ".gif"); // NOI18N
 
222
            }
 
223
            return (icon != null) ? icon : getDefaultIcon(getValidIconPrefix() + ".gif"); // NOI18N
 
224
 
 
225
        } else {
 
226
            if (icon32 == null) {
 
227
                icon32 = loadImage(getValidIconPrefix() + "32.gif"); // NOI18N
 
228
            }
 
229
            return (icon32 != null) ? icon32 : getDefaultIcon(getValidIconPrefix() + "32.gif"); // NOI18N
 
230
        }
 
231
    }
 
232
 
 
233
    /** 
 
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
 
239
     */
 
240
    protected String getString(String key) {
 
241
        return LocaleSupport.getString( key );
 
242
    }
 
243
 
 
244
}
 
245