Mercurial > projects > dwt-addons
comparison dwtx/draw2d/graph/CompoundHorizontalPlacement.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) 2003, 2007 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 module dwtx.draw2d.graph.CompoundHorizontalPlacement; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 import dwtx.dwtxhelper.Collection; | |
17 import dwtx.draw2d.graph.HorizontalPlacement; | |
18 import dwtx.draw2d.graph.Node; | |
19 import dwtx.draw2d.graph.RankList; | |
20 import dwtx.draw2d.graph.Rank; | |
21 import dwtx.draw2d.graph.Subgraph; | |
22 import dwtx.draw2d.graph.SubgraphBoundary; | |
23 import dwtx.draw2d.graph.DirectedGraph; | |
24 import dwtx.draw2d.graph.NodeList; | |
25 import dwtx.draw2d.graph.CompoundDirectedGraph; | |
26 import dwtx.draw2d.graph.Edge; | |
27 import dwtx.draw2d.graph.GraphUtilities; | |
28 | |
29 /** | |
30 * Calculates the X-coordinates for nodes in a compound directed graph. | |
31 * @author Randy Hudson | |
32 * @since 2.1.2 | |
33 */ | |
34 class CompoundHorizontalPlacement : HorizontalPlacement { | |
35 | |
36 class LeftRight { | |
37 //$TODO Delete and use NodePair class, equivalent | |
38 Object left, right; | |
39 this(Object l, Object r) { | |
40 left = l; right = r; | |
41 } | |
42 public override int opEquals(Object obj) { | |
43 LeftRight entry = cast(LeftRight)obj; | |
44 return entry.left.opEquals(left) && entry.right.opEquals(right); | |
45 } | |
46 public override hash_t toHash() { | |
47 return left.toHash() ^ right.toHash(); | |
48 } | |
49 } | |
50 | |
51 Set entries; | |
52 | |
53 public this(){ | |
54 entries = new HashSet(); | |
55 } | |
56 | |
57 /** | |
58 * @see org.eclipse.graph.HorizontalPlacement#applyGPrime() | |
59 */ | |
60 void applyGPrime() { | |
61 super.applyGPrime(); | |
62 NodeList subgraphs = (cast(CompoundDirectedGraph)graph).subgraphs; | |
63 for (int i = 0; i < subgraphs.size(); i++) { | |
64 Subgraph s = cast(Subgraph)subgraphs.get(i); | |
65 s.x = s.left.x; | |
66 s.width = s.right.x + s.right.width - s.x; | |
67 } | |
68 } | |
69 | |
70 /** | |
71 * @see HorizontalPlacement#buildRankSeparators(RankList) | |
72 */ | |
73 void buildRankSeparators(RankList ranks) { | |
74 CompoundDirectedGraph g = cast(CompoundDirectedGraph)graph; | |
75 | |
76 Rank rank; | |
77 for (int row = 0; row < g.ranks.size(); row++) { | |
78 rank = g.ranks.getRank(row); | |
79 Node n = null, prev = null; | |
80 for (int j = 0; j < rank.size(); j++) { | |
81 n = rank.getNode(j); | |
82 if (prev is null) { | |
83 Node left = addSeparatorsLeft(n, null); | |
84 if (left !is null) { | |
85 Edge e = new Edge(graphLeft, getPrime(left), 0, 0); | |
86 prime.edges.add(e); | |
87 e.delta = graph.getPadding(n).left + graph.getMargin().left; | |
88 } | |
89 | |
90 } else { | |
91 Subgraph s = GraphUtilities.getCommonAncestor(prev, n); | |
92 Node left = addSeparatorsRight(prev, s); | |
93 Node right = addSeparatorsLeft(n, s); | |
94 createEdge(left, right); | |
95 } | |
96 prev = n; | |
97 } | |
98 if (n !is null) | |
99 addSeparatorsRight(n, null); | |
100 } | |
101 } | |
102 | |
103 void createEdge(Node left, Node right) { | |
104 LeftRight entry = new LeftRight(left, right); | |
105 if (entries.contains(entry)) | |
106 return; | |
107 entries.add(entry); | |
108 int separation = left.width | |
109 + graph.getPadding(left).right | |
110 + graph.getPadding(right).left; | |
111 prime.edges.add(new Edge( | |
112 getPrime(left), getPrime(right), separation, 0 | |
113 )); | |
114 } | |
115 | |
116 Node addSeparatorsLeft(Node n, Subgraph graph) { | |
117 Subgraph parent = n.getParent(); | |
118 while (parent !is graph && parent !is null) { | |
119 createEdge(getLeft(parent), n); | |
120 n = parent.left; | |
121 parent = parent.getParent(); | |
122 } | |
123 return n; | |
124 } | |
125 | |
126 Node addSeparatorsRight(Node n, Subgraph graph) { | |
127 Subgraph parent = n.getParent(); | |
128 while (parent !is graph && parent !is null) { | |
129 createEdge(n, getRight(parent)); | |
130 n = parent.right; | |
131 parent = parent.getParent(); | |
132 } | |
133 return n; | |
134 } | |
135 | |
136 Node getLeft(Subgraph s) { | |
137 if (s.left is null) { | |
138 s.left = new SubgraphBoundary(s, graph.getPadding(s), 1); | |
139 s.left.rank = (s.head.rank + s.tail.rank) / 2; | |
140 | |
141 Node head = getPrime(s.head); | |
142 Node tail = getPrime(s.tail); | |
143 Node left = getPrime(s.left); | |
144 Node right = getPrime(getRight(s)); | |
145 prime.edges.add(new Edge(left, right, s.width, 0)); | |
146 prime.edges.add(new Edge(left, head, 0, 1)); | |
147 prime.edges.add(new Edge(head, right, 0, 1)); | |
148 prime.edges.add(new Edge(left, tail, 0, 1)); | |
149 prime.edges.add(new Edge(tail, right, 0, 1)); | |
150 } | |
151 return s.left; | |
152 } | |
153 | |
154 Node getRight(Subgraph s) { | |
155 if (s.right is null) { | |
156 s.right = new SubgraphBoundary(s, graph.getPadding(s), 3); | |
157 s.right.rank = (s.head.rank + s.tail.rank) / 2; | |
158 } | |
159 return s.right; | |
160 } | |
161 | |
162 Node getPrime(Node n) { | |
163 Node nPrime = get(n); | |
164 if (nPrime is null) { | |
165 nPrime = new Node(n); | |
166 prime.nodes.add(nPrime); | |
167 map(n, nPrime); | |
168 } | |
169 return nPrime; | |
170 } | |
171 | |
172 public void visit(DirectedGraph g) { | |
173 super.visit(g); | |
174 } | |
175 | |
176 } |