KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > launch4j > binding > JListBinding


1 /*
2     Launch4j (http://launch4j.sourceforge.net/)
3     Cross-platform Java application wrapper for creating Windows native executables.
4
5     Copyright (C) 2004, 2006 Grzegorz Kowal
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */

21
22 /*
23  * Created on May 1, 2006
24  */

25 package net.sf.launch4j.binding;
26
27 import java.awt.Color JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import javax.swing.DefaultListModel JavaDoc;
33 import javax.swing.JList JavaDoc;
34
35 import org.apache.commons.beanutils.PropertyUtils;
36
37 /**
38  * @author Copyright (C) 2006 Grzegorz Kowal
39  */

40 public class JListBinding implements Binding {
41     private final String JavaDoc _property;
42     private final JList JavaDoc _list;
43     private final Color JavaDoc _validColor;
44
45     public JListBinding(String JavaDoc property, JList JavaDoc list) {
46         if (property == null || list == null) {
47             throw new NullPointerException JavaDoc();
48         }
49         if (property.equals("")) {
50             throw new IllegalArgumentException JavaDoc();
51         }
52         _property = property;
53         _list = list;
54         _validColor = _list.getBackground();
55     }
56
57     public String JavaDoc getProperty() {
58         return _property;
59     }
60
61     public void clear(IValidatable bean) {
62         _list.setModel(new DefaultListModel JavaDoc());
63     }
64
65     public void put(IValidatable bean) {
66         try {
67             List JavaDoc list = (List JavaDoc) PropertyUtils.getProperty(bean, _property);
68             DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
69             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
70                 model.addElement(iter.next());
71             }
72             _list.setModel(model);
73         } catch (Exception JavaDoc e) {
74             throw new BindingException(e);
75         }
76     }
77
78     public void get(IValidatable bean) {
79         try {
80             DefaultListModel JavaDoc model = (DefaultListModel JavaDoc) _list.getModel();
81             final int size = model.getSize();
82             List JavaDoc list = new ArrayList JavaDoc(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 JavaDoc 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