001: package net.xoetrope.xui.data;
002:
003: import java.awt.Component;
004:
005: import net.xoetrope.xui.XProjectManager;
006: import net.xoetrope.xui.XListHolder;
007: import net.xoetrope.xui.XStateHolder;
008: import net.xoetrope.xml.XmlElement;
009:
010: /**
011: * <p>Bind a component's state to a data model value/node. The binding allows a
012: * model node to linked to a UI component so that it can be refreshed when new
013: * data is written to the model or conversely when the UI component needs to
014: * write data to the model.
015: * <br>This binding is designed to be used by components such
016: * as RadioButtons or Checkboxes.</p> This state change does not affect the
017: * content displayed by the component.
018: * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
019: * License: see license.txt
020: * @version $Revision: 1.11 $
021: */
022: public class XStateBinding implements XDataBinding {
023: protected Component comp;
024: protected XModel sourceModel;
025: protected XModel outputModel;
026: protected String srcPath;
027: protected String outputPath;
028: protected boolean isLeafNode;
029:
030: /**
031: * Construct a new data binding
032: * @param c the component to be bound
033: * @param dataElement the name of the data in the model
034: */
035: public XStateBinding(Component c, String dataElement) {
036: srcPath = dataElement;
037: outputPath = XModel.prefixOutputPath(srcPath);
038: if (dataElement != null)
039: sourceModel = (XModel) XProjectManager.getModel().get(
040: dataElement);
041: comp = c;
042: isLeafNode = true;
043: }
044:
045: /**
046: * Construct a new data binding
047: * @param c the component to be bound
048: * @param dataElement the name of the data in the model
049: * @param srcModel the model node that acts as the data source for this node
050: * @param leafAttrib null or true for a leaf node where the value controls the state,
051: * or false to use the child values to control not only the state but also the enabled/disabled state
052: */
053: public XStateBinding(Component c, String dataElement,
054: XModel srcModel, String leafAttrib) {
055: srcPath = dataElement;
056: outputPath = XModel.prefixOutputPath(srcPath);
057: sourceModel = srcModel;
058: comp = c;
059: if ((leafAttrib != null) && leafAttrib.equals("false"))
060: isLeafNode = false;
061: else
062: isLeafNode = true;
063: }
064:
065: /**
066: * Updates the component state with the value obtained from the data model.
067: */
068: public void get() {
069: if ((outputModel != null) || (sourceModel != null)) {
070: if (isLeafNode) {
071: if ((outputModel != null))
072: ((XStateHolder) comp).setComponentState(outputModel
073: .get());
074: else
075: ((XStateHolder) comp).setComponentState(sourceModel
076: .get());
077: } else {
078: XModel srcNode = sourceModel.get(0);
079: boolean multipleChildren = (sourceModel
080: .getNumChildren() > 1);
081: comp.setEnabled(multipleChildren);
082: if (!multipleChildren)
083: ((XStateHolder) comp).setComponentState(srcNode
084: .get());
085: else
086: ((XStateHolder) comp).setComponentState(outputModel
087: .get());
088: }
089: }
090: }
091:
092: /**
093: * Updates the data model with the value retrieved from the component.
094: */
095: public void set() {
096: Object state = ((XStateHolder) comp).getComponentState();
097: if (state instanceof Boolean)
098: outputModel.set(((Boolean) state).booleanValue() ? "1"
099: : "0");
100: else
101: outputModel.set(state);
102: }
103:
104: /**
105: * Get the component to which this object binds
106: * @return
107: */
108: public Component getComponent() {
109: return comp;
110: }
111:
112: /**
113: * Get the model component to which this object binds
114: * @return
115: */
116: public String getSourcePath() {
117: return srcPath;
118: }
119:
120: /**
121: * Get the model component to which this object binds ins output/state
122: * @return
123: */
124: public String getOutputPath() {
125: return outputPath;
126: }
127:
128: /**
129: * Set the source node for data in the model
130: * @param newNode the path of the data in the model
131: */
132: public void setSource(XModel newNode) {
133: sourceModel = newNode;
134: if (outputModel.get() == null)
135: outputModel.set(sourceModel.get());
136: }
137:
138: /**
139: * Set the path to the output/state node
140: * @param newPath the name of the path in the model
141: */
142: public void setOutput(XModel newNode) {
143: outputModel = newNode;
144: }
145:
146: /**
147: * Set the model path for the source data
148: */
149: public void setSourcePath(String newPath) {
150: srcPath = newPath;
151: }
152:
153: /**
154: * Set the model path for the output/state data
155: */
156: public void setOutputPath(String newPath) {
157: outputPath = XModel.prefixOutputPath(newPath);
158: }
159:
160: /**
161: * Gets the name of the model node
162: * @return the name
163: */
164: public String getName() {
165: return sourceModel
166: .getAttribValueAsString(XBaseModel.ID_ATTRIBUTE);
167: }
168: }
|