KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > ComponentChooserEditor


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;
21
22 import java.beans.*;
23 import java.util.*;
24 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor;
25 import org.openide.util.NbBundle;
26
27 /**
28  * Property editor allowing to choose a component from all components in form
29  * (FormModel). Choice can be restricted to certain bean types.
30  *
31  * @author Tomas Pavek
32  */

33
34 public class ComponentChooserEditor implements PropertyEditor,
35                                                FormAwareEditor,
36                                                XMLPropertyEditor,
37                                                NamedPropertyEditor
38 {
39     public static final int ALL_COMPONENTS = 0;
40     public static final int NONVISUAL_COMPONENTS = 3;
41 // public static final int VISUAL_COMPONENTS = 1;
42
// public static final int OTHER_COMPONENTS = 2;
43

44     private static final String JavaDoc NULL_REF = "null"; // NOI18N
45
private static final String JavaDoc INVALID_REF = "default"; // NOI18N
46

47     private static String JavaDoc noneText = null;
48     private static String JavaDoc invalidText = null;
49     private static String JavaDoc defaultText = null;
50
51     private FormModel formModel;
52     private List components;
53     private Class JavaDoc[] beanTypes = null;
54     private int componentCategory = 0;
55
56     private Object JavaDoc defaultValue;
57     private ComponentRef value;
58
59     private PropertyChangeSupport changeSupport;
60
61     public ComponentChooserEditor() {
62     }
63
64     public ComponentChooserEditor(Class JavaDoc[] componentTypes) {
65         beanTypes = componentTypes;
66     }
67
68     // --------------
69
// PropertyEditor implementation
70

71     public void setValue(Object JavaDoc value) {
72         defaultValue = null;
73         if (value == null || value instanceof ComponentRef)
74             this.value = (ComponentRef) value;
75         
76         else if (value instanceof RADComponent)
77             this.value = new ComponentRef((RADComponent)value);
78         else if (value instanceof String JavaDoc)
79             this.value = new ComponentRef((String JavaDoc)value);
80         else {
81             this.value = null;
82             defaultValue = value;
83         }
84             
85             //return;
86

87         firePropertyChange();
88     }
89
90     public Object JavaDoc getValue() {
91         if (value != null && INVALID_REF.equals(value.getDescription()))
92             return BeanSupport.NO_VALUE; // special - invalid value was loaded
93

94         return isDefaultValue() ? defaultValue : value;
95     }
96
97     public String JavaDoc[] getTags() {
98         List compList = getComponents();
99
100         int extraValues = 0;
101         int count = 0;
102         String JavaDoc[] names;
103         
104         if( isDefaultValue() ) {
105             extraValues = 2;
106             count = compList.size() + extraValues;
107             names = new String JavaDoc[count];
108             names[0] = defaultString();
109         } else {
110             extraValues = 1;
111             count = compList.size() + extraValues;
112             names = new String JavaDoc[count];
113         }
114         names[extraValues - 1] = noneString();
115         
116         if (count > extraValues) {
117             for (int i=extraValues; i < count; i++)
118                 names[i] = ((RADComponent)compList.get(i-extraValues)).getName();
119             Arrays.sort(names, 1, count);
120         }
121
122         return names;
123     }
124
125     private boolean isDefaultValue() {
126         return value == null && defaultValue != null;
127     }
128     
129     public String JavaDoc getAsText() {
130         if (isDefaultValue())
131             return defaultString();
132         if (value == null)
133             return noneString();
134         if (value.getComponent() == null)
135             return invalidString();
136
137         String JavaDoc str = value.getDescription();
138         return NULL_REF.equals(str) ? noneString() : str;
139     }
140
141     public void setAsText(String JavaDoc str) {
142         if (str == null || str.equals("") || str.equals(noneString())) // NOI18N
143
setValue(null);
144         else {
145             if(defaultString().equals(str)) {
146                 // XXX
147
setValue(defaultValue);
148             } else {
149                 setValue(str);
150             }
151             
152         }
153             
154     }
155
156     public String JavaDoc getJavaInitializationString() {
157         return value != null ? value.getJavaInitString() : null;
158     }
159
160     public void addPropertyChangeListener(PropertyChangeListener l) {
161         synchronized (this) {
162             if (changeSupport == null)
163                 changeSupport = new PropertyChangeSupport(this);
164         }
165         changeSupport.addPropertyChangeListener(l);
166     }
167
168     public void removePropertyChangeListener(PropertyChangeListener l) {
169         if (changeSupport != null)
170             changeSupport.removePropertyChangeListener(l);
171     }
172
173     public boolean isPaintable() {
174         return false;
175     }
176
177     public void paintValue(java.awt.Graphics JavaDoc gfx, java.awt.Rectangle JavaDoc box) {
178     }
179
180     public java.awt.Component JavaDoc getCustomEditor() {
181         return null;
182     }
183
184     public boolean supportsCustomEditor() {
185         return false;
186     }
187
188     // ----------------
189

190     // FormAwareEditor implementation
191
public void setFormModel(FormModel model) {
192         formModel = model;
193     }
194
195     public FormModel getFormModel() {
196         return formModel;
197     }
198
199     public void setBeanTypes(Class JavaDoc[] types) {
200         beanTypes = types;
201     }
202
203     public Class JavaDoc[] getBeanTypes() {
204         return beanTypes;
205     }
206
207     public void setComponentCategory(int cat) {
208         componentCategory = cat;
209     }
210
211     public int getComponentCategory() {
212         return componentCategory;
213     }
214
215     // ----------------
216
// XMLPropertyEditor implementation
217

218     private static final String JavaDoc XML_COMPONENT = "ComponentRef"; // NOI18N
219
private static final String JavaDoc ATTR_NAME = "name"; // NOI18N
220

221     public org.w3c.dom.Node JavaDoc storeToXML(org.w3c.dom.Document JavaDoc doc) {
222         String JavaDoc nameStr;
223         if (value != null)
224             nameStr = value.getComponent() != null ?
225                       value.getDescription() : INVALID_REF;
226         else
227             nameStr = NULL_REF;
228         
229         org.w3c.dom.Element JavaDoc el = doc.createElement(XML_COMPONENT);
230         el.setAttribute(ATTR_NAME, nameStr);
231         return el;
232     }
233
234     public void readFromXML(org.w3c.dom.Node JavaDoc element)
235         throws java.io.IOException JavaDoc
236     {
237         if (!XML_COMPONENT.equals(element.getNodeName()))
238             throw new java.io.IOException JavaDoc();
239
240         org.w3c.dom.NamedNodeMap JavaDoc attributes;
241         org.w3c.dom.Node JavaDoc nameAttr;
242         String JavaDoc name;
243
244         if ((attributes = element.getAttributes()) != null
245               && (nameAttr = attributes.getNamedItem(ATTR_NAME)) != null
246               && (name = nameAttr.getNodeValue()) != null)
247         {
248             value = new ComponentRef(name);
249         }
250     }
251
252     // ---------
253

254     protected List getComponents() {
255         if (components == null)
256             components = new ArrayList();
257         else
258             components.clear();
259
260         if (formModel != null) {
261             Collection<RADComponent> comps;
262             if (componentCategory == NONVISUAL_COMPONENTS)
263                 comps = formModel.getNonVisualComponents();
264             else
265                 comps = formModel.getAllComponents();
266
267             for (RADComponent metacomp : comps)
268                 if (acceptBean(metacomp))
269                     components.add(metacomp);
270         }
271
272         return components;
273     }
274
275     protected boolean acceptBean(RADComponent comp) {
276         if (beanTypes == null)
277             return true;
278
279         boolean match = false;
280         for (int i=0; i < beanTypes.length && !match; i++)
281             match = beanTypes[i].isAssignableFrom(comp.getBeanClass());
282
283         return match;
284     }
285
286     protected String JavaDoc noneString() {
287         if (noneText == null)
288             noneText = FormUtils.getBundleString("CTL_NoComponent"); // NOI18N
289
return noneText;
290     }
291
292     protected String JavaDoc defaultString() {
293         if (defaultText == null)
294             defaultText = FormUtils.getBundleString("CTL_DefaultComponent"); // NOI18N
295
return defaultText;
296     }
297     
298     protected String JavaDoc invalidString() {
299         if (invalidText == null)
300             invalidText = FormUtils.getBundleString("CTL_InvalidReference"); // NOI18N
301
return invalidText;
302     }
303
304     // ------
305

306     protected final void firePropertyChange() {
307         if (changeSupport != null)
308             changeSupport.firePropertyChange(null, null, null);
309     }
310
311     // NamedPropertyEditor implementation
312
public String JavaDoc getDisplayName() {
313         return NbBundle.getBundle(getClass()).getString("CTL_ComponentChooserEditor_DisplayName"); // NOI18N
314
}
315
316     // ------------
317

318     private class ComponentRef implements RADComponent.ComponentReference,
319                                              FormDesignValue
320     {
321         private String JavaDoc componentName;
322         private RADComponent component;
323
324         ComponentRef(String JavaDoc name) {
325             componentName = name;
326         }
327
328         ComponentRef(RADComponent metacomp) {
329             componentName = metacomp.getName();
330             component = metacomp;
331         }
332
333         public FormDesignValue copy(FormProperty formProperty) {
334             return null;
335         }
336         
337         public boolean equals(Object JavaDoc obj) {
338             boolean equal;
339             
340             if (obj instanceof ComponentRef) {
341                 ComponentRef ref = (ComponentRef)obj;
342                 
343                 equal = (ref.component == component);
344                 if (componentName == null) {
345                     equal = equal && (ref.componentName == null);
346                 } else {
347                     equal = equal && componentName.equals(ref.componentName);
348                 }
349             } else {
350                 equal = (obj instanceof RADComponent && obj == component);
351             }
352             
353             return equal;
354         }
355
356         String JavaDoc getJavaInitString() {
357             checkComponent();
358
359             if (component != null) {
360                 if (component == component.getFormModel().getTopRADComponent())
361                     return "this"; // NOI18N
362
}
363             else if (!NULL_REF.equals(componentName))
364                 return null; // invalid reference
365

366             return componentName;
367         }
368
369         public RADComponent getComponent() {
370             checkComponent();
371             return component;
372         }
373
374         /** FormDesignValue implementation. */
375         public String JavaDoc getDescription() {
376             checkComponent();
377             return componentName;
378         }
379
380         /** FormDesignValue implementation. */
381         public Object JavaDoc getDesignValue() {
382             checkComponent();
383             return component != null ?
384                    component.getBeanInstance(): IGNORED_VALUE;
385         }
386
387         private void checkComponent() {
388             if (component == null
389                 && !NULL_REF.equals(componentName)
390                 && !INVALID_REF.equals(componentName))
391             {
392                 List compList = getComponents();
393                 Iterator it = compList.iterator();
394                 while (it.hasNext()) {
395                     RADComponent comp = (RADComponent) it.next();
396                     if (comp.getName().equals(componentName)) {
397                         if (comp.isInModel())
398                             component = comp;
399                         break;
400                     }
401                 }
402             }
403             else if (component != null) {
404                 if (!component.isInModel())
405                     component = null;
406                 else
407                     componentName = component.getName();
408             }
409         }
410     }
411 }
412
Popular Tags