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.Component;
   41   
   42   import java.awt.event.ActionEvent;
   43   import java.awt.event.ActionListener;
   44   import java.awt.event.KeyEvent;
   45   import java.awt.event.KeyAdapter;
   46   
   47   import java.beans.PropertyDescriptor;
   48   import java.beans.PropertyChangeListener;
   49   
   50   import javax.swing.BoxLayout;
   51   import javax.swing.JPanel;
   52   import javax.swing.JTextField;
   53   
   54   /**
   55    * A property editor for editing integers. This editor also supports enumerated
   56    * type properties which are identified if the "enumerationValues" key returns
   57    * a non-null value.
   58    * Note: the init() method must be called before the set/get methods can be
   59    * called.
   60    *
   61    * @version 1.10 02/27/02
   62    * @author  Mark Davidson
   63    */
   64   public class SwingIntegerEditor extends SwingEditorSupport {
   65   
   66       // Property editor to use if the Integer represents an Enumerated type.
   67       private SwingEnumEditor enumEditor = new SwingEnumEditor();
   68   
   69       private JTextField textfield;
   70   
   71       private boolean isEnumeration = false;
   72   
   73       public void setValue(Object value) {
   74           if (isEnumeration)  {
   75               enumEditor.setValue(value);
   76           } else {
   77               if (value != null)  {
   78                   textfield.setText(value.toString());
   79               }
   80   
   81               super.setValue(value);
   82           }
   83       }
   84   
   85       public Object getValue() {
   86           if (isEnumeration)  {
   87               return enumEditor.getValue();
   88           } else {
   89               return super.getValue();
   90           }
   91       }
   92       
   93       /** 
   94        * Must overloade the PropertyChangeListener registration because
   95        * this class is the only interface to the SwingEnumEditor.
   96        */
   97       public void addPropertyChangeListener(PropertyChangeListener l)  {
   98           enumEditor.addPropertyChangeListener(l);
   99           super.addPropertyChangeListener(l);
  100       }
  101   
  102       public void removePropertyChangeListener(PropertyChangeListener l)  {
  103           enumEditor.removePropertyChangeListener(l);
  104           super.removePropertyChangeListener(l);
  105       }
  106   
  107       /**
  108        * Initializes this property editor with the enumerated items.
  109        */
  110       public void init(PropertyDescriptor descriptor) {
  111           Object[] enum = (Object[])descriptor.getValue("enumerationValues");
  112           if ( enum != null ) {
  113               // The property descriptor describes an enumerated item.
  114               isEnumeration = true;
  115   
  116               enumEditor.init(descriptor);
  117   
  118           } else {
  119               // This is an integer item
  120               isEnumeration = false;
  121   
  122               if (textfield == null)  {
  123                   textfield = new JTextField();
  124                   textfield.setDocument(new NumberDocument());
  125                   // disable editing if its a readonly property
  126                   if( descriptor.getWriteMethod() == null ) textfield.setEditable( false );
  127                   // XXX - Textfield should sent an actionPerformed event.
  128                   // this was broken for 1.3 beta
  129                   textfield.addKeyListener(new KeyAdapter()  {
  130                       public void keyPressed(KeyEvent evt)  {
  131                           if (evt.getKeyCode() == KeyEvent.VK_ENTER)  {
  132                               setValue(new Integer(textfield.getText()));
  133                           }
  134                       }
  135                   });
  136                   panel = new JPanel();
  137                   panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
  138                   panel.add(textfield);
  139               }
  140           }
  141       }
  142   
  143       /**
  144        * Return the custom editor for the enumeration or the integer.
  145        */
  146       public Component getCustomEditor()  {
  147           if (isEnumeration)  {
  148               return enumEditor.getCustomEditor();
  149           } else {
  150               return super.getCustomEditor();
  151           }
  152       }
  153   }

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