Path: blob/master/src/java.desktop/share/classes/java/beans/PropertyEditor.java
41152 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*54* @since 1.155*/5657public interface PropertyEditor {5859/**60* Set (or change) the object that is to be edited. Primitive types such61* as "int" must be wrapped as the corresponding object type such as62* "java.lang.Integer".63*64* @param value The new target object to be edited. Note that this65* object should not be modified by the PropertyEditor, rather66* the PropertyEditor should create a new object to hold any67* modified value.68*/69void setValue(Object value);7071/**72* Gets the property value.73*74* @return The value of the property. Primitive types such as "int" will75* be wrapped as the corresponding object type such as "java.lang.Integer".76*/7778Object getValue();7980//----------------------------------------------------------------------8182/**83* Determines whether this property editor is paintable.84*85* @return True if the class will honor the paintValue method.86*/8788boolean isPaintable();8990/**91* Paint a representation of the value into a given area of screen92* real estate. Note that the propertyEditor is responsible for doing93* its own clipping so that it fits into the given rectangle.94* <p>95* If the PropertyEditor doesn't honor paint requests (see isPaintable)96* this method should be a silent noop.97* <p>98* The given Graphics object will have the default font, color, etc of99* the parent container. The PropertyEditor may change graphics attributes100* such as font and color and doesn't need to restore the old values.101*102* @param gfx Graphics object to paint into.103* @param box Rectangle within graphics object into which we should paint.104*/105void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box);106107//----------------------------------------------------------------------108109/**110* Returns a fragment of Java code that can be used to set a property111* to match the editors current state. This method is intended112* for use when generating Java code to reflect changes made through the113* property editor.114* <p>115* The code fragment should be context free and must be a legal Java116* expression as specified by the JLS.117* <p>118* Specifically, if the expression represents a computation then all119* classes and static members should be fully qualified. This rule120* applies to constructors, static methods and non primitive arguments.121* <p>122* Caution should be used when evaluating the expression as it may throw123* exceptions. In particular, code generators must ensure that generated124* code will compile in the presence of an expression that can throw125* checked exceptions.126* <p>127* Example results are:128* <ul>129* <li>Primitive expresssion: {@code 2}130* <li>Class constructor: {@code new java.awt.Color(127,127,34)}131* <li>Static field: {@code java.awt.Color.orange}132* <li>Static method: {@code javax.swing.Box.createRigidArea(new133* java.awt.Dimension(0, 5))}134* </ul>135*136* @return a fragment of Java code representing an initializer for the137* current value. It should not contain a semi-colon138* ('{@code ;}') to end the expression.139*/140String getJavaInitializationString();141142//----------------------------------------------------------------------143144/**145* Gets the property value as text.146*147* @return The property value as a human editable string.148* <p> Returns null if the value can't be expressed as an editable string.149* <p> If a non-null value is returned, then the PropertyEditor should150* be prepared to parse that string back in setAsText().151*/152String getAsText();153154/**155* Set the property value by parsing a given String. May raise156* java.lang.IllegalArgumentException if either the String is157* badly formatted or if this kind of property can't be expressed158* as text.159* @param text The string to be parsed.160*/161void setAsText(String text) throws java.lang.IllegalArgumentException;162163//----------------------------------------------------------------------164165/**166* If the property value must be one of a set of known tagged values,167* then this method should return an array of the tags. This can168* be used to represent (for example) enum values. If a PropertyEditor169* supports tags, then it should support the use of setAsText with170* a tag value as a way of setting the value and the use of getAsText171* to identify the current value.172*173* @return The tag values for this property. May be null if this174* property cannot be represented as a tagged value.175*176*/177String[] getTags();178179//----------------------------------------------------------------------180181/**182* A PropertyEditor may choose to make available a full custom Component183* that edits its property value. It is the responsibility of the184* PropertyEditor to hook itself up to its editor Component itself and185* to report property value changes by firing a PropertyChange event.186* <P>187* The higher-level code that calls getCustomEditor may either embed188* the Component in some larger property sheet, or it may put it in189* its own individual dialog, or ...190*191* @return A java.awt.Component that will allow a human to directly192* edit the current property value. May be null if this is193* not supported.194*/195196java.awt.Component getCustomEditor();197198/**199* Determines whether this property editor supports a custom editor.200*201* @return True if the propertyEditor can provide a custom editor.202*/203boolean supportsCustomEditor();204205//----------------------------------------------------------------------206207/**208* Adds a listener for the value change.209* When the property editor changes its value210* it should fire a {@link PropertyChangeEvent}211* on all registered {@link PropertyChangeListener}s,212* specifying the {@code null} value for the property name213* and itself as the source.214*215* @param listener the {@link PropertyChangeListener} to add216*/217void addPropertyChangeListener(PropertyChangeListener listener);218219/**220* Removes a listener for the value change.221*222* @param listener the {@link PropertyChangeListener} to remove223*/224void removePropertyChangeListener(PropertyChangeListener listener);225226}227228229