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