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.bpel.mapper.tree;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021: import javax.swing.tree.TreePath;
022: import org.netbeans.modules.bpel.mapper.model.BpelMapperModel;
023: import org.netbeans.modules.soa.mappercore.Mapper;
024: import org.netbeans.modules.soa.mappercore.RightTree;
025: import org.netbeans.modules.soa.mappercore.model.MapperModel;
026:
027: /**
028: *
029: * @author Vitaly Bychkov
030: * @author Alexey
031: *
032: */
033: public class RightTreeExpandedState implements TreeExpandedState {
034:
035: private Mapper myMapper;
036: private ArrayList<ArrayList<Step>> expanedNodes = new ArrayList<ArrayList<Step>>();
037:
038: public RightTreeExpandedState(Mapper mapper) {
039: myMapper = mapper;
040: }
041:
042: public void setMapper(Mapper mapper) {
043: myMapper = mapper;
044: }
045:
046: private RightTree getRightTree() {
047: return myMapper != null ? myMapper.getRightTree() : null;
048: }
049:
050: private MapperSwingTreeModel getTreeModel() {
051: if (myMapper == null) {
052: return null;
053: }
054: MapperModel mapperModel = myMapper.getModel();
055: assert mapperModel instanceof BpelMapperModel;
056: return mapperModel != null ? ((BpelMapperModel) mapperModel)
057: .getRightTreeModel() : null;
058: }
059:
060: public void save() {
061: if (myMapper == null) {
062: return;
063: }
064: List<TreePath> expanded = myMapper.getExpandedPathes();
065:
066: if (expanded != null) {
067: for (TreePath treePath : expanded) {
068: ArrayList<Step> path = getStepsByTreePath(treePath);
069: expanedNodes.add(path);
070: }
071: }
072: }
073:
074: public void restore() {
075:
076: List<TreePath> paths = new ArrayList<TreePath>();
077: for (ArrayList<Step> steps : expanedNodes) {
078: TreePath tp = getTreePathbySteps(steps);
079: if (tp != null) {
080: paths.add(tp);
081: }
082: }
083:
084: if (paths.size() < 1) {
085: return;
086: }
087:
088: if (myMapper == null) {
089: return;
090: }
091: myMapper.applyExpandedPathes(paths);
092: }
093:
094: private ArrayList<Step> getStepsByTreePath(TreePath tp) {
095:
096: ArrayList<Step> result = new ArrayList<Step>();
097:
098: MapperSwingTreeModel treeModel = getTreeModel();
099: if (treeModel == null) {
100: return result;
101: }
102:
103: for (int n = 0; n < tp.getPathCount(); n++) {
104: MapperTreeNode node = (MapperTreeNode) tp
105: .getPathComponent(n);
106: String nodeDisplayName = treeModel.getDisplayName(node);
107:
108: Object parent = null;
109:
110: if (n > 0) {
111: parent = tp.getPathComponent(n - 1);
112: }
113:
114: int index = 0;
115:
116: //calculate node index
117: if (parent != null && parent instanceof MapperTreeNode) {
118: List<MapperTreeNode> children = ((MapperTreeNode) parent)
119: .getChildren();
120: for (MapperTreeNode tn : children) {
121: String name1 = treeModel.getDisplayName(tn);
122: if (name1.equals(nodeDisplayName)) {
123: if (node == tn) {
124: break;
125: }
126: index++;
127: }
128: }
129: }
130: result.add(new Step(nodeDisplayName, index));
131: }
132: return result;
133: }
134:
135: private TreePath getTreePathbySteps(ArrayList<Step> steps) {
136: MapperSwingTreeModel treeModel = getTreeModel();
137: if (treeModel == null) {
138: return null;
139: }
140:
141: MapperTreeNode node = (MapperTreeNode) treeModel.getRoot();
142:
143: if (node == null) {
144: return null;
145: }
146:
147: if (steps.size() == 0) {
148: return null;
149: }
150:
151: String nodeDisplayName = treeModel.getDisplayName(node);
152: if (!steps.get(0).getName().equals(nodeDisplayName)) {
153: return null;
154: }
155:
156: TreePath result = new TreePath(node);
157:
158: for (int n = 1; n < steps.size(); n++) {
159: Step step = steps.get(n);
160:
161: List<MapperTreeNode> children = node.getChildren();
162:
163: node = getTreeNodeByStep(treeModel, children, step);
164: if (node == null) {
165: return null;
166: }
167:
168: result = result.pathByAddingChild(node);
169: }
170: return result;
171: }
172:
173: private MapperTreeNode getTreeNodeByStep(
174: MapperSwingTreeModel treeModel,
175: List<MapperTreeNode> children, Step step) {
176: if (treeModel == null || children == null) {
177: return null;
178: }
179: //
180: int index = 0;
181: for (MapperTreeNode tn : children) {
182: String nodeDisplayName = treeModel.getDisplayName(tn);
183: if (step.getName().equals(nodeDisplayName)) {
184: if (index == step.index) {
185: return tn;
186: }
187: index++;
188: }
189: }
190: return null;
191: }
192: }
|