Path: blob/master/src/java.desktop/share/classes/java/beans/PropertyEditorSupport.java
41152 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*34* @since 1.135*/3637public class PropertyEditorSupport implements PropertyEditor {3839/**40* Constructs a {@code PropertyEditorSupport} object.41*42* @since 1.543*/44public PropertyEditorSupport() {45setSource(this);46}4748/**49* Constructs a {@code PropertyEditorSupport} object.50*51* @param source the source used for event firing52* @since 1.553*/54public PropertyEditorSupport(Object source) {55if (source == null) {56throw new NullPointerException();57}58setSource(source);59}6061/**62* Returns the bean that is used as the63* source of events. If the source has not64* been explicitly set then this instance of65* {@code PropertyEditorSupport} is returned.66*67* @return the source object or this instance68* @since 1.569*/70public Object getSource() {71return source;72}7374/**75* Sets the source bean.76* <p>77* The source bean is used as the source of events78* for the property changes. This source should be used for information79* purposes only and should not be modified by the PropertyEditor.80*81* @param source source object to be used for events82* @since 1.583*/84public void setSource(Object source) {85this.source = source;86}8788/**89* Set (or change) the object that is to be edited.90*91* @param value The new target object to be edited. Note that this92* object should not be modified by the PropertyEditor, rather93* the PropertyEditor should create a new object to hold any94* modified value.95*/96public void setValue(Object value) {97this.value = value;98firePropertyChange();99}100101/**102* Gets the value of the property.103*104* @return The value of the property.105*/106public Object getValue() {107return value;108}109110//----------------------------------------------------------------------111112/**113* Determines whether the class will honor the paintValue method.114*115* @return True if the class will honor the paintValue method.116*/117118public boolean isPaintable() {119return false;120}121122/**123* Paint a representation of the value into a given area of screen124* real estate. Note that the propertyEditor is responsible for doing125* its own clipping so that it fits into the given rectangle.126* <p>127* If the PropertyEditor doesn't honor paint requests (see isPaintable)128* this method should be a silent noop.129*130* @param gfx Graphics object to paint into.131* @param box Rectangle within graphics object into which we should paint.132*/133public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {134}135136//----------------------------------------------------------------------137138/**139* This method is intended for use when generating Java code to set140* the value of the property. It should return a fragment of Java code141* that can be used to initialize a variable with the current property142* value.143* <p>144* Example results are "2", "new Color(127,127,34)", "Color.orange", etc.145*146* @return A fragment of Java code representing an initializer for the147* current value.148*/149public String getJavaInitializationString() {150return "???";151}152153//----------------------------------------------------------------------154155/**156* Gets the property value as a string suitable for presentation157* to a human to edit.158*159* @return The property value as a string suitable for presentation160* to a human to edit.161* <p> Returns null if the value can't be expressed as a string.162* <p> If a non-null value is returned, then the PropertyEditor should163* be prepared to parse that string back in setAsText().164*/165public String getAsText() {166return (this.value != null)167? this.value.toString()168: null;169}170171/**172* Sets the property value by parsing a given String. May raise173* java.lang.IllegalArgumentException if either the String is174* badly formatted or if this kind of property can't be expressed175* as text.176*177* @param text The string to be parsed.178*/179public void setAsText(String text) throws java.lang.IllegalArgumentException {180if (value instanceof String) {181setValue(text);182return;183}184throw new java.lang.IllegalArgumentException(text);185}186187//----------------------------------------------------------------------188189/**190* If the property value must be one of a set of known tagged values,191* then this method should return an array of the tag values. This can192* be used to represent (for example) enum values. If a PropertyEditor193* supports tags, then it should support the use of setAsText with194* a tag value as a way of setting the value.195*196* @return The tag values for this property. May be null if this197* property cannot be represented as a tagged value.198*199*/200public String[] getTags() {201return null;202}203204//----------------------------------------------------------------------205206/**207* A PropertyEditor may chose to make available a full custom Component208* that edits its property value. It is the responsibility of the209* PropertyEditor to hook itself up to its editor Component itself and210* to report property value changes by firing a PropertyChange event.211* <P>212* The higher-level code that calls getCustomEditor may either embed213* the Component in some larger property sheet, or it may put it in214* its own individual dialog, or ...215*216* @return A java.awt.Component that will allow a human to directly217* edit the current property value. May be null if this is218* not supported.219*/220221public java.awt.Component getCustomEditor() {222return null;223}224225/**226* Determines whether the propertyEditor can provide a custom editor.227*228* @return True if the propertyEditor can provide a custom editor.229*/230public boolean supportsCustomEditor() {231return false;232}233234//----------------------------------------------------------------------235236/**237* Adds a listener for the value change.238* When the property editor changes its value239* it should fire a {@link PropertyChangeEvent}240* on all registered {@link PropertyChangeListener}s,241* specifying the {@code null} value for the property name.242* If the source property is set,243* it should be used as the source of the event.244* <p>245* The same listener object may be added more than once,246* and will be called as many times as it is added.247* If {@code listener} is {@code null},248* no exception is thrown and no action is taken.249*250* @param listener the {@link PropertyChangeListener} to add251*/252public synchronized void addPropertyChangeListener(253PropertyChangeListener listener) {254if (listeners == null) {255listeners = new java.util.Vector<>();256}257listeners.addElement(listener);258}259260/**261* Removes a listener for the value change.262* <p>263* If the same listener was added more than once,264* it will be notified one less time after being removed.265* If {@code listener} is {@code null}, or was never added,266* no exception is thrown and no action is taken.267*268* @param listener the {@link PropertyChangeListener} to remove269*/270public synchronized void removePropertyChangeListener(271PropertyChangeListener listener) {272if (listeners == null) {273return;274}275listeners.removeElement(listener);276}277278/**279* Report that we have been modified to any interested listeners.280*/281public void firePropertyChange() {282java.util.Vector<PropertyChangeListener> targets;283synchronized (this) {284if (listeners == null) {285return;286}287targets = unsafeClone(listeners);288}289// Tell our listeners that "everything" has changed.290PropertyChangeEvent evt = new PropertyChangeEvent(source, null, null, null);291292for (int i = 0; i < targets.size(); i++) {293PropertyChangeListener target = targets.elementAt(i);294target.propertyChange(evt);295}296}297298@SuppressWarnings("unchecked")299private <T> java.util.Vector<T> unsafeClone(java.util.Vector<T> v) {300return (java.util.Vector<T>)v.clone();301}302303//----------------------------------------------------------------------304305private Object value;306private Object source;307private java.util.Vector<PropertyChangeListener> listeners;308}309310311