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