KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > launch4j > binding > JRadioButtonBinding


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  */

25 package net.sf.launch4j.binding;
26
27 import java.awt.Color JavaDoc;
28
29 import javax.swing.JRadioButton JavaDoc;
30
31 import org.apache.commons.beanutils.PropertyUtils;
32
33 /**
34  * @author Copyright (C) 2005 Grzegorz Kowal
35  */

36 public class JRadioButtonBinding implements Binding {
37     private final String JavaDoc _property;
38     private final JRadioButton JavaDoc[] _buttons;
39     private final int _defaultValue;
40     private final Color JavaDoc _validColor;
41
42     public JRadioButtonBinding(String JavaDoc property, JRadioButton JavaDoc[] buttons, int defaultValue) {
43         if (property == null || buttons == null) {
44             throw new NullPointerException JavaDoc();
45         }
46         for (int i = 0; i < buttons.length; i++) {
47             if (buttons[i] == null) {
48                 throw new NullPointerException JavaDoc();
49             }
50         }
51         if (property.equals("")
52                 || buttons.length == 0
53                 || defaultValue < 0 || defaultValue >= buttons.length) {
54             throw new IllegalArgumentException JavaDoc();
55         }
56         _property = property;
57         _buttons = buttons;
58         _defaultValue = defaultValue;
59         _validColor = buttons[0].getBackground();
60     }
61
62     public String JavaDoc 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 JavaDoc i = (Integer JavaDoc) PropertyUtils.getProperty(bean, _property);
73             if (i == null) {
74                 throw new BindingException(
75                         Messages.getString("JRadioButtonBinding.property.null"));
76             }
77             select(i.intValue());
78         } catch (Exception JavaDoc e) {
79             throw new BindingException(e);
80         }
81     }
82
83     public void get(IValidatable bean) {
84         try {
85             for (int i = 0; i < _buttons.length; i++) {
86                 if (_buttons[i].isSelected()) {
87                     PropertyUtils.setProperty(bean, _property, new Integer JavaDoc(i));
88                     return;
89                 }
90             }
91             throw new BindingException(
92                     Messages.getString("JRadioButtonBinding.nothing.selected"));
93         } catch (Exception JavaDoc e) {
94             throw new BindingException(e);
95         }
96     }
97
98     private void select(int index) {
99         if (index < 0 || index >= _buttons.length) {
100             throw new BindingException(
101                     Messages.getString("JRadioButtonBinding.index.out.of.bounds"));
102         }
103         _buttons[index].setSelected(true);
104     }
105
106     public void markValid() {
107         for (int i = 0; i < _buttons.length; i++) {
108             if (_buttons[i].isSelected()) {
109                 _buttons[i].setBackground(_validColor);
110                 _buttons[i].requestFocusInWindow();
111                 return;
112             }
113         }
114         throw new BindingException(
115                 Messages.getString("JRadioButtonBinding.nothing.selected"));
116     }
117
118     public void markInvalid() {
119         for (int i = 0; i < _buttons.length; i++) {
120             if (_buttons[i].isSelected()) {
121                 _buttons[i].setBackground(Binding.INVALID_COLOR);
122                 return;
123             }
124         }
125         throw new BindingException(
126                 Messages.getString("JRadioButtonBinding.nothing.selected"));
127     }
128     
129     public void setEnabled(boolean enabled) {
130         for (int i = 0; i < _buttons.length; i++) {
131             _buttons[i].setEnabled(enabled);
132         }
133     }
134 }
135
Popular Tags