Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/beans/PropertyEditor.java
38829 views
/*1* Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.beans;2627/**28* A PropertyEditor class provides support for GUIs that want to29* allow users to edit a property value of a given type.30* <p>31* PropertyEditor supports a variety of different kinds of ways of32* displaying and updating property values. Most PropertyEditors will33* only need to support a subset of the different options available in34* this API.35* <P>36* Simple PropertyEditors may only support the getAsText and setAsText37* methods and need not support (say) paintValue or getCustomEditor. More38* complex types may be unable to support getAsText and setAsText but will39* instead support paintValue and getCustomEditor.40* <p>41* Every propertyEditor must support one or more of the three simple42* display styles. Thus it can either (1) support isPaintable or (2)43* both return a non-null String[] from getTags() and return a non-null44* value from getAsText or (3) simply return a non-null String from45* getAsText().46* <p>47* Every property editor must support a call on setValue when the argument48* object is of the type for which this is the corresponding propertyEditor.49* In addition, each property editor must either support a custom editor,50* or support setAsText.51* <p>52* Each PropertyEditor should have a null constructor.53*/5455public interface PropertyEditor {5657/**58* Set (or change) the object that is to be edited. Primitive types such59* as "int" must be wrapped as the corresponding object type such as60* "java.lang.Integer".61*62* @param value The new target object to be edited. Note that this63* object should not be modified by the PropertyEditor, rather64* the PropertyEditor should create a new object to hold any65* modified value.66*/67void setValue(Object value);6869/**70* Gets the property value.71*72* @return The value of the property. Primitive types such as "int" will73* be wrapped as the corresponding object type such as "java.lang.Integer".74*/7576Object getValue();7778//----------------------------------------------------------------------7980/**81* Determines whether this property editor is paintable.82*83* @return True if the class will honor the paintValue method.84*/8586boolean isPaintable();8788/**89* Paint a representation of the value into a given area of screen90* real estate. Note that the propertyEditor is responsible for doing91* its own clipping so that it fits into the given rectangle.92* <p>93* If the PropertyEditor doesn't honor paint requests (see isPaintable)94* this method should be a silent noop.95* <p>96* The given Graphics object will have the default font, color, etc of97* the parent container. The PropertyEditor may change graphics attributes98* such as font and color and doesn't need to restore the old values.99*100* @param gfx Graphics object to paint into.101* @param box Rectangle within graphics object into which we should paint.102*/103void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box);104105//----------------------------------------------------------------------106107/**108* Returns a fragment of Java code that can be used to set a property109* to match the editors current state. This method is intended110* for use when generating Java code to reflect changes made through the111* property editor.112* <p>113* The code fragment should be context free and must be a legal Java114* expression as specified by the JLS.115* <p>116* Specifically, if the expression represents a computation then all117* classes and static members should be fully qualified. This rule118* applies to constructors, static methods and non primitive arguments.119* <p>120* Caution should be used when evaluating the expression as it may throw121* exceptions. In particular, code generators must ensure that generated122* code will compile in the presence of an expression that can throw123* checked exceptions.124* <p>125* Example results are:126* <ul>127* <li>Primitive expresssion: <code>2</code>128* <li>Class constructor: <code>new java.awt.Color(127,127,34)</code>129* <li>Static field: <code>java.awt.Color.orange</code>130* <li>Static method: <code>javax.swing.Box.createRigidArea(new131* java.awt.Dimension(0, 5))</code>132* </ul>133*134* @return a fragment of Java code representing an initializer for the135* current value. It should not contain a semi-colon136* ('<code>;</code>') to end the expression.137*/138String getJavaInitializationString();139140//----------------------------------------------------------------------141142/**143* Gets the property value as text.144*145* @return The property value as a human editable string.146* <p> Returns null if the value can't be expressed as an editable string.147* <p> If a non-null value is returned, then the PropertyEditor should148* be prepared to parse that string back in setAsText().149*/150String getAsText();151152/**153* Set the property value by parsing a given String. May raise154* java.lang.IllegalArgumentException if either the String is155* badly formatted or if this kind of property can't be expressed156* as text.157* @param text The string to be parsed.158*/159void setAsText(String text) throws java.lang.IllegalArgumentException;160161//----------------------------------------------------------------------162163/**164* If the property value must be one of a set of known tagged values,165* then this method should return an array of the tags. This can166* be used to represent (for example) enum values. If a PropertyEditor167* supports tags, then it should support the use of setAsText with168* a tag value as a way of setting the value and the use of getAsText169* to identify the current value.170*171* @return The tag values for this property. May be null if this172* property cannot be represented as a tagged value.173*174*/175String[] getTags();176177//----------------------------------------------------------------------178179/**180* A PropertyEditor may choose to make available a full custom Component181* that edits its property value. It is the responsibility of the182* PropertyEditor to hook itself up to its editor Component itself and183* to report property value changes by firing a PropertyChange event.184* <P>185* The higher-level code that calls getCustomEditor may either embed186* the Component in some larger property sheet, or it may put it in187* its own individual dialog, or ...188*189* @return A java.awt.Component that will allow a human to directly190* edit the current property value. May be null if this is191* not supported.192*/193194java.awt.Component getCustomEditor();195196/**197* Determines whether this property editor supports a custom editor.198*199* @return True if the propertyEditor can provide a custom editor.200*/201boolean supportsCustomEditor();202203//----------------------------------------------------------------------204205/**206* Adds a listener for the value change.207* When the property editor changes its value208* it should fire a {@link PropertyChangeEvent}209* on all registered {@link PropertyChangeListener}s,210* specifying the {@code null} value for the property name211* and itself as the source.212*213* @param listener the {@link PropertyChangeListener} to add214*/215void addPropertyChangeListener(PropertyChangeListener listener);216217/**218* Removes a listener for the value change.219*220* @param listener the {@link PropertyChangeListener} to remove221*/222void removePropertyChangeListener(PropertyChangeListener listener);223224}225226227