KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > beans > PropertyEditorSupport


1 /*
2  * @(#)PropertyEditorSupport.java 1.20 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.beans;
9
10 import java.beans.*;
11
12 /**
13  * This is a support class to help build property editors.
14  * <p>
15  * It can be used either as a base class or as a delagatee.
16  */

17
18 public class PropertyEditorSupport implements PropertyEditor JavaDoc {
19
20     /**
21      * Constructs a <code>PropertyEditorSupport</code> object.
22      *
23      * @since 1.5
24      */

25     public PropertyEditorSupport() {
26     setSource(this);
27     }
28
29     /**
30      * Constructs a <code>PropertyEditorSupport</code> object.
31      *
32      * @param source the source used for event firing
33      * @since 1.5
34      */

35     public PropertyEditorSupport(Object JavaDoc source) {
36     if (source == null) {
37        throw new NullPointerException JavaDoc();
38     }
39     setSource(source);
40     }
41
42     /**
43      * Returns the bean that is used as the
44      * source of events. If the source has not
45      * been explicitly set then this instance of
46      * <code>PropertyEditorSupport</code> is returned.
47      *
48      * @return the source object or this instance
49      * @since 1.5
50      */

51     public Object JavaDoc getSource() {
52     return source;
53     }
54
55     /**
56      * Sets the source bean.
57      * <p>
58      * The source bean is used as the source of events
59      * for the property changes. This source should be used for information
60      * purposes only and should not be modified by the PropertyEditor.
61      *
62      * @param source source object to be used for events
63      * @since 1.5
64      */

65     public void setSource(Object JavaDoc source) {
66     this.source = source;
67     }
68
69     /**
70      * Set (or change) the object that is to be edited.
71      *
72      * @param value The new target object to be edited. Note that this
73      * object should not be modified by the PropertyEditor, rather
74      * the PropertyEditor should create a new object to hold any
75      * modified value.
76      */

77     public void setValue(Object JavaDoc value) {
78     this.value = value;
79     firePropertyChange();
80     }
81
82     /**
83      * Gets the value of the property.
84      *
85      * @return The value of the property.
86      */

87     public Object JavaDoc getValue() {
88     return value;
89     }
90
91     //----------------------------------------------------------------------
92

93     /**
94      * Determines whether the class will honor the painValue method.
95      *
96      * @return True if the class will honor the paintValue method.
97      */

98
99     public boolean isPaintable() {
100     return false;
101     }
102
103     /**
104      * Paint a representation of the value into a given area of screen
105      * real estate. Note that the propertyEditor is responsible for doing
106      * its own clipping so that it fits into the given rectangle.
107      * <p>
108      * If the PropertyEditor doesn't honor paint requests (see isPaintable)
109      * this method should be a silent noop.
110      *
111      * @param gfx Graphics object to paint into.
112      * @param box Rectangle within graphics object into which we should paint.
113      */

114     public void paintValue(java.awt.Graphics JavaDoc gfx, java.awt.Rectangle JavaDoc box) {
115     }
116
117     //----------------------------------------------------------------------
118

119     /**
120      * This method is intended for use when generating Java code to set
121      * the value of the property. It should return a fragment of Java code
122      * that can be used to initialize a variable with the current property
123      * value.
124      * <p>
125      * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
126      *
127      * @return A fragment of Java code representing an initializer for the
128      * current value.
129      */

130     public String JavaDoc getJavaInitializationString() {
131     return "???";
132     }
133
134     //----------------------------------------------------------------------
135

136     /**
137      * Gets the property value as a string suitable for presentation
138      * to a human to edit.
139      *
140      * @return The property value as a string suitable for presentation
141      * to a human to edit.
142      * <p> Returns "null" is the value can't be expressed as a string.
143      * <p> If a non-null value is returned, then the PropertyEditor should
144      * be prepared to parse that string back in setAsText().
145      */

146     public String JavaDoc getAsText() {
147     if (value instanceof String JavaDoc) {
148         return (String JavaDoc)value;
149     }
150     return ("" + value);
151     }
152
153     /**
154      * Sets the property value by parsing a given String. May raise
155      * java.lang.IllegalArgumentException if either the String is
156      * badly formatted or if this kind of property can't be expressed
157      * as text.
158      *
159      * @param text The string to be parsed.
160      */

161     public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
162     if (value instanceof String JavaDoc) {
163         setValue(text);
164         return;
165     }
166     throw new java.lang.IllegalArgumentException JavaDoc(text);
167     }
168
169     //----------------------------------------------------------------------
170

171     /**
172      * If the property value must be one of a set of known tagged values,
173      * then this method should return an array of the tag values. This can
174      * be used to represent (for example) enum values. If a PropertyEditor
175      * supports tags, then it should support the use of setAsText with
176      * a tag value as a way of setting the value.
177      *
178      * @return The tag values for this property. May be null if this
179      * property cannot be represented as a tagged value.
180      *
181      */

182     public String JavaDoc[] getTags() {
183     return null;
184     }
185
186     //----------------------------------------------------------------------
187

188     /**
189      * A PropertyEditor may chose to make available a full custom Component
190      * that edits its property value. It is the responsibility of the
191      * PropertyEditor to hook itself up to its editor Component itself and
192      * to report property value changes by firing a PropertyChange event.
193      * <P>
194      * The higher-level code that calls getCustomEditor may either embed
195      * the Component in some larger property sheet, or it may put it in
196      * its own individual dialog, or ...
197      *
198      * @return A java.awt.Component that will allow a human to directly
199      * edit the current property value. May be null if this is
200      * not supported.
201      */

202
203     public java.awt.Component JavaDoc getCustomEditor() {
204     return null;
205     }
206
207     /**
208      * Determines whether the propertyEditor can provide a custom editor.
209      *
210      * @return True if the propertyEditor can provide a custom editor.
211      */

212     public boolean supportsCustomEditor() {
213     return false;
214     }
215   
216     //----------------------------------------------------------------------
217

218     /**
219      * Register a listener for the PropertyChange event. The class will
220      * fire a PropertyChange value whenever the value is updated.
221      *
222      * @param listener An object to be invoked when a PropertyChange
223      * event is fired.
224      */

225     public synchronized void addPropertyChangeListener(
226                 PropertyChangeListener JavaDoc listener) {
227     if (listeners == null) {
228         listeners = new java.util.Vector JavaDoc();
229     }
230     listeners.addElement(listener);
231     }
232
233     /**
234      * Remove a listener for the PropertyChange event.
235      *
236      * @param listener The PropertyChange listener to be removed.
237      */

238     public synchronized void removePropertyChangeListener(
239                 PropertyChangeListener JavaDoc listener) {
240     if (listeners == null) {
241         return;
242     }
243     listeners.removeElement(listener);
244     }
245
246     /**
247      * Report that we have been modified to any interested listeners.
248      */

249     public void firePropertyChange() {
250     java.util.Vector JavaDoc targets;
251     synchronized (this) {
252         if (listeners == null) {
253             return;
254         }
255         targets = (java.util.Vector JavaDoc) listeners.clone();
256     }
257     // Tell our listeners that "everything" has changed.
258
PropertyChangeEvent JavaDoc evt = new PropertyChangeEvent JavaDoc(source, null, null, null);
259
260     for (int i = 0; i < targets.size(); i++) {
261         PropertyChangeListener JavaDoc target = (PropertyChangeListener JavaDoc)targets.elementAt(i);
262         target.propertyChange(evt);
263     }
264     }
265
266     //----------------------------------------------------------------------
267

268     private Object JavaDoc value;
269     private Object JavaDoc source;
270     private java.util.Vector JavaDoc listeners;
271 }
272
Popular Tags