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