001: /*
002: Launch4j (http://launch4j.sourceforge.net/)
003: Cross-platform Java application wrapper for creating Windows native executables.
004:
005: Copyright (c) 2004, 2007 Grzegorz Kowal
006:
007: All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modification,
010: are permitted provided that the following conditions are met:
011:
012: * Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014: * Redistributions in binary form must reproduce the above copyright notice,
015: this list of conditions and the following disclaimer in the documentation
016: and/or other materials provided with the distribution.
017: * Neither the name of the Launch4j nor the names of its contributors
018: may be used to endorse or promote products derived from this software without
019: specific prior written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
024: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
025: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
026: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
027: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
028: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
029: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
030: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: /*
035: * Created on May 11, 2005
036: */
037: package net.sf.launch4j.binding;
038:
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041: import java.util.Arrays;
042:
043: import javax.swing.JToggleButton;
044:
045: import org.apache.commons.beanutils.PropertyUtils;
046:
047: /**
048: * @author Copyright (C) 2005 Grzegorz Kowal
049: */
050: public class OptComponentBinding implements Binding, ActionListener {
051: private final Bindings _bindings;
052: private final String _property;
053: private final Class _clazz;
054: private final JToggleButton _button;
055: private final boolean _enabledByDefault;
056:
057: public OptComponentBinding(Bindings bindings, String property,
058: Class clazz, JToggleButton button, boolean enabledByDefault) {
059: if (property == null || clazz == null || button == null) {
060: throw new NullPointerException();
061: }
062: if (property.equals("")) {
063: throw new IllegalArgumentException();
064: }
065: if (!Arrays.asList(clazz.getInterfaces()).contains(
066: IValidatable.class)) {
067: throw new IllegalArgumentException(Messages
068: .getString("OptComponentBinding.must.implement")
069: + IValidatable.class);
070: }
071: _bindings = bindings;
072: _property = property;
073: _clazz = clazz;
074: _button = button;
075: _button.addActionListener(this );
076: _enabledByDefault = enabledByDefault;
077: }
078:
079: public String getProperty() {
080: return _property;
081: }
082:
083: public void clear(IValidatable bean) {
084: _button.setSelected(_enabledByDefault);
085: updateComponents();
086: }
087:
088: public void put(IValidatable bean) {
089: try {
090: Object component = PropertyUtils.getProperty(bean,
091: _property);
092: _button.setSelected(component != null);
093: updateComponents();
094: } catch (Exception e) {
095: throw new BindingException(e);
096: }
097: }
098:
099: public void get(IValidatable bean) {
100: try {
101: PropertyUtils.setProperty(bean, _property, _button
102: .isSelected() ? _clazz.newInstance() : null);
103: } catch (Exception e) {
104: throw new BindingException(e);
105: }
106: }
107:
108: public void markValid() {
109: }
110:
111: public void markInvalid() {
112: }
113:
114: public void setEnabled(boolean enabled) {
115: } // XXX implement?
116:
117: public void actionPerformed(ActionEvent e) {
118: updateComponents();
119: }
120:
121: private void updateComponents() {
122: _bindings.setComponentsEnabled(_property, _button.isSelected());
123: }
124: }
|