01: package net.xoetrope.swing.tree;
02:
03: import javax.swing.tree.DefaultMutableTreeNode;
04:
05: import net.xoetrope.xui.data.XBaseModel;
06: import net.xoetrope.xui.data.XModel;
07: import net.xoetrope.xui.data.XModelAdapter;
08:
09: /**
10: * An adapter to allow the XModel to be used with tree controls
11: * <p>Copyright (c) Xoetrope Ltd., 2001-2004</p>
12: * $Revision: 1.2 $
13: */
14: public class XTreeModelAdapter extends DefaultMutableTreeNode implements
15: XModelAdapter {
16: protected XModel model;
17:
18: /**
19: * Constructs a new adapter for the specified model node.
20: * @param the model node to adapt
21: */
22: public XTreeModelAdapter(XModel src) {
23: super (src.get());
24: model = src;
25:
26: populateTreeModel(src);
27: }
28:
29: /**
30: * Get the number of children belong to the model node that this object adapts
31: * @return the number of children
32: */
33: public int getNumChildren() {
34: return model.getNumChildren();
35: }
36:
37: /**
38: * Gets the individual list item value
39: * @param i The index of the listitem
40: * @return The value of the listitem
41: */
42: public Object get(int i) {
43: return model.get(i).get();
44: }
45:
46: /**
47: * Set the value of the listitem
48: * @param o The new value
49: */
50: public void set(Object o) {
51: model.set(o);
52: }
53:
54: /**
55: * Gets the value of the selected item from the list.
56: * @return the selected list item/node
57: */
58: public Object getSelected() {
59: return model.get();
60: }
61:
62: /**
63: * Set the adapter source
64: * @param src the model
65: */
66: public void setModel(XModel src) {
67: model = src;
68: }
69:
70: /**
71: * Get the model being used by this adapter
72: * @return
73: */
74: public XModel getModel() {
75: return model;
76: }
77:
78: /**
79: * Gets the name of the model node
80: * @return the name
81: */
82: public String getTagName() {
83: return model.getAttribValueAsString(XBaseModel.ID_ATTRIBUTE);
84: }
85:
86: /**
87: * Add the children of this node to the tree model
88: */
89: protected void populateTreeModel(XModel src) {
90: int numChildren = src.getNumChildren();
91: // We only need go one level deep as the ctor will call this method for the
92: // new child nodes
93: for (int i = 0; i < numChildren; i++)
94: add(new XTreeModelAdapter(src.get(i)));
95: }
96: }
|