1 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 ; 14 import java.awt.event.ItemListener ; 15 16 import javax.swing.ButtonModel ; 17 import javax.swing.JCheckBox ; 18 import javax.swing.JComponent ; 19 import javax.swing.JToggleButton ; 20 21 22 32 public class BooleanBinding extends AbstractBinding { 33 private JComponent component; 34 private ButtonModel buttonModel; 35 36 public BooleanBinding(JToggleButton toggleButton, 37 DataModel dataModel, String fieldName) { 38 super(toggleButton, dataModel, fieldName, AbstractBinding.AUTO_VALIDATE_NONE); 41 initModel(toggleButton.getModel()); 42 } 43 44 public BooleanBinding(JComponent component, ButtonModel buttonModel, 45 DataModel dataModel, String fieldName) { 46 super(component, dataModel, fieldName, AbstractBinding.AUTO_VALIDATE_NONE); 47 initModel(buttonModel); 48 } 49 50 public JComponent getComponent() { 51 return component; 52 } 53 54 protected void setComponent(JComponent component) { 55 this.component = component; 56 } 57 58 protected Object getComponentValue(){ 59 return Boolean.valueOf(buttonModel.isSelected()); 60 } 61 62 protected void setComponentValue(Object value) { 63 if (value != null) { 64 buttonModel.setSelected( ( (Boolean ) value).booleanValue()); 65 } else { 66 buttonModel.setSelected(false); 67 } 68 } 69 70 private void initModel(ButtonModel model) { 71 buttonModel = model; 72 buttonModel.addItemListener(new ItemListener () { 73 public void itemStateChanged(ItemEvent e) { 74 if (!pulling) { 75 setModified(true); 76 } 77 } 78 }); 79 } 80 81 } | Popular Tags |