001: /*
002: * @(#)SplitButtonGroup.java 2/18/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import javax.swing.*;
009:
010: /**
011: * SplitButtonGroup extends ButtonGroup to provide the same button grouping function
012: * for JideToggleSplitButton.
013: * <p/>
014: * SplitButtonGroup supports regular JButton or JideButton as well.
015: */
016: public class SplitButtonGroup extends ButtonGroup {
017: /**
018: * The current selection.
019: */
020: ButtonModel selection = null;
021:
022: /**
023: * Creates a new <code>ButtonGroup</code>.
024: */
025: public SplitButtonGroup() {
026: }
027:
028: /**
029: * Adds the button to the group.
030: *
031: * @param b the button to be added
032: */
033: @Override
034: public void add(AbstractButton b) {
035: if (b == null) {
036: return;
037: }
038: buttons.addElement(b);
039:
040: if (b instanceof JideSplitButton) {
041: if (((JideSplitButton) b).isButtonSelected()) {
042: if (selection == null) {
043: selection = b.getModel();
044: } else {
045: ((JideSplitButton) b).setButtonSelected(false);
046: }
047: }
048: } else {
049: if (b.isSelected()) {
050: if (selection == null) {
051: selection = b.getModel();
052: } else {
053: b.setSelected(false);
054: }
055: }
056: }
057:
058: b.getModel().setGroup(this );
059: }
060:
061: /**
062: * Removes the button from the group.
063: *
064: * @param b the button to be removed
065: */
066: @Override
067: public void remove(AbstractButton b) {
068: if (b == null) {
069: return;
070: }
071: buttons.removeElement(b);
072: if (b.getModel() == selection) {
073: selection = null;
074: }
075: b.getModel().setGroup(null);
076: }
077:
078: /**
079: * Sets the selected value for the <code>ButtonModel</code>.
080: * Only one button in the group may be selected at a time.
081: *
082: * @param m the <code>ButtonModel</code>
083: * @param b <code>true</code> if this button is to be
084: * selected, otherwise <code>false</code>
085: */
086: @Override
087: public void setSelected(ButtonModel m, boolean b) {
088: if (b && m != null && m != selection) {
089: ButtonModel oldSelection = selection;
090: selection = m;
091: if (oldSelection != null) {
092: if (oldSelection instanceof SplitButtonModel)
093: ((SplitButtonModel) oldSelection)
094: .setButtonSelected(false);
095: else
096: oldSelection.setSelected(false);
097: }
098: if (m instanceof SplitButtonModel)
099: ((SplitButtonModel) m).setButtonSelected(true);
100: else
101: m.setSelected(true);
102: }
103: }
104:
105: /**
106: * Returns whether a <code>ButtonModel</code> is selected.
107: *
108: * @return <code>true</code> if the button is selected,
109: * otherwise returns <code>false</code>
110: */
111: @Override
112: public boolean isSelected(ButtonModel m) {
113: return (m == selection);
114: }
115:
116: }
|