1
2
3
4
5
6
7
8
9
10
11 package edu.uci.ics.jung.visualization.layout;
12
13 import java.awt.Dimension;
14 import java.awt.geom.Point2D;
15 import java.io.FileInputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.io.Serializable;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24
25 import com.google.common.base.Function;
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.collect.Maps;
28
29 import edu.uci.ics.jung.algorithms.layout.Layout;
30 import edu.uci.ics.jung.visualization.util.Caching;
31 import edu.uci.ics.jung.visualization.util.ChangeEventSupport;
32
33
34
35
36
37
38
39
40
41
42
43 public class PersistentLayoutImpl<V, E> extends ObservableCachingLayout<V,E>
44 implements PersistentLayout<V,E>, ChangeEventSupport, Caching {
45
46
47
48
49 protected Map<V, Point> locations;
50
51
52
53
54 protected Set<V> dontmove;
55
56
57
58
59 protected boolean locked;
60
61
62
63
64
65
66 public PersistentLayoutImpl(Layout<V,E> layout) {
67 super(layout);
68 this.locations = Maps.asMap(
69 ImmutableSet.copyOf(layout.getGraph().getVertices()),
70 new RandomPointFactory<V>(getSize()));
71 this.dontmove = new HashSet<V>();
72 }
73
74
75
76
77
78
79 protected void initializeLocations() {
80 for(V v : getGraph().getVertices()) {
81 Point2D coord = delegate.apply(v);
82 if (!dontmove.contains(v))
83 initializeLocation(v, coord);
84 }
85 }
86
87
88
89
90
91
92
93
94
95
96 protected void initializeLocation(V v, Point2D coord) {
97 Point point = locations.get(v);
98 coord.setLocation(point.x, point.y);
99 }
100
101
102
103
104
105
106 public void persist(String fileName) throws IOException {
107
108 for(V v : getGraph().getVertices()) {
109 Point p = new Point(transform(v));
110 locations.put(v, p);
111 }
112 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
113 fileName));
114 oos.writeObject(locations);
115 oos.close();
116 }
117
118
119
120
121
122
123
124 @SuppressWarnings("unchecked")
125 public void restore(String fileName) throws IOException,
126 ClassNotFoundException {
127 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
128 fileName));
129 locations = (Map<V, Point>) ois.readObject();
130 ois.close();
131 initializeLocations();
132 locked = true;
133 fireStateChanged();
134 }
135
136 public void lock(boolean locked) {
137 this.locked = locked;
138 }
139
140
141
142
143
144
145 public boolean done() {
146 return super.done() || locked;
147 }
148
149
150
151
152
153
154 public void lock(V v, boolean state) {
155 dontmove.add(v);
156 delegate.lock(v, state);
157 }
158
159 @SuppressWarnings("serial")
160 public static class RandomPointFactory<V> implements Function<V,Point>, Serializable {
161
162 Dimension d;
163 public RandomPointFactory(Dimension d) {
164 this.d = d;
165 }
166 public edu.uci.ics.jung.visualization.layout.PersistentLayout.Point apply(V v) {
167 double x = Math.random() * d.width;
168 double y = Math.random() * d.height;
169 return new Point(x,y);
170 }
171 }
172
173 }