1 21 22 25 package net.sf.launch4j.binding; 26 27 import java.awt.Color ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 import javax.swing.JTextArea ; 34 import javax.swing.JToggleButton ; 35 36 import org.apache.commons.beanutils.BeanUtils; 37 import org.apache.commons.beanutils.PropertyUtils; 38 39 42 public class OptJTextAreaBinding implements Binding, ActionListener { 43 private final String _property; 44 private final String _stateProperty; 45 private final JToggleButton _button; 46 private final JTextArea _textArea; 47 private final Color _validColor; 48 49 public OptJTextAreaBinding(String property, String stateProperty, 50 JToggleButton button, JTextArea textArea) { 51 if (property == null || button == null || textArea == null) { 52 throw new NullPointerException (); 53 } 54 if (property.equals("")) { 55 throw new IllegalArgumentException (); 56 } 57 _property = property; 58 _stateProperty = stateProperty; 59 _button = button; 60 _textArea = textArea; 61 _validColor = _textArea.getBackground(); 62 button.addActionListener(this); 63 } 64 65 public String getProperty() { 66 return _property; 67 } 68 69 public void clear(IValidatable bean) { 70 put(bean); 71 } 72 73 public void put(IValidatable bean) { 74 try { 75 boolean selected = "true".equals(BeanUtils.getProperty(bean, 76 _stateProperty)); 77 _button.setSelected(selected); 78 _textArea.setEnabled(selected); 79 List list = (List ) PropertyUtils.getProperty(bean, _property); 80 StringBuffer sb = new StringBuffer (); 81 if (list != null) { 82 for (int i = 0; i < list.size(); i++) { 83 sb.append(list.get(i)); 84 if (i < list.size() - 1) { 85 sb.append("\n"); 86 } 87 } 88 } 89 _textArea.setText(sb.toString()); 90 } catch (Exception e) { 91 throw new BindingException(e); 92 } 93 } 94 95 public void get(IValidatable bean) { 96 try { 97 String text = _textArea.getText(); 98 if (_button.isSelected() && !text.equals("")) { 99 String [] items = text.split("\n"); 100 List list = new ArrayList (); 101 for (int i = 0; i < items.length; i++) { 102 list.add(items[i]); 103 } 104 PropertyUtils.setProperty(bean, _property, list); 105 } else { 106 PropertyUtils.setProperty(bean, _property, null); 107 } 108 } catch (Exception e) { 109 throw new BindingException(e); 110 } 111 } 112 113 public void markValid() { 114 _textArea.setBackground(_validColor); 115 _textArea.requestFocusInWindow(); 116 } 117 118 public void markInvalid() { 119 _textArea.setBackground(Binding.INVALID_COLOR); 120 } 121 122 public void setEnabled(boolean enabled) { 123 _textArea.setEnabled(enabled); 124 } 125 126 public void actionPerformed(ActionEvent e) { 127 _textArea.setEnabled(_button.isSelected()); 128 } 129 } 130 | Popular Tags |