1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2004, 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
|
|
12
|
package org.eclipse.draw2d.graph;
|
13
|
|
14
|
import org.eclipse.draw2d.geometry.Insets;
|
15
|
|
16
|
/**
|
17
|
* Encapsulates the conversion of a long edge to multiple short edges and back.
|
18
|
*
|
19
|
* @since 3.1
|
20
|
*/
|
21
|
class VirtualNodeCreation extends RevertableChange {
|
22
|
|
23
|
private final Edge edge;
|
24
|
private final DirectedGraph graph;
|
25
|
private Node nodes[];
|
26
|
private Edge[] edges;
|
27
|
|
28
|
private static final int INNER_EDGE_X = 2;
|
29
|
private static final int LONG_EDGE_X = 8;
|
30
|
|
31
|
/**
|
32
|
* Breaks a single edge into multiple edges containing virtual nodes.
|
33
|
*
|
34
|
* @since 3.1
|
35
|
* @param edge
|
36
|
* The edge to convert
|
37
|
* @param graph
|
38
|
* the graph containing the edge
|
39
|
*/
|
40
|
public VirtualNodeCreation(Edge edge, DirectedGraph graph) {
|
41
|
this.edge = edge;
|
42
|
this.graph = graph;
|
43
|
|
44
|
int size = edge.target.rank - edge.source.rank - 1;
|
45
|
int offset = edge.source.rank + 1;
|
46
|
|
47
|
Node prevNode = edge.source;
|
48
|
Node currentNode;
|
49
|
Edge currentEdge;
|
50
|
nodes = new Node[size];
|
51
|
edges = new Edge[size + 1];
|
52
|
|
53
|
Insets padding = new Insets(0, edge.padding, 0, edge.padding);
|
54
|
|
55
|
Subgraph s = GraphUtilities.getCommonAncestor(edge.source, edge.target);
|
56
|
|
57
|
for (int i = 0; i < size; i++) {
|
58
|
nodes[i] = currentNode = new VirtualNode(
|
59
|
"Virtual" + i + ':' + edge, s); //$NON-NLS-1$
|
60
|
currentNode.width = edge.width;
|
61
|
if (s != null) {
|
62
|
currentNode.nestingIndex = s.nestingIndex;
|
63
|
}
|
64
|
|
65
|
currentNode.height = 0;
|
66
|
currentNode.setPadding(padding);
|
67
|
currentNode.rank = offset + i;
|
68
|
graph.ranks.getRank(offset + i).add(currentNode);
|
69
|
|
70
|
currentEdge = new Edge(prevNode, currentNode, 1, edge.weight
|
71
|
* LONG_EDGE_X);
|
72
|
if (i == 0) {
|
73
|
currentEdge.weight = edge.weight * INNER_EDGE_X;
|
74
|
currentEdge.offsetSource = edge.offsetSource;
|
75
|
}
|
76
|
graph.edges.add(edges[i] = currentEdge);
|
77
|
graph.nodes.add(currentNode);
|
78
|
prevNode = currentNode;
|
79
|
}
|
80
|
|
81
|
currentEdge = new Edge(prevNode, edge.target, 1, edge.weight
|
82
|
* INNER_EDGE_X);
|
83
|
currentEdge.offsetTarget = edge.offsetTarget;
|
84
|
graph.edges.add(edges[edges.length - 1] = currentEdge);
|
85
|
graph.removeEdge(edge);
|
86
|
}
|
87
|
|
88
|
void revert() {
|
89
|
edge.start = edges[0].start;
|
90
|
edge.end = edges[edges.length - 1].end;
|
91
|
edge.vNodes = new NodeList();
|
92
|
for (int i = 0; i < edges.length; i++) {
|
93
|
graph.removeEdge(edges[i]);
|
94
|
}
|
95
|
for (int i = 0; i < nodes.length; i++) {
|
96
|
edge.vNodes.add(nodes[i]);
|
97
|
graph.removeNode(nodes[i]);
|
98
|
}
|
99
|
edge.source.outgoing.add(edge);
|
100
|
edge.target.incoming.add(edge);
|
101
|
|
102
|
graph.edges.add(edge);
|
103
|
}
|
104
|
|
105
|
}
|