Mercurial > projects > dwt-addons
comparison dwtx/draw2d/graph/VirtualNodeCreation.d @ 98:95307ad235d9
Added Draw2d code, still work in progress
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Aug 2008 00:52:14 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2004, 2005 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 | |
14 module dwtx.draw2d.graph.VirtualNodeCreation; | |
15 | |
16 import dwt.dwthelper.utils; | |
17 import tango.text.convert.Format; | |
18 | |
19 import dwtx.draw2d.geometry.Insets; | |
20 import dwtx.draw2d.graph.Edge; | |
21 import dwtx.draw2d.graph.DirectedGraph; | |
22 import dwtx.draw2d.graph.RevertableChange; | |
23 import dwtx.draw2d.graph.Node; | |
24 import dwtx.draw2d.graph.Subgraph; | |
25 import dwtx.draw2d.graph.VirtualNode; | |
26 import dwtx.draw2d.graph.GraphUtilities; | |
27 import dwtx.draw2d.graph.NodeList; | |
28 | |
29 /** | |
30 * Encapsulates the conversion of a long edge to multiple short edges and back. | |
31 * @since 3.1 | |
32 */ | |
33 class VirtualNodeCreation : RevertableChange { | |
34 | |
35 private const Edge edge; | |
36 private const DirectedGraph graph; | |
37 private Node nodes[]; | |
38 private Edge[] edges; | |
39 | |
40 private static const int INNER_EDGE_X = 2; | |
41 private static const int LONG_EDGE_X = 8; | |
42 | |
43 /** | |
44 * Breaks a single edge into multiple edges containing virtual nodes. | |
45 * @since 3.1 | |
46 * @param edge The edge to convert | |
47 * @param graph the graph containing the edge | |
48 */ | |
49 public this(Edge edge, DirectedGraph graph) { | |
50 this.edge = edge; | |
51 this.graph = graph; | |
52 | |
53 int size = edge.target.rank - edge.source.rank - 1; | |
54 int offset = edge.source.rank + 1; | |
55 | |
56 Node prevNode = edge.source; | |
57 Node currentNode; | |
58 Edge currentEdge; | |
59 nodes = new Node[size]; | |
60 edges = new Edge[size + 1]; | |
61 | |
62 Insets padding = new Insets(0, edge.padding, 0, edge.padding); | |
63 | |
64 Subgraph s = GraphUtilities.getCommonAncestor(edge.source, edge.target); | |
65 | |
66 for (int i = 0; i < size; i++) { | |
67 nodes[i] = currentNode = new VirtualNode(stringcast(Format("Virtual{}:{}", i, edge)), s); //$NON-NLS-1$ | |
68 currentNode.width = edge.width; | |
69 if (s !is null) { | |
70 currentNode.nestingIndex = s.nestingIndex; | |
71 } | |
72 | |
73 currentNode.height = 0; | |
74 currentNode.setPadding(padding); | |
75 currentNode.rank = offset + i; | |
76 graph.ranks.getRank(offset + i).add(currentNode); | |
77 | |
78 currentEdge = new Edge(prevNode, currentNode, 1, edge.weight * LONG_EDGE_X); | |
79 if (i is 0) { | |
80 currentEdge.weight = edge.weight * INNER_EDGE_X; | |
81 currentEdge.offsetSource = edge.offsetSource; | |
82 } | |
83 graph.edges.add(edges[i] = currentEdge); | |
84 graph.nodes.add(currentNode); | |
85 prevNode = currentNode; | |
86 } | |
87 | |
88 currentEdge = new Edge(prevNode, edge.target, 1, edge.weight * INNER_EDGE_X); | |
89 currentEdge.offsetTarget = edge.offsetTarget; | |
90 graph.edges.add(edges[edges.length - 1] = currentEdge); | |
91 graph.removeEdge(edge); | |
92 } | |
93 | |
94 void revert() { | |
95 edge.start = edges[0].start; | |
96 edge.end = edges[edges.length - 1].end; | |
97 edge.vNodes = new NodeList(); | |
98 for (int i = 0; i < edges.length; i++) { | |
99 graph.removeEdge(edges[i]); | |
100 } | |
101 for (int i = 0; i < nodes.length; i++) { | |
102 edge.vNodes.add(nodes[i]); | |
103 graph.removeNode(nodes[i]); | |
104 } | |
105 edge.source.outgoing.add(edge); | |
106 edge.target.incoming.add(edge); | |
107 | |
108 graph.edges.add(edge); | |
109 } | |
110 | |
111 } |