001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: import java.awt.*;
008: import java.util.*;
009: import java.io.*;
010:
011: import javax.swing.*;
012:
013: /**
014: * SButtonGroup defines a set of mutually exclusive set of buttons. Selecting one
015: * button will deselect the other buttons.
016: * <B>
017: * All the buttons are initially unselected.
018: *
019: * @author Robin Sharp
020: */
021:
022: public class SButtonGroup extends SContainer {
023: /**
024: * Create a SButtonGroup.
025: */
026: public SButtonGroup() {
027: }
028:
029: /**
030: * Returns the name of the L&F class that renders this component.
031: */
032: public Class getUIClass() {
033: return SButtonGroup.class;
034: }
035:
036: /**
037: * Adds the button to the group. <br>
038: * This will setName on the RadioButton to getName().
039: */
040: public void add(SRadioButton button) {
041: if (button == null) {
042: return;
043: }
044:
045: button.setGroup(this );
046: super .add(button);
047: }
048:
049: /**
050: * Removes the button from the group. <br>
051: * If this is the selected button then it is removed as the selected button.<br>
052: * This will setName on the RadioButton to null.
053: */
054: public void remove(SRadioButton button) {
055: if (button == null) {
056: return;
057: }
058:
059: if (isSelected(button)) {
060: setSelected(button, false);
061: }
062:
063: button.setGroup(null);
064: super .remove(button);
065:
066: }
067:
068: /**
069: * Return all the buttons that are participating in this group.
070: */
071: public Enumeration getElements() {
072: return getAllComponents();
073: }
074:
075: /**
076: * Get the selected index, or -1 if no item is selected
077: */
078: public int getSelectedIndex() {
079: for (int index = 0; index < getComponentCount(); index++) {
080: if (((SRadioButton) getComponent(index)).isSelected()) {
081: return index;
082: }
083: }
084:
085: return -1;
086: }
087:
088: /**
089: * Sets the selected value for the button.
090: */
091: public SButtonGroup setSelectedIndex(int index) {
092: return setSelected(index, true);
093: }
094:
095: /**
096: * Get the selected Radio Button, or null if one is not
097: * selected.
098: */
099: public SRadioButton getSelectedItem() {
100: for (int index = 0; index < getComponentCount(); index++) {
101: if (((SRadioButton) getComponent(index)).isSelected()) {
102: return (SRadioButton) getComponent(index);
103: }
104: }
105:
106: return null;
107: }
108:
109: /**
110: * Sets the selected value for the button.
111: */
112: public SButtonGroup setSelectedItem(SRadioButton button) {
113: return setSelected(button, true);
114: }
115:
116: /**
117: * Sets the selected value for the button.
118: */
119: public SButtonGroup setSelected(int index, boolean selected) {
120: setSelected((SRadioButton) getComponent(index), selected);
121: return this ;
122: }
123:
124: /**
125: * Sets the selected value for the button.
126: */
127: public SButtonGroup setSelected(SRadioButton button,
128: boolean selected) {
129: if (getComponentIndex(button) < 0) {
130: throw new IllegalArgumentException("Button "
131: + button.getText() + " is not in the group.");
132: }
133:
134: //Do nothing
135: if (selected && button.isSelected())
136: return this ;
137: else if (!selected && button.isSelected())
138: return this ;
139:
140: //Switch all the other buttons off
141: for (int index = 0; index < getComponentCount(); index++) {
142: if (getComponent(index) != button) {
143: ((SRadioButton) getComponent(index)).setSelected(false);
144: }
145: }
146:
147: button.setSelected(selected);
148:
149: return this ;
150: }
151:
152: /**
153: * Returns the selected value for the button.
154: */
155: public boolean isSelected(SRadioButton button) {
156: return button.isSelected();
157: }
158:
159: /**
160: * Set the value as Text. It is assumes that the text is the index.
161: */
162: public SComponent setValueAsText(String text) {
163: try {
164: setSelected(((SRadioButton) getComponent(Integer
165: .parseInt(text))), true);
166: } catch (Exception e) {
167: //Fail silently
168: }
169:
170: return this;
171: }
172:
173: }
|