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