Home » openjdk-7 » javax » swing » beaninfo » [javadoc | source]

    1   /*
    2    * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
    3    * 
    4    * Redistribution and use in source and binary forms, with or
    5    * without modification, are permitted provided that the following
    6    * conditions are met:
    7    * 
    8    * - Redistributions of source code must retain the above copyright
    9    *   notice, this list of conditions and the following disclaimer.
   10    * 
   11    * - Redistribution in binary form must reproduce the above
   12    *   copyright notice, this list of conditions and the following
   13    *   disclaimer in the documentation and/or other materials
   14    *   provided with the distribution.
   15    * 
   16    * Neither the name of Sun Microsystems, Inc. or the names of
   17    * contributors may be used to endorse or promote products derived
   18    * from this software without specific prior written permission.
   19    * 
   20    * This software is provided "AS IS," without a warranty of any
   21    * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
   22    * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
   23    * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
   24    * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
   25    * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
   26    * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
   27    * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
   28    * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
   29    * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
   30    * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
   31    * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
   32    * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
   33    * 
   34    * You acknowledge that this software is not designed, licensed or
   35    * intended for use in the design, construction, operation or
   36    * maintenance of any nuclear facility.
   37    */
   38   package javax.swing.beaninfo;
   39   
   40   import java.awt.event.ActionEvent;
   41   import java.awt.event.ActionListener;
   42   
   43   import java.beans.PropertyDescriptor;
   44   
   45   import javax.swing.BoxLayout;
   46   import javax.swing.JComboBox;
   47   import javax.swing.JPanel;
   48   
   49   /**
   50    * A property editor for a swing enumerated type. Handles the case in which the
   51    * PropertyDescriptor has a value for "enumerationValues".
   52    * Note: the init() method must be called before the set/get methods can be
   53    * called.
   54    *
   55    * @version %I% %G%
   56    * @author  Mark Davidson
   57    */
   58   public class SwingEnumEditor extends SwingEditorSupport implements ActionListener {
   59   
   60       public JComboBox combobox;
   61   
   62       public void setValue(Object value) {
   63           super.setValue(value);
   64   
   65           // Set combo box if it's a new value. We want to reduce number
   66           // of extraneous events.
   67           EnumeratedItem item = (EnumeratedItem)combobox.getSelectedItem();
   68           if (value != null && !value.equals(item.getValue()))  {
   69               for (int i = 0; i < combobox.getItemCount(); ++i ) {
   70                   item = (EnumeratedItem)combobox.getItemAt(i);
   71                   if (item.getValue().equals(value)) {
   72                       // XXX - hack! Combo box shouldn't call action event
   73                       // for setSelectedItem!!
   74                       combobox.removeActionListener(this);
   75                       combobox.setSelectedItem(item);
   76                       combobox.addActionListener(this);
   77                       return;
   78                   }
   79               }
   80           }
   81       }
   82   
   83       /**
   84        * Initializes this property editor with the enumerated items. Instances
   85        * can be shared but there are issues.
   86        * <p>
   87        * This method does a lot of jiggery pokery since enumerated
   88        * types are unlike any other homogenous types. Enumerated types may not
   89        * represent the same set of values.
   90        * <p>
   91        * One method would be to empty the list of values which would have the side
   92        * effect of firing notification events. Another method would be to recreate
   93        * the combobox.
   94        */
   95       public void init(PropertyDescriptor descriptor) {
   96           Object[] enum = (Object[])descriptor.getValue( "enumerationValues" );
   97           if (enum != null) {
   98               if (combobox == null)  {
   99                   combobox = new JComboBox();
  100   
  101                   panel = new JPanel();
  102                   panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
  103                   panel.add(combobox);
  104               } else {
  105                   // Remove action listener to reduce extra events.
  106                   combobox.removeActionListener(this);
  107                   combobox.removeAllItems();
  108               }
  109   
  110               for ( int i = 0; i < enum.length; i += 3 ) {
  111                   combobox.addItem(new EnumeratedItem((Integer)enum[i+1], (String)enum[i] ) );
  112               }
  113   
  114               combobox.addActionListener(this);
  115           }
  116       }
  117   
  118       /**
  119        * Event is set when a combo selection changes.
  120        */
  121       public void actionPerformed(ActionEvent evt)  {
  122           EnumeratedItem item = (EnumeratedItem)combobox.getSelectedItem();
  123           if (item != null && !getValue().equals(item.getValue()))  {
  124               setValue(item.getValue());
  125           }
  126       }
  127   
  128       /**
  129        * Object which holds an enumerated item plus its label.
  130        */
  131       private class EnumeratedItem  {
  132           private Integer value;
  133           private String name;
  134   
  135           public EnumeratedItem(Integer value, String name) {
  136               this.value = value;
  137               this.name = name;
  138           }
  139   
  140           public Integer getValue() {
  141               return value;
  142           }
  143   
  144           public String toString() {
  145               return name;
  146           }
  147       }
  148   }

Home » openjdk-7 » javax » swing » beaninfo » [javadoc | source]