Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/beans/PropertyEditorSupport.java
38829 views
/*1* Copyright (c) 1996, 2013, 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;2627import java.beans.*;2829/**30* This is a support class to help build property editors.31* <p>32* It can be used either as a base class or as a delegate.33*/3435public class PropertyEditorSupport implements PropertyEditor {3637/**38* Constructs a <code>PropertyEditorSupport</code> object.39*40* @since 1.541*/42public PropertyEditorSupport() {43setSource(this);44}4546/**47* Constructs a <code>PropertyEditorSupport</code> object.48*49* @param source the source used for event firing50* @since 1.551*/52public PropertyEditorSupport(Object source) {53if (source == null) {54throw new NullPointerException();55}56setSource(source);57}5859/**60* Returns the bean that is used as the61* source of events. If the source has not62* been explicitly set then this instance of63* <code>PropertyEditorSupport</code> is returned.64*65* @return the source object or this instance66* @since 1.567*/68public Object getSource() {69return source;70}7172/**73* Sets the source bean.74* <p>75* The source bean is used as the source of events76* for the property changes. This source should be used for information77* purposes only and should not be modified by the PropertyEditor.78*79* @param source source object to be used for events80* @since 1.581*/82public void setSource(Object source) {83this.source = source;84}8586/**87* Set (or change) the object that is to be edited.88*89* @param value The new target object to be edited. Note that this90* object should not be modified by the PropertyEditor, rather91* the PropertyEditor should create a new object to hold any92* modified value.93*/94public void setValue(Object value) {95this.value = value;96firePropertyChange();97}9899/**100* Gets the value of the property.101*102* @return The value of the property.103*/104public Object getValue() {105return value;106}107108//----------------------------------------------------------------------109110/**111* Determines whether the class will honor the paintValue method.112*113* @return True if the class will honor the paintValue method.114*/115116public boolean isPaintable() {117return false;118}119120/**121* Paint a representation of the value into a given area of screen122* real estate. Note that the propertyEditor is responsible for doing123* its own clipping so that it fits into the given rectangle.124* <p>125* If the PropertyEditor doesn't honor paint requests (see isPaintable)126* this method should be a silent noop.127*128* @param gfx Graphics object to paint into.129* @param box Rectangle within graphics object into which we should paint.130*/131public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {132}133134//----------------------------------------------------------------------135136/**137* This method is intended for use when generating Java code to set138* the value of the property. It should return a fragment of Java code139* that can be used to initialize a variable with the current property140* value.141* <p>142* Example results are "2", "new Color(127,127,34)", "Color.orange", etc.143*144* @return A fragment of Java code representing an initializer for the145* current value.146*/147public String getJavaInitializationString() {148return "???";149}150151//----------------------------------------------------------------------152153/**154* Gets the property value as a string suitable for presentation155* to a human to edit.156*157* @return The property value as a string suitable for presentation158* to a human to edit.159* <p> Returns null if the value can't be expressed as a string.160* <p> If a non-null value is returned, then the PropertyEditor should161* be prepared to parse that string back in setAsText().162*/163public String getAsText() {164return (this.value != null)165? this.value.toString()166: null;167}168169/**170* Sets the property value by parsing a given String. May raise171* java.lang.IllegalArgumentException if either the String is172* badly formatted or if this kind of property can't be expressed173* as text.174*175* @param text The string to be parsed.176*/177public void setAsText(String text) throws java.lang.IllegalArgumentException {178if (value instanceof String) {179setValue(text);180return;181}182throw new java.lang.IllegalArgumentException(text);183}184185//----------------------------------------------------------------------186187/**188* If the property value must be one of a set of known tagged values,189* then this method should return an array of the tag values. This can190* be used to represent (for example) enum values. If a PropertyEditor191* supports tags, then it should support the use of setAsText with192* a tag value as a way of setting the value.193*194* @return The tag values for this property. May be null if this195* property cannot be represented as a tagged value.196*197*/198public String[] getTags() {199return null;200}201202//----------------------------------------------------------------------203204/**205* A PropertyEditor may chose to make available a full custom Component206* that edits its property value. It is the responsibility of the207* PropertyEditor to hook itself up to its editor Component itself and208* to report property value changes by firing a PropertyChange event.209* <P>210* The higher-level code that calls getCustomEditor may either embed211* the Component in some larger property sheet, or it may put it in212* its own individual dialog, or ...213*214* @return A java.awt.Component that will allow a human to directly215* edit the current property value. May be null if this is216* not supported.217*/218219public java.awt.Component getCustomEditor() {220return null;221}222223/**224* Determines whether the propertyEditor can provide a custom editor.225*226* @return True if the propertyEditor can provide a custom editor.227*/228public boolean supportsCustomEditor() {229return false;230}231232//----------------------------------------------------------------------233234/**235* Adds a listener for the value change.236* When the property editor changes its value237* it should fire a {@link PropertyChangeEvent}238* on all registered {@link PropertyChangeListener}s,239* specifying the {@code null} value for the property name.240* If the source property is set,241* it should be used as the source of the event.242* <p>243* The same listener object may be added more than once,244* and will be called as many times as it is added.245* If {@code listener} is {@code null},246* no exception is thrown and no action is taken.247*248* @param listener the {@link PropertyChangeListener} to add249*/250public synchronized void addPropertyChangeListener(251PropertyChangeListener listener) {252if (listeners == null) {253listeners = new java.util.Vector<>();254}255listeners.addElement(listener);256}257258/**259* Removes a listener for the value change.260* <p>261* If the same listener was added more than once,262* it will be notified one less time after being removed.263* If {@code listener} is {@code null}, or was never added,264* no exception is thrown and no action is taken.265*266* @param listener the {@link PropertyChangeListener} to remove267*/268public synchronized void removePropertyChangeListener(269PropertyChangeListener listener) {270if (listeners == null) {271return;272}273listeners.removeElement(listener);274}275276/**277* Report that we have been modified to any interested listeners.278*/279public void firePropertyChange() {280java.util.Vector<PropertyChangeListener> targets;281synchronized (this) {282if (listeners == null) {283return;284}285targets = unsafeClone(listeners);286}287// Tell our listeners that "everything" has changed.288PropertyChangeEvent evt = new PropertyChangeEvent(source, null, null, null);289290for (int i = 0; i < targets.size(); i++) {291PropertyChangeListener target = targets.elementAt(i);292target.propertyChange(evt);293}294}295296@SuppressWarnings("unchecked")297private <T> java.util.Vector<T> unsafeClone(java.util.Vector<T> v) {298return (java.util.Vector<T>)v.clone();299}300301//----------------------------------------------------------------------302303private Object value;304private Object source;305private java.util.Vector<PropertyChangeListener> listeners;306}307308309