1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 super();
60 }
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 super( buttonList );
69 }
70
71 /***
72 * Overridden to set vertical-center alignment and 2-pixel vgap.
73 */
74 protected void initLayout()
75 {
76 super.initLayout();
77 buttonPanelLayout.setAlignment( BetterFlowLayout.CENTER_VERTICAL );
78 buttonPanelLayout.setVgap( 2 );
79 }
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 String buttonLabel = aLabel;
89 JRadioButton newButton = new JRadioButton();
90 newButton.setName( aLabel );
91 newButton.setText( buttonLabel );
92 newButton.setActionCommand( aLabel );
93 newButton.addActionListener( this );
94
95
96 newButton.setBorder( new EmptyBorder( 0, 4, 0, 4 ) );
97
98 if ( buttonGroup == null )
99 {
100 buttonGroup = new ButtonGroup();
101
102
103
104 hiddenButton = new JRadioButton( "Hidden Button" );
105 buttonGroup.add( hiddenButton );
106 }
107 buttonGroup.add( newButton );
108
109 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 if ( aName != null )
120 {
121 Component c = null;
122 int count = buttonContainer.getComponentCount();
123 for ( int i = 0; i < count; i++ )
124 {
125 c = buttonContainer.getComponent( i );
126 if ( c instanceof AbstractButton )
127 {
128 if ( c.getName().equals( aName ) )
129 {
130 ((AbstractButton)c).setSelected( true );
131 return;
132 }
133 }
134 }
135 }
136
137
138 hiddenButton.setSelected( true );
139 }
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 String result = null;
149 Component c = null;
150 int count = buttonContainer.getComponentCount();
151 for ( int i = 0; i < count; i++ )
152 {
153 c = buttonContainer.getComponent( i );
154 if ( ( c instanceof AbstractButton ) && ( ((AbstractButton)c).isSelected() ) )
155 {
156 return c.getName();
157 }
158 }
159 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 if ( aValue == null ) return false;
170 return aValue.equals( getValue() );
171 }
172
173 }
174