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.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.ItemEvent;
44 import java.awt.event.ItemListener;
45
46 import javax.swing.BoxLayout;
47 import javax.swing.JCheckBox;
48 import javax.swing.JPanel;
49
50 /**
51 * An editor which represents a boolean value. This editor is implemented
52 * as a checkbox with the text of the check box reflecting the state of the
53 * checkbox.
54 *
55 * @version 1.2 02/27/02
56 * @author Mark Davidson
57 */
58 public class SwingBooleanEditor extends SwingEditorSupport {
59
60 private JCheckBox checkbox;
61
62 public SwingBooleanEditor() {
63 checkbox = new JCheckBox();
64 checkbox.addItemListener(new ItemListener() {
65 public void itemStateChanged(ItemEvent evt) {
66 if (evt.getStateChange() == ItemEvent.SELECTED) {
67 setValue(Boolean.TRUE);
68 } else {
69 setValue(Boolean.FALSE);
70 }
71 }
72 });
73 panel = new JPanel();
74 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
75 panel.add(checkbox);
76 }
77
78 public void setValue(Object value ) {
79 super.setValue(value);
80 if (value != null) {
81 try {
82 checkbox.setText(value.toString());
83 if (checkbox.isSelected() != ((Boolean)value).booleanValue()) {
84 // Don't call setSelected unless the state actually changes
85 // to avoid a loop.
86 checkbox.setSelected(((Boolean)value).booleanValue());
87 }
88 } catch (Exception ex) {
89 ex.printStackTrace();
90 }
91 }
92 }
93
94 public Object getValue() {
95 return new Boolean(checkbox.isSelected());
96 }
97 }