Save This Page
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   
   39   package javax.swing.beaninfo;
   40   
   41   import java.awt.Rectangle;
   42   
   43   import javax.swing.BoxLayout;
   44   import javax.swing.JTextField;
   45   import javax.swing.JLabel;
   46   import javax.swing.JPanel;
   47   
   48   /**
   49    * An editor for editing Rectangle.
   50    *
   51    * @version  %I% %G%
   52    * @author  Mark Davidson
   53    */
   54   public class SwingRectangleEditor extends SwingEditorSupport {
   55       
   56       private JTextField xTF;
   57       private JTextField yTF;
   58       private JTextField widthTF;
   59       private JTextField heightTF;
   60   
   61       public SwingRectangleEditor() {
   62           xTF = new JTextField();
   63           xTF.setDocument(new NumberDocument());
   64           yTF = new JTextField();
   65           yTF.setDocument(new NumberDocument());
   66           widthTF = new JTextField();
   67           widthTF.setDocument(new NumberDocument());
   68           heightTF = new JTextField();
   69           heightTF.setDocument(new NumberDocument());
   70           
   71           panel = new JPanel();
   72           panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
   73           panel.add(new JLabel("X: "));
   74           panel.add(xTF);
   75           panel.add(new JLabel("Y: "));
   76           panel.add(yTF);
   77           panel.add(new JLabel("Width: "));
   78           panel.add(widthTF);
   79           panel.add(new JLabel("Height: "));
   80           panel.add(heightTF);
   81       }
   82       
   83       public void setValue(Object value)  {
   84           super.setValue(value);
   85           
   86   	if (value != null) {
   87   	    Rectangle rect = (Rectangle)value;
   88   	    xTF.setText(Integer.toString(rect.x));
   89   	    yTF.setText(Integer.toString(rect.y));
   90   	    widthTF.setText(Integer.toString(rect.width));
   91   	    heightTF.setText(Integer.toString(rect.height));
   92   	} else {
   93   	    // null rect
   94   	    xTF.setText("");
   95   	    yTF.setText("");
   96   	    widthTF.setText("");
   97   	    heightTF.setText("");
   98   	}
   99       }
  100       
  101       public Object getValue()  {
  102   	try {
  103   	    int x = Integer.parseInt(xTF.getText());
  104   	    int y = Integer.parseInt(yTF.getText());
  105   	    int width = Integer.parseInt(widthTF.getText());
  106   	    int height = Integer.parseInt(heightTF.getText());
  107   	    
  108   	    return new Rectangle(x, y, width, height);
  109   	} catch (NumberFormatException ex) {
  110   	    // Fall out but return null
  111   	}
  112   	return null;
  113       }
  114   
  115   }

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