001: package net.xoetrope.xui.data;
002:
003: /**
004: * <p>Adapts an XModel to provide access to child nodes as a list in a way that
005: * is more convenient for use with UI components and so that some state
006: * information can be maintained.</p>
007: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
008: * License: see license.txt
009: * @version $Revision: 1.19 $
010: */
011: public class XListModelAdapter implements XModelAdapter {
012: protected XModel model;
013: protected int columnIdx = -1;
014:
015: /**
016: * Constructs a new adapter for the specified model node.
017: * @param the model node to adapt
018: */
019: public XListModelAdapter(XModel src) {
020: model = src;
021: }
022:
023: /**
024: * Constructs a new adapter.
025: */
026: public XListModelAdapter() {
027: }
028:
029: /**
030: * Set the index of the column to use when getting values from this adapter
031: * @param colIdx the (sero based) column index.
032: */
033: public void setKeyColumn(int colIdx) {
034: columnIdx = colIdx;
035: }
036:
037: /**
038: * Get the number of children belong to the model node that this object adapts
039: * @return the number of children
040: */
041: public int getNumChildren() {
042: return model.getNumChildren();
043: }
044:
045: /**
046: * Gets the individual list item value
047: * @param i The index of the listitem
048: * @return The value of the listitem
049: */
050: public Object get(int i) {
051: if (columnIdx >= 0)
052: return model.get(i).get(columnIdx);
053: else
054: return model.get(i).get();
055: }
056:
057: /**
058: * Set the value of the listitem
059: * @param o The new value
060: */
061: public void set(Object o) {
062: model.set(o);
063: }
064:
065: /**
066: * Gets the value of the selected item from the list.
067: * @return the selected list item/node
068: */
069: public Object getSelected() {
070: return model.get();
071: }
072:
073: /**
074: * Set the adapter source
075: * @param src the model
076: */
077: public void setModel(XModel src) {
078: model = src;
079: }
080:
081: /**
082: * Get the model being used by this adapter
083: * @return
084: */
085: public XModel getModel() {
086: return model;
087: }
088:
089: /**
090: * Gets the name of the model node
091: * @return the name
092: */
093: public String getTagName() {
094: return model.getAttribValueAsString(XBaseModel.ID_ATTRIBUTE);
095: }
096:
097: /**
098: * Locate a key value in the underlying data source
099: * @param key the key to locate
100: * @param columnIdx the index of the key column
101: * @return the row/record index taht contains the first instance of the key,
102: * or -1 if the key is not found
103: */
104: public int find(String key, int columnIdx) {
105: int numChildren = model.getNumChildren();
106: for (int i = 0; i < numChildren; i++) {
107: String s = model.get(i).get(columnIdx).toString();
108: if (s.equals(key))
109: return i;
110: }
111: return -1;
112: }
113: }
|