001/* java.beans.PropertyEditorSupport
002   Copyright (C) 1998, 2004 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.beans;
040
041
042/**
043 * PropertyEditorSupport helps with PropertyEditors,
044 * implementing base functionality that they usually must
045 * have but which is a pain to implement.  You may extend
046 * from this class or use it as a standalone.<P>
047 *
048 * This class does not do any painting or actual editing.
049 * For that, you must use or extend it.  See the
050 * PropertyEditor class for better descriptions of what
051 * the various methods do.
052 *
053 * @author John Keiser
054 * @author Robert Schuster
055 * @since 1.1
056 * @status updated to 1.5
057 */
058public class PropertyEditorSupport implements PropertyEditor
059{
060  Object eventSource;
061  Object value;
062  PropertyChangeSupport pSupport;
063
064  /** Call this constructor when you are deriving from
065   * PropertyEditorSupport.
066   *
067   * Using this constructor the event source is this PropertyEditorSupport
068   * instance itself.
069   *
070   * @since 1.5
071   * @specnote this was <code>protected</code> prior to 1.5
072   */
073  public PropertyEditorSupport()
074  {
075    eventSource = this;
076    pSupport = new PropertyChangeSupport(this);
077  }
078
079  /** Call this constructor when you are using
080   * PropertyEditorSupport as a helper object.
081   *
082   * This constructor throws a NullPointerException when <code>source</code> is <code>null</code>,
083   * for compatibility reasons with J2SDK 1.5.0 .
084   *
085   * @param source The source to use when firing
086   * property change events.
087   * @since 1.5
088   * @specnote this was <code>protected</code> prior to 1.5
089   */
090  public PropertyEditorSupport(Object source)
091  {
092    // note: constructor rejects source being null for the sake of compatibility
093    // with official 1.5.0 implementation
094    if (source == null)
095      throw new NullPointerException("Event source must not be null.");
096
097    eventSource = source;
098    pSupport = new PropertyChangeSupport(eventSource);
099  }
100
101  /** Sets the current value of the property and a property change
102   * event is fired to all registered PropertyChangeListener instances.
103   *
104   * @param newValue The new value for the property.
105   */
106  public void setValue(Object newValue)
107  {
108    value = newValue;
109
110    // specification in java.beans.PropertyChangeEvent says
111    // that without a property name (first argument) the
112    // new and the old value should always be null
113    pSupport.firePropertyChange(null, null, null);
114  }
115
116  /** Gets the current value of the property.
117   *
118   * @return the current value of the property.
119   */
120  public Object getValue()
121  {
122    return value;
123  }
124
125  /** Gets whether this object is paintable or not.
126   *
127   * @return <CODE>false</CODE>
128   */
129  public boolean isPaintable()
130  {
131    return false;
132  }
133
134  /** Paints this object.  This class does nothing in
135   * this method.
136   */
137  public void paintValue(java.awt.Graphics g, java.awt.Rectangle r)
138  {
139  }
140
141  /** Gets the Java initialization String for the current
142   * value of the Object.  This class returns gibberish or
143   * null (though the spec does not say which).<P>
144   * <STRONG>Implementation Note:</STRONG> This class
145   * returns the string "@$#^" to make sure the code will
146   * be broken, so that you will know to override it when
147   * you create your own property editor.
148   *
149   * @return the Java initialization string.
150   */
151  public String getJavaInitializationString()
152  {
153    return "@$#^";
154  }
155
156  /** Gets the value as text.
157   * In this class, you cannot count on getAsText() doing
158   * anything useful, although in this implementation I
159   * do toString().
160   *
161   * @return the value as text.
162   */
163  public String getAsText()
164  {
165    return value != null ? value.toString() : "null";
166  }
167
168  /** Sets the value as text.
169   * In this class, you cannot count on setAsText() doing
170   * anything useful across implementations.
171   * <STRONG>Implementation Note:</STRONG> In this
172   * implementation it checks if the String is "null", and
173   * if it is, sets the value to null, otherwise it throws
174   * an IllegalArgumentException.
175   *
176   * @param s the text to convert to a new value.
177   * @exception IllegalArgumentException if the text is
178   * malformed.
179   */
180  public void setAsText(String s) throws IllegalArgumentException
181  {
182    if (s.equals("null"))
183      setValue(null);
184    else
185      throw new IllegalArgumentException();
186  }
187
188  /** Returns a list of possible choices for the value.
189   *
190   * @return <CODE>null</CODE>
191   */
192  public String[] getTags()
193  {
194    return null;
195  }
196
197  /** Returns a custom component to edit the value.
198   *
199   * @return <CODE>null</CODE> in this class.
200   */
201  public java.awt.Component getCustomEditor()
202  {
203    return null;
204  }
205
206  /** Finds out whether this property editor supports a
207   * custom component to edit its value.
208   *
209   * @return <CODE>false</CODE> in this class.
210   */
211  public boolean supportsCustomEditor()
212  {
213    return false;
214  }
215
216  /** Adds a property change listener to this property editor.
217   *
218   * @param l the listener to add.
219   */
220  public void addPropertyChangeListener(PropertyChangeListener l)
221  {
222    pSupport.addPropertyChangeListener(l);
223  }
224
225  /** Removes a property change listener from this property editor.
226   *
227   * @param l the listener to remove.
228   */
229  public void removePropertyChangeListener(PropertyChangeListener l)
230  {
231    pSupport.removePropertyChangeListener(l);
232  }
233
234  /** Notifies people that we've changed, although we don't
235   * tell them just how.
236   */
237  public void firePropertyChange()
238  {
239    pSupport.firePropertyChange(null, null, null);
240  }
241
242  /** Returns the bean that is used as the source of events.
243   *
244   * @return The event source object
245   * @since 1.5
246   */
247  public Object getSource()
248  {
249    return eventSource;
250  }
251
252  /** Sets the bean that is used as the source of events
253   * when property changes occur.
254   *
255   * The event source bean is for informational purposes only
256   * and should not be changed by the <code>PropertyEditor</code>.
257   *
258   * @param source
259   * @since 1.5
260   */
261  public void setSource(Object source)
262  {
263    eventSource = source;
264  }
265}