Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
RadioButtonPanel |
|
| 2.5714285714285716;2.571 |
1 | /* |
|
2 | Wotonomy: OpenStep design patterns for pure Java applications. |
|
3 | Copyright (C) 2000 Blacksmith, Inc. |
|
4 | ||
5 | This library is free software; you can redistribute it and/or |
|
6 | modify it under the terms of the GNU Lesser General Public |
|
7 | License as published by the Free Software Foundation; either |
|
8 | version 2.1 of the License, or (at your option) any later version. |
|
9 | ||
10 | This library is distributed in the hope that it will be useful, |
|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 | Lesser General Public License for more details. |
|
14 | ||
15 | You should have received a copy of the GNU Lesser General Public |
|
16 | License along with this library; if not, see http://www.gnu.org |
|
17 | */ |
|
18 | ||
19 | package net.wotonomy.ui.swing.components; |
|
20 | ||
21 | import java.awt.Component; |
|
22 | ||
23 | import javax.swing.AbstractButton; |
|
24 | import javax.swing.ButtonGroup; |
|
25 | import javax.swing.JRadioButton; |
|
26 | import javax.swing.border.EmptyBorder; |
|
27 | ||
28 | /** |
|
29 | * RadioButtonPanel is a simple extension of ButtonPanel. |
|
30 | * Differences are that it uses radio buttons and the |
|
31 | * default alignment is vertical. The radio buttons are |
|
32 | * placed in a ButtonGroup and the panel defaults to having |
|
33 | * no buttons selected. |
|
34 | * |
|
35 | * @author michael@mpowers.net |
|
36 | * @author $Author: cgruber $ |
|
37 | * @version $Revision: 904 $ |
|
38 | * $Date: 2006-02-18 23:19:05 +0000 (Sat, 18 Feb 2006) $ |
|
39 | */ |
|
40 | public class RadioButtonPanel extends ButtonPanel |
|
41 | { |
|
42 | /** |
|
43 | * A ButtonGroup to help manage button state. |
|
44 | */ |
|
45 | protected ButtonGroup buttonGroup; |
|
46 | ||
47 | /** |
|
48 | * ButtonGroup does not make it easy to unselect all buttons. |
|
49 | * The preferred way to do it is actually to create a hidden button. |
|
50 | */ |
|
51 | protected JRadioButton hiddenButton; |
|
52 | ||
53 | /** |
|
54 | * Constructs a RadioButtonPanel. Three buttons are created |
|
55 | * so the panel is filled when used in a GUI-builder environment. |
|
56 | */ |
|
57 | public RadioButtonPanel() |
|
58 | { |
|
59 | 0 | super(); |
60 | 0 | } |
61 | ||
62 | /** |
|
63 | * Constructs a ButtonPanel using specified buttons. |
|
64 | * @param buttonList An array containing the strings to be used in labeling the buttons. |
|
65 | */ |
|
66 | public RadioButtonPanel( String[] buttonList ) |
|
67 | { |
|
68 | 0 | super( buttonList ); |
69 | 0 | } |
70 | ||
71 | /** |
|
72 | * Overridden to set vertical-center alignment and 2-pixel vgap. |
|
73 | */ |
|
74 | protected void initLayout() |
|
75 | { |
|
76 | 0 | super.initLayout(); |
77 | 0 | buttonPanelLayout.setAlignment( BetterFlowLayout.CENTER_VERTICAL ); |
78 | 0 | buttonPanelLayout.setVgap( 2 ); // looks nicer than java l&f recommendation (imho) |
79 | 0 | } |
80 | ||
81 | /** |
|
82 | * Overridden to return a JRadioButton. |
|
83 | * @param aLabel The label for the component that will be created. |
|
84 | * @return The newly created component. |
|
85 | */ |
|
86 | protected Component createComponentWithLabel( String aLabel ) |
|
87 | { |
|
88 | 0 | String buttonLabel = aLabel; |
89 | 0 | JRadioButton newButton = new JRadioButton(); |
90 | 0 | newButton.setName( aLabel ); |
91 | 0 | newButton.setText( buttonLabel ); |
92 | 0 | newButton.setActionCommand( aLabel ); |
93 | 0 | newButton.addActionListener( this ); |
94 | ||
95 | // reduce insets per java l&f guidelines (was 4 on each side) |
|
96 | 0 | newButton.setBorder( new EmptyBorder( 0, 4, 0, 4 ) ); |
97 | ||
98 | 0 | if ( buttonGroup == null ) |
99 | { |
|
100 | 0 | buttonGroup = new ButtonGroup(); |
101 | ||
102 | // cheesy hack to allow a buttongroup to have no items selected. |
|
103 | // note that the button is not added to container or buttonList. |
|
104 | 0 | hiddenButton = new JRadioButton( "Hidden Button" ); |
105 | 0 | buttonGroup.add( hiddenButton ); |
106 | } |
|
107 | 0 | buttonGroup.add( newButton ); |
108 | ||
109 | 0 | return newButton; |
110 | } |
|
111 | ||
112 | /** |
|
113 | * Selects the button whose name matches the given text value. |
|
114 | * @param newText A String matching the name of one of the buttons. |
|
115 | * If null, empty, or not matching, all buttons are deselected. |
|
116 | */ |
|
117 | public void setValue(String aName) |
|
118 | { |
|
119 | 0 | if ( aName != null ) |
120 | { |
|
121 | 0 | Component c = null; |
122 | 0 | int count = buttonContainer.getComponentCount(); |
123 | 0 | for ( int i = 0; i < count; i++ ) |
124 | { |
|
125 | 0 | c = buttonContainer.getComponent( i ); |
126 | 0 | if ( c instanceof AbstractButton ) |
127 | { |
|
128 | 0 | if ( c.getName().equals( aName ) ) |
129 | { |
|
130 | 0 | ((AbstractButton)c).setSelected( true ); |
131 | 0 | return; |
132 | } |
|
133 | } |
|
134 | } |
|
135 | } |
|
136 | ||
137 | // null, empty, or not matching - deselect all |
|
138 | 0 | hiddenButton.setSelected( true ); |
139 | 0 | } |
140 | ||
141 | /** |
|
142 | * Gets the name of the currently selected button. |
|
143 | * @return A string matching the name of the currently selected button, |
|
144 | * or null of no button is selected. |
|
145 | */ |
|
146 | public String getValue() |
|
147 | { |
|
148 | 0 | String result = null; |
149 | 0 | Component c = null; |
150 | 0 | int count = buttonContainer.getComponentCount(); |
151 | 0 | for ( int i = 0; i < count; i++ ) |
152 | { |
|
153 | 0 | c = buttonContainer.getComponent( i ); |
154 | 0 | if ( ( c instanceof AbstractButton ) && ( ((AbstractButton)c).isSelected() ) ) |
155 | { |
|
156 | 0 | return c.getName(); |
157 | } |
|
158 | } |
|
159 | 0 | return result; |
160 | } |
|
161 | ||
162 | /** |
|
163 | * Tests whether the specified value is checked. |
|
164 | * @param aValue A value to be tested. |
|
165 | * @return True if the specified value is checked, otherwise false. |
|
166 | */ |
|
167 | public boolean getValue( String aValue ) |
|
168 | { |
|
169 | 0 | if ( aValue == null ) return false; |
170 | 0 | return aValue.equals( getValue() ); |
171 | } |
|
172 | ||
173 | } |
|
174 |