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. package java.beans;
  8. import java.beans.*;
  9. /**
  10. * This is a support class to help build property editors.
  11. * <p>
  12. * It can be used either as a base class or as a delagatee.
  13. */
  14. public class PropertyEditorSupport implements PropertyEditor {
  15. /**
  16. * Constructs a <code>PropertyEditorSupport</code> object.
  17. *
  18. * @since 1.5
  19. */
  20. public PropertyEditorSupport() {
  21. setSource(this);
  22. }
  23. /**
  24. * Constructs a <code>PropertyEditorSupport</code> object.
  25. *
  26. * @param source the source used for event firing
  27. * @since 1.5
  28. */
  29. public PropertyEditorSupport(Object source) {
  30. if (source == null) {
  31. throw new NullPointerException();
  32. }
  33. setSource(source);
  34. }
  35. /**
  36. * Returns the bean that is used as the
  37. * source of events. If the source has not
  38. * been explicitly set then this instance of
  39. * <code>PropertyEditorSupport</code> is returned.
  40. *
  41. * @return the source object or this instance
  42. * @since 1.5
  43. */
  44. public Object getSource() {
  45. return source;
  46. }
  47. /**
  48. * Sets the source bean.
  49. * <p>
  50. * The source bean is used as the source of events
  51. * for the property changes. This source should be used for information
  52. * purposes only and should not be modified by the PropertyEditor.
  53. *
  54. * @param source source object to be used for events
  55. * @since 1.5
  56. */
  57. public void setSource(Object source) {
  58. this.source = source;
  59. }
  60. /**
  61. * Set (or change) the object that is to be edited.
  62. *
  63. * @param value The new target object to be edited. Note that this
  64. * object should not be modified by the PropertyEditor, rather
  65. * the PropertyEditor should create a new object to hold any
  66. * modified value.
  67. */
  68. public void setValue(Object value) {
  69. this.value = value;
  70. firePropertyChange();
  71. }
  72. /**
  73. * Gets the value of the property.
  74. *
  75. * @return The value of the property.
  76. */
  77. public Object getValue() {
  78. return value;
  79. }
  80. //----------------------------------------------------------------------
  81. /**
  82. * Determines whether the class will honor the painValue method.
  83. *
  84. * @return True if the class will honor the paintValue method.
  85. */
  86. public boolean isPaintable() {
  87. return false;
  88. }
  89. /**
  90. * Paint a representation of the value into a given area of screen
  91. * real estate. Note that the propertyEditor is responsible for doing
  92. * its own clipping so that it fits into the given rectangle.
  93. * <p>
  94. * If the PropertyEditor doesn't honor paint requests (see isPaintable)
  95. * this method should be a silent noop.
  96. *
  97. * @param gfx Graphics object to paint into.
  98. * @param box Rectangle within graphics object into which we should paint.
  99. */
  100. public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
  101. }
  102. //----------------------------------------------------------------------
  103. /**
  104. * This method is intended for use when generating Java code to set
  105. * the value of the property. It should return a fragment of Java code
  106. * that can be used to initialize a variable with the current property
  107. * value.
  108. * <p>
  109. * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
  110. *
  111. * @return A fragment of Java code representing an initializer for the
  112. * current value.
  113. */
  114. public String getJavaInitializationString() {
  115. return "???";
  116. }
  117. //----------------------------------------------------------------------
  118. /**
  119. * Gets the property value as a string suitable for presentation
  120. * to a human to edit.
  121. *
  122. * @return The property value as a string suitable for presentation
  123. * to a human to edit.
  124. * <p> Returns "null" is the value can't be expressed as a string.
  125. * <p> If a non-null value is returned, then the PropertyEditor should
  126. * be prepared to parse that string back in setAsText().
  127. */
  128. public String getAsText() {
  129. if (value instanceof String) {
  130. return (String)value;
  131. }
  132. return ("" + value);
  133. }
  134. /**
  135. * Sets the property value by parsing a given String. May raise
  136. * java.lang.IllegalArgumentException if either the String is
  137. * badly formatted or if this kind of property can't be expressed
  138. * as text.
  139. *
  140. * @param text The string to be parsed.
  141. */
  142. public void setAsText(String text) throws java.lang.IllegalArgumentException {
  143. if (value instanceof String) {
  144. setValue(text);
  145. return;
  146. }
  147. throw new java.lang.IllegalArgumentException(text);
  148. }
  149. //----------------------------------------------------------------------
  150. /**
  151. * If the property value must be one of a set of known tagged values,
  152. * then this method should return an array of the tag values. This can
  153. * be used to represent (for example) enum values. If a PropertyEditor
  154. * supports tags, then it should support the use of setAsText with
  155. * a tag value as a way of setting the value.
  156. *
  157. * @return The tag values for this property. May be null if this
  158. * property cannot be represented as a tagged value.
  159. *
  160. */
  161. public String[] getTags() {
  162. return null;
  163. }
  164. //----------------------------------------------------------------------
  165. /**
  166. * A PropertyEditor may chose to make available a full custom Component
  167. * that edits its property value. It is the responsibility of the
  168. * PropertyEditor to hook itself up to its editor Component itself and
  169. * to report property value changes by firing a PropertyChange event.
  170. * <P>
  171. * The higher-level code that calls getCustomEditor may either embed
  172. * the Component in some larger property sheet, or it may put it in
  173. * its own individual dialog, or ...
  174. *
  175. * @return A java.awt.Component that will allow a human to directly
  176. * edit the current property value. May be null if this is
  177. * not supported.
  178. */
  179. public java.awt.Component getCustomEditor() {
  180. return null;
  181. }
  182. /**
  183. * Determines whether the propertyEditor can provide a custom editor.
  184. *
  185. * @return True if the propertyEditor can provide a custom editor.
  186. */
  187. public boolean supportsCustomEditor() {
  188. return false;
  189. }
  190. //----------------------------------------------------------------------
  191. /**
  192. * Register a listener for the PropertyChange event. The class will
  193. * fire a PropertyChange value whenever the value is updated.
  194. *
  195. * @param listener An object to be invoked when a PropertyChange
  196. * event is fired.
  197. */
  198. public synchronized void addPropertyChangeListener(
  199. PropertyChangeListener listener) {
  200. if (listeners == null) {
  201. listeners = new java.util.Vector();
  202. }
  203. listeners.addElement(listener);
  204. }
  205. /**
  206. * Remove a listener for the PropertyChange event.
  207. *
  208. * @param listener The PropertyChange listener to be removed.
  209. */
  210. public synchronized void removePropertyChangeListener(
  211. PropertyChangeListener listener) {
  212. if (listeners == null) {
  213. return;
  214. }
  215. listeners.removeElement(listener);
  216. }
  217. /**
  218. * Report that we have been modified to any interested listeners.
  219. */
  220. public void firePropertyChange() {
  221. java.util.Vector targets;
  222. synchronized (this) {
  223. if (listeners == null) {
  224. return;
  225. }
  226. targets = (java.util.Vector) listeners.clone();
  227. }
  228. // Tell our listeners that "everything" has changed.
  229. PropertyChangeEvent evt = new PropertyChangeEvent(source, null, null, null);
  230. for (int i = 0; i < targets.size(); i++) {
  231. PropertyChangeListener target = (PropertyChangeListener)targets.elementAt(i);
  232. target.propertyChange(evt);
  233. }
  234. }
  235. //----------------------------------------------------------------------
  236. private Object value;
  237. private Object source;
  238. private java.util.Vector listeners;
  239. }