1. /*
  2. * @(#)PropertyEditor.java 1.37 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. /**
  9. * A PropertyEditor class provides support for GUIs that want to
  10. * allow users to edit a property value of a given type.
  11. * <p>
  12. * PropertyEditor supports a variety of different kinds of ways of
  13. * displaying and updating property values. Most PropertyEditors will
  14. * only need to support a subset of the different options available in
  15. * this API.
  16. * <P>
  17. * Simple PropertyEditors may only support the getAsText and setAsText
  18. * methods and need not support (say) paintValue or getCustomEditor. More
  19. * complex types may be unable to support getAsText and setAsText but will
  20. * instead support paintValue and getCustomEditor.
  21. * <p>
  22. * Every propertyEditor must support one or more of the three simple
  23. * display styles. Thus it can either (1) support isPaintable or (2)
  24. * both return a non-null String[] from getTags() and return a non-null
  25. * value from getAsText or (3) simply return a non-null String from
  26. * getAsText().
  27. * <p>
  28. * Every property editor must support a call on setValue when the argument
  29. * object is of the type for which this is the corresponding propertyEditor.
  30. * In addition, each property editor must either support a custom editor,
  31. * or support setAsText.
  32. * <p>
  33. * Each PropertyEditor should have a null constructor.
  34. */
  35. public interface PropertyEditor {
  36. /**
  37. * Set (or change) the object that is to be edited. Primitive types such
  38. * as "int" must be wrapped as the corresponding object type such as
  39. * "java.lang.Integer".
  40. *
  41. * @param value The new target object to be edited. Note that this
  42. * object should not be modified by the PropertyEditor, rather
  43. * the PropertyEditor should create a new object to hold any
  44. * modified value.
  45. */
  46. void setValue(Object value);
  47. /**
  48. * Gets the property value.
  49. *
  50. * @return The value of the property. Primitive types such as "int" will
  51. * be wrapped as the corresponding object type such as "java.lang.Integer".
  52. */
  53. Object getValue();
  54. //----------------------------------------------------------------------
  55. /**
  56. * Determines whether this property editor is paintable.
  57. *
  58. * @return True if the class will honor the paintValue method.
  59. */
  60. boolean isPaintable();
  61. /**
  62. * Paint a representation of the value into a given area of screen
  63. * real estate. Note that the propertyEditor is responsible for doing
  64. * its own clipping so that it fits into the given rectangle.
  65. * <p>
  66. * If the PropertyEditor doesn't honor paint requests (see isPaintable)
  67. * this method should be a silent noop.
  68. * <p>
  69. * The given Graphics object will have the default font, color, etc of
  70. * the parent container. The PropertyEditor may change graphics attributes
  71. * such as font and color and doesn't need to restore the old values.
  72. *
  73. * @param gfx Graphics object to paint into.
  74. * @param box Rectangle within graphics object into which we should paint.
  75. */
  76. void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box);
  77. //----------------------------------------------------------------------
  78. /**
  79. * Returns a fragment of Java code that can be used to set a property
  80. * to match the editors current state. This method is intended
  81. * for use when generating Java code to reflect changes made through the
  82. * property editor.
  83. * <p>
  84. * The code fragment should be context free and must be a legal Java
  85. * expression as specified by the JLS.
  86. * <p>
  87. * Specifically, if the expression represents a computation then all
  88. * classes and static members should be fully qualified. This rule
  89. * applies to constructors, static methods and non primitive arguments.
  90. * <p>
  91. * Caution should be used when evaluating the expression as it may throw
  92. * exceptions. In particular, code generators must ensure that generated
  93. * code will compile in the presence of an expression that can throw
  94. * checked exceptions.
  95. * <p>
  96. * Example results are:
  97. * <ul>
  98. * <li>Primitive expresssion: <code>2</code>
  99. * <li>Class constructor: <code>new java.awt.Color(127,127,34)</code>
  100. * <li>Static field: <code>java.awt.Color.orange</code>
  101. * <li>Static method: <code>javax.swing.Box.createRigidArea(new
  102. * java.awt.Dimension(0, 5))</code>
  103. * </ul>
  104. *
  105. * @return a fragment of Java code representing an initializer for the
  106. * current value. It should not contain a semi-colon
  107. * ('<code></code>') to end the expression.
  108. */
  109. String getJavaInitializationString();
  110. //----------------------------------------------------------------------
  111. /**
  112. * Gets the property value as text.
  113. *
  114. * @return The property value as a human editable string.
  115. * <p> Returns null if the value can't be expressed as an editable string.
  116. * <p> If a non-null value is returned, then the PropertyEditor should
  117. * be prepared to parse that string back in setAsText().
  118. */
  119. String getAsText();
  120. /**
  121. * Set the property value by parsing a given String. May raise
  122. * java.lang.IllegalArgumentException if either the String is
  123. * badly formatted or if this kind of property can't be expressed
  124. * as text.
  125. * @param text The string to be parsed.
  126. */
  127. void setAsText(String text) throws java.lang.IllegalArgumentException;
  128. //----------------------------------------------------------------------
  129. /**
  130. * If the property value must be one of a set of known tagged values,
  131. * then this method should return an array of the tags. This can
  132. * be used to represent (for example) enum values. If a PropertyEditor
  133. * supports tags, then it should support the use of setAsText with
  134. * a tag value as a way of setting the value and the use of getAsText
  135. * to identify the current value.
  136. *
  137. * @return The tag values for this property. May be null if this
  138. * property cannot be represented as a tagged value.
  139. *
  140. */
  141. String[] getTags();
  142. //----------------------------------------------------------------------
  143. /**
  144. * A PropertyEditor may choose to make available a full custom Component
  145. * that edits its property value. It is the responsibility of the
  146. * PropertyEditor to hook itself up to its editor Component itself and
  147. * to report property value changes by firing a PropertyChange event.
  148. * <P>
  149. * The higher-level code that calls getCustomEditor may either embed
  150. * the Component in some larger property sheet, or it may put it in
  151. * its own individual dialog, or ...
  152. *
  153. * @return A java.awt.Component that will allow a human to directly
  154. * edit the current property value. May be null if this is
  155. * not supported.
  156. */
  157. java.awt.Component getCustomEditor();
  158. /**
  159. * Determines whether this property editor supports a custom editor.
  160. *
  161. * @return True if the propertyEditor can provide a custom editor.
  162. */
  163. boolean supportsCustomEditor();
  164. //----------------------------------------------------------------------
  165. /**
  166. * Register a listener for the PropertyChange event. When a
  167. * PropertyEditor changes its value it should fire a PropertyChange
  168. * event on all registered PropertyChangeListeners, specifying the
  169. * null value for the property name and itself as the source.
  170. *
  171. * @param listener An object to be invoked when a PropertyChange
  172. * event is fired.
  173. */
  174. void addPropertyChangeListener(PropertyChangeListener listener);
  175. /**
  176. * Remove a listener for the PropertyChange event.
  177. *
  178. * @param listener The PropertyChange listener to be removed.
  179. */
  180. void removePropertyChangeListener(PropertyChangeListener listener);
  181. }