KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > BooleanBinding


1 /*
2  * $Id: BooleanBinding.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.binding;
9
10 import org.jdesktop.swing.data.DataModel;
11 import org.jdesktop.swing.data.MetaData;
12
13 import java.awt.event.ItemEvent JavaDoc;
14 import java.awt.event.ItemListener JavaDoc;
15
16 import javax.swing.ButtonModel JavaDoc;
17 import javax.swing.JCheckBox JavaDoc;
18 import javax.swing.JComponent JavaDoc;
19 import javax.swing.JToggleButton JavaDoc;
20
21
22 /**
23  * Class which binds a component that supports setting a boolean
24  * value (JCheckBox) to a data model field which is type Boolean.
25  * Although this binding is most commonly used for checkboxes, it may
26  * be used with any component that defines a ButtonModel to represent
27  * its selected state.
28  *
29  * @author Amy Fowler
30  * @version 1.0
31  */

32 public class BooleanBinding extends AbstractBinding {
33     private JComponent JavaDoc component;
34     private ButtonModel JavaDoc buttonModel;
35
36     public BooleanBinding(JToggleButton JavaDoc toggleButton,
37                            DataModel dataModel, String JavaDoc fieldName) {
38         // checkboxes don't need to be validated because they cannot have a null
39
// value and type conversion should not be necessary.
40
super(toggleButton, dataModel, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
41         initModel(toggleButton.getModel());
42     }
43
44     public BooleanBinding(JComponent JavaDoc component, ButtonModel JavaDoc buttonModel,
45                            DataModel dataModel, String JavaDoc fieldName) {
46         super(component, dataModel, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
47         initModel(buttonModel);
48     }
49
50     public JComponent JavaDoc getComponent() {
51         return component;
52     }
53
54     protected void setComponent(JComponent JavaDoc component) {
55         this.component = component;
56     }
57
58     protected Object JavaDoc getComponentValue(){
59         return Boolean.valueOf(buttonModel.isSelected());
60     }
61
62     protected void setComponentValue(Object JavaDoc value) {
63         if (value != null) {
64             buttonModel.setSelected( ( (Boolean JavaDoc) value).booleanValue());
65         } else {
66             buttonModel.setSelected(false);
67         }
68     }
69
70     private void initModel(ButtonModel JavaDoc model) {
71         buttonModel = model;
72         buttonModel.addItemListener(new ItemListener JavaDoc() {
73             public void itemStateChanged(ItemEvent JavaDoc e) {
74                 if (!pulling) {
75                     setModified(true);
76                 }
77             }
78         });
79     }
80
81 }
Popular Tags