1 21 22 25 package net.sf.launch4j.binding; 26 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.util.Arrays ; 30 31 import javax.swing.JToggleButton ; 32 33 import org.apache.commons.beanutils.PropertyUtils; 34 35 38 public class OptComponentBinding implements Binding, ActionListener { 39 private final Bindings _bindings; 40 private final String _property; 41 private final Class _clazz; 42 private final JToggleButton _button; 43 private final boolean _enabledByDefault; 44 45 public OptComponentBinding(Bindings bindings, String property, Class clazz, 46 JToggleButton button, boolean enabledByDefault) { 47 if (property == null || clazz == null || button == null) { 48 throw new NullPointerException (); 49 } 50 if (property.equals("")) { 51 throw new IllegalArgumentException (); 52 } 53 if (!Arrays.asList(clazz.getInterfaces()).contains(IValidatable.class)) { 54 throw new IllegalArgumentException ( 55 Messages.getString("OptComponentBinding.must.implement") 56 + IValidatable.class); 57 } 58 _bindings = bindings; 59 _property = property; 60 _clazz = clazz; 61 _button = button; 62 _button.addActionListener(this); 63 _enabledByDefault = enabledByDefault; 64 } 65 66 public String getProperty() { 67 return _property; 68 } 69 70 public void clear(IValidatable bean) { 71 _button.setSelected(_enabledByDefault); 72 updateComponents(); 73 } 74 75 public void put(IValidatable bean) { 76 try { 77 Object component = PropertyUtils.getProperty(bean, _property); 78 _button.setSelected(component != null); 79 updateComponents(); 80 } catch (Exception e) { 81 throw new BindingException(e); 82 } 83 } 84 85 public void get(IValidatable bean) { 86 try { 87 PropertyUtils.setProperty(bean, _property, _button.isSelected() 88 ? _clazz.newInstance() : null); 89 } catch (Exception e) { 90 throw new BindingException(e); 91 } 92 } 93 94 public void markValid() {} 95 96 public void markInvalid() {} 97 98 public void setEnabled(boolean enabled) {} 100 public void actionPerformed(ActionEvent e) { 101 updateComponents(); 102 } 103 104 private void updateComponents() { 105 _bindings.setComponentsEnabled(_property, _button.isSelected()); 106 } 107 } 108 | Popular Tags |