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

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 Oct 8, 2004
9  *
10  */
11 package edu.uci.ics.jung.visualization;
12  
13 import java.awt.Dimension;
14 import java.io.FileInputStream;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Set;
24  
25 import javax.swing.event.ChangeEvent;
26 import javax.swing.event.ChangeListener;
27  
28 import edu.uci.ics.jung.graph.Vertex;
29 import edu.uci.ics.jung.utils.ChangeEventSupport;
30 import edu.uci.ics.jung.utils.DefaultChangeEventSupport;
31 import edu.uci.ics.jung.utils.Pair;
32 import edu.uci.ics.jung.utils.UserData;
33  
34 /**
35  * Implementation of PersistentLayout.
36  * Defers to another layout until 'restore' is called,
37  * then it uses the saved vertex locations
38  *
39  * @author Tom Nelson - RABA Technologies
40  *
41  *
42  */
43 public class PersistentLayoutImpl extends LayoutDecorator
44     implements PersistentLayout {
45  
460    protected ChangeEventSupport changeSupport =
47         new DefaultChangeEventSupport(this);
48  
49     /**
50      * a container for Vertices
51      */
52     protected Map map;
53  
54     /**
55      * a key for this class
56      */
57     protected Object key;
58  
59     /**
60      * a collection of Vertices that should not move
61      */
62     protected Set dontmove;
63  
64     /**
65      * whether the graph is locked (stops the VisualizationViewer rendering thread)
66      */
67     protected boolean locked;
68  
690    private static final Object BASE_KEY = "edu.uci.ics.jung.Base_Visualization_Key";
70     
71     protected RadiusGraphElementAccessor elementAccessor;
72  
73     /**
74      * create an instance with a passed layout
75      * create containers for graph components
76      * @param layout
77      */
78     public PersistentLayoutImpl(Layout layout) {
790        super(layout);
800        this.map = new HashMap();
810        this.dontmove = new HashSet();
820        this.elementAccessor = new RadiusGraphElementAccessor(layout);
830        if(layout instanceof ChangeEventSupport) {
840            ((ChangeEventSupport)layout).addChangeListener(new ChangeListener() {
85                 public void stateChanged(ChangeEvent e) {
86                     fireStateChanged();
87                 }
88             });
89         }
900    }
91  
92     /**
93      * This method calls <tt>initialize_local_vertex</tt> for each vertex, and
94      * also adds initial coordinate information for each vertex. (The vertex's
95      * initial location is set by calling <tt>initializeLocation</tt>.
96      */
97     protected void initializeLocations() {
980        for (Iterator iter = getGraph().getVertices().iterator(); iter
990                .hasNext();) {
1000            Vertex v = (Vertex) iter.next();
101  
1020            Coordinates coord = (Coordinates) v.getUserDatum(getBaseKey());
1030            if (coord == null) {
1040                coord = new Coordinates();
1050                v.addUserDatum(getBaseKey(), coord, UserData.REMOVE);
106             }
1070            if (!dontmove.contains(v))
1080                initializeLocation(v, coord, getCurrentSize());
109         }
1100    }
111  
112  
113     /**
114      * Sets persisted location for a vertex within the dimensions of the space.
115      * If the vertex has not been persisted, sets a random location. If you want
116      * to initialize in some different way, override this method.
117      *
118      * @param v
119      * @param coord
120      * @param d
121      */
122     protected void initializeLocation(Vertex v, Coordinates coord, Dimension d) {
123         double x;
124         double y;
1250        Point point = (Point) map.get(new Integer(v.hashCode()));
1260        if (point == null) {
1270            x = Math.random() * d.getWidth();
1280            y = Math.random() * d.getHeight();
129         } else {
1300            x = point.x;
1310            y = point.y;
132         }
1330        coord.setX(x);
1340        coord.setY(y);
1350    }
136  
137     /**
138      * save the Vertex locations to a file
139      * @param fileName the file to save to
140      * @throws an IOException if the file cannot be used
141      */
142     public void persist(String fileName) throws IOException {
1430        Set set = getGraph().getVertices();
1440        for (Iterator iterator = set.iterator(); iterator.hasNext();) {
1450            Vertex v = (Vertex) iterator.next();
1460            Point p = new Point(getX(v), getY(v));
1470            map.put(new Integer(v.hashCode()), p);
148         }
1490        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
150                 fileName));
1510        oos.writeObject(map);
1520        oos.close();
1530    }
154  
155     /**
156      * Restore the graph Vertex locations from a file
157      * @param fileName the file to use
158      * @throws IOException for file problems
159      * @throws ClassNotFoundException for classpath problems
160      */
161     public void restore(String fileName) throws IOException,
162             ClassNotFoundException {
1630        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
164                 fileName));
1650        map = (Map) ois.readObject();
1660        ois.close();
1670        initializeLocations();
1680        locked = true;
1690    }
170  
171     public void lock(boolean locked) {
1720        this.locked = locked;
1730    }
174  
175     /*
176      * (non-Javadoc)
177      *
178      * @see edu.uci.ics.jung.visualization.Layout#incrementsAreDone()
179      */
180     public boolean incrementsAreDone() {
1810        return locked;
182     }
183  
184     /*
185      * (non-Javadoc)
186      *
187      * @see edu.uci.ics.jung.visualization.Layout#lockVertex(edu.uci.ics.jung.graph.Vertex)
188      */
189     public void lockVertex(Vertex v) {
1900        dontmove.add(v);
1910        delegate.lockVertex(v);
1920    }
193  
194     /*
195      * (non-Javadoc)
196      *
197      * @see edu.uci.ics.jung.visualization.Layout#unlockVertex(edu.uci.ics.jung.graph.Vertex)
198      */
199     public void unlockVertex(Vertex v) {
2000        dontmove.remove(v);
2010        delegate.unlockVertex(v);
2020    }
203  
204     /**
205      * Returns a visualization-specific key (that is, specific to
206      * the layout in use) that can be used to access
207      * UserData related to the <tt>AbstractLayout</tt>.
208      */
209     public Object getBaseKey() {
2100        if (key == null)
2110            key = new Pair(delegate, BASE_KEY);
2120        return key;
213     }
214     
215     public void update() {
2160        if(delegate instanceof LayoutMutable) {
2170            ((LayoutMutable)delegate).update();
218         }
2190    }
220  
221     public void addChangeListener(ChangeListener l) {
2220        if(delegate instanceof ChangeEventSupport) {
2230            ((ChangeEventSupport)delegate).addChangeListener(l);
224         }
2250        changeSupport.addChangeListener(l);
2260    }
227  
228     public void removeChangeListener(ChangeListener l) {
2290        if(delegate instanceof ChangeEventSupport) {
2300            ((ChangeEventSupport)delegate).removeChangeListener(l);
231         }
2320        changeSupport.removeChangeListener(l);
2330    }
234  
235     public ChangeListener[] getChangeListeners() {
2360        return changeSupport.getChangeListeners();
237     }
238  
239     public void fireStateChanged() {
2400        changeSupport.fireStateChanged();
2410    }
242 }

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.