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: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.mapper.tree;
021:
022: import java.util.Comparator;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import javax.swing.tree.TreeModel;
027: import javax.swing.tree.TreePath;
028:
029: /**
030: * Compares two tree nodes by their order in tree (above, beneath)
031: *
032: * @author nk160297
033: */
034: class TreePathComparator implements Comparator<TreePath> {
035:
036: private TreeModel mTreeModel;
037:
038: public TreePathComparator(TreeModel treeModel) {
039: mTreeModel = treeModel;
040: }
041:
042: public int compare(TreePath path1, TreePath path2) {
043: List<Object> nodeList1 = getInversedPathList(path1);
044: List<Object> nodeList2 = getInversedPathList(path2);
045: //
046: Iterator<Object> nodeListItr1 = nodeList1.iterator();
047: Iterator<Object> nodeListItr2 = nodeList2.iterator();
048: //
049: Object n1 = null;
050: Object n2 = null;
051: Object commonParent = null;
052: while (nodeListItr1.hasNext() && nodeListItr2.hasNext()) {
053: //
054: // If the previous nodes were the same (n1 == n2) then
055: // now they are the common parent (commonParent == n1 == n2)
056: commonParent = n1;
057: //
058: n1 = nodeListItr1.next();
059: n2 = nodeListItr2.next();
060: //
061: if (n1 != n2) {
062: break;
063: }
064: }
065: //
066: assert (n1 != null) && (n2 != null);
067: //
068: if (commonParent == null) {
069: // The previous while does only one step!
070: // nodeListItr1.hasNext() == false || nodeListItr2.hasNext() == false
071: //
072: boolean list1HasNext = nodeListItr1.hasNext();
073: boolean list2HasNext = nodeListItr2.hasNext();
074: //
075: // Both nodes are the same and both are root
076: if (!list1HasNext && !list2HasNext) {
077: if (n1 == n2) {
078: return 0; // The roots are the same
079: } else {
080: assert false : "trying to compare TreePath from different trees"; // NOI18N
081: }
082: } else if (list1HasNext) {
083: return 1; // the second is the root but the first isn't
084: } else if (list2HasNext) {
085: return -1; // the first is the root but the secon isn't
086: }
087: }
088: //
089: assert commonParent != null;
090: //
091: int n1Index = mTreeModel.getIndexOfChild(commonParent, n1);
092: int n2Index = mTreeModel.getIndexOfChild(commonParent, n2);
093: //
094: return n1Index - n2Index;
095: }
096:
097: public List<Object> getInversedPathList(TreePath treePath) {
098: LinkedList<Object> result = new LinkedList<Object>();
099: //
100: TreePath path = treePath;
101: while (path != null) {
102: Object obj = path.getLastPathComponent();
103: result.addFirst((Object) obj);
104: path = path.getParentPath();
105: }
106: //
107: return result;
108: }
109:
110: }
|