Coverage details for edu.uci.ics.jung.visualization.FadingVertexLayout

LineHitsSource
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() {
750        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      */
960    public FadingVertexLayout(int fadelevels, Layout layout) {
970        this.fadelevels = fadelevels;
980        this.layout = layout;
990        this.hiddenNodes = new HashSet();
1000    }
101  
102     /**
103      * A pass-through to the contained Layout
104      */
105     public void initialize(Dimension d) {
106  
1070        layout.initialize(d);
108  
1090        Graph graph = layout.getGraph();
1100        for (Iterator iter = graph.getVertices().iterator(); iter.hasNext();) {
1110            ArchetypeVertex vert = (ArchetypeVertex) iter.next();
1120            vert.addUserDatum(
113                 getFadingKey(),
114                 new FadingVertexLayoutData(),
115                 UserData.REMOVE);
116         }
117  
1180    }
119  
120     public String getStatus() {
1210        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() {
1290        return layout.getVisibleEdges();
130     }
131  
132     /**
133      * A pass-through.
134      * @see Layout#getGraph()
135      */
136     public Graph getGraph() {
1370        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) {
1460        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) {
1550        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() {
1640        tick();
1650        layout.advancePositions();
1660    }
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() {
1750        Graph graph = layout.getGraph();
1760        for (Iterator iter = graph.getVertices().iterator(); iter.hasNext();) {
1770            Vertex av = (Vertex) iter.next();
1780            FadingVertexLayoutData fnvd =
179                 (FadingVertexLayoutData) av.getUserDatum(getFadingKey());
1800            if (fnvd.level < fadelevels) {
1810                fnvd.level++;
1820                if (fnvd.isHidden) {
1830                    moveOutward(av, getX(av), getY(av), 1.01);
184                 }
185             }
186         }
1870    }
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  
1970    private Object key = null;
198     public Object getFadingKey() {
1990        if (key == null)
2000            key = new Pair(this, FADINGNODEVIZX);
2010        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) {
2140        layout.applyFilter(g_int);
2150        Graph graph = layout.getGraph();
2160        Set nowShowingNodes =
217             GraphUtils.getEqualVertices(g_int.getVertices(), graph);
218  
2190        int newlyShowing = 0;
2200        int newlyHidden = 0;
221  
2220        for (Iterator iter = graph.getVertices().iterator(); iter.hasNext();) {
2230            ArchetypeVertex vert = (ArchetypeVertex) iter.next();
2240            if (nowShowingNodes.contains(vert) && hiddenNodes.contains(vert)) {
225  
226                 // v is now NEWLY SHOWING
2270                newlyShowing++;
2280                hiddenNodes.remove(vert);
2290                FadingVertexLayoutData fnvd =
230                     (FadingVertexLayoutData) vert.getUserDatum(getFadingKey());
2310                fnvd.isHidden = false;
2320                fnvd.level = 0;
233                 // the node should also be moved to near its neighbors
2340                moveVertexPrettily((Vertex) vert.getEqualVertex(g_int));
2350            } else if (
236                 !nowShowingNodes.contains(vert)
237                     && !hiddenNodes.contains(vert)) {
238                 // System.out.println(v + " " + v.getClass() );
2390                newlyHidden++;
2400                hiddenNodes.add(vert);
2410                FadingVertexLayoutData fnvd =
242                     (FadingVertexLayoutData) vert.getUserDatum(getFadingKey());
2430                fnvd.isHidden = true;
2440                fnvd.level = 0;
245                 // the node should gradually surf away from its neighbors
246             }
247         }
2480    }
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
2600        Set s = vert.getNeighbors();
2610        if (s.size() == 0)
2620            return;
2630        double x = 0;
2640        double y = 0;
265         // average location
2660        for (Iterator iter = s.iterator(); iter.hasNext();) {
2670            Vertex neighbor = (Vertex) iter.next();
2680            x += layout.getX(neighbor);
2690            y += layout.getY(neighbor);
270         }
2710        x /= s.size();
2720        y /= s.size();
2730        moveOutward(vert, x, y, 0.9);
2740    }
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) {
2860        x -= currentSize.width / 2;
2870        y -= currentSize.height / 2;
2880        x *= speed;
2890        y *= speed;
2900        x += currentSize.width / 2;
2910        y += currentSize.height / 2;
2920        forceMove(vert, x, y);
2930    }
294  
295     /** Passthrough.
296      * @see edu.uci.ics.jung.visualization.Layout#resize(java.awt.Dimension)
297      */
298     public void resize(Dimension d) {
2990        if (currentSize != null) {
3000            synchronized (currentSize) {
3010                this.currentSize = d;
3020            }
303         } else {
3040            this.currentSize = d;
305         }
3060        layout.resize(d);
3070    }
308  
309     /** Passthrough.
310      * @see edu.uci.ics.jung.visualization.Layout#restart()
311      */
312     public void restart() {
3130        layout.restart();
3140    }
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) {
3200        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) {
3270        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() {
3420        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) {
3530        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) {
3640        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) {
3730        layout.lockVertex(vert);
374  
3750    }
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) {
3820        layout.unlockVertex(vert);
3830    }
384     
385     /**
386      * Passthrough.
387      * @see edu.uci.ics.jung.visualization.Layout#isLocked(Vertex)
388      */
389     public boolean isLocked(Vertex v)
390     {
3910        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) {
3980        layout.forceMove(picked, x, y);
3990    }
400  
401     /**
402      * Returns the number of levels that vertices fade through.
403      */
404     public int getMaxLevel() {
4050        return fadelevels;
406     }
407  
408     /** Passthrough.
409      * @see edu.uci.ics.jung.visualization.Layout#isIncremental()
410      */
411     public boolean isIncremental() {
4120        return layout.isIncremental();
413     }
414  
415     /** Passthrough.
416      * @see edu.uci.ics.jung.visualization.Layout#incrementsAreDone()
417      */
418     public boolean incrementsAreDone() {
4190        return layout.incrementsAreDone();
420     }
421  
422     /* (non-Javadoc)
423      * @see edu.uci.ics.jung.visualization.HasGraphLayout#getGraphLayout()
424      */
425     public Layout getGraphLayout() {
4260        return this;
427     }
428  
429     public Point2D getLocation(ArchetypeVertex v)
430     {
4310        return layout.getLocation(v);
432     }
433  
434     public Iterator getVertexIterator()
435     {
4360        return layout.getVertexIterator();
437     }
438  
439 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.