1 21 22 25 package net.sf.launch4j.binding; 26 27 import java.awt.Color ; 28 29 import javax.swing.JRadioButton ; 30 31 import org.apache.commons.beanutils.PropertyUtils; 32 33 36 public class JRadioButtonBinding implements Binding { 37 private final String _property; 38 private final JRadioButton [] _buttons; 39 private final int _defaultValue; 40 private final Color _validColor; 41 42 public JRadioButtonBinding(String property, JRadioButton [] buttons, int defaultValue) { 43 if (property == null || buttons == null) { 44 throw new NullPointerException (); 45 } 46 for (int i = 0; i < buttons.length; i++) { 47 if (buttons[i] == null) { 48 throw new NullPointerException (); 49 } 50 } 51 if (property.equals("") 52 || buttons.length == 0 53 || defaultValue < 0 || defaultValue >= buttons.length) { 54 throw new IllegalArgumentException (); 55 } 56 _property = property; 57 _buttons = buttons; 58 _defaultValue = defaultValue; 59 _validColor = buttons[0].getBackground(); 60 } 61 62 public String getProperty() { 63 return _property; 64 } 65 66 public void clear(IValidatable bean) { 67 select(_defaultValue); 68 } 69 70 public void put(IValidatable bean) { 71 try { 72 Integer i = (Integer ) PropertyUtils.getProperty(bean, _property); 73 if (i == null) { 74 throw new BindingException( 75 Messages.getString("JRadioButtonBinding.property.null")); 76 } 77 select(i.intValue()); 78 } catch (Exception e) { 79 throw new BindingException(e); 80 } 81 } 82 83 public void get(IValidatable bean) { 84 try { 85 for (int i = 0; i < _buttons.length; i++) { 86 if (_buttons[i].isSelected()) { 87 PropertyUtils.setProperty(bean, _property, new Integer (i)); 88 return; 89 } 90 } 91 throw new BindingException( 92 Messages.getString("JRadioButtonBinding.nothing.selected")); 93 } catch (Exception e) { 94 throw new BindingException(e); 95 } 96 } 97 98 private void select(int index) { 99 if (index < 0 || index >= _buttons.length) { 100 throw new BindingException( 101 Messages.getString("JRadioButtonBinding.index.out.of.bounds")); 102 } 103 _buttons[index].setSelected(true); 104 } 105 106 public void markValid() { 107 for (int i = 0; i < _buttons.length; i++) { 108 if (_buttons[i].isSelected()) { 109 _buttons[i].setBackground(_validColor); 110 _buttons[i].requestFocusInWindow(); 111 return; 112 } 113 } 114 throw new BindingException( 115 Messages.getString("JRadioButtonBinding.nothing.selected")); 116 } 117 118 public void markInvalid() { 119 for (int i = 0; i < _buttons.length; i++) { 120 if (_buttons[i].isSelected()) { 121 _buttons[i].setBackground(Binding.INVALID_COLOR); 122 return; 123 } 124 } 125 throw new BindingException( 126 Messages.getString("JRadioButtonBinding.nothing.selected")); 127 } 128 129 public void setEnabled(boolean enabled) { 130 for (int i = 0; i < _buttons.length; i++) { 131 _buttons[i].setEnabled(enabled); 132 } 133 } 134 } 135 | Popular Tags |