1 56 package org.objectstyle.cayenne.swing; 57 58 import java.awt.Component ; 59 import java.awt.event.ItemEvent ; 60 import java.awt.event.ItemListener ; 61 62 import javax.swing.AbstractButton ; 63 64 69 public class ItemEventBinding extends BindingBase { 70 71 protected AbstractButton boundItem; 72 73 public ItemEventBinding(AbstractButton boundItem, String expression) { 74 super(expression); 75 this.boundItem = boundItem; 76 77 boundItem.addItemListener(new ItemListener () { 78 79 public void itemStateChanged(ItemEvent e) { 80 updateModel(); 81 } 82 }); 83 } 84 85 public Component getView() { 86 return boundItem; 87 } 88 89 public void updateView() { 90 Object value = getValue(); 91 boolean b = false; 92 93 if (value != null) { 95 if (value instanceof Boolean ) { 96 b = ((Boolean ) value).booleanValue(); 97 } 98 else if (value instanceof Number ) { 99 b = ((Number ) value).intValue() != 0; 100 } 101 } 102 103 modelUpdateDisabled = true; 104 try { 105 boundItem.setSelected(b); 106 } 107 finally { 108 modelUpdateDisabled = false; 109 } 110 } 111 112 protected void updateModel() { 113 setValue(boundItem.isSelected() ? Boolean.TRUE : Boolean.FALSE); 114 } 115 } | Popular Tags |