1. /*
  2. * @(#)PropertyEditor.java 1.32 01/11/29
  3. *
  4. * Copyright 2002 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. * This method is intended for use when generating Java code to set
  80. * the value of the property. It should return a fragment of Java code
  81. * that can be used to initialize a variable with the current property
  82. * value.
  83. * <p>
  84. * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
  85. *
  86. * @return A fragment of Java code representing an initializer for the
  87. * current value.
  88. */
  89. String getJavaInitializationString();
  90. //----------------------------------------------------------------------
  91. /**
  92. * Gets the property value as text.
  93. *
  94. * @return The property value as a human editable string.
  95. * <p> Returns null if the value can't be expressed as an editable 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. String getAsText();
  100. /**
  101. * Set the property value by parsing a given String. May raise
  102. * java.lang.IllegalArgumentException if either the String is
  103. * badly formatted or if this kind of property can't be expressed
  104. * as text.
  105. * @param text The string to be parsed.
  106. */
  107. void setAsText(String text) throws java.lang.IllegalArgumentException;
  108. //----------------------------------------------------------------------
  109. /**
  110. * If the property value must be one of a set of known tagged values,
  111. * then this method should return an array of the tags. This can
  112. * be used to represent (for example) enum values. If a PropertyEditor
  113. * supports tags, then it should support the use of setAsText with
  114. * a tag value as a way of setting the value and the use of getAsText
  115. * to identify the current value.
  116. *
  117. * @return The tag values for this property. May be null if this
  118. * property cannot be represented as a tagged value.
  119. *
  120. */
  121. String[] getTags();
  122. //----------------------------------------------------------------------
  123. /**
  124. * A PropertyEditor may choose to make available a full custom Component
  125. * that edits its property value. It is the responsibility of the
  126. * PropertyEditor to hook itself up to its editor Component itself and
  127. * to report property value changes by firing a PropertyChange event.
  128. * <P>
  129. * The higher-level code that calls getCustomEditor may either embed
  130. * the Component in some larger property sheet, or it may put it in
  131. * its own individual dialog, or ...
  132. *
  133. * @return A java.awt.Component that will allow a human to directly
  134. * edit the current property value. May be null if this is
  135. * not supported.
  136. */
  137. java.awt.Component getCustomEditor();
  138. /**
  139. * Determines whether this property editor supports a custom editor.
  140. *
  141. * @return True if the propertyEditor can provide a custom editor.
  142. */
  143. boolean supportsCustomEditor();
  144. //----------------------------------------------------------------------
  145. /**
  146. * Register a listener for the PropertyChange event. When a
  147. * PropertyEditor changes its value it should fire a PropertyChange
  148. * event on all registered PropertyChangeListeners, specifying the
  149. * null value for the property name and itself as the source.
  150. *
  151. * @param listener An object to be invoked when a PropertyChange
  152. * event is fired.
  153. */
  154. void addPropertyChangeListener(PropertyChangeListener listener);
  155. /**
  156. * Remove a listener for the PropertyChange event.
  157. *
  158. * @param listener The PropertyChange listener to be removed.
  159. */
  160. void removePropertyChangeListener(PropertyChangeListener listener);
  161. }