001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.beans.editors;
026:
027: import java.awt.Component;
028: import java.awt.Graphics;
029: import java.awt.Rectangle;
030: import java.beans.PropertyChangeEvent;
031: import java.beans.PropertyChangeListener;
032: import java.beans.PropertyEditor;
033: import java.util.ArrayList;
034: import java.util.List;
035:
036: /**
037: * Property editor for java.lang.Enum subclasses.
038: *
039: * @see PropertyEditor
040: *
041: * @since 1.7
042: *
043: * @version 1.6 05/05/07
044: * @author Sergey A. Malenkov
045: */
046: public final class EnumEditor implements PropertyEditor {
047: private final List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
048:
049: private final Class type;
050: private final String[] tags;
051:
052: private Object value;
053:
054: public EnumEditor(Class type) {
055: Object[] values = type.getEnumConstants();
056: if (values == null) {
057: throw new IllegalArgumentException("Unsupported " + type);
058: }
059: this .type = type;
060: this .tags = new String[values.length];
061: for (int i = 0; i < values.length; i++) {
062: this .tags[i] = ((Enum) values[i]).name();
063: }
064: }
065:
066: public Object getValue() {
067: return this .value;
068: }
069:
070: public void setValue(Object value) {
071: if ((value != null) && (this .type != value.getClass())) {
072: throw new IllegalArgumentException("Unsupported value: "
073: + value);
074: }
075: Object oldValue;
076: PropertyChangeListener[] listeners;
077: synchronized (this .listeners) {
078: oldValue = this .value;
079: this .value = value;
080:
081: if ((value == null) ? oldValue == null : value
082: .equals(oldValue)) {
083: return; // do not fire event if value is not changed
084: }
085: int size = this .listeners.size();
086: if (size == 0) {
087: return; // do not fire event if there are no any listener
088: }
089: listeners = this .listeners
090: .toArray(new PropertyChangeListener[size]);
091: }
092: PropertyChangeEvent event = new PropertyChangeEvent(this , null,
093: oldValue, value);
094: for (PropertyChangeListener listener : listeners) {
095: listener.propertyChange(event);
096: }
097: }
098:
099: public String getAsText() {
100: return (this .value != null) ? ((Enum) this .value).name() : null;
101: }
102:
103: public void setAsText(String text) {
104: setValue((text != null) ? Enum.valueOf(this .type, text) : null);
105: }
106:
107: public String[] getTags() {
108: return this .tags.clone();
109: }
110:
111: public String getJavaInitializationString() {
112: String name = getAsText();
113: return (name != null) ? this .type.getName() + '.' + name
114: : "null";
115: }
116:
117: public boolean isPaintable() {
118: return false;
119: }
120:
121: public void paintValue(Graphics gfx, Rectangle box) {
122: }
123:
124: public boolean supportsCustomEditor() {
125: return false;
126: }
127:
128: public Component getCustomEditor() {
129: return null;
130: }
131:
132: public void addPropertyChangeListener(
133: PropertyChangeListener listener) {
134: synchronized (this .listeners) {
135: this .listeners.add(listener);
136: }
137: }
138:
139: public void removePropertyChangeListener(
140: PropertyChangeListener listener) {
141: synchronized (this.listeners) {
142: this.listeners.remove(listener);
143: }
144: }
145: }
|