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