/* 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 java.util.List; import java.util.Vector; import javax.swing.AbstractButton; import javax.swing.JCheckBox; import javax.swing.border.EmptyBorder; /** * CheckButtonPanel is a simple extension of ButtonPanel. Differences are that * it uses JCheckBoxes and the default alignment is vertical. The panel defaults * to having no buttons selected. * * @author michael@mpowers.net * @author $Author: cgruber $ * @version $Revision: 904 $ */ public class CheckButtonPanel extends ButtonPanel { /** * Constructs a CheckButtonPanel. Three buttons are created so the panel is * filled when used in a GUI-builder environment. */ public CheckButtonPanel() { super(); } /** * Constructs a ButtonPanel using specified buttons. * * @param buttonList An array containing the strings to be used in labeling the * buttons. */ public CheckButtonPanel(String[] buttonList) { super(buttonList); } /** * Overridden to set vertical-center alignment and zero vgap. */ protected void initLayout() { super.initLayout(); buttonPanelLayout.setAlignment(BetterFlowLayout.CENTER_VERTICAL); buttonPanelLayout.setVgap(0); } /** * 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; JCheckBox newButton = new JCheckBox(); 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(1, 4, 1, 4)); return newButton; } /** * Sets the value of the button whose name matches the given text value. * * @param aName A String matching the name of one of the buttons. If null, * empty, or not matching, nothing happens. * @param aValue A value to set the button. */ public void setValue(String aName, boolean aValue) { 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(aValue); c.repaint(); return; } } } } // null, empty, or not matching - exit. System.out.println("CheckButtonPanel.setValue: not found: " + aName); } /** * Sets the state of the specified buttons to the specified value. * * @param aLabelArray An Array of Strings listing the buttons to be set. * @param aValue The value to which the specified buttons will be set. */ public void setValues(String[] aLabelArray, boolean aValue) { if (aLabelArray != null) { for (int i = 0; i < aLabelArray.length; i++) { setValue(aLabelArray[i], aValue); } } } /** * Convenience method to set all checkboxes on the panel. * * @param aValue The value to which all checkboxes on the panel will be set. */ public void setAllValues(boolean aValue) { setValues(getLabels(), aValue); } /** * Convenience method to check all boxes on the panel. */ public void checkAll() { setAllValues(true); } /** * Convenience method to clear all boxes on the panel. */ public void clearAll() { setAllValues(false); } /** * A convenience method to set only those buttons on the entire panel that * should be checked. Buttons not in the list are unchecked. * * @param aLabelArray An Array of Strings listing the buttons to be set. */ public void setCheckedValues(String[] aLabelArray) { setAllValues(false); setValues(aLabelArray, true); } /** * Gets the labels of all checkboxes that are checked. * * @return A List of Strings containing the labels of the boxes that are * checked. */ public List getCheckedValueList() { Vector v = new Vector(); Component c = null; int count = buttonContainer.getComponentCount(); for (int i = 0; i < count; i++) { c = buttonContainer.getComponent(i); if (c instanceof AbstractButton) { if (((AbstractButton) c).isSelected()) { v.addElement(c.getName()); } } } return v; } /** * Gets the labels of all checkboxes that are checked. * * @return A String Array containing the labels of the boxes that are checked. */ public String[] getCheckedValues() { List v = getCheckedValueList(); String[] result = new String[v.size()]; for (int i = 0; i < v.size(); i++) { result[i] = (String) v.get(i); } return result; } /** * Gets the value of the specified button. * * @param aName A String matching the name of one of the buttons. * @return True if the button is checked, False if it is not checked. NOTE: If * the button is not found in the list, False is returned. */ public boolean getValue(String aName) { 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())) { if (((AbstractButton) c).getText().equals(aName)) { return ((AbstractButton) c).isSelected(); } } } return false; } // for testing public static void main(String[] argv) { try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch (Exception exc) { } javax.swing.JFrame dialog = new javax.swing.JFrame(); java.awt.BorderLayout bl = new java.awt.BorderLayout(20, 20); CheckButtonPanel panel = new CheckButtonPanel(new String[] { "One", "Two", "Three" }); dialog.getContentPane().setLayout(bl); dialog.getContentPane().add(panel, java.awt.BorderLayout.CENTER); dialog.setLocation(50, 50); // dialog.setSize( 450, 150 ); panel.setAlignment(BetterFlowLayout.CENTER_VERTICAL); panel.getButton("One").setEnabled(false); panel.setValues(new String[] { "One" }, true); panel.setValue("Three", true); // panel.setCheckedValues( new String[] { "Two" } ); String[] values = panel.getCheckedValues(); for (int i = 0; i < values.length; i++) { System.out.println(values[i]); } dialog.pack(); dialog.setVisible(true); } }