1 21 22 25 package net.sf.launch4j.binding; 26 27 import java.awt.Color ; 28 29 import javax.swing.JToggleButton ; 30 31 import org.apache.commons.beanutils.PropertyUtils; 32 33 38 public class JToggleButtonBinding implements Binding { 39 private final String _property; 40 private final JToggleButton _button; 41 private final boolean _defaultValue; 42 private final Color _validColor; 43 44 public JToggleButtonBinding(String property, JToggleButton button, 45 boolean defaultValue) { 46 if (property == null || button == null) { 47 throw new NullPointerException (); 48 } 49 if (property.equals("")) { 50 throw new IllegalArgumentException (); 51 } 52 _property = property; 53 _button = button; 54 _defaultValue = defaultValue; 55 _validColor = _button.getBackground(); 56 } 57 58 public String getProperty() { 59 return _property; 60 } 61 62 public void clear(IValidatable bean) { 63 _button.setSelected(_defaultValue); 64 } 65 66 public void put(IValidatable bean) { 67 try { 68 Boolean b = (Boolean ) PropertyUtils.getProperty(bean, _property); 69 _button.setSelected(b != null && b.booleanValue()); 70 } catch (Exception e) { 71 throw new BindingException(e); 72 } 73 } 74 75 public void get(IValidatable bean) { 76 try { 77 PropertyUtils.setProperty(bean, _property, 78 Boolean.valueOf(_button.isSelected())); 79 } catch (Exception e) { 80 throw new BindingException(e); 81 } 82 } 83 84 public void markValid() { 85 _button.setBackground(_validColor); 86 _button.requestFocusInWindow(); 87 } 88 89 public void markInvalid() { 90 _button.setBackground(Binding.INVALID_COLOR); 91 } 92 93 public void setEnabled(boolean enabled) { 94 _button.setEnabled(enabled); 95 } 96 } 97 | Popular Tags |