001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.beaninfo.swing;
043:
044: import java.awt.Image;
045: import java.beans.*;
046: import java.util.ResourceBundle;
047: import org.openide.util.NbBundle;
048: import org.openide.util.Utilities;
049:
050: /**
051: * A class that provides some support for simplier BeanInfos in this package.
052: *
053: * @author petr.nejedly@sun.com
054: */
055: abstract class BISupport extends SimpleBeanInfo {
056:
057: private PropertyDescriptor[] pds;
058: private String icon;
059: private Class beanClass;
060:
061: protected BISupport(String iconBaseName, Class beanClass) {
062: icon = iconBaseName;
063: this .beanClass = beanClass;
064: }
065:
066: @Override
067: public BeanDescriptor getBeanDescriptor() {
068: return new BeanDescriptor(beanClass);
069: }
070:
071: @Override
072: public synchronized PropertyDescriptor[] getPropertyDescriptors() {
073: if (pds == null) {
074: try {
075: pds = createPropertyDescriptors();
076: } catch (IntrospectionException e) {
077: pds = super .getPropertyDescriptors();
078: }
079: }
080: return pds;
081: }
082:
083: @Override
084: public Image getIcon(int type) {
085: if (type == ICON_COLOR_32x32 || type == ICON_MONO_32x32)
086: return Utilities
087: .loadImage("org/netbeans/modules/form/beaninfo/swing/"
088: + icon + "32.gif"); // NOI18N
089: else
090: return Utilities
091: .loadImage("org/netbeans/modules/form/beaninfo/swing/"
092: + icon + ".gif"); // NOI18N
093: }
094:
095: protected PropertyDescriptor[] createPropertyDescriptors()
096: throws IntrospectionException {
097: return new PropertyDescriptor[0];
098: }
099:
100: protected String getString(String key) {
101: return NbBundle.getMessage(BISupport.class, key);
102: }
103:
104: protected PropertyDescriptor createRW(Class beanClass, String name)
105: throws IntrospectionException {
106: String title = Character.toUpperCase(name.charAt(0))
107: + name.substring(1);
108:
109: PropertyDescriptor pd = new PropertyDescriptor(name, beanClass,
110: "get" + title, "set" + title); // NOI18N
111:
112: setProps(pd, beanClass, title);
113: return pd;
114: }
115:
116: protected PropertyDescriptor createRO(Class beanClass, String name)
117: throws IntrospectionException {
118: String title = Character.toUpperCase(name.charAt(0))
119: + name.substring(1);
120:
121: PropertyDescriptor pd = new PropertyDescriptor(name, beanClass,
122: "get" + title, null); // NOI18N
123:
124: setProps(pd, beanClass, title);
125: return pd;
126: }
127:
128: private void setProps(PropertyDescriptor pd, Class beanClass,
129: String title) {
130: String className = beanClass.getName();
131: int dotIdx = className.lastIndexOf('.');
132: className = dotIdx > 0 ? className.substring(dotIdx + 1)
133: : className;
134: pd.setDisplayName(getString("PROP_" + className + '.' + title)); // NOI18N
135: pd.setShortDescription(getString("HINT_" + className + '.'
136: + title)); // NOI18N
137: }
138:
139: static class TaggedPropertyEditor extends PropertyEditorSupport {
140: private String[] tags;
141: private int[] values;
142: private String[] javaInitStrings;
143: private String[] tagKeys;
144:
145: protected TaggedPropertyEditor(int[] values,
146: String[] javaInitStrings, String[] tagKeys) {
147: this .values = values;
148: this .javaInitStrings = javaInitStrings;
149: this .tagKeys = tagKeys;
150: }
151:
152: @Override
153: public String[] getTags() {
154: if (tags == null)
155: createTags();
156: return tags;
157: }
158:
159: @Override
160: public String getAsText() {
161: Object valObj = getValue();
162: if (valObj instanceof Integer) {
163: if (tags == null)
164: createTags();
165:
166: int value = ((Integer) valObj).intValue();
167: for (int i = 0; i < values.length; i++)
168: if (value == values[i])
169: return tags[i];
170: }
171: return null;
172: }
173:
174: @Override
175: public void setAsText(String str) {
176: if (tags == null)
177: createTags();
178:
179: int value = -1;
180:
181: for (int i = 0; i < tags.length; i++) {
182: if (str.equals(tags[i])) {
183: value = values[i];
184: break;
185: }
186: }
187:
188: if (value != -1)
189: setValue(new Integer(value));
190: }
191:
192: @Override
193: public String getJavaInitializationString() {
194: Object valObj = getValue();
195: if (valObj instanceof Integer) {
196: int value = ((Integer) valObj).intValue();
197: for (int i = 0; i < values.length; i++)
198: if (value == values[i])
199: return javaInitStrings[i];
200: }
201: return "???"; // NOI18N
202: }
203:
204: private void createTags() {
205: tags = new String[tagKeys.length];
206: ResourceBundle bundle = NbBundle.getBundle(BISupport.class);
207: for (int i = 0; i < tagKeys.length; i++) {
208: tags[i] = bundle.getString(tagKeys[i]);
209: }
210: }
211: }
212: }
|