001: package net.xoetrope.optional.data;
002:
003: import java.awt.Component;
004:
005: import net.xoetrope.optional.data.sql.DatabaseTableModel;
006: import net.xoetrope.xui.XModelHolder;
007: import net.xoetrope.xui.XProjectManager;
008: import net.xoetrope.xui.data.XDataBinding;
009: import net.xoetrope.xui.data.XModel;
010: import net.xoetrope.xui.data.XRowSelector;
011:
012: /**
013: * A binding that binds a table component to a database table. Normally this
014: * object should be setup by the XOptionalBindingFactory
015: * <p>Copyright Xoetrope(c) 2003-2004</p>
016: * $Revision: 1.2 $
017: */
018: public class XTableTableBinding implements XDataBinding {
019: protected XModelHolder target;
020: protected boolean useUnique;
021: protected XModel outputNode;
022: protected XModel sourceNode;
023: protected String outputPath;
024: protected String sourcePath;
025:
026: public XTableTableBinding(Object comp, XModel xmodel) {
027: target = (XModelHolder) comp;
028: target.setModel(xmodel);
029: sourceNode = xmodel;
030: }
031:
032: /**
033: * Updates the table component with the value obtained from the data model.
034: * This method does nothing as the table should dynamically pull its data from
035: * the model. Any change in the model is effected by the setSore or
036: * setSourcePath methods.
037: */
038: public void get() {
039: }
040:
041: /**
042: * Updates the data model with the value retrieved from the table component.
043: */
044: public void set() {
045: int selIdx = ((XRowSelector) target).getSelectedRow();
046: if (selIdx >= 0) {
047: Object result = ((DatabaseTableModel) sourceNode)
048: .getFieldValue(selIdx, 0);
049: if (result != null)
050: outputNode.set(result);
051: }
052: }
053:
054: /**
055: * Get the UI component that participates in this binding
056: * @return the component
057: */
058: public Component getComponent() {
059: return (Component) target;
060: }
061:
062: /**
063: * Get the model path for the source data
064: */
065: public String getSourcePath() {
066: return sourcePath;
067: }
068:
069: /**
070: * Get the model path for the output/state data
071: */
072: public String getOutputPath() {
073: return outputPath;
074: }
075:
076: /**
077: * Set the model path for the source data
078: */
079: public void setSourcePath(String newPath) {
080: if (newPath != null) {
081: sourceNode = (XModel) XProjectManager.getModel().get(
082: newPath);
083: sourcePath = newPath;
084: target.setModel(sourceNode);
085: }
086: }
087:
088: /**
089: * Set the model path for the output/state data
090: */
091: public void setOutputPath(String newPath) {
092: if (newPath != null) {
093: outputPath = XModel.prefixOutputPath(newPath);
094: outputNode = (XModel) XProjectManager.getModel().get(
095: outputPath);
096: }
097: }
098:
099: /**
100: * Update the model node used in the binding. Note that this method does not
101: * modify the path values stored by this node.
102: * @param newNode the new model for the data source
103: */
104: public void setSource(XModel newNode) {
105: sourceNode = newNode;
106: target.setModel(sourceNode);
107: }
108:
109: /**
110: * Update the path values stored by this node. The output path is used to
111: * store selection data and state.
112: * @param newNode the new model for saving the output data
113: */
114: public void setOutput(XModel newModel) {
115: outputNode = newModel;
116: }
117: }
|