KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > beaninfo > swing > BISupport


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.form.beaninfo.swing;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.*;
24 import java.util.ResourceBundle JavaDoc;
25 import org.openide.util.NbBundle;
26 import org.openide.util.Utilities;
27
28 /**
29  * A class that provides some support for simplier BeanInfos in this package.
30  *
31  * @author petr.nejedly@sun.com
32  */

33 abstract class BISupport extends SimpleBeanInfo {
34
35     private PropertyDescriptor[] pds;
36     private String JavaDoc icon;
37     private Class JavaDoc beanClass;
38
39     protected BISupport(String JavaDoc iconBaseName, Class JavaDoc beanClass) {
40         icon = iconBaseName;
41         this.beanClass = beanClass;
42     }
43
44     public BeanDescriptor getBeanDescriptor() {
45         return new BeanDescriptor(beanClass);
46     }
47
48     public synchronized PropertyDescriptor[] getPropertyDescriptors() {
49         if (pds == null) {
50             try {
51                 pds = createPropertyDescriptors();
52             } catch (IntrospectionException e) {
53                 pds = super.getPropertyDescriptors();
54             }
55         }
56         return pds;
57     }
58
59     
60     public Image JavaDoc getIcon(int type) {
61         if (type == ICON_COLOR_32x32 || type == ICON_MONO_32x32)
62             return Utilities.loadImage("org/netbeans/modules/form/beaninfo/swing/" + icon + "32.gif"); // NOI18N
63
else
64             return Utilities.loadImage("org/netbeans/modules/form/beaninfo/swing/" + icon + ".gif"); // NOI18N
65
}
66
67     protected PropertyDescriptor[] createPropertyDescriptors() throws IntrospectionException {
68         return new PropertyDescriptor[0];
69     }
70     
71     protected String JavaDoc getString(String JavaDoc key) {
72         return NbBundle.getMessage(BISupport.class, key);
73     }
74     
75     protected PropertyDescriptor createRW( Class JavaDoc beanClass, String JavaDoc name)
76                                             throws IntrospectionException {
77         String JavaDoc title = Character.toUpperCase(name.charAt(0)) + name.substring(1);
78
79         PropertyDescriptor pd = new PropertyDescriptor(name, beanClass,
80                 "get" + title, "set" + title ); // NOI18N
81

82         setProps(pd, beanClass, title);
83         return pd;
84     }
85
86     protected PropertyDescriptor createRO( Class JavaDoc beanClass, String JavaDoc name)
87                                             throws IntrospectionException {
88         String JavaDoc title = Character.toUpperCase(name.charAt(0)) + name.substring(1);
89
90         PropertyDescriptor pd = new PropertyDescriptor(name, beanClass,
91                 "get" + title, null); // NOI18N
92

93         setProps(pd, beanClass, title);
94         return pd;
95     }
96
97     private void setProps( PropertyDescriptor pd, Class JavaDoc beanClass, String JavaDoc title ) {
98         String JavaDoc className = beanClass.getName();
99         int dotIdx = className.lastIndexOf('.');
100         className = dotIdx > 0 ? className.substring(dotIdx+1) : className;
101         pd.setDisplayName(getString("PROP_" + className + '.' + title)); // NOI18N
102
pd.setShortDescription(getString("HINT_" + className + '.' + title)); // NOI18N
103
}
104
105
106
107     static class TaggedPropertyEditor extends PropertyEditorSupport {
108         private String JavaDoc[] tags;
109         private int[] values;
110         private String JavaDoc[] javaInitStrings;
111         private String JavaDoc[] tagKeys;
112
113         protected TaggedPropertyEditor( int[] values, String JavaDoc[] javaInitStrings, String JavaDoc[] tagKeys ) {
114             this.values = values;
115             this.javaInitStrings = javaInitStrings;
116             this.tagKeys = tagKeys;
117         }
118         
119         public String JavaDoc[] getTags() {
120             if (tags == null) createTags();
121             return tags;
122         }
123
124         public String JavaDoc getAsText() {
125             Object JavaDoc valObj = getValue();
126             if (valObj instanceof Integer JavaDoc) {
127                 if (tags == null) createTags();
128
129                 int value = ((Integer JavaDoc)valObj).intValue();
130                 for (int i = 0; i < values.length; i++)
131                     if (value == values[i])
132                         return tags[i];
133             }
134             return null;
135         }
136
137         public void setAsText(String JavaDoc str) {
138             if (tags == null) createTags();
139
140             int value = -1;
141
142             for (int i = 0; i < tags.length; i++) {
143                 if (str.equals(tags[i])) {
144                     value = values[i];
145                     break;
146                 }
147             }
148
149             if (value != -1)
150                 setValue(new Integer JavaDoc(value));
151         }
152
153         public String JavaDoc getJavaInitializationString() {
154             Object JavaDoc valObj = getValue();
155             if (valObj instanceof Integer JavaDoc) {
156                 int value = ((Integer JavaDoc)valObj).intValue();
157                 for (int i = 0; i < values.length; i++)
158                     if (value == values[i])
159                         return javaInitStrings[i];
160             }
161             return "???"; // NOI18N
162
}
163
164         private void createTags() {
165             tags = new String JavaDoc[tagKeys.length];
166             ResourceBundle JavaDoc bundle = NbBundle.getBundle(BISupport.class);
167             for (int i=0; i<tagKeys.length; i++) {
168                 tags[i] = bundle.getString(tagKeys[i]);
169             }
170         }
171     }
172 }
173
Popular Tags