source: release-kits/shared/launch4j/src/net/sf/launch4j/binding/JRadioButtonBinding.java@ 15024

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

putting launch4j in the shared area

File size: 3.6 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 10, 2005
24 */
25package net.sf.launch4j.binding;
26
27import java.awt.Color;
28
29import javax.swing.JRadioButton;
30
31import org.apache.commons.beanutils.PropertyUtils;
32
33/**
34 * @author Copyright (C) 2005 Grzegorz Kowal
35 */
36public class JRadioButtonBinding implements Binding {
37 private final String _property;
38 private final JRadioButton[] _buttons;
39 private final int _defaultValue;
40 private final Color _validColor;
41
42 public JRadioButtonBinding(String property, JRadioButton[] buttons, int defaultValue) {
43 if (property == null || buttons == null) {
44 throw new NullPointerException();
45 }
46 for (int i = 0; i < buttons.length; i++) {
47 if (buttons[i] == null) {
48 throw new NullPointerException();
49 }
50 }
51 if (property.equals("") //$NON-NLS-1$
52 || buttons.length == 0
53 || defaultValue < 0 || defaultValue >= buttons.length) {
54 throw new IllegalArgumentException();
55 }
56 _property = property;
57 _buttons = buttons;
58 _defaultValue = defaultValue;
59 _validColor = buttons[0].getBackground();
60 }
61
62 public String getProperty() {
63 return _property;
64 }
65
66 public void clear(IValidatable bean) {
67 select(_defaultValue);
68 }
69
70 public void put(IValidatable bean) {
71 try {
72 Integer i = (Integer) PropertyUtils.getProperty(bean, _property);
73 if (i == null) {
74 throw new BindingException(Messages.getString("JRadioButtonBinding.property.null")); //$NON-NLS-1$
75 }
76 select(i.intValue());
77 } catch (Exception e) {
78 throw new BindingException(e);
79 }
80 }
81
82 public void get(IValidatable bean) {
83 try {
84 for (int i = 0; i < _buttons.length; i++) {
85 if (_buttons[i].isSelected()) {
86 PropertyUtils.setProperty(bean, _property, new Integer(i));
87 return;
88 }
89 }
90 throw new BindingException(Messages.getString("JRadioButtonBinding.nothing.selected")); //$NON-NLS-1$
91 } catch (Exception e) {
92 throw new BindingException(e);
93 }
94 }
95
96 private void select(int index) {
97 if (index < 0 || index >= _buttons.length) {
98 throw new BindingException(Messages.getString("JRadioButtonBinding.index.out.of.bounds")); //$NON-NLS-1$
99 }
100 _buttons[index].setSelected(true);
101 }
102
103 public void markValid() {
104 for (int i = 0; i < _buttons.length; i++) {
105 if (_buttons[i].isSelected()) {
106 _buttons[i].setBackground(_validColor);
107 _buttons[i].requestFocusInWindow();
108 return;
109 }
110 }
111 throw new BindingException(Messages.getString("JRadioButtonBinding.nothing.selected")); //$NON-NLS-1$
112 }
113
114 public void markInvalid() {
115 for (int i = 0; i < _buttons.length; i++) {
116 if (_buttons[i].isSelected()) {
117 _buttons[i].setBackground(Binding.INVALID_COLOR);
118 return;
119 }
120 }
121 throw new BindingException(Messages.getString("JRadioButtonBinding.nothing.selected")); //$NON-NLS-1$
122 }
123
124 public void setEnabled(boolean enabled) {
125 for (int i = 0; i < _buttons.length; i++) {
126 _buttons[i].setEnabled(enabled);
127 }
128 }
129}
Note: See TracBrowser for help on using the repository browser.