001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * Portions Copyrighted 2007 Sun Microsystems, Inc.
016: */
017: package org.netbeans.modules.xslt.mapper.model;
018:
019: import java.util.ArrayList;
020: import java.util.Enumeration;
021: import java.util.Iterator;
022: import java.util.List;
023: import javax.swing.JTree;
024: import javax.swing.tree.TreePath;
025: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
026:
027: /**
028: *
029: * @author Alexey
030: */
031: public class TreeExpandedState {
032:
033: private JTree tree;
034: private ArrayList<ArrayList<Step>> expanedNodes = new ArrayList<ArrayList<Step>>();
035:
036: private class Step {
037:
038: protected String name;
039: protected int index;
040:
041: public Step(String name, int index) {
042: this .name = name;
043: this .index = index;
044: }
045:
046: public int getIndex() {
047: return this .index;
048: }
049:
050: public String getName() {
051: return this .name;
052: }
053: }
054:
055: public TreeExpandedState(JTree tree) {
056: this .tree = tree;
057: }
058:
059: public void save() {
060:
061: TreeNode root = (TreeNode) tree.getModel().getRoot();
062:
063: TreePath root_tp = TreeNode.getTreePath(root);
064: //save the expanded state
065: Enumeration<TreePath> expanded = tree
066: .getExpandedDescendants(root_tp);
067: if (expanded != null) {
068: while (expanded.hasMoreElements()) {
069: TreePath tp = expanded.nextElement();
070: ArrayList<Step> path = getStepsByTreePath(tp);
071: expanedNodes.add(path);
072: }
073: }
074: }
075:
076: public void restore() {
077:
078: for (ArrayList<Step> steps : expanedNodes) {
079: TreePath tp = getTreePathbySteps(steps);
080: if (tp != null) {
081: tree.expandPath(tp);
082: }
083: }
084: }
085:
086: private ArrayList<Step> getStepsByTreePath(TreePath tp) {
087:
088: ArrayList<Step> result = new ArrayList<Step>();
089:
090: for (int n = 0; n < tp.getPathCount(); n++) {
091: TreeNode node = (TreeNode) tp.getPathComponent(n);
092: String name = node.toString();
093:
094: TreeNode parent = node.getParent();
095:
096: int index = 0;
097:
098: //calculate node index
099: if (parent != null) {
100: List<TreeNode> children = parent.getChildren();
101: for (TreeNode tn : children) {
102: String name1 = tn.toString();
103: if (name1.equals(name)) {
104: if (node == tn) {
105: break;
106: }
107: index++;
108: }
109: }
110: }
111: result.add(new Step(name, index));
112: }
113: return result;
114: }
115:
116: private TreePath getTreePathbySteps(ArrayList<Step> steps) {
117:
118: TreeNode node = (TreeNode) tree.getModel().getRoot();
119:
120: if (node == null) {
121: return null;
122: }
123:
124: if (steps.size() == 0) {
125: return null;
126: }
127:
128: if (!steps.get(0).getName().equals(node.toString())) {
129: return null;
130: }
131:
132: Iterator it = steps.iterator();
133:
134: TreePath result = new TreePath(node);
135:
136: for (int n = 1; n < steps.size(); n++) {
137: Step step = steps.get(n);
138:
139: List<TreeNode> children = node.getChildren();
140:
141: node = getTreeNodeByStep(children, step);
142: if (node == null) {
143: return null;
144: }
145:
146: result = result.pathByAddingChild(node);
147: }
148: return result;
149: }
150:
151: private TreeNode getTreeNodeByStep(List<TreeNode> children,
152: Step step) {
153: int index = 0;
154: for (TreeNode tn : children) {
155: if (step.getName().equals(tn.toString())) {
156: if (index == step.index) {
157: return tn;
158: }
159: index++;
160: }
161: }
162: return null;
163: }
164: }
|