KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.*;
23 import java.beans.*;
24 import java.util.*;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.security.*;
27
28 import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor;
29 import org.openide.explorer.propertysheet.PropertyEnv;
30 import org.openide.explorer.propertysheet.ExPropertyEditor;
31 import org.openide.nodes.*;
32
33 /** A multiplexing PropertyEditor used in the form editor.
34  * It allows multiple editors to be used with one currently selected.
35  *
36  * @author Ian Formanek
37  */

38
39 public class FormPropertyEditor implements PropertyEditor,
40                                            PropertyChangeListener,
41                                            EnhancedPropertyEditor,
42                                            ExPropertyEditor
43 {
44     private static String JavaDoc NO_VALUE_TEXT;
45
46     private Object JavaDoc value = BeanSupport.NO_VALUE;
47
48     private FormProperty property;
49     private WeakReference JavaDoc propertyEnv;
50
51     private PropertyEditor[] allEditors;
52     private PropertyChangeSupport changeSupport;
53     
54     /** Crates a new FormPropertyEditor */
55     FormPropertyEditor(FormProperty property) {
56         this.property = property;
57         PropertyEditor prEd = property.getCurrentEditor();
58         if (prEd != null) {
59             prEd.addPropertyChangeListener(this);
60             value = prEd.getValue();
61         }
62     }
63
64     Class JavaDoc getPropertyType() {
65         return property.getValueType();
66     }
67
68     FormProperty getProperty() {
69         return property;
70     }
71
72     FormPropertyContext getPropertyContext() {
73         return property.getPropertyContext();
74     }
75
76     PropertyEnv getPropertyEnv() {
77         return propertyEnv != null ? (PropertyEnv) propertyEnv.get() : null;
78     }
79
80     PropertyEditor getCurrentEditor() {
81         return property.getCurrentEditor();
82     }
83
84     // -----------------------------------------------------------------------------
85
// PropertyChangeListener implementation
86

87     public void propertyChange(PropertyChangeEvent evt) {
88         PropertyEditor prEd = property.getCurrentEditor();
89         if (prEd != null)
90             value = prEd.getValue();
91
92         // we run this as privileged to avoid security problems - because
93
// the property change can be fired from untrusted property editor code
94
AccessController.doPrivileged(new PrivilegedAction() {
95             public Object JavaDoc run() {
96                 FormPropertyEditor.this.firePropertyChange();
97                 return null;
98             }
99         });
100     }
101
102     // -----------------------------------------------------------------------------
103
// PropertyEditor implementation
104

105     /**
106      * Set(or change) the object that is to be edited.
107      * @param value The new target object to be edited. Note that this
108      * object should not be modified by the PropertyEditor, rather
109      * the PropertyEditor should create a new object to hold any
110      * modified value.
111      */

112     public void setValue(Object JavaDoc newValue) {
113         value = newValue;
114
115         PropertyEditor prEd = property.getCurrentEditor();
116         if (value != BeanSupport.NO_VALUE && prEd != null)
117             prEd.setValue(value);
118     }
119
120     /**
121      * Gets the value of the property.
122      *
123      * @return The value of the property.
124      */

125     public Object JavaDoc getValue() {
126         PropertyEditor prEd = property.getCurrentEditor();
127         return prEd != null ? prEd.getValue() : value;
128     }
129
130     // -----------------------------------------------------------------------------
131

132     /**
133      * Determines whether the class will honor the painValue method.
134      *
135      * @return True if the class will honor the paintValue method.
136      */

137     public boolean isPaintable() {
138         PropertyEditor prEd = property.getCurrentEditor();
139         return prEd != null ? prEd.isPaintable() : false;
140     }
141
142     /**
143      * Paint a representation of the value into a given area of screen
144      * real estate. Note that the propertyEditor is responsible for doing
145      * its own clipping so that it fits into the given rectangle.
146      * <p>
147      * If the PropertyEditor doesn't honor paint requests(see isPaintable)
148      * this method should be a silent noop.
149      *
150      * @param gfx Graphics object to paint into.
151      * @param box Rectangle within graphics object into which we should paint.
152      */

153     public void paintValue(Graphics gfx, Rectangle box) {
154         PropertyEditor prEd = property.getCurrentEditor();
155         if (prEd != null)
156             prEd.paintValue(gfx, box);
157     }
158
159     // -----------------------------------------------------------------------------
160

161     /**
162      * This method is intended for use when generating Java code to set
163      * the value of the property. It should return a fragment of Java code
164      * that can be used to initialize a variable with the current property
165      * value.
166      * <p>
167      * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
168      *
169      * @return A fragment of Java code representing an initializer for the
170      * current value.
171      */

172     public String JavaDoc getJavaInitializationString() {
173         PropertyEditor prEd = property.getCurrentEditor();
174         return prEd != null ? prEd.getJavaInitializationString() : null;
175     }
176
177     // -----------------------------------------------------------------------------
178

179     /**
180      * Gets the property value as a string suitable for presentation
181      * to a human to edit.
182      *
183      * @return The property value as a string suitable for presentation
184      * to a human to edit.
185      * <p> Returns "null" is the value can't be expressed as a string.
186      * <p> If a non-null value is returned, then the PropertyEditor should
187      * be prepared to parse that string back in setAsText().
188      */

189     public String JavaDoc getAsText() {
190         if (value == BeanSupport.NO_VALUE) {
191             if (NO_VALUE_TEXT == null)
192                 NO_VALUE_TEXT = FormUtils.getBundleString("CTL_ValueNotSet"); // NOI18N
193
return NO_VALUE_TEXT;
194         }
195
196         PropertyEditor prEd = property.getCurrentEditor();
197         return prEd != null ? prEd.getAsText() : null;
198     }
199
200     /**
201      * Sets the property value by parsing a given String. May raise
202      * java.lang.IllegalArgumentException if either the String is
203      * badly formatted or if this kind of property can't be expressed
204      * as text.
205      *
206      * @param text The string to be parsed.
207      */

208     public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
209         PropertyEditor prEd = property.getCurrentEditor();
210         if (prEd != null)
211             prEd.setAsText(text);
212     }
213
214     // -----------------------------------------------------------------------------
215

216     /**
217      * If the property value must be one of a set of known tagged values,
218      * then this method should return an array of the tag values. This can
219      * be used to represent(for example) enum values. If a PropertyEditor
220      * supports tags, then it should support the use of setAsText with
221      * a tag value as a way of setting the value.
222      *
223      * @return The tag values for this property. May be null if this
224      * property cannot be represented as a tagged value.
225      *
226      */

227     public String JavaDoc[] getTags() {
228         PropertyEditor prEd = property.getCurrentEditor();
229         return prEd != null ? prEd.getTags() : null;
230     }
231
232     // -----------------------------------------------------------------------------
233

234     /**
235      * A PropertyEditor may chose to make available a full custom Component
236      * that edits its property value. It is the responsibility of the
237      * PropertyEditor to hook itself up to its editor Component itself and
238      * to report property value changes by firing a PropertyChange event.
239      * <P>
240      * The higher-level code that calls getCustomEditor may either embed
241      * the Component in some larger property sheet, or it may put it in
242      * its own individual dialog, or ...
243      *
244      * @return A java.awt.Component that will allow a human to directly
245      * edit the current property value. May be null if this is
246      * not supported.
247      */

248
249     public Component getCustomEditor() {
250         Component customEditor;
251
252         PropertyEditor prEd = property.getCurrentEditor();
253         if (prEd != null && prEd.supportsCustomEditor()) {
254             customEditor = prEd.getCustomEditor();
255             if (customEditor instanceof Window)
256                 return customEditor;
257         }
258         else customEditor = null;
259
260         return new FormCustomEditor(this, customEditor);
261     }
262
263     /**
264      * Determines whether the propertyEditor can provide a custom editor.
265      *
266      * @return True if the propertyEditor can provide a custom editor.
267      */

268     public boolean supportsCustomEditor() {
269         PropertyEditor[] editors = getAllEditors();
270
271         if (!property.canWrite()) { // read only property
272
for (int i=0; i < editors.length; i++)
273                 if (!editors[i].getClass().equals(RADConnectionPropertyEditor.class)
274                         && editors[i].supportsCustomEditor())
275                     return true;
276             return false;
277         }
278
279         // writable property
280
if (editors.length > 1)
281             return true; // we must at least allow to choose the editor
282
if (editors.length == 1)
283             return editors[0].supportsCustomEditor();
284
285         return false;
286     }
287
288     synchronized PropertyEditor[] getAllEditors() {
289         if (allEditors == null) {
290             PropertyEditor expliciteEditor = property.getExpliciteEditor();
291             PropertyEditor[] typeEditors =
292                 FormPropertyEditorManager.getAllEditors(property);
293             if (expliciteEditor != null) {
294                 // expliciteEditor could be already in typeEditors
295
for (int i=0; i < typeEditors.length; i++)
296                     if (expliciteEditor.getClass().equals(typeEditors[i].getClass())) {
297                         typeEditors[i] = expliciteEditor;
298                         expliciteEditor = null;
299                         break;
300                     }
301             }
302             if (expliciteEditor != null) {
303                 allEditors = new PropertyEditor[typeEditors.length+1];
304                 allEditors[0] = expliciteEditor;
305                 System.arraycopy(typeEditors, 0, allEditors, 1, typeEditors.length);
306             }
307             else allEditors = typeEditors;
308         }
309         return allEditors;
310     }
311
312     // -----------------------------------------------------------------------------
313
// EnhancedPropertyEditor implementation
314

315     /** Get an in-place editor.
316      * @return a custom property editor to be shown inside the property
317      * sheet
318      */

319     public Component getInPlaceCustomEditor() {
320         PropertyEditor prEd = property.getCurrentEditor();
321         return prEd instanceof EnhancedPropertyEditor ?
322                ((EnhancedPropertyEditor)prEd).getInPlaceCustomEditor() : null;
323     }
324
325     /** Test for support of in-place custom editors.
326      * @return <code>true</code> if supported
327      */

328     public boolean hasInPlaceCustomEditor() {
329         PropertyEditor prEd = property.getCurrentEditor();
330         return prEd instanceof EnhancedPropertyEditor ?
331                ((EnhancedPropertyEditor)prEd).hasInPlaceCustomEditor() : false;
332     }
333
334     /** Test for support of editing of tagged values.
335      * Must also accept custom strings, otherwise you may may specify a standard property editor accepting only tagged values.
336      * @return <code>true</code> if supported
337      */

338     public boolean supportsEditingTaggedValues() {
339         PropertyEditor prEd = property.getCurrentEditor();
340         return prEd instanceof EnhancedPropertyEditor ?
341                ((EnhancedPropertyEditor)prEd).supportsEditingTaggedValues() : false;
342     }
343
344     // -------------------------------------------------------------
345
// FormPropertyContainer implementation
346

347 // public Node.Property[] getProperties() {
348
// if (modifiedEditor instanceof FormPropertyContainer)
349
// return ((FormPropertyContainer)modifiedEditor).getProperties();
350
// else
351
// return null;
352
// }
353

354     // -----------------------------------------------------------------------------
355

356     /**
357      * Register a listener for the PropertyChange event. The class will
358      * fire a PropertyChange value whenever the value is updated.
359      *
360      * @param listener An object to be invoked when a PropertyChange
361      * event is fired.
362      */

363     public void addPropertyChangeListener(PropertyChangeListener l) {
364         synchronized (this) {
365             if (changeSupport == null)
366                 changeSupport = new PropertyChangeSupport(this);
367         }
368         changeSupport.addPropertyChangeListener(l);
369     }
370
371     /**
372      * Remove a listener for the PropertyChange event.
373      *
374      * @param listener The PropertyChange listener to be removed.
375      */

376     public void removePropertyChangeListener(PropertyChangeListener l) {
377         if (changeSupport != null)
378             changeSupport.removePropertyChangeListener(l);
379     }
380
381     /**
382      * Report that we have been modified to any interested listeners.
383      */

384     void firePropertyChange() {
385         if (changeSupport != null)
386             changeSupport.firePropertyChange(null, null, null);
387     }
388
389     // -------------
390
// ExPropertyEditor implementation
391

392     /**
393      * This method is called by the IDE to pass
394      * the environment to the property editor.
395      */

396     public void attachEnv(PropertyEnv env) {
397         propertyEnv = new WeakReference JavaDoc(env);
398         PropertyEditor prEd = property.getCurrentEditor();
399         if (prEd instanceof ExPropertyEditor)
400             ((ExPropertyEditor)prEd).attachEnv(env);
401     }
402
403     // ---------
404
// delegating hashCode() and equals(Object) methods to modifiedEditor - for
405
// PropertyPanel mapping property editors to PropertyEnv
406

407     public int hashCode() {
408         PropertyEditor prEd = property.getCurrentEditor();
409         return prEd != null ? prEd.hashCode() : super.hashCode();
410     }
411
412     public boolean equals(Object JavaDoc obj) {
413         return obj != null ? hashCode() == obj.hashCode() : false;
414     }
415 }
416
Popular Tags