| Line | Hits | Source |
|---|---|---|
| 1 | /* | |
| 2 | * Copyright (c) 2003, the JUNG Project and the Regents of the University | |
| 3 | * of California | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * This software is open-source under the BSD license; see either | |
| 7 | * "license.txt" or | |
| 8 | * http://jung.sourceforge.net/license.txt for a description. | |
| 9 | */ | |
| 10 | /* | |
| 11 | * Created on May 5, 2003 | |
| 12 | * | |
| 13 | * To change the template for this generated file go to | |
| 14 | * Window>Preferences>Java>Code Generation>Code and Comments | |
| 15 | */ | |
| 16 | package edu.uci.ics.jung.visualization; | |
| 17 | ||
| 18 | import java.awt.Dimension; | |
| 19 | import java.awt.geom.AffineTransform; | |
| 20 | import java.awt.geom.Point2D; | |
| 21 | import java.util.HashSet; | |
| 22 | import java.util.Iterator; | |
| 23 | import java.util.Set; | |
| 24 | ||
| 25 | import edu.uci.ics.jung.graph.ArchetypeVertex; | |
| 26 | import edu.uci.ics.jung.graph.Graph; | |
| 27 | import edu.uci.ics.jung.graph.Vertex; | |
| 28 | import edu.uci.ics.jung.utils.GraphUtils; | |
| 29 | import edu.uci.ics.jung.utils.Pair; | |
| 30 | import edu.uci.ics.jung.utils.UserData; | |
| 31 | ||
| 32 | /** | |
| 33 | * Implements a pass-through visaulizer that fades out | |
| 34 | * nodes that have been removed, and fades in nodes that | |
| 35 | * have appeared. Adds a field FADINGNODEVIZ to the | |
| 36 | * nodes, which directs the _Renderer to view those nodes | |
| 37 | * as faded. | |
| 38 | * <p> | |
| 39 | * In order to use this class, create a FadingNodeLayout | |
| 40 | * that takes as an arguemnt the Layout you actually wish | |
| 41 | * to use: | |
| 42 | * <pre> | |
| 43 | * Layout v= new FadingNodeLayout( 10, new SpringLayout( g )); | |
| 44 | * </pre> | |
| 45 | * In order to operate, this implementation tracks the vertices that | |
| 46 | * are visible before and after each call to <tt>applyFilte</tt>. | |
| 47 | * <ul> | |
| 48 | * <li><em>Nodes that had been visible, but are now filtered out</em> | |
| 49 | * get the field <tt>FADINGNODEVIZ</tt> added, and have the level | |
| 50 | * set to 0.</li> | |
| 51 | * <li><em>Nodes that been filtered out, but are now visible</em>, | |
| 52 | * have the field <tt>FADINGNODEVIZ</tt> removed.</li> | |
| 53 | * </ul> | |
| 54 | * Other nodes are not altered. However, each time that | |
| 55 | * <tt>advancePositions</tt> is called, all nodes that | |
| 56 | * are currently hidden have their fading level incremented. | |
| 57 | * <p> | |
| 58 | * In this documentaiton, code that is labelled as a <tt>passthrough</tt> | |
| 59 | * has no functionality except to pass the data through to the contained | |
| 60 | * layout. | |
| 61 | * <p> | |
| 62 | * Be sure to use a <tt>_Renderer</tt> that knows to pay attention to | |
| 63 | * the Fading information. In particular, it must know that the | |
| 64 | * FADINGNODEVIZ field gives information about the fade level. | |
| 65 | * | |
| 66 | * @author danyelf | |
| 67 | * @deprecated If you are using this code, PLEASE CONTACT US | |
| 68 | */ | |
| 69 | public class FadingVertexLayout implements Layout | |
| 70 | { | |
| 71 | /** | |
| 72 | * @see edu.uci.ics.jung.visualization.Layout#getCurrentSize() | |
| 73 | */ | |
| 74 | public Dimension getCurrentSize() { | |
| 75 | 0 | return layout.getCurrentSize(); |
| 76 | } | |
| 77 | ||
| 78 | AffineTransform transform; | |
| 79 | AffineTransform inverse; | |
| 80 | ||
| 81 | private Dimension currentSize; | |
| 82 | // private Graph graph; | |
| 83 | private Layout layout; | |
| 84 | private int fadelevels; | |
| 85 | ||
| 86 | private Set hiddenNodes; | |
| 87 | ||
| 88 | /** | |
| 89 | * Adds user data to every vertex in the graph. Initially, no | |
| 90 | * nodes are hidden. | |
| 91 | * @param fadelevels The number of levels through which | |
| 92 | * a vertex should fade once it is removed. | |
| 93 | * @param l The layout that is responsible for | |
| 94 | * fading the information. | |
| 95 | */ | |
| 96 | 0 | public FadingVertexLayout(int fadelevels, Layout layout) { |
| 97 | 0 | this.fadelevels = fadelevels; |
| 98 | 0 | this.layout = layout; |
| 99 | 0 | this.hiddenNodes = new HashSet(); |
| 100 | 0 | } |
| 101 | ||
| 102 | /** | |
| 103 | * A pass-through to the contained Layout | |
| 104 | */ | |
| 105 | public void initialize(Dimension d) { | |
| 106 | ||
| 107 | 0 | layout.initialize(d); |
| 108 | ||
| 109 | 0 | Graph graph = layout.getGraph(); |
| 110 | 0 | for (Iterator iter = graph.getVertices().iterator(); iter.hasNext();) { |
| 111 | 0 | ArchetypeVertex vert = (ArchetypeVertex) iter.next(); |
| 112 | 0 | vert.addUserDatum( |
| 113 | getFadingKey(), | |
| 114 | new FadingVertexLayoutData(), | |
| 115 | UserData.REMOVE); | |
| 116 | } | |
| 117 | ||
| 118 | 0 | } |
| 119 | ||
| 120 | public String getStatus() { | |
| 121 | 0 | return layout.getStatus(); |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * Returns *all* edges. Note that this is unusual: for example | |
| 126 | * @see edu.uci.ics.jung.visualization.Layout#getVisibleEdges() | |
| 127 | */ | |
| 128 | public Set getVisibleEdges() { | |
| 129 | 0 | return layout.getVisibleEdges(); |
| 130 | } | |
| 131 | ||
| 132 | /** | |
| 133 | * A pass-through. | |
| 134 | * @see Layout#getGraph() | |
| 135 | */ | |
| 136 | public Graph getGraph() { | |
| 137 | 0 | return layout.getGraph(); |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * @deprecated Use PickSupport instead | |
| 142 | * A pass-through. | |
| 143 | * @see edu.uci.ics.jung.visualization.Layout#getVertex(double, double) | |
| 144 | */ | |
| 145 | public Vertex getVertex(double x, double y) { | |
| 146 | 0 | return layout.getVertex(x, y); |
| 147 | } | |
| 148 | ||
| 149 | /** | |
| 150 | * @deprecated Use PickSupport instead | |
| 151 | * A pass-through. | |
| 152 | * @see edu.uci.ics.jung.visualization.Layout#getVertex(double, double, double) | |
| 153 | */ | |
| 154 | public Vertex getVertex(double x, double y, double maxDistance) { | |
| 155 | 0 | return layout.getVertex(x, y, maxDistance); |
| 156 | } | |
| 157 | ||
| 158 | /** In addition to being a passthrough, this also advances | |
| 159 | * the fade function by calling <tt>{@link #tick() tick}</tt> | |
| 160 | * | |
| 161 | * @see edu.uci.ics.jung.visualization.Layout#advancePositions() | |
| 162 | */ | |
| 163 | public void advancePositions() { | |
| 164 | 0 | tick(); |
| 165 | 0 | layout.advancePositions(); |
| 166 | 0 | } |
| 167 | ||
| 168 | /** | |
| 169 | * This method advances each node that is fading away. | |
| 170 | * Each vertex's "fade" count is advanced | |
| 171 | * one towards {@link #getMaxLevel() getMaxLevel()}, and then moved | |
| 172 | * outward. | |
| 173 | */ | |
| 174 | protected void tick() { | |
| 175 | 0 | Graph graph = layout.getGraph(); |
| 176 | 0 | for (Iterator iter = graph.getVertices().iterator(); iter.hasNext();) { |
| 177 | 0 | Vertex av = (Vertex) iter.next(); |
| 178 | 0 | FadingVertexLayoutData fnvd = |
| 179 | (FadingVertexLayoutData) av.getUserDatum(getFadingKey()); | |
| 180 | 0 | if (fnvd.level < fadelevels) { |
| 181 | 0 | fnvd.level++; |
| 182 | 0 | if (fnvd.isHidden) { |
| 183 | 0 | moveOutward(av, getX(av), getY(av), 1.01); |
| 184 | } | |
| 185 | } | |
| 186 | } | |
| 187 | 0 | } |
| 188 | ||
| 189 | public class FadingVertexLayoutData { | |
| 190 | public boolean isHidden = false; | |
| 191 | public int level = 0; | |
| 192 | } | |
| 193 | ||
| 194 | private static final String FADINGNODEVIZX = | |
| 195 | "edu.uci.ics.jung.FadingNodeLayoutKey"; | |
| 196 | ||
| 197 | 0 | private Object key = null; |
| 198 | public Object getFadingKey() { | |
| 199 | 0 | if (key == null) |
| 200 | 0 | key = new Pair(this, FADINGNODEVIZX); |
| 201 | 0 | return key; |
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * Tracks the changes in the set of visible vertices from the set of | |
| 206 | * actual vertices. <br> | |
| 207 | * Vertices that have been REMOVED will be faded out through FADELEVELS | |
| 208 | * steps; vertices that have been ADDED will be faded in through FADELEVELS | |
| 209 | * steps. All hidden vertices will be labelled as hidden; all showing | |
| 210 | * vertices will be labeled as nonhidden. | |
| 211 | * @see edu.uci.ics.jung.visualization.Layout#applyFilter(edu.uci.ics.jung.graph.Graph) | |
| 212 | */ | |
| 213 | public void applyFilter(Graph g_int) { | |
| 214 | 0 | layout.applyFilter(g_int); |
| 215 | 0 | Graph graph = layout.getGraph(); |
| 216 | 0 | Set nowShowingNodes = |
| 217 | GraphUtils.getEqualVertices(g_int.getVertices(), graph); | |
| 218 | ||
| 219 | 0 | int newlyShowing = 0; |
| 220 | 0 | int newlyHidden = 0; |
| 221 | ||
| 222 | 0 | for (Iterator iter = graph.getVertices().iterator(); iter.hasNext();) { |
| 223 | 0 | ArchetypeVertex vert = (ArchetypeVertex) iter.next(); |
| 224 | 0 | if (nowShowingNodes.contains(vert) && hiddenNodes.contains(vert)) { |
| 225 | ||
| 226 | // v is now NEWLY SHOWING | |
| 227 | 0 | newlyShowing++; |
| 228 | 0 | hiddenNodes.remove(vert); |
| 229 | 0 | FadingVertexLayoutData fnvd = |
| 230 | (FadingVertexLayoutData) vert.getUserDatum(getFadingKey()); | |
| 231 | 0 | fnvd.isHidden = false; |
| 232 | 0 | fnvd.level = 0; |
| 233 | // the node should also be moved to near its neighbors | |
| 234 | 0 | moveVertexPrettily((Vertex) vert.getEqualVertex(g_int)); |
| 235 | 0 | } else if ( |
| 236 | !nowShowingNodes.contains(vert) | |
| 237 | && !hiddenNodes.contains(vert)) { | |
| 238 | // System.out.println(v + " " + v.getClass() ); | |
| 239 | 0 | newlyHidden++; |
| 240 | 0 | hiddenNodes.add(vert); |
| 241 | 0 | FadingVertexLayoutData fnvd = |
| 242 | (FadingVertexLayoutData) vert.getUserDatum(getFadingKey()); | |
| 243 | 0 | fnvd.isHidden = true; |
| 244 | 0 | fnvd.level = 0; |
| 245 | // the node should gradually surf away from its neighbors | |
| 246 | } | |
| 247 | } | |
| 248 | 0 | } |
| 249 | ||
| 250 | /** | |
| 251 | * This code is called when a Vertex is being brought | |
| 252 | * back onto the page. Currently, it merely examines the | |
| 253 | * vertex' neighbors, picks an average point, and moves the | |
| 254 | * vertex nearby, assuming that the neighbors will be placed | |
| 255 | * correctly. | |
| 256 | * @param vert | |
| 257 | */ | |
| 258 | protected void moveVertexPrettily(Vertex vert) { | |
| 259 | // vertex neighbors | |
| 260 | 0 | Set s = vert.getNeighbors(); |
| 261 | 0 | if (s.size() == 0) |
| 262 | 0 | return; |
| 263 | 0 | double x = 0; |
| 264 | 0 | double y = 0; |
| 265 | // average location | |
| 266 | 0 | for (Iterator iter = s.iterator(); iter.hasNext();) { |
| 267 | 0 | Vertex neighbor = (Vertex) iter.next(); |
| 268 | 0 | x += layout.getX(neighbor); |
| 269 | 0 | y += layout.getY(neighbor); |
| 270 | } | |
| 271 | 0 | x /= s.size(); |
| 272 | 0 | y /= s.size(); |
| 273 | 0 | moveOutward(vert, x, y, 0.9); |
| 274 | 0 | } |
| 275 | ||
| 276 | /** | |
| 277 | * Moves a vertex outward, toward the outer edge of the screen | |
| 278 | * by calling {@link #forceMove(Vertex, double, double) forceMove} on the vertex. | |
| 279 | * | |
| 280 | * @param vert | |
| 281 | * @param x The desired origin X coordinate | |
| 282 | * @param y The desired origin Y coordinate | |
| 283 | * @param speed The speed with which the vertex moves outward | |
| 284 | */ | |
| 285 | protected void moveOutward(Vertex vert, double x, double y, double speed) { | |
| 286 | 0 | x -= currentSize.width / 2; |
| 287 | 0 | y -= currentSize.height / 2; |
| 288 | 0 | x *= speed; |
| 289 | 0 | y *= speed; |
| 290 | 0 | x += currentSize.width / 2; |
| 291 | 0 | y += currentSize.height / 2; |
| 292 | 0 | forceMove(vert, x, y); |
| 293 | 0 | } |
| 294 | ||
| 295 | /** Passthrough. | |
| 296 | * @see edu.uci.ics.jung.visualization.Layout#resize(java.awt.Dimension) | |
| 297 | */ | |
| 298 | public void resize(Dimension d) { | |
| 299 | 0 | if (currentSize != null) { |
| 300 | 0 | synchronized (currentSize) { |
| 301 | 0 | this.currentSize = d; |
| 302 | 0 | } |
| 303 | } else { | |
| 304 | 0 | this.currentSize = d; |
| 305 | } | |
| 306 | 0 | layout.resize(d); |
| 307 | 0 | } |
| 308 | ||
| 309 | /** Passthrough. | |
| 310 | * @see edu.uci.ics.jung.visualization.Layout#restart() | |
| 311 | */ | |
| 312 | public void restart() { | |
| 313 | 0 | layout.restart(); |
| 314 | 0 | } |
| 315 | ||
| 316 | /** Passthrough. Note that you can't get X and Y of hidden nodes. | |
| 317 | * @see edu.uci.ics.jung.visualization.Layout#getX(edu.uci.ics.jung.graph.Vertex) | |
| 318 | */ | |
| 319 | public double getX(Vertex vert) { | |
| 320 | 0 | return layout.getX(vert); |
| 321 | } | |
| 322 | ||
| 323 | /** Passthrough. Note that you can't get X and Y of hidden nodes. | |
| 324 | * @see edu.uci.ics.jung.visualization.Layout#getX(edu.uci.ics.jung.graph.Vertex) | |
| 325 | */ | |
| 326 | public double getY(Vertex vert) { | |
| 327 | 0 | return layout.getY(vert); |
| 328 | } | |
| 329 | ||
| 330 | /** | |
| 331 | * Returns both the visible and the hidden vertices. | |
| 332 | * This function is an exception to the usual behavior of | |
| 333 | * <code>getVisibleVertices</code>. Where usually only visible | |
| 334 | * vertices would be passed, this function also passes the | |
| 335 | * hidden ones, and counts on the _Renderer (or other calling | |
| 336 | * client) to know what to do with it appropriately. This is done | |
| 337 | * in order to ensure that fading vertices are still shown. | |
| 338 | * | |
| 339 | * @see edu.uci.ics.jung.visualization.Layout#getVisibleVertices() | |
| 340 | */ | |
| 341 | public Set getVisibleVertices() { | |
| 342 | 0 | return layout.getGraph().getVertices(); |
| 343 | } | |
| 344 | ||
| 345 | /** | |
| 346 | * Static utility function returns the fade level of a | |
| 347 | * given vertex. This vertex must be visaulized by a Fading | |
| 348 | * Node Layout, or this function | |
| 349 | * will throw an exception. | |
| 350 | * @param v | |
| 351 | */ | |
| 352 | public int getFadeLevel(Vertex v) { | |
| 353 | 0 | return ((FadingVertexLayoutData) v.getUserDatum(getFadingKey())).level; |
| 354 | } | |
| 355 | ||
| 356 | /** | |
| 357 | * Static utility function returns the fade level of a | |
| 358 | * given vertex. This vertex must be visaulized by a Fading | |
| 359 | * Node Layout, or this function | |
| 360 | * will throw an exception. | |
| 361 | * @param v | |
| 362 | */ | |
| 363 | public boolean isHidden(Vertex v) { | |
| 364 | 0 | return ( |
| 365 | (FadingVertexLayoutData) v.getUserDatum(getFadingKey())).isHidden; | |
| 366 | } | |
| 367 | ||
| 368 | /** | |
| 369 | * Passthrough. | |
| 370 | * @see edu.uci.ics.jung.visualization.Layout#lockVertex(edu.uci.ics.jung.graph.Vertex) | |
| 371 | */ | |
| 372 | public void lockVertex(Vertex vert) { | |
| 373 | 0 | layout.lockVertex(vert); |
| 374 | ||
| 375 | 0 | } |
| 376 | ||
| 377 | /** | |
| 378 | * Passthrough. | |
| 379 | * @see edu.uci.ics.jung.visualization.Layout#unlockVertex(edu.uci.ics.jung.graph.Vertex) | |
| 380 | */ | |
| 381 | public void unlockVertex(Vertex vert) { | |
| 382 | 0 | layout.unlockVertex(vert); |
| 383 | 0 | } |
| 384 | ||
| 385 | /** | |
| 386 | * Passthrough. | |
| 387 | * @see edu.uci.ics.jung.visualization.Layout#isLocked(Vertex) | |
| 388 | */ | |
| 389 | public boolean isLocked(Vertex v) | |
| 390 | { | |
| 391 | 0 | return layout.isLocked(v); |
| 392 | } | |
| 393 | ||
| 394 | /** Simply passes through the vertex. | |
| 395 | * @see edu.uci.ics.jung.visualization.Layout#forceMove(edu.uci.ics.jung.graph.Vertex, int, int) | |
| 396 | */ | |
| 397 | public void forceMove(Vertex picked, double x, double y) { | |
| 398 | 0 | layout.forceMove(picked, x, y); |
| 399 | 0 | } |
| 400 | ||
| 401 | /** | |
| 402 | * Returns the number of levels that vertices fade through. | |
| 403 | */ | |
| 404 | public int getMaxLevel() { | |
| 405 | 0 | return fadelevels; |
| 406 | } | |
| 407 | ||
| 408 | /** Passthrough. | |
| 409 | * @see edu.uci.ics.jung.visualization.Layout#isIncremental() | |
| 410 | */ | |
| 411 | public boolean isIncremental() { | |
| 412 | 0 | return layout.isIncremental(); |
| 413 | } | |
| 414 | ||
| 415 | /** Passthrough. | |
| 416 | * @see edu.uci.ics.jung.visualization.Layout#incrementsAreDone() | |
| 417 | */ | |
| 418 | public boolean incrementsAreDone() { | |
| 419 | 0 | return layout.incrementsAreDone(); |
| 420 | } | |
| 421 | ||
| 422 | /* (non-Javadoc) | |
| 423 | * @see edu.uci.ics.jung.visualization.HasGraphLayout#getGraphLayout() | |
| 424 | */ | |
| 425 | public Layout getGraphLayout() { | |
| 426 | 0 | return this; |
| 427 | } | |
| 428 | ||
| 429 | public Point2D getLocation(ArchetypeVertex v) | |
| 430 | { | |
| 431 | 0 | return layout.getLocation(v); |
| 432 | } | |
| 433 | ||
| 434 | public Iterator getVertexIterator() | |
| 435 | { | |
| 436 | 0 | return layout.getVertexIterator(); |
| 437 | } | |
| 438 | ||
| 439 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |