src/share/classes/com/sun/beans/editors/EnumEditor.java

Print this page


   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


  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 
  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 
  65     public Object getValue() {
  66         return this.value;
  67     }
  68 
  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 


  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 
  94     public String getAsText() {
  95         return ( this.value != null )
  96                 ? ( ( Enum )this.value ).name()
  97                 : null;
  98     }
  99 
 100     public void setAsText( String text ) {
 101         setValue( ( text != null )
 102                 ? Enum.valueOf( this.type, text )
 103                 : null );


 104     }
 105 
 106     public String[] getTags() {
 107         return this.tags.clone();
 108     }
 109 
 110     public String getJavaInitializationString() {
 111         String name = getAsText();
 112         return ( name != null )
 113                 ? 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 
   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


  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 


  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