GNU Classpath (0.95) | |
Frames | No Frames |
1: /* ButtonGroup.java -- 2: Copyright (C) 2002, 2006, Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package javax.swing; 39: 40: import java.io.Serializable; 41: import java.util.Enumeration; 42: import java.util.Vector; 43: 44: 45: /** 46: * Logically groups a set of buttons, so that only one of the buttons in 47: * a <code>ButtonGroup</code> can be selected at the same time. If one 48: * button in a <code>ButtonGroup</code> is selected, all other buttons 49: * are automatically deselected. 50: * 51: * While <code>ButtonGroup</code> can be used for all buttons that are derived 52: * from {@link AbstractButton}, it is normally only used for 53: * {@link JRadioButton}s, {@link JRadioButtonMenuItem}s and 54: * {@link JToggleButton}s. 55: * 56: * You could use it for {@link JCheckBox}es, but for the sake of usability 57: * this is strongly discouraged because the common expectation of checkboxes 58: * is that the user is allowed to make multiple selections. 59: * 60: * It makes no sense to put {@link JButton}s or {@link JMenuItem}s in 61: * a <code>ButtonGroup</code> because they don't implement the 62: * <code>selected</code> semantics. 63: * 64: * @author original author unknown 65: */ 66: public class ButtonGroup implements Serializable 67: { 68: private static final long serialVersionUID = 4259076101881721375L; 69: 70: /** Stores references to the buttons added to this button group. */ 71: protected Vector<AbstractButton> buttons = new Vector<AbstractButton>(); 72: 73: /** The currently selected button model. */ 74: ButtonModel sel; 75: 76: /** 77: * Creates a new button group. 78: */ 79: public ButtonGroup() 80: { 81: // Nothing to do here. 82: } 83: 84: /** 85: * Adds a button to this group. If the button is in the selected state, then: 86: * <ul> 87: * <li>if the group has no current selection, the new button becomes the 88: * selected button for the group;</li> 89: * <li>if the group already has a selected button, the new button is set to 90: * "not selected".</li> 91: * </ul> 92: * 93: * @param b the button to add (<code>null</code> is ignored). 94: */ 95: public void add(AbstractButton b) 96: { 97: if (b == null) 98: return; 99: b.getModel().setGroup(this); 100: if (b.isSelected()) 101: { 102: if (sel == null) 103: sel = b.getModel(); 104: else 105: b.setSelected(false); 106: } 107: buttons.addElement(b); 108: } 109: 110: /** 111: * Removes the specified button from this group. If the button is the 112: * selected button, the current selection is set to <code>null</code>. 113: * The group for the removed button's model is set to <code>null</code>. 114: * 115: * @param b the button to remove (<code>null</code> is ignored). 116: */ 117: public void remove(AbstractButton b) 118: { 119: if (b == null) 120: return; 121: b.getModel().setGroup(null); 122: if (b.getModel() == sel) 123: sel = null; 124: buttons.removeElement(b); 125: } 126: 127: /** 128: * Returns the currently added buttons. 129: * 130: * @return <code>Enumeration</code> over all added buttons 131: */ 132: public Enumeration<AbstractButton> getElements() 133: { 134: return buttons.elements(); 135: } 136: 137: /** 138: * Returns the currently selected button model. 139: * 140: * @return the currently selected button model, null if none was selected 141: * yet 142: */ 143: public ButtonModel getSelection() 144: { 145: return sel; 146: } 147: 148: /** 149: * Returns the button that has the specified model, or <code>null</code> if 150: * there is no such button in the group. 151: * 152: * @param m the button model. 153: * 154: * @return The button that has the specified model, or <code>null</code>. 155: */ 156: AbstractButton findButton(ButtonModel m) 157: { 158: for (int i = 0; i < buttons.size(); i++) 159: { 160: AbstractButton a = (AbstractButton) buttons.get(i); 161: if (a.getModel() == m) 162: return a; 163: } 164: return null; 165: } 166: 167: /** 168: * Sets the currently selected button model. Only one button of a group can 169: * be selected at a time. 170: * 171: * @param m the model to select 172: * @param b true if this button is to be selected, false otherwise 173: */ 174: public void setSelected(ButtonModel m, boolean b) 175: { 176: if ((sel != m || b) && (! b || sel == m)) 177: return; 178: 179: if (b && sel != m) 180: { 181: ButtonModel old = sel; 182: sel = m; 183: 184: if (old != null) 185: old.setSelected(false); 186: 187: if (m != null) 188: sel.setSelected(true); 189: 190: AbstractButton button = findButton(old); 191: if (button != null) 192: button.repaint(); 193: } 194: else if (!b && sel == m) 195: m.setSelected(true); 196: } 197: 198: /** 199: * Checks if the given <code>ButtonModel</code> is selected in this button 200: * group. 201: * 202: * @param m the button model (<code>null</code> permitted). 203: * 204: * @return <code>true</code> if <code>m</code> is the selected button model 205: * in this group, and <code>false</code> otherwise. 206: */ 207: public boolean isSelected(ButtonModel m) 208: { 209: return m == sel; 210: } 211: 212: /** 213: * Return the number of buttons in this button group. 214: * 215: * @return the number of buttons 216: * 217: * @since 1.3 218: */ 219: public int getButtonCount() 220: { 221: return buttons.size(); 222: } 223: }
GNU Classpath (0.95) |