PropertyEditorSupport.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* java.beans.PropertyEditorSupport
  2. Copyright (C) 1998 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.beans;
  32. /**
  33. ** PropertyEditorSupport helps with PropertyEditors,
  34. ** implementing base functionality that they usually must
  35. ** have but which is a pain to implement. You may extend
  36. ** from this class or use it as a standalone.<P>
  37. **
  38. ** This class does not do any painting or actual editing.
  39. ** For that, you must use or extend it. See the
  40. ** PropertyEditor class for better descriptions of what
  41. ** the various methods do.
  42. **
  43. ** @author John Keiser
  44. ** @since JDK1.1
  45. ** @version 1.1.0, 29 Jul 1998
  46. **/
  47. public class PropertyEditorSupport implements PropertyEditor {
  48. Object eventSource;
  49. Object val;
  50. PropertyChangeSupport pSupport;
  51. /** Call this constructor when you are deriving from
  52. ** PropertyEditorSupport.
  53. **/
  54. protected PropertyEditorSupport() {
  55. this.eventSource = this;
  56. this.pSupport = new PropertyChangeSupport(this);
  57. }
  58. /** Call this constructor when you are using
  59. ** PropertyEditorSupport as a helper object.
  60. ** @param eventSource the source to use when firing
  61. ** property change events.
  62. **/
  63. protected PropertyEditorSupport(Object eventSource) {
  64. this.eventSource = eventSource;
  65. this.pSupport = new PropertyChangeSupport(this);
  66. }
  67. /** Set the current value of the property.
  68. ** <STRONG>Implementation Note</STRONG> Sun does not
  69. ** state what exactly this version of the method does.
  70. ** Thus, in this implementation, it sets the value, and
  71. ** then if the old and new values are different, it
  72. ** fires a property change event with no property name
  73. ** and the old and new values.
  74. ** @param val the new value for the property.
  75. **/
  76. public void setValue(Object val) {
  77. Object oldVal = val;
  78. this.val = val;
  79. if(!oldVal.equals(val)) {
  80. pSupport.firePropertyChange(null,oldVal,val);
  81. }
  82. }
  83. /** Get the current value of the property.
  84. ** @return the current value of the property.
  85. **/
  86. public Object getValue() {
  87. return val;
  88. }
  89. /** Get whether this object is paintable or not.
  90. ** @return <CODE>false</CODE>
  91. **/
  92. public boolean isPaintable() {
  93. return false;
  94. }
  95. /** Paint this object. This class does nothing in
  96. ** this method.
  97. **/
  98. public void paintValue(java.awt.Graphics g, java.awt.Rectangle r) {
  99. }
  100. /** Get the Java initialization String for the current
  101. ** value of the Object. This class returns gibberish or
  102. ** null (though the spec does not say which).<P>
  103. ** <STRONG>Implementation Note:</STRONG> This class
  104. ** returns the string "@$#^" to make sure the code will
  105. ** be broken, so that you will know to override it when
  106. ** you create your own property editor.
  107. ** @return the Java initialization string.
  108. **/
  109. public String getJavaInitializationString() {
  110. return "@$#^";
  111. }
  112. /** Get the value as text.
  113. ** In this class, you cannot count on getAsText() doing
  114. ** anything useful, although in this implementation I
  115. ** do toString().
  116. ** @return the value as text.
  117. **/
  118. public String getAsText() {
  119. return val != null ? val.toString() : "null";
  120. }
  121. /** Set the value as text.
  122. ** In this class, you cannot count on setAsText() doing
  123. ** anything useful across implementations.
  124. ** <STRONG>Implementation Note:</STRONG> In this
  125. ** implementation it checks if the String is "null", and
  126. ** if it is, sets the value to null, otherwise it throws
  127. ** an IllegalArgumentException.
  128. ** @param s the text to convert to a new value.
  129. ** @exception IllegalArgumentException if the text is
  130. ** malformed.
  131. **/
  132. public void setAsText(String s) throws IllegalArgumentException {
  133. if(s.equals("null")) {
  134. setValue(null);
  135. } else {
  136. throw new IllegalArgumentException();
  137. }
  138. }
  139. /** Returns a list of possible choices for the value.
  140. ** @return <CODE>null</CODE>
  141. **/
  142. public String[] getTags() {
  143. return null;
  144. }
  145. /** Return a custom component to edit the value.
  146. ** @return <CODE>null</CODE> in this class.
  147. **/
  148. public java.awt.Component getCustomEditor() {
  149. return null;
  150. }
  151. /** Find out whether this property editor supports a
  152. ** custom component to edit its value.
  153. ** @return <CODE>false</CODE> in this class.
  154. **/
  155. public boolean supportsCustomEditor() {
  156. return false;
  157. }
  158. /** Add a property change listener to this property editor.
  159. ** @param l the listener to add.
  160. **/
  161. public void addPropertyChangeListener(PropertyChangeListener l) {
  162. pSupport.addPropertyChangeListener(l);
  163. }
  164. /** Remove a property change listener from this property editor.
  165. ** @param l the listener to remove.
  166. **/
  167. public void removePropertyChangeListener(PropertyChangeListener l) {
  168. pSupport.removePropertyChangeListener(l);
  169. }
  170. /** Notify people that we've changed, although we don't
  171. ** tell them just how. The only thing I can think of to
  172. ** send in the event is the new value (since the old value
  173. ** is unavailable and there is no property name).
  174. ** I confess I do not understand the point of this method.
  175. **/
  176. public void firePropertyChange() {
  177. pSupport.firePropertyChange(null,null,val);
  178. }
  179. }