Coverage details for edu.uci.ics.jung.visualization.contrib.TreeLayout

LineHitsSource
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  
370    public static int DEFAULT_DISTX = 50;
380    public static int DEFAULT_DISTY = 50;
39    // private static Logger logger = Logger.getLogger(TreeLayout.class.getName());
40  
41     public static Vector getAtomics(Vertex p) {
420        Vector v = new Vector();
430        getAtomics(p, v);
440        return v;
45     }
46  
47     private static void getAtomics(Vertex p, Vector v) {
480        for (Iterator i = p.getSuccessors().iterator(); i.hasNext();) {
490            Vertex c = (Vertex) i.next();
500            if (c.getSuccessors().isEmpty()) {
510                v.add(c);
52             } else {
530                getAtomics(c, v);
54             }
55         }
560    }
570    private transient Set allreadyDone = new HashSet();
58 // private transient int currentSiblings_;
59  
600    private int distX = DEFAULT_DISTX;
610    private int distY = DEFAULT_DISTY;
620    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) {
700        super(g);
71         //logger.info("Constructor for TreeLayout called" + g.getClass());
72         //logger.info("Setting root Node" + g.getRoot());
730        this.m_rootVertex = g.getRoot();
740    }
75  
76     public TreeLayout(SparseTree g, int distx) {
770        super(g);
78         //logger.info("Constructor for TreeLayout called" + g.getClass());
79         //logger.info("Setting root Node" + g.getRoot());
800        this.m_rootVertex = g.getRoot();
810        this.distX = distx;
820    }
83  
84     public TreeLayout(SparseTree g, int distx, int disty) {
850        super(g);
86         //logger.info("Constructor for TreeLayout called" + g.getClass());
87         //logger.info("Setting root Node" + g.getRoot());
880        this.m_rootVertex = g.getRoot();
890        this.distX = distx;
900        this.distY = disty;
910    }
92  
93     /**
94      * ?
95      *
96      * @see edu.uci.ics.jung.visualization.Layout#advancePositions()
97      */
98     public void advancePositions() {
99         ////logger.info("method called");
1000    }
101  
102     public void applyFilter(Graph g) {
103         //logger.info("method called");
1040        super.applyFilter(g);
1050    }
106     void buildTree() {
1070        this.m_currentPoint = new Point(this.getCurrentSize().width / 2, 20);
1080        if (m_rootVertex != null && getGraph() != null) {
109             //logger.info("BUILDTREE called");
110             //int size =
1110            calculateDimensionX(m_rootVertex);
112             //logger.info("The tree has got a x-dimension of:" + size);
1130            buildTree(m_rootVertex, this.m_currentPoint.x);
114         }
1150    }
116  
117     void buildTree(Vertex v, int x) {
118  
1190        if (!allreadyDone.contains(v)) {
1200            allreadyDone.add(v);
121  
122             //go one level further down
1230            this.m_currentPoint.y += this.distY;
1240            this.m_currentPoint.x = x;
125  
1260            this.setCurrentPositionFor(v);
127  
1280            int sizeXofCurrent = ((Integer) v.getUserDatum(this
129                     .getDimensionBaseKey())).intValue();
130  
1310            int lastX = x - sizeXofCurrent / 2;
132  
133             int sizeXofChild;
134             int startXofChild;
135  
1360            for (Iterator j = v.getSuccessors().iterator(); j.hasNext();) {
1370                Vertex element = (Vertex) j.next();
1380                sizeXofChild = ((Integer) element.getUserDatum(this
139                         .getDimensionBaseKey())).intValue();
1400                startXofChild = lastX + sizeXofChild / 2;
1410                buildTree(element, startXofChild);
1420                lastX = lastX + sizeXofChild + distX;
143             }
1440            this.m_currentPoint.y -= this.distY;
145         }
1460    }
147     private int calculateDimensionX(Vertex v) {
148         //logger.info("calculating dimension for vertex " + v);
1490        int size = 0;
1500        int childrenNum = v.getSuccessors().size();
151         //logger.info("vertex " + v + " has got " + childrenNum + " successors");
1520        if (childrenNum != 0) {
153             Vertex element;
1540            for (Iterator iter = v.getSuccessors().iterator(); iter.hasNext();)
155 {
1560                element = (Vertex) iter.next();
1570                size += calculateDimensionX(element) + distX;
158             }
159         }
1600        size = Math.max(0, size - distX);
1610        v.setUserDatum(this.getDimensionBaseKey(), new Integer(size),
162                 UserData.REMOVE);
163         //logger.info("dimension for vertex " + v + " is " + size);
1640        return size;
165     }
166  
167     public int getDepth(Vertex v) {
1680        int depth = 0;
1690        for (Iterator i = v.getSuccessors().iterator(); i.hasNext();) {
1700            Vertex c = (Vertex) i.next();
1710            if (c.getSuccessors().isEmpty()) {
1720                depth = 0;
173             } else {
1740                depth = Math.max(depth, getDepth(c));
175             }
176         }
177  
1780        return depth + 1;
179     }
180  
181     private Object getDimensionBaseKey() {
1820        if (m_dimensionKey == null) {
1830            m_dimensionKey = new Pair(this, C_DIMENSION_X_BASE_KEY);
184         }
1850        return m_dimensionKey;
186     }
187     /**
188      * @return Returns the rootVertex_.
189      */
190     public Vertex getRootVertex() {
1910        return m_rootVertex;
192     }
193  
194     /**
195      * ?
196      *
197      * @see edu.uci.ics.jung.visualization.Layout#incrementsAreDone()
198      */
199     public boolean incrementsAreDone() {
2000        return true;
201     }
202     public void initialize(Dimension size) {
203         //logger.info("method called " + size);
2040        super.initialize(size);
2050        buildTree();
2060    }
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");
2230    }
224  
225     protected void initializeLocations() {
2260        for (Iterator iter = this.getGraph().getVertices().iterator(); iter
2270                .hasNext();) {
2280            Vertex v = (Vertex) iter.next();
229  
2300            Coordinates coord = (Coordinates) v.getUserDatum(getBaseKey());
2310            if (coord == null) {
2320                coord = new Coordinates();
2330                v.addUserDatum(getBaseKey(), coord, UserData.REMOVE);
234             }
2350            initialize_local_vertex(v);
236         }
237         //logger.info("we have " + getVisibleVertices().size()
238         // + " visible vertices in our graph");
2390    }
240  
241     /**
242      * ?
243      *
244      * @see edu.uci.ics.jung.visualization.Layout#isIncremental()
245      */
246     public boolean isIncremental() {
2470        return false;
248     }
249  
250     private void setCurrentPositionFor(Vertex vertex) {
2510        Coordinates coord = getCoordinates(vertex);
252         //logger.info("coordinates for vertex before change" + vertex + "("
253              // + (int) coord.getX() + "," + (int) coord.getY() + ")");
2540        coord.setX(m_currentPoint.x);
2550        coord.setY(m_currentPoint.y);
256         //logger.info("coordinates for vertex after change" + vertex + "("
257           // + (int) coord.getX() + "," + (int) coord.getY() + ")");
2580    }
259  
260     /**
261      * @param rootVertex_
262      * The rootVertex_ to set.
263      */
264     public void setRootVertex(Vertex rootVertex_) {
2650        this.m_rootVertex = rootVertex_;
2660        m_currentVertex = rootVertex_;
2670    }
268 }

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.