001: package net.xoetrope.swing.tree;
002:
003: import java.util.Vector;
004:
005: import java.awt.Component;
006: import javax.swing.JTree;
007: import javax.swing.tree.DefaultTreeModel;
008: import javax.swing.tree.TreePath;
009:
010: import net.xoetrope.xui.data.XDataBinding;
011: import net.xoetrope.xui.data.XModel;
012: import net.xoetrope.xui.data.XModelAdapter;
013: import net.xoetrope.xui.XProjectManager;
014: import java.awt.Component;
015: import javax.swing.tree.TreeModel;
016: import javax.swing.tree.DefaultTreeModel;
017: import java.util.Vector;
018: import javax.swing.tree.TreePath;
019: import net.xoetrope.xml.XmlElement;
020:
021: /**
022: * This class provides a means of binding a model node and its children to an
023: * XModel node.
024: * <p>Copyright: Copyright (c) Xoetrope Ltd., 2001-2004</p>
025: * <p>$Revision: 1.5 $ </p>
026: * License see license.txt
027: */
028: public class XTreeBinding implements XDataBinding {
029: protected JTree comp;
030: protected XModelAdapter model;
031: protected XModel outputNode;
032: protected String srcPath;
033: protected String outputPath;
034:
035: /**
036: * Construct a new data binding
037: * @param c the JTree component to be bound
038: * @param node the model node
039: */
040: public XTreeBinding(JTree c, XModelAdapter node) {
041: model = node;
042: comp = c;
043: comp.setModel(new DefaultTreeModel((XTreeModelAdapter) model));
044: }
045:
046: /**
047: * Updates the Tree component with the value obtained from the data model.
048: * Filters the list by ignoring repeat items (note that this does not
049: * guarantee unique list entries unless the useUnique flag is set).
050: */
051: public void get() {
052: comp.setModel(new DefaultTreeModel(new XTreeModelAdapter(model
053: .getModel())));
054:
055: // in the case of dynamic bindings we want to update the model with what is seleted by default.
056: set();
057: }
058:
059: /**
060: * Updates the data model with the value retrieved from the TextComponent.
061: */
062: public void set() {
063: if (outputNode != null)
064: outputNode.set(getTreePath());
065: }
066:
067: /**
068: * Gets the component on which this binding operates
069: * @return the bound component
070: */
071: public Component getComponent() {
072: return (Component) comp;
073: }
074:
075: /**
076: * Gets the name of the model node
077: * @return the name
078: */
079: public String getName() {
080: return ((XTreeModelAdapter) model).getTagName();
081: }
082:
083: /**
084: * Get the model component to which this object binds
085: * @return
086: */
087: public String getSourcePath() {
088: return srcPath;
089: }
090:
091: /**
092: * Set the model path for the source data
093: */
094: public void setSourcePath(String newPath) {
095: srcPath = newPath;
096: }
097:
098: /**
099: * Set the model path for the output/state data
100: */
101: public void setOutputPath(String newPath) {
102: outputPath = XModel.prefixOutputPath(newPath);
103: }
104:
105: /**
106: * Get the model component to which this object binds
107: * @return
108: */
109: public String getOutputPath() {
110: return outputPath;
111: }
112:
113: /**
114: * Set the source for this bindings's data
115: * @param newNode the path to the data in the model
116: */
117: public void setSource(XModel newNode) {
118: if (model.getModel() != newNode) {
119: model.setModel(newNode);
120: get();
121: }
122: }
123:
124: /**
125: * Set the output node where state data for this binding will be saved
126: * @param newPath the path of the output/state node
127: */
128: public void setOutput(XModel newNode) {
129: if ((outputNode == null) || !outputNode.equals(newNode)) {
130: outputNode = newNode;
131: if ((outputNode.get() == null)
132: && (comp.getModel().getChildCount(
133: comp.getModel().getRoot()) > 0)) {
134: // Try selecting the previous selection
135: try {
136: String sel = getTreePath();
137:
138: // Split the selection path
139: Vector nodes = new Vector();
140: int pos = sel.indexOf('/');
141: int start = 0;
142: while (pos > 0) {
143: nodes.add(sel.substring(start, pos++));
144: start = pos;
145: pos = sel.indexOf('/', start);
146: }
147: nodes.add(sel.substring(start));
148:
149: if (nodes.size() > 0) {
150: Object nodeArray[] = nodes.toArray();
151: TreePath path = new TreePath(nodeArray);
152: comp.setSelectionPath(path);
153: comp.scrollPathToVisible(path);
154: }
155: } catch (Exception e) {
156: }
157: }
158: }
159: }
160:
161: protected String getTreePath() {
162: String pathName = null;
163: if (comp.getSelectionPath() != null) {
164: Object path[] = comp.getSelectionPath().getPath();
165:
166: pathName = "";
167: for (int i = 1; i < path.length; i++) {
168: pathName += path[i].toString() + "/";
169: }
170: if (pathName.length() > 0)
171: pathName = pathName.substring(0, pathName.length() - 1);
172: }
173:
174: if (pathName == null)
175: return "";
176: else if (pathName.trim().compareTo("") != 0)
177: return pathName;
178: else
179: return "";
180: }
181: }
|