001: package net.xoetrope.xui.data;
002:
003: import java.awt.Component;
004:
005: import net.xoetrope.xml.XmlElement;
006: import net.xoetrope.xui.XProjectManager;
007: import net.xoetrope.xui.XTextHolder;
008:
009: /**
010: * <p>Bind a TextComponent to a data model value/node. The binding allows a model node to
011: * linked to a UI component so that it can be refreshed when new data is written
012: * to the model or conversely when the UI component needs to write data to the
013: * model.<br>This binding is designed to be used by components such
014: * as TextComponents or TextFields.</p> For a text component the source and
015: * destination area synonymous.
016: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
017: * License: see license.txt
018: * @version $Revision: 1.25 $
019: */
020: public class XTextBinding implements XDataBinding {
021: protected Component comp;
022: protected XModel sourceModel;
023: protected XModel outputModel;
024: protected String srcPath;
025: protected String outputPath;
026: protected String attribStr;
027:
028: public XTextBinding() {
029: }
030:
031: /**
032: * Construct a new data binding
033: * @param c the component to be bound
034: * @param dataElement the name of the data in the model
035: */
036: public XTextBinding(Component c, String dataElement) {
037: this (c, dataElement, null, null);
038: }
039:
040: /**
041: * Construct a new data binding
042: * @param c the component to be bound
043: * @param dataElement the name of the data in the model
044: * @param srcModel the source model node
045: */
046: public XTextBinding(Component c, String dataElement, XModel srcModel) {
047: this (c, dataElement, srcModel, null);
048: }
049:
050: /**
051: * Construct a new data binding
052: * @param c the component to be bound
053: * @param dataElement the name of the data in the model
054: * @param srcModel the source model node
055: * @param attrib display an attribute of the source node if non null
056: */
057: public XTextBinding(Component c, String dataElement,
058: XModel srcModel, String attrib) {
059: srcPath = dataElement;
060: outputPath = XModel.prefixOutputPath(srcPath);
061: if ((srcModel == null) && (srcPath != null))
062: sourceModel = (XModel) XProjectManager.getModel().get(
063: srcPath);
064: else
065: sourceModel = srcModel;
066: comp = c;
067: attribStr = attrib;
068: }
069:
070: /**
071: * Updates the TextComponent with the value obtained from the data model.
072: */
073: public void get() {
074: if (sourceModel != null) {
075: Object objValue = null;
076: if ((attribStr != null) && (attribStr.length() > 0)) {
077: int attribPos = ((XModel) sourceModel)
078: .getAttribute(attribStr);
079: if (attribPos >= 0)
080: objValue = ((XModel) sourceModel)
081: .getAttribValue(attribPos);
082: } else
083: objValue = sourceModel.get();
084: if (objValue != null)
085: ((XTextHolder) comp).setText(objValue.toString());
086: else
087: ((XTextHolder) comp).setText("");
088: }
089: }
090:
091: /**
092: * Updates the data model with the value retrieved from the TextComponent.
093: */
094: public void set() {
095: if (attribStr == null) {
096: sourceModel.set(((XTextHolder) comp).getText());
097: if (outputModel != null)
098: outputModel.set(((XTextHolder) comp).getText());
099: } else {
100: int attIdx = sourceModel.getAttribute(attribStr);
101: String compValue = ((XTextHolder) comp).getText();
102: sourceModel.setAttribValue(attIdx, compValue);
103: if (outputModel != null) {
104: int attribIdx = outputModel.getAttribute(attribStr);
105: if (attribIdx < 0) {
106: boolean appendState = XBaseModel
107: .getAppendByDefault();
108: XBaseModel.setAppendByDefault(true);
109: attribIdx = outputModel.getAttribute(attribStr);
110: XBaseModel.setAppendByDefault(appendState);
111: }
112: outputModel.setAttribValue(attribIdx, compValue);
113: }
114: }
115: }
116:
117: /**
118: * Get the component to which this object binds
119: * @return
120: */
121: public Component getComponent() {
122: return comp;
123: }
124:
125: /**
126: * Get the model component to which this object binds
127: * @return
128: */
129: public String getSourcePath() {
130: return srcPath;
131: }
132:
133: /**
134: * Get the model component to which this object binds ins output/state
135: * @return
136: */
137: public String getOutputPath() {
138: return outputPath;
139: }
140:
141: /**
142: * Set the source node for data in the model
143: * @param newNode the path of the data in the model
144: */
145: public void setSource(XModel newNode) {
146: sourceModel = newNode;
147: }
148:
149: /**
150: * Set the path to the output/state node
151: * @param newPath the name of the path in the model
152: */
153: public void setOutput(XModel newNode) {
154: outputModel = newNode;
155: }
156:
157: /**
158: * Set the model path for the source data
159: */
160: public void setSourcePath(String newPath) {
161: srcPath = newPath;
162: }
163:
164: /**
165: * Set the model path for the output/state data
166: */
167: public void setOutputPath(String newPath) {
168: outputPath = XModel.prefixOutputPath(newPath);
169: }
170:
171: /**
172: * Gets the name of the model node
173: * @return the name
174: */
175: public String getName() {
176: return sourceModel
177: .getAttribValueAsString(XBaseModel.ID_ATTRIBUTE);
178: }
179: }
|