1 /*
   2  * Copyright (c) 2006, 2014, 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     @SuppressWarnings("rawtypes")
  49     private final Class<? extends Enum> type;
  50     private final String[] tags;
  51 
  52     private Object value;
  53 
  54     public EnumEditor(Class<?> type) {
  55         Object[] values = type.getEnumConstants();
  56         if ( values == null ) {
  57             throw new IllegalArgumentException( "Unsupported " + type );
  58         }
  59         this.type = type.asSubclass(java.lang.Enum.class);
  60         this.tags = new String[values.length];
  61         for ( int i = 0; i < values.length; i++ ) {
  62             this.tags[i] = ( ( Enum )values[i] ).name();
  63         }
  64     }
  65 
  66     public Object getValue() {
  67         return this.value;
  68     }
  69 
  70     public void setValue( Object value ) {
  71         if ( ( value != null ) && !this.type.isInstance( value ) ) {
  72             throw new IllegalArgumentException( "Unsupported value: " + value );
  73         }
  74         Object oldValue;
  75         PropertyChangeListener[] listeners;
  76         synchronized ( this.listeners ) {
  77             oldValue = this.value;
  78             this.value = value;
  79 
  80             if ( ( value == null ) ? oldValue == null : value.equals( oldValue ) ) {
  81                 return; // do not fire event if value is not changed
  82             }
  83             int size = this.listeners.size();
  84             if ( size == 0 ) {
  85                 return; // do not fire event if there are no any listener
  86             }
  87             listeners = this.listeners.toArray( new PropertyChangeListener[size] );
  88         }
  89         PropertyChangeEvent event = new PropertyChangeEvent( this, null, oldValue, value );
  90         for ( PropertyChangeListener listener : listeners ) {
  91             listener.propertyChange( event );
  92         }
  93     }
  94 
  95     public String getAsText() {
  96         return ( this.value != null )
  97                 ? ( ( Enum )this.value ).name()
  98                 : null;
  99     }
 100 
 101     public void setAsText( String text ) {
 102         @SuppressWarnings("unchecked")
 103         Object tmp = ( text != null )
 104             ? Enum.valueOf( (Class)this.type, text )
 105             : null;
 106         setValue(tmp);
 107     }
 108 
 109     public String[] getTags() {
 110         return this.tags.clone();
 111     }
 112 
 113     public String getJavaInitializationString() {
 114         String name = getAsText();
 115         return ( name != null )
 116                 ? this.type.getName() + '.' + name
 117                 : "null";
 118     }
 119 
 120     public boolean isPaintable() {
 121         return false;
 122     }
 123 
 124     public void paintValue( Graphics gfx, Rectangle box ) {
 125     }
 126 
 127     public boolean supportsCustomEditor() {
 128         return false;
 129     }
 130 
 131     public Component getCustomEditor() {
 132         return null;
 133     }
 134 
 135     public void addPropertyChangeListener( PropertyChangeListener listener ) {
 136         synchronized ( this.listeners ) {
 137             this.listeners.add( listener );
 138         }
 139     }
 140 
 141     public void removePropertyChangeListener( PropertyChangeListener listener ) {
 142         synchronized ( this.listeners ) {
 143             this.listeners.remove( listener );
 144         }
 145     }
 146 }