Project

General

Profile

Download (2.27 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2003, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.draw2d.graph;
12

    
13
import java.util.Stack;
14

    
15
/**
16
 * This class takes a DirectedGraph with an optimal rank assignment and a
17
 * spanning tree, and populates the ranks of the DirectedGraph. Virtual nodes
18
 * are inserted for edges that span 1 or more ranks.
19
 * <P>
20
 * Ranks are populated using a pre-order depth-first traversal of the spanning
21
 * tree. For each node, all edges requiring virtual nodes are added to the
22
 * ranks.
23
 * 
24
 * @author Randy Hudson
25
 * @since 2.1.2
26
 */
27
class PopulateRanks extends GraphVisitor {
28

    
29
	private Stack changes = new Stack();
30

    
31
	/**
32
	 * @see GraphVisitor#visit(DirectedGraph)
33
	 */
34
	public void visit(DirectedGraph g) {
35
		if (g.forestRoot != null) {
36
			for (int i = g.forestRoot.outgoing.size() - 1; i >= 0; i--)
37
				g.removeEdge(g.forestRoot.outgoing.getEdge(i));
38
			g.removeNode(g.forestRoot);
39
		}
40
		g.ranks = new RankList();
41
		for (int i = 0; i < g.nodes.size(); i++) {
42
			Node node = g.nodes.getNode(i);
43
			g.ranks.getRank(node.rank).add(node);
44
		}
45
		for (int i = 0; i < g.nodes.size(); i++) {
46
			Node node = g.nodes.getNode(i);
47
			for (int j = 0; j < node.outgoing.size();) {
48
				Edge e = node.outgoing.getEdge(j);
49
				if (e.getLength() > 1)
50
					changes.push(new VirtualNodeCreation(e, g));
51
				else
52
					j++;
53
			}
54
		}
55
	}
56

    
57
	/**
58
	 * @see GraphVisitor#revisit(DirectedGraph)
59
	 */
60
	public void revisit(DirectedGraph g) {
61
		for (int r = 0; r < g.ranks.size(); r++) {
62
			Rank rank = g.ranks.getRank(r);
63
			Node prev = null, cur;
64
			for (int n = 0; n < rank.size(); n++) {
65
				cur = rank.getNode(n);
66
				cur.left = prev;
67
				if (prev != null) {
68
					prev.right = cur;
69
				}
70
				prev = cur;
71
			}
72
		}
73
		for (int i = 0; i < changes.size(); i++) {
74
			RevertableChange change = (RevertableChange) changes.get(i);
75
			change.revert();
76
		}
77
	}
78

    
79
}
(30-30/49)