PropertyEditorSupport.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* java.beans.PropertyEditorSupport
  2. Copyright (C) 1998, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.beans;
  32. /**
  33. * PropertyEditorSupport helps with PropertyEditors,
  34. * implementing base functionality that they usually must
  35. * have but which is a pain to implement. You may extend
  36. * from this class or use it as a standalone.<P>
  37. *
  38. * This class does not do any painting or actual editing.
  39. * For that, you must use or extend it. See the
  40. * PropertyEditor class for better descriptions of what
  41. * the various methods do.
  42. *
  43. * @author John Keiser
  44. * @author Robert Schuster
  45. * @since 1.1
  46. * @status updated to 1.5
  47. */
  48. public class PropertyEditorSupport implements PropertyEditor
  49. {
  50. Object eventSource;
  51. Object value;
  52. PropertyChangeSupport pSupport;
  53. /** Call this constructor when you are deriving from
  54. * PropertyEditorSupport.
  55. *
  56. * Using this constructor the event source is this PropertyEditorSupport
  57. * instance itself.
  58. *
  59. * @since 1.5
  60. * @specnote this was <code>protected</code> prior to 1.5
  61. */
  62. public PropertyEditorSupport()
  63. {
  64. eventSource = this;
  65. pSupport = new PropertyChangeSupport(this);
  66. }
  67. /** Call this constructor when you are using
  68. * PropertyEditorSupport as a helper object.
  69. *
  70. * This constructor throws a NullPointerException when <code>source</code> is <code>null</code>,
  71. * for compatibility reasons with J2SDK 1.5.0 .
  72. *
  73. * @param source The source to use when firing
  74. * property change events.
  75. * @since 1.5
  76. * @specnote this was <code>protected</code> prior to 1.5
  77. */
  78. public PropertyEditorSupport(Object source)
  79. {
  80. // note: constructor rejects source being null for the sake of compatibility
  81. // with official 1.5.0 implementation
  82. if (source == null)
  83. throw new NullPointerException("Event source must not be null.");
  84. eventSource = source;
  85. pSupport = new PropertyChangeSupport(eventSource);
  86. }
  87. /** Sets the current value of the property and a property change
  88. * event is fired to all registered PropertyChangeListener instances.
  89. *
  90. * @param newValue The new value for the property.
  91. */
  92. public void setValue(Object newValue)
  93. {
  94. value = newValue;
  95. // specification in java.beans.PropertyChangeEvent says
  96. // that without a property name (first argument) the
  97. // new and the old value should always be null
  98. pSupport.firePropertyChange(null, null, null);
  99. }
  100. /** Gets the current value of the property.
  101. *
  102. * @return the current value of the property.
  103. */
  104. public Object getValue()
  105. {
  106. return value;
  107. }
  108. /** Gets whether this object is paintable or not.
  109. *
  110. * @return <CODE>false</CODE>
  111. */
  112. public boolean isPaintable()
  113. {
  114. return false;
  115. }
  116. /** Paints this object. This class does nothing in
  117. * this method.
  118. */
  119. public void paintValue(java.awt.Graphics g, java.awt.Rectangle r)
  120. {
  121. }
  122. /** Gets the Java initialization String for the current
  123. * value of the Object. This class returns gibberish or
  124. * null (though the spec does not say which).<P>
  125. * <STRONG>Implementation Note:</STRONG> This class
  126. * returns the string "@$#^" to make sure the code will
  127. * be broken, so that you will know to override it when
  128. * you create your own property editor.
  129. *
  130. * @return the Java initialization string.
  131. */
  132. public String getJavaInitializationString()
  133. {
  134. return "@$#^";
  135. }
  136. /** Gets the value as text.
  137. * In this class, you cannot count on getAsText() doing
  138. * anything useful, although in this implementation I
  139. * do toString().
  140. *
  141. * @return the value as text.
  142. */
  143. public String getAsText()
  144. {
  145. return value != null ? value.toString() : "null";
  146. }
  147. /** Sets the value as text.
  148. * In this class, you cannot count on setAsText() doing
  149. * anything useful across implementations.
  150. * <STRONG>Implementation Note:</STRONG> In this
  151. * implementation it checks if the String is "null", and
  152. * if it is, sets the value to null, otherwise it throws
  153. * an IllegalArgumentException.
  154. *
  155. * @param s the text to convert to a new value.
  156. * @exception IllegalArgumentException if the text is
  157. * malformed.
  158. */
  159. public void setAsText(String s) throws IllegalArgumentException
  160. {
  161. if (s.equals("null"))
  162. setValue(null);
  163. else
  164. throw new IllegalArgumentException();
  165. }
  166. /** Returns a list of possible choices for the value.
  167. *
  168. * @return <CODE>null</CODE>
  169. */
  170. public String[] getTags()
  171. {
  172. return null;
  173. }
  174. /** Returns a custom component to edit the value.
  175. *
  176. * @return <CODE>null</CODE> in this class.
  177. */
  178. public java.awt.Component getCustomEditor()
  179. {
  180. return null;
  181. }
  182. /** Finds out whether this property editor supports a
  183. * custom component to edit its value.
  184. *
  185. * @return <CODE>false</CODE> in this class.
  186. */
  187. public boolean supportsCustomEditor()
  188. {
  189. return false;
  190. }
  191. /** Adds a property change listener to this property editor.
  192. *
  193. * @param l the listener to add.
  194. */
  195. public void addPropertyChangeListener(PropertyChangeListener l)
  196. {
  197. pSupport.addPropertyChangeListener(l);
  198. }
  199. /** Removes a property change listener from this property editor.
  200. *
  201. * @param l the listener to remove.
  202. */
  203. public void removePropertyChangeListener(PropertyChangeListener l)
  204. {
  205. pSupport.removePropertyChangeListener(l);
  206. }
  207. /** Notifies people that we've changed, although we don't
  208. * tell them just how.
  209. */
  210. public void firePropertyChange()
  211. {
  212. pSupport.firePropertyChange(null, null, null);
  213. }
  214. /** Returns the bean that is used as the source of events.
  215. *
  216. * @return The event source object
  217. * @since 1.5
  218. */
  219. public Object getSource()
  220. {
  221. return eventSource;
  222. }
  223. /** Sets the bean that is used as the source of events
  224. * when property changes occur.
  225. *
  226. * The event source bean is for informational purposes only
  227. * and should not be changed by the <code>PropertyEditor</code>.
  228. *
  229. * @param source
  230. * @since 1.5
  231. */
  232. public void setSource(Object source)
  233. {
  234. eventSource = source;
  235. }
  236. }