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