/* 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()); } }