01: package net.xoetrope.swing;
02:
03: import javax.swing.JCheckBox;
04:
05: import net.xoetrope.xui.XAttributedComponent;
06: import net.xoetrope.xui.XStateHolder;
07: import net.xoetrope.xui.XTextHolder;
08: import net.xoetrope.xui.XValueHolder;
09:
10: /**
11: * A wrapper for the Swing JCheckbox class
12: * <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
13: * License: see license.txt
14: * @version 1.0
15: */
16: public class XCheckbox extends JCheckBox implements XStateHolder,
17: XTextHolder, XValueHolder, XAttributedComponent {
18: protected Object value;
19:
20: /**
21: * Construct a new checkbox
22: */
23: public XCheckbox() {
24: }
25:
26: /**
27: * Set the checkbox text
28: * @param s the new text
29: */
30: public void setText(String s) {
31: // This method is required by the XTextHolder interface.
32: super .setText(s);
33: }
34:
35: /**
36: * Set one or more attributes of the component. Currently this handles the
37: * attributes
38: * <OL>
39: * <LI>selected, value=true|false</LI>
40: * <LI>alignment, values=left|right|center|leading|trailing</LI>
41: * </OL>
42: * @param attribName the attribute name
43: * @param attribValue the attribute value
44: */
45: public void setAttribute(String attribName, String attribValue) {
46: if (attribName.compareTo("selected") == 0)
47: setSelected(attribValue.compareTo("true") == 0);
48: else if (attribName.toLowerCase().compareTo("alignment") == 0)
49: setHorizontalAlignment(XAlignmentHelper
50: .getAlignmentOption(attribValue));
51: }
52:
53: /**
54: * Get the component state
55: * @param the new component state
56: */
57: public Object getComponentState() {
58: return new Boolean(isSelected());
59: }
60:
61: /**
62: * Set the component state
63: * @return the object state
64: */
65: public void setComponentState(Object o) {
66: if (o != null) {
67: String objValue = o.toString();
68: boolean value = objValue.equals("1");
69: if (!value)
70: value |= objValue.equals("true");
71: setSelected(value);
72: }
73: }
74:
75: /**
76: * Get the checkbox's value if it has one or else get the text
77: * @return the value for this button
78: */
79: public Object getValue() {
80: if (value != null)
81: return value;
82:
83: return getText();
84: }
85:
86: /**
87: * Set the value associated with this button
88: * @param newValue the new button value
89: */
90: public void setValue(Object newValue) {
91: value = newValue;
92: }
93: }
|