001: package net.xoetrope.optional.data.sql;
002:
003: import net.xoetrope.xui.data.XModel;
004: import net.xoetrope.xui.data.XModelAdapter;
005:
006: /**
007: * <p>Adapts an XModel to provide access to child nodes as a list.</p>
008: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
009: * License: see license.txt
010: * $Revision: 1.2 $
011: */
012: public class DatabaseTableModelAdapter implements XModelAdapter {
013: private DatabaseTableModel model;
014: private int outputFieldIdx = 0;
015:
016: /**
017: * Create a new adapter for a model node
018: * @param src the node to adapt
019: */
020: public DatabaseTableModelAdapter(XModel src) {
021: model = (DatabaseTableModel) src;
022: }
023:
024: /**
025: * Create a new adapter for a model node
026: */
027: public DatabaseTableModelAdapter() {
028: }
029:
030: /**
031: * Get the number of children that the model node has
032: * @return the number of children
033: */
034: public int getNumChildren() {
035: return model.getNumChildren();
036: }
037:
038: /**
039: * Gets the individual list item value
040: * @param i The index of the listitem
041: * @return The value of the listitem
042: */
043: public Object get(int i) {
044: DatabaseRowModel row = (DatabaseRowModel) model.get(i);
045: return row.getFieldValue(outputFieldIdx);
046: }
047:
048: /**
049: * Gets the individual list item value
050: * @param i The index of the listitem
051: * @param fieldIdx the field index
052: * @return The value of the listitem
053: */
054: public Object get(int i, int fieldIdx) {
055: DatabaseRowModel row = (DatabaseRowModel) model.get(i);
056: return row.getFieldValue(fieldIdx);
057: }
058:
059: /**
060: * Set the value of the listitem
061: * @param o The new value
062: */
063: public void set(Object o) {
064: model.set(o);
065: }
066:
067: /**
068: * Gets the value of the selected item from the list.
069: * @return
070: */
071: public Object getSelected() {
072: return model.get();
073: }
074:
075: /**
076: * Gets the value of the selected item from the list.
077: * @return
078: */
079: public Object getSelected(int fieldIdx) {
080: return model.getFieldValue(fieldIdx);
081: }
082:
083: /**
084: * Set the field to return for lists;
085: * @param fieldIdx
086: */
087: public void setOutputField(int fieldIdx) {
088: outputFieldIdx = fieldIdx;
089: }
090:
091: /**
092: * Set the adapter source
093: */
094: public XModel getModel() {
095: return model;
096: }
097:
098: /**
099: * Set the adapter source
100: * @param src the model
101: */
102: public void setModel(XModel src) {
103: model = (DatabaseTableModel) src;
104: }
105:
106: /**
107: * Locate a key value in the underlying data source
108: * @param key the key to locate
109: * @param keyColumnIdx the index of the key column
110: * @return the row/record index taht contains the first instance of the key,
111: * or -1 if the key is not found
112: */
113: public int find(String key, int keyColumnIdx) {
114: model.sync();
115: int numChildren = model.getNumChildren();
116: for (int i = 0; i < numChildren; i++) {
117: String s = model.get(i).get(keyColumnIdx).toString();
118: if (s.equals(key))
119: return i;
120: }
121: return -1;
122: }
123:
124: /**
125: * Force a sync/update of the table
126: */
127: public void sync() {
128: model.sync();
129: }
130: }
|