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.XRadioHolder;
008: import net.xoetrope.xui.XValueHolder;
009: import net.xoetrope.xml.XmlElement;
010:
011: /**
012: * <p>Bind a TextComponent to a data model value/node. The binding allows a model node to
013: * linked to a UI component so that it can be refreshed when new data is written
014: * to the model or conversely when the UI component needs to write data to the
015: * model.<br>This binding is designed to be used by components such
016: * as TextComponents or TextFields.</p> For a text component the source and
017: * destination area synonymous.
018: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
019: * License: see license.txt
020: * @version $Revision: 1.9 $
021: */
022: public class XRadioBinding implements XDataBinding {
023: protected XRadioHolder radioHolder;
024: protected XModel sourceModel;
025: protected XModel outputModel;
026: protected String srcPath;
027: protected String outputPath;
028:
029: /**
030: * Construct a new data binding
031: * @param c the component to be bound
032: * @param dataElement the name of the data in the model
033: */
034: public XRadioBinding(Component c, String dataElement) {
035: this (c, dataElement, null);
036: }
037:
038: /**
039: * Construct a new data binding
040: * @param c the component to be bound
041: * @param dataElement the name of the data in the model
042: * @param srcModel the model node that acts as the data source for this node
043: */
044: public XRadioBinding(Component c, String dataElement,
045: XModel srcModel) {
046: srcPath = dataElement;
047: outputPath = XModel.prefixOutputPath(srcPath);
048: sourceModel = srcModel;
049: radioHolder = (XRadioHolder) c;
050: }
051:
052: /**
053: * Updates the TextComponent with the value obtained from the data model.
054: */
055: public void get() {
056: if (outputModel != null)
057: radioHolder.setSelectedObject((String) outputModel.get());
058: }
059:
060: /**
061: * Updates the data model with the value retrieved from the TextComponent.
062: */
063: public void set() {
064: outputModel.set(radioHolder.getSelectedObject());
065: }
066:
067: /**
068: * Get the component to which this object binds
069: * @return
070: */
071: public Component getComponent() {
072: return (Component) radioHolder;
073: }
074:
075: /**
076: * Get the model component to which this object binds
077: * @return
078: */
079: public String getSourcePath() {
080: return srcPath;
081: }
082:
083: /**
084: * Get the model component to which this object binds ins output/state
085: * @return
086: */
087: public String getOutputPath() {
088: return outputPath;
089: }
090:
091: /**
092: * Set the source node for data in the model
093: * @param newNode the path of the data in the model
094: */
095: public void setSource(XModel newNode) {
096: sourceModel = newNode;
097: }
098:
099: /**
100: * Set the path to the output/state node
101: * @param newPath the name of the path in the model
102: */
103: public void setOutput(XModel newNode) {
104: outputModel = newNode;
105: }
106:
107: /**
108: * Set the model path for the source data
109: */
110: public void setSourcePath(String newPath) {
111: srcPath = newPath;
112: }
113:
114: /**
115: * Set the model path for the output/state data
116: */
117: public void setOutputPath(String newPath) {
118: outputPath = XModel.prefixOutputPath(newPath);
119: }
120:
121: /**
122: * Gets the name of the model node
123: * @return the name
124: */
125: public String getName() {
126: return sourceModel
127: .getAttribValueAsString(XBaseModel.ID_ATTRIBUTE);
128: }
129: }
|