summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.ui.swing/src/main/java/net/wotonomy/ui/swing/components/RadioButtonPanel.java
blob: 62cbc2c4bd4bcd3cbac5fbcf4d59030fb6e4074c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.ui.swing.components;

import java.awt.Component;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

/**
 * RadioButtonPanel is a simple extension of ButtonPanel. Differences are that
 * it uses radio buttons and the default alignment is vertical. The radio
 * buttons are placed in a ButtonGroup and the panel defaults to having no
 * buttons selected.
 *
 * @author michael@mpowers.net
 * @author $Author: cgruber $
 * @version $Revision: 904 $ $Date: 2006-02-18 18:19:05 -0500 (Sat, 18 Feb 2006)
 *          $
 */
public class RadioButtonPanel extends ButtonPanel {
	/**
	 * A ButtonGroup to help manage button state.
	 */
	protected ButtonGroup buttonGroup;

	/**
	 * ButtonGroup does not make it easy to unselect all buttons. The preferred way
	 * to do it is actually to create a hidden button.
	 */
	protected JRadioButton hiddenButton;

	/**
	 * Constructs a RadioButtonPanel. Three buttons are created so the panel is
	 * filled when used in a GUI-builder environment.
	 */
	public RadioButtonPanel() {
		super();
	}

	/**
	 * Constructs a ButtonPanel using specified buttons.
	 * 
	 * @param buttonList An array containing the strings to be used in labeling the
	 *                   buttons.
	 */
	public RadioButtonPanel(String[] buttonList) {
		super(buttonList);
	}

	/**
	 * Overridden to set vertical-center alignment and 2-pixel vgap.
	 */
	protected void initLayout() {
		super.initLayout();
		buttonPanelLayout.setAlignment(BetterFlowLayout.CENTER_VERTICAL);
		buttonPanelLayout.setVgap(2); // looks nicer than java l&f recommendation (imho)
	}

	/**
	 * Overridden to return a JRadioButton.
	 * 
	 * @param aLabel The label for the component that will be created.
	 * @return The newly created component.
	 */
	protected Component createComponentWithLabel(String aLabel) {
		String buttonLabel = aLabel;
		JRadioButton newButton = new JRadioButton();
		newButton.setName(aLabel);
		newButton.setText(buttonLabel);
		newButton.setActionCommand(aLabel);
		newButton.addActionListener(this);

		// reduce insets per java l&f guidelines (was 4 on each side)
		newButton.setBorder(new EmptyBorder(0, 4, 0, 4));

		if (buttonGroup == null) {
			buttonGroup = new ButtonGroup();

			// cheesy hack to allow a buttongroup to have no items selected.
			// note that the button is not added to container or buttonList.
			hiddenButton = new JRadioButton("Hidden Button");
			buttonGroup.add(hiddenButton);
		}
		buttonGroup.add(newButton);

		return newButton;
	}

	/**
	 * Selects the button whose name matches the given text value.
	 * 
	 * @param newText A String matching the name of one of the buttons. If null,
	 *                empty, or not matching, all buttons are deselected.
	 */
	public void setValue(String aName) {
		if (aName != null) {
			Component c = null;
			int count = buttonContainer.getComponentCount();
			for (int i = 0; i < count; i++) {
				c = buttonContainer.getComponent(i);
				if (c instanceof AbstractButton) {
					if (c.getName().equals(aName)) {
						((AbstractButton) c).setSelected(true);
						return;
					}
				}
			}
		}

		// null, empty, or not matching - deselect all
		hiddenButton.setSelected(true);
	}

	/**
	 * Gets the name of the currently selected button.
	 * 
	 * @return A string matching the name of the currently selected button, or null
	 *         of no button is selected.
	 */
	public String getValue() {
		String result = null;
		Component c = null;
		int count = buttonContainer.getComponentCount();
		for (int i = 0; i < count; i++) {
			c = buttonContainer.getComponent(i);
			if ((c instanceof AbstractButton) && (((AbstractButton) c).isSelected())) {
				return c.getName();
			}
		}
		return result;
	}

	/**
	 * Tests whether the specified value is checked.
	 * 
	 * @param aValue A value to be tested.
	 * @return True if the specified value is checked, otherwise false.
	 */
	public boolean getValue(String aValue) {
		if (aValue == null)
			return false;
		return aValue.equals(getValue());
	}

}