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