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 Apr 30, 2005
|
---|
24 | */
|
---|
25 | package net.sf.launch4j.binding;
|
---|
26 |
|
---|
27 | import java.beans.PropertyChangeEvent;
|
---|
28 | import java.beans.PropertyChangeListener;
|
---|
29 | import java.util.HashMap;
|
---|
30 | import java.util.Iterator;
|
---|
31 | import java.util.Map;
|
---|
32 |
|
---|
33 | import javax.swing.JComponent;
|
---|
34 | import javax.swing.JRadioButton;
|
---|
35 | import javax.swing.JTextArea;
|
---|
36 | import javax.swing.JToggleButton;
|
---|
37 | import javax.swing.text.JTextComponent;
|
---|
38 |
|
---|
39 | import org.apache.commons.beanutils.PropertyUtils;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Creates and handles bindings.
|
---|
43 | *
|
---|
44 | * @author Copyright (C) 2005 Grzegorz Kowal
|
---|
45 | */
|
---|
46 | public class Bindings implements PropertyChangeListener {
|
---|
47 | private final Map _bindings = new HashMap();
|
---|
48 | private final Map _optComponents = new HashMap();
|
---|
49 | private boolean _modified = false;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Used to track component modifications.
|
---|
53 | */
|
---|
54 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
55 | if ("AccessibleValue".equals(evt.getPropertyName()) //$NON-NLS-1$
|
---|
56 | || "AccessibleText".equals(evt.getPropertyName())) { //$NON-NLS-1$
|
---|
57 | _modified = true;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Any of the components modified?
|
---|
63 | */
|
---|
64 | public boolean isModified() {
|
---|
65 | return _modified;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public Binding getBinding(String property) {
|
---|
69 | return (Binding) _bindings.get(property);
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void registerPropertyChangeListener(JComponent c) {
|
---|
73 | c.getAccessibleContext().addPropertyChangeListener(this);
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void registerPropertyChangeListener(JComponent[] cs) {
|
---|
77 | for (int i = 0; i < cs.length; i++) {
|
---|
78 | cs[i].getAccessibleContext().addPropertyChangeListener(this);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private boolean isPropertyNull(IValidatable bean, Binding b) {
|
---|
83 | try {
|
---|
84 | for (Iterator iter = _optComponents.keySet().iterator(); iter.hasNext();) {
|
---|
85 | String property = (String) iter.next();
|
---|
86 | if (b.getProperty().startsWith(property)) {
|
---|
87 | return PropertyUtils.getProperty(bean, property) == null;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return false;
|
---|
91 | } catch (Exception e) {
|
---|
92 | throw new BindingException(e);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Enables or disables all components bound to properties that begin with given prefix.
|
---|
98 | */
|
---|
99 | public void setComponentsEnabled(String prefix, boolean enabled) {
|
---|
100 | for (Iterator iter = _bindings.values().iterator(); iter.hasNext();) {
|
---|
101 | Binding b = (Binding) iter.next();
|
---|
102 | if (b.getProperty().startsWith(prefix)) {
|
---|
103 | b.setEnabled(enabled);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Clear all components, set them to their default values.
|
---|
110 | * Clears the _modified flag.
|
---|
111 | */
|
---|
112 | public void clear(IValidatable bean) {
|
---|
113 | for (Iterator iter = _optComponents.values().iterator(); iter.hasNext();) {
|
---|
114 | ((Binding) iter.next()).clear(bean);
|
---|
115 | }
|
---|
116 | for (Iterator iter = _bindings.values().iterator(); iter.hasNext();) {
|
---|
117 | ((Binding) iter.next()).clear(bean);
|
---|
118 | }
|
---|
119 | _modified = false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Copies data from the Java Bean to the UI components.
|
---|
124 | * Clears the _modified flag.
|
---|
125 | */
|
---|
126 | public void put(IValidatable bean) {
|
---|
127 | for (Iterator iter = _optComponents.values().iterator(); iter.hasNext();) {
|
---|
128 | ((Binding) iter.next()).put(bean);
|
---|
129 | }
|
---|
130 | for (Iterator iter = _bindings.values().iterator(); iter.hasNext();) {
|
---|
131 | Binding b = (Binding) iter.next();
|
---|
132 | if (isPropertyNull(bean, b)) {
|
---|
133 | b.clear(null);
|
---|
134 | } else {
|
---|
135 | b.put(bean);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | _modified = false;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Copies data from UI components to the Java Bean and checks it's class invariants.
|
---|
143 | * Clears the _modified flag.
|
---|
144 | * @throws InvariantViolationException
|
---|
145 | * @throws BindingException
|
---|
146 | */
|
---|
147 | public void get(IValidatable bean) {
|
---|
148 | try {
|
---|
149 | for (Iterator iter = _optComponents.values().iterator(); iter.hasNext();) {
|
---|
150 | ((Binding) iter.next()).get(bean);
|
---|
151 | }
|
---|
152 | for (Iterator iter = _bindings.values().iterator(); iter.hasNext();) {
|
---|
153 | Binding b = (Binding) iter.next();
|
---|
154 | if (!isPropertyNull(bean, b)) {
|
---|
155 | b.get(bean);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | bean.checkInvariants();
|
---|
159 | for (Iterator iter = _optComponents.keySet().iterator(); iter.hasNext();) {
|
---|
160 | String property = (String) iter.next();
|
---|
161 | IValidatable component = (IValidatable) PropertyUtils.getProperty(bean, property);
|
---|
162 | if (component != null) {
|
---|
163 | component.checkInvariants();
|
---|
164 | }
|
---|
165 | }
|
---|
166 | _modified = false; // XXX
|
---|
167 | } catch (InvariantViolationException e) {
|
---|
168 | e.setBinding(getBinding(e.getProperty()));
|
---|
169 | throw e;
|
---|
170 | } catch (Exception e) {
|
---|
171 | throw new BindingException(e);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | private Bindings add(Binding b) {
|
---|
176 | if (_bindings.containsKey(b.getProperty())) {
|
---|
177 | throw new BindingException(Messages.getString("Bindings.duplicate.binding")); //$NON-NLS-1$
|
---|
178 | }
|
---|
179 | _bindings.put(b.getProperty(), b);
|
---|
180 | return this;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Add an optional (nullable) Java Bean component of type clazz.
|
---|
185 | */
|
---|
186 | public Bindings addOptComponent(String property, Class clazz, JToggleButton c,
|
---|
187 | boolean enabledByDefault) {
|
---|
188 | Binding b = new OptComponentBinding(this, property, clazz, c, enabledByDefault);
|
---|
189 | if (_optComponents.containsKey(property)) {
|
---|
190 | throw new BindingException(Messages.getString("Bindings.duplicate.binding")); //$NON-NLS-1$
|
---|
191 | }
|
---|
192 | _optComponents.put(property, b);
|
---|
193 | return this;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Add an optional (nullable) Java Bean component of type clazz.
|
---|
198 | */
|
---|
199 | public Bindings addOptComponent(String property, Class clazz, JToggleButton c) {
|
---|
200 | return addOptComponent(property, clazz, c, false);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Handles JEditorPane, JTextArea, JTextField
|
---|
205 | */
|
---|
206 | public Bindings add(String property, JTextComponent c, String defaultValue) {
|
---|
207 | registerPropertyChangeListener(c);
|
---|
208 | return add(new JTextComponentBinding(property, c, defaultValue));
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Handles JEditorPane, JTextArea, JTextField
|
---|
213 | */
|
---|
214 | public Bindings add(String property, JTextComponent c) {
|
---|
215 | registerPropertyChangeListener(c);
|
---|
216 | return add(new JTextComponentBinding(property, c, "")); //$NON-NLS-1$
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Handles JToggleButton, JCheckBox
|
---|
221 | */
|
---|
222 | public Bindings add(String property, JToggleButton c, boolean defaultValue) {
|
---|
223 | registerPropertyChangeListener(c);
|
---|
224 | return add(new JToggleButtonBinding(property, c, defaultValue));
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Handles JToggleButton, JCheckBox
|
---|
229 | */
|
---|
230 | public Bindings add(String property, JToggleButton c) {
|
---|
231 | registerPropertyChangeListener(c);
|
---|
232 | return add(new JToggleButtonBinding(property, c, false));
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Handles JRadioButton
|
---|
237 | */
|
---|
238 | public Bindings add(String property, JRadioButton[] cs, int defaultValue) {
|
---|
239 | registerPropertyChangeListener(cs);
|
---|
240 | return add(new JRadioButtonBinding(property, cs, defaultValue));
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Handles JRadioButton
|
---|
245 | */
|
---|
246 | public Bindings add(String property, JRadioButton[] cs) {
|
---|
247 | registerPropertyChangeListener(cs);
|
---|
248 | return add(new JRadioButtonBinding(property, cs, 0));
|
---|
249 | }
|
---|
250 |
|
---|
251 | public Bindings add(String property, String stateProperty,
|
---|
252 | JToggleButton button, JTextArea textArea) {
|
---|
253 | registerPropertyChangeListener(button);
|
---|
254 | registerPropertyChangeListener(textArea);
|
---|
255 | return add(new OptListBinding(property, stateProperty, button, textArea));
|
---|
256 | }
|
---|
257 | }
|
---|