01: /*
02: $Id: RadioParameter.java,v 1.3 2005/02/14 12:06:19 vauclair Exp $
03:
04: Copyright (C) 2002-2005 Sebastien Vauclair
05:
06: This file is part of Extensible Java Profiler.
07:
08: Extensible Java Profiler is free software; you can redistribute it and/or
09: modify it under the terms of the GNU General Public License as published by
10: the Free Software Foundation; either version 2 of the License, or
11: (at your option) any later version.
12:
13: Extensible Java Profiler is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: GNU General Public License for more details.
17:
18: You should have received a copy of the GNU General Public License
19: along with Extensible Java Profiler; if not, write to the Free Software
20: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: */
22:
23: package ejp.presenter.api.filters.parameters;
24:
25: import javax.swing.ButtonGroup;
26: import javax.swing.JRadioButton;
27:
28: import ejp.presenter.gui.Utils;
29:
30: public class RadioParameter extends AbstractParameter {
31:
32: private final ButtonGroup m_buttonGroup = new ButtonGroup();
33:
34: private final JRadioButton[] m_jrbItems;
35:
36: public RadioParameter(String aName, String aTitle,
37: String aToolTipText, String[] labels_) {
38: super (aName, aTitle, aToolTipText);
39:
40: m_jrbItems = new JRadioButton[labels_.length];
41: for (int i = 0; i < labels_.length; i++) {
42: String label = labels_[i];
43: JRadioButton jrb = new JRadioButton();
44: Utils.setCommonProperties(jrb);
45: m_buttonGroup.add(jrb);
46:
47: addLine(label, "Click to select.", jrb, null);
48:
49: m_jrbItems[i] = jrb;
50: }
51:
52: // select first button
53: if (m_jrbItems.length > 0) {
54: m_jrbItems[0].doClick();
55: }
56: }
57:
58: public void setReadOnly() {
59: for (int i = 0; i < m_jrbItems.length; i++) {
60: m_jrbItems[i].setEnabled(false);
61: }
62: }
63:
64: public Object getValue() {
65: for (int i = 0; i < m_jrbItems.length; i++) {
66: if (m_jrbItems[i].isSelected()) {
67: return new Integer(i);
68: }
69: }
70: throw new IllegalStateException("No button is selected");
71: }
72:
73: public String getValueAsText() {
74: return getValue().toString();
75: }
76:
77: public void setValue(Object aObject) throws ClassCastException {
78: int index = ((Integer) aObject).intValue();
79: m_jrbItems[index].doClick();
80: }
81:
82: public void setValueAsText(String aTextValue)
83: throws IllegalArgumentException {
84: int i;
85: try {
86: i = Integer.parseInt(aTextValue);
87: } catch (NumberFormatException nfe) {
88: throw new IllegalArgumentException("Value \"" + aTextValue
89: + "\" does not parse as an int");
90: }
91: setValue(new Integer(i));
92: }
93: }
|