001: package net.xoetrope.optional.data;
002:
003: import java.awt.Component;
004:
005: import net.xoetrope.optional.data.sql.DatabaseFieldModel;
006: import net.xoetrope.optional.data.sql.DatabaseRowModel;
007: import net.xoetrope.optional.data.sql.DatabaseTableModel;
008: import net.xoetrope.xui.XProjectManager;
009: import net.xoetrope.xui.XTextHolder;
010: import net.xoetrope.xui.data.XModel;
011: import net.xoetrope.xui.data.XTextBinding;
012:
013: /**
014: * A text binding that binds to a database table field. Normally this
015: * object should be setup by the XOptionalBindingFactory
016: * <p>Copyright Xoetrope Ltd. (c) 2003-2004</p>
017: * $Revision: 1.2 $
018: */
019: public class XTextTableBinding extends XTextBinding {
020: private int fieldIdx;
021:
022: public XTextTableBinding() {
023: super ();
024: fieldIdx = 0;
025: }
026:
027: /**
028: * Construct a new data binding
029: * @param c the component to be bound
030: * @param dataElement the name of the data in the model
031: */
032: public XTextTableBinding(Component c, String dataElement) {
033: this (c, dataElement, null, null);
034: }
035:
036: /**
037: * Construct a new data binding
038: * @param c the component to be bound
039: * @param dataElement the name of the data in the model
040: * @param srcModel the source model node
041: */
042: public XTextTableBinding(Component c, String dataElement,
043: XModel srcModel) {
044: this (c, dataElement, srcModel, null);
045: }
046:
047: /**
048: * Construct a new data binding
049: * @param c the component to be bound
050: * @param dataElement the name of the data in the model
051: * @param srcModel the source model node
052: * @param attrib display an attribute of the source node if non null
053: */
054: public XTextTableBinding(Component c, String dataElement,
055: XModel srcModel, String attrib) {
056: srcPath = dataElement;
057: outputPath = XModel.prefixOutputPath(srcPath);
058: if ((srcModel == null) && (srcPath != null))
059: srcModel = (XModel) XProjectManager.getModel().get(srcPath);
060:
061: setSource(srcModel);
062:
063: comp = c;
064: attribStr = attrib;
065: }
066:
067: /**
068: * Updates the TextComponent with the value obtained from the data model.
069: */
070: public void get() {
071: if (sourceModel != null) {
072: if (sourceModel instanceof DatabaseTableModel) {
073: String value = ((DatabaseTableModel) sourceModel)
074: .getAttribValueAsString(fieldIdx);
075: if (value != null)
076: ((XTextHolder) comp).setText(value);
077: else
078: ((XTextHolder) comp).setText("");
079: } else
080: super .get();
081: }
082: }
083:
084: /**
085: * Updates the data model with the value retrieved from the TextComponent.
086: */
087: public void set() {
088: // if ( attribStr == null ) {
089: // sourceModel.set( ( ( XTextHolder )comp ).getText() );
090: // if ( outputModel != null )
091: // outputModel.set( ( ( XTextHolder )comp ).getText() );
092: // }
093: }
094:
095: /**
096: * Set the source node for data in the model
097: * @param newNode the path of the data in the model
098: */
099: public void setSource(XModel newNode) {
100: if (newNode instanceof DatabaseFieldModel) {
101: fieldIdx = ((DatabaseFieldModel) newNode).getFieldIndex();
102: newNode = ((DatabaseFieldModel) newNode).getTable();
103: } else if (newNode instanceof DatabaseRowModel)
104: newNode = ((DatabaseRowModel) newNode).getTable();
105:
106: sourceModel = newNode;
107: }
108: }
|