source: release-kits/wirk3/bin/launch4j/src/net/sf/launch4j/binding/OptComponentBinding.java@ 15023

Last change on this file since 15023 was 15023, checked in by oranfry, 16 years ago

did the bulk of the work on wirk3

File size: 3.0 KB
Line 
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 */
25package net.sf.launch4j.binding;
26
27import java.awt.event.ActionEvent;
28import java.awt.event.ActionListener;
29import java.util.Arrays;
30
31import javax.swing.JToggleButton;
32
33import org.apache.commons.beanutils.PropertyUtils;
34
35/**
36 * @author Copyright (C) 2005 Grzegorz Kowal
37 */
38public class OptComponentBinding implements Binding, ActionListener {
39 private final Bindings _bindings;
40 private final String _property;
41 private final Class _clazz;
42 private final JToggleButton _button;
43 private final boolean _enabledByDefault;
44
45 public OptComponentBinding(Bindings bindings, String property, Class clazz,
46 JToggleButton button, boolean enabledByDefault) {
47 if (property == null || clazz == null || button == null) {
48 throw new NullPointerException();
49 }
50 if (property.equals("")) { //$NON-NLS-1$
51 throw new IllegalArgumentException();
52 }
53 if (!Arrays.asList(clazz.getInterfaces()).contains(IValidatable.class)) {
54 throw new IllegalArgumentException(Messages.getString("OptComponentBinding.must.implement") //$NON-NLS-1$
55 + IValidatable.class);
56 }
57 _bindings = bindings;
58 _property = property;
59 _clazz = clazz;
60 _button = button;
61 _button.addActionListener(this);
62 _enabledByDefault = enabledByDefault;
63 }
64
65 public String getProperty() {
66 return _property;
67 }
68
69 public void clear(IValidatable bean) {
70 _button.setSelected(_enabledByDefault);
71 updateComponents();
72 }
73
74 public void put(IValidatable bean) {
75 try {
76 Object component = PropertyUtils.getProperty(bean, _property);
77 _button.setSelected(component != null);
78 updateComponents();
79 } catch (Exception e) {
80 throw new BindingException(e);
81 }
82 }
83
84 public void get(IValidatable bean) {
85 try {
86 PropertyUtils.setProperty(bean, _property, _button.isSelected()
87 ? _clazz.newInstance() : null);
88 } catch (Exception e) {
89 throw new BindingException(e);
90 }
91 }
92
93 public void markValid() {}
94
95 public void markInvalid() {}
96
97 public void setEnabled(boolean enabled) {} // XXX implement?
98
99 public void actionPerformed(ActionEvent e) {
100 updateComponents();
101 }
102
103 private void updateComponents() {
104 _bindings.setComponentsEnabled(_property, _button.isSelected());
105 }
106}
Note: See TracBrowser for help on using the repository browser.