KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > palette > PaletteItem


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.palette;
21
22 import java.beans.*;
23 import java.awt.Image JavaDoc;
24
25 import org.openide.ErrorManager;
26 import org.openide.nodes.Node;
27
28 import org.netbeans.modules.form.FormUtils;
29 import org.netbeans.modules.form.project.*;
30
31 /**
32  * PaletteItem holds important information about one component (item)
33  * in the palette.
34  *
35  * @author Tomas Pavek
36  */

37
38 public final class PaletteItem implements Node.Cookie {
39
40     private PaletteItemDataObject itemDataObject;
41
42     // raw data (as read from the item file - to be resolved lazily)
43
ClassSource componentClassSource;
44 // Boolean isContainer_explicit;
45
String JavaDoc componentType_explicit;
46
47     // resolved data (derived from the raw data)
48
private Class JavaDoc componentClass;
49     private Throwable JavaDoc lastError; // error occurred when loading component class
50
// private Boolean componentIsContainer;
51
private int componentType = -1;
52
53     // type of component constants
54
private static final int LAYOUT = 1;
55     private static final int BORDER = 2;
56     private static final int VISUAL = 4; // bit flag
57
private static final int MENU = 8; // bit flag
58
private static final int TYPE_MASK = 15;
59
60     // -------
61

62     PaletteItem(PaletteItemDataObject dobj) {
63         itemDataObject = dobj;
64     }
65
66     public PaletteItem(ClassSource componentClassSource) {
67         this.componentClassSource = componentClassSource;
68     }
69
70     public void setComponentClassSource(String JavaDoc className,
71                                  String JavaDoc[] cpTypes,
72                                  String JavaDoc[] cpNames)
73     {
74         componentClass = null;
75         lastError = null;
76         componentType = -1;
77         componentClassSource = new ClassSource(className, cpTypes, cpNames);
78     }
79
80     void setComponentExplicitType(String JavaDoc type) {
81         componentType_explicit = type;
82     }
83
84     // -------
85

86     /** @return a node visually representing this palette item */
87     public Node getNode() {
88         return (itemDataObject == null) ? null : itemDataObject.getNodeDelegate();
89     }
90
91     /** @return a String identifying this palette item */
92     public String JavaDoc getId() {
93         return getComponentClassName();
94     }
95
96     public String JavaDoc getComponentClassName() {
97         return componentClassSource.getClassName();
98     }
99
100     public ClassSource getComponentClassSource() {
101         return componentClassSource;
102     }
103
104     /** @return the class of the component represented by this pallete item.
105      * May return null - if class loading fails. */

106     public Class JavaDoc getComponentClass() {
107         if (componentClass == null && lastError == null)
108             componentClass = loadComponentClass();
109         return componentClass;
110     }
111
112     /** @return the exception occurred when trying to resolve the component
113      * class of this pallette item */

114     public Throwable JavaDoc getError() {
115         return lastError;
116     }
117
118     /** @return type of the component as String, e.g. "visual", "menu",
119      * "layout", border */

120     public String JavaDoc getExplicitComponentType() {
121         return componentType_explicit;
122     }
123
124     /** @return whether the component of this palette item is a visual component
125      * (java.awt.Component subclass) */

126     public boolean isVisual() {
127         if (componentType == -1)
128             resolveComponentType();
129         return (componentType & VISUAL) != 0;
130     }
131
132     /** @return whether the component of this palette item is a menu component */
133     public boolean isMenu() {
134         if (componentType == -1)
135             resolveComponentType();
136         return (componentType & MENU) != 0;
137     }
138
139     /** @return whether the component of this palette item is a layout mamanger
140      * (java.awt.LayoutManager implementation) */

141     public boolean isLayout() {
142         if (componentType == -1)
143             resolveComponentType();
144         return (componentType & TYPE_MASK) == LAYOUT;
145     }
146
147     /** @return whether the component of this palette item is a border
148      * (javax.swing.border.Border implementation) */

149     public boolean isBorder() {
150         if (componentType == -1)
151             resolveComponentType();
152         return (componentType & TYPE_MASK) == BORDER;
153     }
154
155 // public boolean isContainer() {
156
// if (componentIsContainer == null) {
157
// if (isContainer_explicit != null)
158
// componentIsContainer = isContainer_explicit;
159
// else {
160
// Class compClass = getComponentClass();
161
// if (compClass != null
162
// && java.awt.Container.class.isAssignableFrom(compClass))
163
// {
164
// BeanDescriptor bd = getBeanDescriptor();
165
// componentIsContainer =
166
// bd != null && Boolean.FALSE.equals(bd.getValue("isContainer")) ? // NOI18N
167
// Boolean.FALSE : Boolean.TRUE;
168
// }
169
// else componentIsContainer = Boolean.FALSE;
170
// }
171
// }
172
// return componentIsContainer.booleanValue();
173
// }
174

175     public String JavaDoc toString() {
176         return PaletteUtils.getItemComponentDescription(this);
177     }
178
179     String JavaDoc getDisplayName() {
180         BeanDescriptor bd = getBeanDescriptor();
181         return bd != null ? bd.getDisplayName() : null;
182     }
183
184     String JavaDoc getTooltip() {
185         BeanDescriptor bd = getBeanDescriptor();
186         return bd != null ? bd.getShortDescription() : null;
187     }
188
189     Image JavaDoc getIcon(int type) {
190         BeanInfo bi = getBeanInfo();
191         return bi != null ? bi.getIcon(type) : null;
192     }
193
194     void reset() {
195         componentClass = null;
196         lastError = null;
197 // componentIsContainer = null;
198
componentType = -1;
199
200         itemDataObject.displayName = null;
201         itemDataObject.tooltip = null;
202         itemDataObject.icon16 = null;
203         itemDataObject.icon32 = null;
204     }
205
206     // -------
207

208     private Class JavaDoc loadComponentClass() {
209         try {
210             return FormUtils.loadSystemClass(getComponentClassName());
211         } catch (ClassNotFoundException JavaDoc cnfex) {}
212
213         try {
214             return ClassPathUtils.loadClass(getComponentClassSource());
215         }
216         catch (Exception JavaDoc ex) {
217             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
218             lastError = ex;
219         }
220         catch (LinkageError JavaDoc ex) {
221             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
222             lastError = ex;
223         }
224         return null;
225     }
226
227     private BeanInfo getBeanInfo() {
228         Class JavaDoc compClass = getComponentClass();
229         if (compClass != null) {
230             try {
231                 return FormUtils.getBeanInfo(compClass);
232             }
233             catch (Exception JavaDoc ex) {} // ignore failure
234
//catch (LinkageError ex) {}
235
catch (Error JavaDoc er) {} // Issue 74002
236
}
237         return null;
238     }
239
240     private BeanDescriptor getBeanDescriptor() {
241         Class JavaDoc compClass = getComponentClass();
242         if (compClass != null) {
243             try {
244                 return FormUtils.getBeanInfo(compClass).getBeanDescriptor();
245             }
246             catch (Exception JavaDoc ex) {} // ignore failure
247
//catch (LinkageError ex) {}
248
catch (Error JavaDoc er) {} // Issue 74002
249
}
250         return null;
251     }
252
253     private void resolveComponentType() {
254         if (componentType_explicit == null) {
255             componentType = 0;
256
257             Class JavaDoc compClass = getComponentClass();
258             if (compClass == null)
259                 return;
260
261             if (java.awt.LayoutManager JavaDoc.class.isAssignableFrom(compClass)) {
262                 // PENDING LayoutSupportDelegate - should have special entry in pallette item file?
263
componentType = LAYOUT;
264                 return;
265             }
266
267             if (javax.swing.border.Border JavaDoc.class.isAssignableFrom(compClass)) {
268                 componentType = BORDER;
269                 return;
270             }
271
272             if (java.awt.Component JavaDoc.class.isAssignableFrom(compClass))
273                 componentType |= VISUAL;
274
275             if (java.awt.MenuComponent JavaDoc.class.isAssignableFrom(compClass)
276                   || javax.swing.JMenuItem JavaDoc.class.isAssignableFrom(compClass)
277                   || javax.swing.JMenuBar JavaDoc.class.isAssignableFrom(compClass)
278                   || javax.swing.JPopupMenu JavaDoc.class.isAssignableFrom(compClass))
279                 componentType |= MENU;
280         }
281         else if ("visual".equalsIgnoreCase(componentType_explicit)) // NOI18N
282
componentType = VISUAL;
283         else if ("layout".equalsIgnoreCase(componentType_explicit)) // NOI18N
284
componentType = LAYOUT;
285         else if ("border".equalsIgnoreCase(componentType_explicit)) // NOI18N
286
componentType = BORDER;
287         else if ("menu".equalsIgnoreCase(componentType_explicit)) // NOI18N
288
componentType = MENU | VISUAL;
289         else
290             componentType = 0;
291     }
292 }
293
Popular Tags