Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2005, the JUNG Project and the Regents of the University of | |
3 | * California All rights reserved. | |
4 | * | |
5 | * This software is open-source under the BSD license; see either "license.txt" | |
6 | * or http://jung.sourceforge.net/license.txt for a description. | |
7 | * | |
8 | * Created on Jul 9, 2005 | |
9 | */ | |
10 | ||
11 | package edu.uci.ics.jung.visualization.contrib; | |
12 | import java.awt.Dimension; | |
13 | import java.awt.Point; | |
14 | import java.util.HashSet; | |
15 | import java.util.Iterator; | |
16 | import java.util.Set; | |
17 | import java.util.Vector; | |
18 | ||
19 | import edu.uci.ics.jung.graph.Graph; | |
20 | import edu.uci.ics.jung.graph.Vertex; | |
21 | import edu.uci.ics.jung.graph.impl.SparseTree; | |
22 | import edu.uci.ics.jung.utils.Pair; | |
23 | import edu.uci.ics.jung.utils.UserData; | |
24 | import edu.uci.ics.jung.visualization.AbstractLayout; | |
25 | import edu.uci.ics.jung.visualization.Coordinates; | |
26 | import edu.uci.ics.jung.visualization.Layout; | |
27 | ||
28 | /** | |
29 | * @author Karlheinz Toni | |
30 | * | |
31 | */ | |
32 | ||
33 | public class TreeLayout extends AbstractLayout implements Layout { | |
34 | ||
35 | private static final String C_DIMENSION_X_BASE_KEY = "DimsionX"; | |
36 | ||
37 | 0 | public static int DEFAULT_DISTX = 50; |
38 | 0 | public static int DEFAULT_DISTY = 50; |
39 | // private static Logger logger = Logger.getLogger(TreeLayout.class.getName()); | |
40 | ||
41 | public static Vector getAtomics(Vertex p) { | |
42 | 0 | Vector v = new Vector(); |
43 | 0 | getAtomics(p, v); |
44 | 0 | return v; |
45 | } | |
46 | ||
47 | private static void getAtomics(Vertex p, Vector v) { | |
48 | 0 | for (Iterator i = p.getSuccessors().iterator(); i.hasNext();) { |
49 | 0 | Vertex c = (Vertex) i.next(); |
50 | 0 | if (c.getSuccessors().isEmpty()) { |
51 | 0 | v.add(c); |
52 | } else { | |
53 | 0 | getAtomics(c, v); |
54 | } | |
55 | } | |
56 | 0 | } |
57 | 0 | private transient Set allreadyDone = new HashSet(); |
58 | // private transient int currentSiblings_; | |
59 | ||
60 | 0 | private int distX = DEFAULT_DISTX; |
61 | 0 | private int distY = DEFAULT_DISTY; |
62 | 0 | private transient Point m_currentPoint = new Point(); |
63 | ||
64 | private transient Vertex m_currentVertex; | |
65 | ||
66 | private Pair m_dimensionKey; | |
67 | private Vertex m_rootVertex; | |
68 | ||
69 | public TreeLayout(SparseTree g) { | |
70 | 0 | super(g); |
71 | //logger.info("Constructor for TreeLayout called" + g.getClass()); | |
72 | //logger.info("Setting root Node" + g.getRoot()); | |
73 | 0 | this.m_rootVertex = g.getRoot(); |
74 | 0 | } |
75 | ||
76 | public TreeLayout(SparseTree g, int distx) { | |
77 | 0 | super(g); |
78 | //logger.info("Constructor for TreeLayout called" + g.getClass()); | |
79 | //logger.info("Setting root Node" + g.getRoot()); | |
80 | 0 | this.m_rootVertex = g.getRoot(); |
81 | 0 | this.distX = distx; |
82 | 0 | } |
83 | ||
84 | public TreeLayout(SparseTree g, int distx, int disty) { | |
85 | 0 | super(g); |
86 | //logger.info("Constructor for TreeLayout called" + g.getClass()); | |
87 | //logger.info("Setting root Node" + g.getRoot()); | |
88 | 0 | this.m_rootVertex = g.getRoot(); |
89 | 0 | this.distX = distx; |
90 | 0 | this.distY = disty; |
91 | 0 | } |
92 | ||
93 | /** | |
94 | * ? | |
95 | * | |
96 | * @see edu.uci.ics.jung.visualization.Layout#advancePositions() | |
97 | */ | |
98 | public void advancePositions() { | |
99 | ////logger.info("method called"); | |
100 | 0 | } |
101 | ||
102 | public void applyFilter(Graph g) { | |
103 | //logger.info("method called"); | |
104 | 0 | super.applyFilter(g); |
105 | 0 | } |
106 | void buildTree() { | |
107 | 0 | this.m_currentPoint = new Point(this.getCurrentSize().width / 2, 20); |
108 | 0 | if (m_rootVertex != null && getGraph() != null) { |
109 | //logger.info("BUILDTREE called"); | |
110 | //int size = | |
111 | 0 | calculateDimensionX(m_rootVertex); |
112 | //logger.info("The tree has got a x-dimension of:" + size); | |
113 | 0 | buildTree(m_rootVertex, this.m_currentPoint.x); |
114 | } | |
115 | 0 | } |
116 | ||
117 | void buildTree(Vertex v, int x) { | |
118 | ||
119 | 0 | if (!allreadyDone.contains(v)) { |
120 | 0 | allreadyDone.add(v); |
121 | ||
122 | //go one level further down | |
123 | 0 | this.m_currentPoint.y += this.distY; |
124 | 0 | this.m_currentPoint.x = x; |
125 | ||
126 | 0 | this.setCurrentPositionFor(v); |
127 | ||
128 | 0 | int sizeXofCurrent = ((Integer) v.getUserDatum(this |
129 | .getDimensionBaseKey())).intValue(); | |
130 | ||
131 | 0 | int lastX = x - sizeXofCurrent / 2; |
132 | ||
133 | int sizeXofChild; | |
134 | int startXofChild; | |
135 | ||
136 | 0 | for (Iterator j = v.getSuccessors().iterator(); j.hasNext();) { |
137 | 0 | Vertex element = (Vertex) j.next(); |
138 | 0 | sizeXofChild = ((Integer) element.getUserDatum(this |
139 | .getDimensionBaseKey())).intValue(); | |
140 | 0 | startXofChild = lastX + sizeXofChild / 2; |
141 | 0 | buildTree(element, startXofChild); |
142 | 0 | lastX = lastX + sizeXofChild + distX; |
143 | } | |
144 | 0 | this.m_currentPoint.y -= this.distY; |
145 | } | |
146 | 0 | } |
147 | private int calculateDimensionX(Vertex v) { | |
148 | //logger.info("calculating dimension for vertex " + v); | |
149 | 0 | int size = 0; |
150 | 0 | int childrenNum = v.getSuccessors().size(); |
151 | //logger.info("vertex " + v + " has got " + childrenNum + " successors"); | |
152 | 0 | if (childrenNum != 0) { |
153 | Vertex element; | |
154 | 0 | for (Iterator iter = v.getSuccessors().iterator(); iter.hasNext();) |
155 | { | |
156 | 0 | element = (Vertex) iter.next(); |
157 | 0 | size += calculateDimensionX(element) + distX; |
158 | } | |
159 | } | |
160 | 0 | size = Math.max(0, size - distX); |
161 | 0 | v.setUserDatum(this.getDimensionBaseKey(), new Integer(size), |
162 | UserData.REMOVE); | |
163 | //logger.info("dimension for vertex " + v + " is " + size); | |
164 | 0 | return size; |
165 | } | |
166 | ||
167 | public int getDepth(Vertex v) { | |
168 | 0 | int depth = 0; |
169 | 0 | for (Iterator i = v.getSuccessors().iterator(); i.hasNext();) { |
170 | 0 | Vertex c = (Vertex) i.next(); |
171 | 0 | if (c.getSuccessors().isEmpty()) { |
172 | 0 | depth = 0; |
173 | } else { | |
174 | 0 | depth = Math.max(depth, getDepth(c)); |
175 | } | |
176 | } | |
177 | ||
178 | 0 | return depth + 1; |
179 | } | |
180 | ||
181 | private Object getDimensionBaseKey() { | |
182 | 0 | if (m_dimensionKey == null) { |
183 | 0 | m_dimensionKey = new Pair(this, C_DIMENSION_X_BASE_KEY); |
184 | } | |
185 | 0 | return m_dimensionKey; |
186 | } | |
187 | /** | |
188 | * @return Returns the rootVertex_. | |
189 | */ | |
190 | public Vertex getRootVertex() { | |
191 | 0 | return m_rootVertex; |
192 | } | |
193 | ||
194 | /** | |
195 | * ? | |
196 | * | |
197 | * @see edu.uci.ics.jung.visualization.Layout#incrementsAreDone() | |
198 | */ | |
199 | public boolean incrementsAreDone() { | |
200 | 0 | return true; |
201 | } | |
202 | public void initialize(Dimension size) { | |
203 | //logger.info("method called " + size); | |
204 | 0 | super.initialize(size); |
205 | 0 | buildTree(); |
206 | 0 | } |
207 | /** | |
208 | * ? | |
209 | * | |
210 | * @see edu.uci.ics.jung.visualization.AbstractLayout#initialize_local() | |
211 | */ | |
212 | // protected void initialize_local() { | |
213 | // | |
214 | // } | |
215 | ||
216 | /** | |
217 | * ? | |
218 | * | |
219 | * @see edu.uci.ics.jung.visualization.AbstractLayout#initialize_local_vertex(edu.uci.ics.jung.graph.Vertex) | |
220 | */ | |
221 | protected void initialize_local_vertex(Vertex v) { | |
222 | //logger.info("method called"); | |
223 | 0 | } |
224 | ||
225 | protected void initializeLocations() { | |
226 | 0 | for (Iterator iter = this.getGraph().getVertices().iterator(); iter |
227 | 0 | .hasNext();) { |
228 | 0 | Vertex v = (Vertex) iter.next(); |
229 | ||
230 | 0 | Coordinates coord = (Coordinates) v.getUserDatum(getBaseKey()); |
231 | 0 | if (coord == null) { |
232 | 0 | coord = new Coordinates(); |
233 | 0 | v.addUserDatum(getBaseKey(), coord, UserData.REMOVE); |
234 | } | |
235 | 0 | initialize_local_vertex(v); |
236 | } | |
237 | //logger.info("we have " + getVisibleVertices().size() | |
238 | // + " visible vertices in our graph"); | |
239 | 0 | } |
240 | ||
241 | /** | |
242 | * ? | |
243 | * | |
244 | * @see edu.uci.ics.jung.visualization.Layout#isIncremental() | |
245 | */ | |
246 | public boolean isIncremental() { | |
247 | 0 | return false; |
248 | } | |
249 | ||
250 | private void setCurrentPositionFor(Vertex vertex) { | |
251 | 0 | Coordinates coord = getCoordinates(vertex); |
252 | //logger.info("coordinates for vertex before change" + vertex + "(" | |
253 | // + (int) coord.getX() + "," + (int) coord.getY() + ")"); | |
254 | 0 | coord.setX(m_currentPoint.x); |
255 | 0 | coord.setY(m_currentPoint.y); |
256 | //logger.info("coordinates for vertex after change" + vertex + "(" | |
257 | // + (int) coord.getX() + "," + (int) coord.getY() + ")"); | |
258 | 0 | } |
259 | ||
260 | /** | |
261 | * @param rootVertex_ | |
262 | * The rootVertex_ to set. | |
263 | */ | |
264 | public void setRootVertex(Vertex rootVertex_) { | |
265 | 0 | this.m_rootVertex = rootVertex_; |
266 | 0 | m_currentVertex = rootVertex_; |
267 | 0 | } |
268 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |