Project

General

Profile

Download (4.2 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.HashSet;
14
import java.util.Set;
15

    
16
/**
17
 * Calculates the X-coordinates for nodes in a compound directed graph.
18
 * 
19
 * @author Randy Hudson
20
 * @since 2.1.2
21
 */
22
class CompoundHorizontalPlacement extends HorizontalPlacement {
23

    
24
	class LeftRight {
25
		// $TODO Delete and use NodePair class, equivalent
26
		Object left, right;
27

    
28
		LeftRight(Object l, Object r) {
29
			left = l;
30
			right = r;
31
		}
32

    
33
		public boolean equals(Object obj) {
34
			LeftRight entry = (LeftRight) obj;
35
			return entry.left.equals(left) && entry.right.equals(right);
36
		}
37

    
38
		public int hashCode() {
39
			return left.hashCode() ^ right.hashCode();
40
		}
41
	}
42

    
43
	Set entries = new HashSet();
44

    
45
	/**
46
	 * @see org.eclipse.graph.HorizontalPlacement#applyGPrime()
47
	 */
48
	void applyGPrime() {
49
		super.applyGPrime();
50
		NodeList subgraphs = ((CompoundDirectedGraph) graph).subgraphs;
51
		for (int i = 0; i < subgraphs.size(); i++) {
52
			Subgraph s = (Subgraph) subgraphs.get(i);
53
			s.x = s.left.x;
54
			s.width = s.right.x + s.right.width - s.x;
55
		}
56
	}
57

    
58
	/**
59
	 * @see HorizontalPlacement#buildRankSeparators(RankList)
60
	 */
61
	void buildRankSeparators(RankList ranks) {
62
		CompoundDirectedGraph g = (CompoundDirectedGraph) graph;
63

    
64
		Rank rank;
65
		for (int row = 0; row < g.ranks.size(); row++) {
66
			rank = g.ranks.getRank(row);
67
			Node n = null, prev = null;
68
			for (int j = 0; j < rank.size(); j++) {
69
				n = rank.getNode(j);
70
				if (prev == null) {
71
					Node left = addSeparatorsLeft(n, null);
72
					if (left != null) {
73
						Edge e = new Edge(graphLeft, getPrime(left), 0, 0);
74
						prime.edges.add(e);
75
						e.delta = graph.getPadding(n).left
76
								+ graph.getMargin().left;
77
					}
78

    
79
				} else {
80
					Subgraph s = GraphUtilities.getCommonAncestor(prev, n);
81
					Node left = addSeparatorsRight(prev, s);
82
					Node right = addSeparatorsLeft(n, s);
83
					createEdge(left, right);
84
				}
85
				prev = n;
86
			}
87
			if (n != null)
88
				addSeparatorsRight(n, null);
89
		}
90
	}
91

    
92
	void createEdge(Node left, Node right) {
93
		LeftRight entry = new LeftRight(left, right);
94
		if (entries.contains(entry))
95
			return;
96
		entries.add(entry);
97
		int separation = left.width + graph.getPadding(left).right
98
				+ graph.getPadding(right).left;
99
		prime.edges
100
				.add(new Edge(getPrime(left), getPrime(right), separation, 0));
101
	}
102

    
103
	Node addSeparatorsLeft(Node n, Subgraph graph) {
104
		Subgraph parent = n.getParent();
105
		while (parent != graph && parent != null) {
106
			createEdge(getLeft(parent), n);
107
			n = parent.left;
108
			parent = parent.getParent();
109
		}
110
		return n;
111
	}
112

    
113
	Node addSeparatorsRight(Node n, Subgraph graph) {
114
		Subgraph parent = n.getParent();
115
		while (parent != graph && parent != null) {
116
			createEdge(n, getRight(parent));
117
			n = parent.right;
118
			parent = parent.getParent();
119
		}
120
		return n;
121
	}
122

    
123
	Node getLeft(Subgraph s) {
124
		if (s.left == null) {
125
			s.left = new SubgraphBoundary(s, graph.getPadding(s), 1);
126
			s.left.rank = (s.head.rank + s.tail.rank) / 2;
127

    
128
			Node head = getPrime(s.head);
129
			Node tail = getPrime(s.tail);
130
			Node left = getPrime(s.left);
131
			Node right = getPrime(getRight(s));
132
			prime.edges.add(new Edge(left, right, s.width, 0));
133
			prime.edges.add(new Edge(left, head, 0, 1));
134
			prime.edges.add(new Edge(head, right, 0, 1));
135
			prime.edges.add(new Edge(left, tail, 0, 1));
136
			prime.edges.add(new Edge(tail, right, 0, 1));
137
		}
138
		return s.left;
139
	}
140

    
141
	Node getRight(Subgraph s) {
142
		if (s.right == null) {
143
			s.right = new SubgraphBoundary(s, graph.getPadding(s), 3);
144
			s.right.rank = (s.head.rank + s.tail.rank) / 2;
145
		}
146
		return s.right;
147
	}
148

    
149
	Node getPrime(Node n) {
150
		Node nPrime = get(n);
151
		if (nPrime == null) {
152
			nPrime = new Node(n);
153
			prime.nodes.add(nPrime);
154
			map(n, nPrime);
155
		}
156
		return nPrime;
157
	}
158

    
159
	public void visit(DirectedGraph g) {
160
		super.visit(g);
161
	}
162

    
163
}
(7-7/49)