001: package net.xoetrope.awt;
002:
003: import java.awt.Checkbox;
004: import java.awt.CheckboxGroup;
005:
006: import net.xoetrope.xui.XAttributedComponent;
007: import net.xoetrope.xui.XRadioButtonGroup;
008: import net.xoetrope.xui.XRadioHolder;
009: import net.xoetrope.xui.XStateHolder;
010: import net.xoetrope.xui.XTextHolder;
011: import net.xoetrope.xui.XValueHolder;
012:
013: /**
014: * A wrapper for the Checkbox class
015: * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
016: * License: see license.txt
017: * @version $Revision: 1.19 $
018: */
019: public class XRadioButton extends Checkbox implements XStateHolder,
020: XTextHolder, XValueHolder, XRadioHolder, XAttributedComponent,
021: XRadioButtonGroup {
022: Object value;
023:
024: /**
025: * Constructs a blank radio button
026: */
027: public XRadioButton() {
028: }
029:
030: /**
031: * Sets the text of the button
032: * @param s the new caption
033: */
034: public void setText(String s) {
035: setLabel(s);
036: }
037:
038: /**
039: * Gets the text/caption
040: * @return the text value
041: */
042: public String getText() {
043: return getLabel();
044: }
045:
046: /**
047: * Create a new checkbox group and adds this radio button top the new group
048: * @return the new group
049: */
050: public Object createGroup() {
051: CheckboxGroup cbGroup = new CheckboxGroup();
052: super .setCheckboxGroup(cbGroup);
053: return cbGroup;
054: }
055:
056: /**
057: * Set the button group
058: * @param grp the group control this radio button
059: */
060: public void setRadioButtonGroup(Object grp) {
061: super .setCheckboxGroup((CheckboxGroup) grp);
062: }
063:
064: /**
065: * Gets the group controlling this radio button
066: * @return the ButtonGroup
067: */
068: public Object getRadioButtonGroup() {
069: return getCheckboxGroup();
070: }
071:
072: /**
073: * Gets the selected radio button
074: * @return the selected radio button
075: */
076: public Object getSelectedObject() {
077: try {
078: return getCheckboxGroup().getSelectedCheckbox();
079: } catch (Exception ex) {
080: }
081:
082: return null;
083: }
084:
085: /**
086: * Select an item
087: * @param i the index of the item to select
088: */
089: public void setSelectedObject(Object object) {
090: getCheckboxGroup().setSelectedCheckbox((Checkbox) object);
091: }
092:
093: /**
094: * Selects this radio button
095: */
096: public void setSelected() {
097: setState(true);
098: getCheckboxGroup().setSelectedCheckbox(this );
099: }
100:
101: /**
102: * Adds the checkbox to the same group as this component.
103: * @param cb the checkbox/radiobutton to be added to the group
104: */
105: public void associate(Object cb) {
106: ((XRadioButton) cb).setRadioButtonGroup(getCheckboxGroup());
107: }
108:
109: /**
110: * Set one or more attributes of the component. Currently this handles the
111: * attributes
112: * <OL>
113: * <LI>selected, value=true|false</LI>
114: * </OL>
115: * @param attribName the attribute name
116: * @param attribValue the attribute value
117: */
118: public void setAttribute(String attribName, String attribValue) {
119: if (attribName.compareTo("selected") == 0) {
120: CheckboxGroup cbGroup = getCheckboxGroup();
121: if (cbGroup == null)
122: cbGroup = (CheckboxGroup) createGroup();
123: if (attribValue.compareTo("true") == 0) {
124: setState(true);
125: cbGroup.setSelectedCheckbox(this );
126: }
127: }
128: }
129:
130: /**
131: * Get the component state
132: * @param the new component state
133: */
134: public Object getComponentState() {
135: return new Boolean(getState());
136: }
137:
138: /**
139: * Set the component state
140: * @return the object state
141: */
142: public void setComponentState(Object o) {
143: if (o != null) {
144: String objValue = o.toString();
145: boolean value = objValue.equals("1");
146: if (!value)
147: value |= objValue.equals("true");
148: setState(value);
149: }
150: }
151:
152: /**
153: * Get the radiobutton's value if it has one or else get the text
154: * @return the value for this button
155: */
156: public Object getValue() {
157: if (value != null)
158: return value;
159:
160: return getText();
161: }
162:
163: /**
164: * Set the value associated with this button
165: * @param newValue the new button value
166: */
167: public void setValue(Object newValue) {
168: value = newValue;
169: }
170: }
|