| 1 21 22 25 package net.sf.launch4j.binding; 26 27 import java.awt.Color ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import javax.swing.DefaultListModel ; 33 import javax.swing.JList ; 34 35 import org.apache.commons.beanutils.PropertyUtils; 36 37 40 public class JListBinding implements Binding { 41 private final String _property; 42 private final JList _list; 43 private final Color _validColor; 44 45 public JListBinding(String property, JList list) { 46 if (property == null || list == null) { 47 throw new NullPointerException (); 48 } 49 if (property.equals("")) { 50 throw new IllegalArgumentException (); 51 } 52 _property = property; 53 _list = list; 54 _validColor = _list.getBackground(); 55 } 56 57 public String getProperty() { 58 return _property; 59 } 60 61 public void clear(IValidatable bean) { 62 _list.setModel(new DefaultListModel ()); 63 } 64 65 public void put(IValidatable bean) { 66 try { 67 List list = (List ) PropertyUtils.getProperty(bean, _property); 68 DefaultListModel model = new DefaultListModel (); 69 for (Iterator iter = list.iterator(); iter.hasNext();) { 70 model.addElement(iter.next()); 71 } 72 _list.setModel(model); 73 } catch (Exception e) { 74 throw new BindingException(e); 75 } 76 } 77 78 public void get(IValidatable bean) { 79 try { 80 DefaultListModel model = (DefaultListModel ) _list.getModel(); 81 final int size = model.getSize(); 82 List list = new ArrayList (size); 83 for (int i = 0; i < size; i++) { 84 list.add(model.get(i)); 85 } 86 PropertyUtils.setProperty(bean, _property, list); 87 } catch (Exception e) { 88 throw new BindingException(e); 89 } 90 } 91 92 public void markValid() { 93 _list.setBackground(_validColor); 94 _list.requestFocusInWindow(); 95 } 96 97 public void markInvalid() { 98 _list.setBackground(Binding.INVALID_COLOR); 99 } 100 101 public void setEnabled(boolean enabled) { 102 _list.setEnabled(enabled); 103 } 104 } 105 | Popular Tags |