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 10, 2005
036: */
037: package net.sf.launch4j.binding;
038:
039: import java.awt.Color;
040:
041: import javax.swing.JRadioButton;
042:
043: import org.apache.commons.beanutils.PropertyUtils;
044:
045: /**
046: * @author Copyright (C) 2005 Grzegorz Kowal
047: */
048: public class JRadioButtonBinding implements Binding {
049: private final String _property;
050: private final JRadioButton[] _buttons;
051: private final int _defaultValue;
052: private final Color _validColor;
053:
054: public JRadioButtonBinding(String property, JRadioButton[] buttons,
055: int defaultValue) {
056: if (property == null || buttons == null) {
057: throw new NullPointerException();
058: }
059: for (int i = 0; i < buttons.length; i++) {
060: if (buttons[i] == null) {
061: throw new NullPointerException();
062: }
063: }
064: if (property.equals("") || buttons.length == 0
065: || defaultValue < 0 || defaultValue >= buttons.length) {
066: throw new IllegalArgumentException();
067: }
068: _property = property;
069: _buttons = buttons;
070: _defaultValue = defaultValue;
071: _validColor = buttons[0].getBackground();
072: }
073:
074: public String getProperty() {
075: return _property;
076: }
077:
078: public void clear(IValidatable bean) {
079: select(_defaultValue);
080: }
081:
082: public void put(IValidatable bean) {
083: try {
084: Integer i = (Integer) PropertyUtils.getProperty(bean,
085: _property);
086: if (i == null) {
087: throw new BindingException(Messages
088: .getString("JRadioButtonBinding.property.null"));
089: }
090: select(i.intValue());
091: } catch (Exception e) {
092: throw new BindingException(e);
093: }
094: }
095:
096: public void get(IValidatable bean) {
097: try {
098: for (int i = 0; i < _buttons.length; i++) {
099: if (_buttons[i].isSelected()) {
100: PropertyUtils.setProperty(bean, _property,
101: new Integer(i));
102: return;
103: }
104: }
105: throw new BindingException(Messages
106: .getString("JRadioButtonBinding.nothing.selected"));
107: } catch (Exception e) {
108: throw new BindingException(e);
109: }
110: }
111:
112: private void select(int index) {
113: if (index < 0 || index >= _buttons.length) {
114: throw new BindingException(
115: Messages
116: .getString("JRadioButtonBinding.index.out.of.bounds"));
117: }
118: _buttons[index].setSelected(true);
119: }
120:
121: public void markValid() {
122: for (int i = 0; i < _buttons.length; i++) {
123: if (_buttons[i].isSelected()) {
124: _buttons[i].setBackground(_validColor);
125: _buttons[i].requestFocusInWindow();
126: return;
127: }
128: }
129: throw new BindingException(Messages
130: .getString("JRadioButtonBinding.nothing.selected"));
131: }
132:
133: public void markInvalid() {
134: for (int i = 0; i < _buttons.length; i++) {
135: if (_buttons[i].isSelected()) {
136: _buttons[i].setBackground(Binding.INVALID_COLOR);
137: return;
138: }
139: }
140: throw new BindingException(Messages
141: .getString("JRadioButtonBinding.nothing.selected"));
142: }
143:
144: public void setEnabled(boolean enabled) {
145: for (int i = 0; i < _buttons.length; i++) {
146: _buttons[i].setEnabled(enabled);
147: }
148: }
149: }
|