1 /*
2  * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package com.sun.beans.editors;
26 
27 import java.awt.Component;
28 import java.awt.Graphics;
29 import java.awt.Rectangle;
30 import java.beans.PropertyChangeEvent;
31 import java.beans.PropertyChangeListener;
32 import java.beans.PropertyEditor;
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 /**
37  * Property editor for java.lang.Enum subclasses.
38  *
39  * @see PropertyEditor
40  *
41  * @since 1.7
42  *
43  * @author Sergey A. Malenkov
44  */
45 public final class EnumEditor implements PropertyEditor {
46     private final List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
47 
48     private final Class type;
49     private final String[] tags;
50 
51     private Object value;
52 
EnumEditor( Class type )53     public EnumEditor( Class type ) {
54         Object[] values = type.getEnumConstants();
55         if ( values == null ) {
56             throw new IllegalArgumentException( "Unsupported " + type );
57         }
58         this.type = type;
59         this.tags = new String[values.length];
60         for ( int i = 0; i < values.length; i++ ) {
61             this.tags[i] = ( ( Enum )values[i] ).name();
62         }
63     }
64 
getValue()65     public Object getValue() {
66         return this.value;
67     }
68 
setValue( Object value )69     public void setValue( Object value ) {
70         if ( ( value != null ) && !this.type.isInstance( value ) ) {
71             throw new IllegalArgumentException( "Unsupported value: " + value );
72         }
73         Object oldValue;
74         PropertyChangeListener[] listeners;
75         synchronized ( this.listeners ) {
76             oldValue = this.value;
77             this.value = value;
78 
79             if ( ( value == null ) ? oldValue == null : value.equals( oldValue ) ) {
80                 return; // do not fire event if value is not changed
81             }
82             int size = this.listeners.size();
83             if ( size == 0 ) {
84                 return; // do not fire event if there are no any listener
85             }
86             listeners = this.listeners.toArray( new PropertyChangeListener[size] );
87         }
88         PropertyChangeEvent event = new PropertyChangeEvent( this, null, oldValue, value );
89         for ( PropertyChangeListener listener : listeners ) {
90             listener.propertyChange( event );
91         }
92     }
93 
getAsText()94     public String getAsText() {
95         return ( this.value != null )
96                 ? ( ( Enum )this.value ).name()
97                 : null;
98     }
99 
setAsText( String text )100     public void setAsText( String text ) {
101         setValue( ( text != null )
102                 ? Enum.valueOf( this.type, text )
103                 : null );
104     }
105 
getTags()106     public String[] getTags() {
107         return this.tags.clone();
108     }
109 
getJavaInitializationString()110     public String getJavaInitializationString() {
111         String name = getAsText();
112         return ( name != null )
113                 ? this.type.getName() + '.' + name
114                 : "null";
115     }
116 
isPaintable()117     public boolean isPaintable() {
118         return false;
119     }
120 
paintValue( Graphics gfx, Rectangle box )121     public void paintValue( Graphics gfx, Rectangle box ) {
122     }
123 
supportsCustomEditor()124     public boolean supportsCustomEditor() {
125         return false;
126     }
127 
getCustomEditor()128     public Component getCustomEditor() {
129         return null;
130     }
131 
addPropertyChangeListener( PropertyChangeListener listener )132     public void addPropertyChangeListener( PropertyChangeListener listener ) {
133         synchronized ( this.listeners ) {
134             this.listeners.add( listener );
135         }
136     }
137 
removePropertyChangeListener( PropertyChangeListener listener )138     public void removePropertyChangeListener( PropertyChangeListener listener ) {
139         synchronized ( this.listeners ) {
140             this.listeners.remove( listener );
141         }
142     }
143 }
144