KickJava   Java API By Example, From Geeks To Geeks.

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


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 11, 2005
24  */

25 package net.sf.launch4j.binding;
26
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.util.Arrays JavaDoc;
30
31 import javax.swing.JToggleButton JavaDoc;
32
33 import org.apache.commons.beanutils.PropertyUtils;
34
35 /**
36  * @author Copyright (C) 2005 Grzegorz Kowal
37  */

38 public class OptComponentBinding implements Binding, ActionListener JavaDoc {
39     private final Bindings _bindings;
40     private final String JavaDoc _property;
41     private final Class JavaDoc _clazz;
42     private final JToggleButton JavaDoc _button;
43     private final boolean _enabledByDefault;
44
45     public OptComponentBinding(Bindings bindings, String JavaDoc property, Class JavaDoc clazz,
46                                 JToggleButton JavaDoc button, boolean enabledByDefault) {
47         if (property == null || clazz == null || button == null) {
48             throw new NullPointerException JavaDoc();
49         }
50         if (property.equals("")) {
51             throw new IllegalArgumentException JavaDoc();
52         }
53         if (!Arrays.asList(clazz.getInterfaces()).contains(IValidatable.class)) {
54             throw new IllegalArgumentException JavaDoc(
55                     Messages.getString("OptComponentBinding.must.implement")
56                     + IValidatable.class);
57         }
58         _bindings = bindings;
59         _property = property;
60         _clazz = clazz;
61         _button = button;
62         _button.addActionListener(this);
63         _enabledByDefault = enabledByDefault;
64     }
65
66     public String JavaDoc getProperty() {
67         return _property;
68     }
69
70     public void clear(IValidatable bean) {
71         _button.setSelected(_enabledByDefault);
72         updateComponents();
73     }
74
75     public void put(IValidatable bean) {
76         try {
77             Object JavaDoc component = PropertyUtils.getProperty(bean, _property);
78             _button.setSelected(component != null);
79             updateComponents();
80         } catch (Exception JavaDoc e) {
81             throw new BindingException(e);
82         }
83     }
84
85     public void get(IValidatable bean) {
86         try {
87             PropertyUtils.setProperty(bean, _property, _button.isSelected()
88                     ? _clazz.newInstance() : null);
89         } catch (Exception JavaDoc e) {
90             throw new BindingException(e);
91         }
92     }
93
94     public void markValid() {}
95
96     public void markInvalid() {}
97
98     public void setEnabled(boolean enabled) {} // XXX implement?
99

100     public void actionPerformed(ActionEvent JavaDoc e) {
101         updateComponents();
102     }
103     
104     private void updateComponents() {
105         _bindings.setComponentsEnabled(_property, _button.isSelected());
106     }
107 }
108
Popular Tags