1
2
3
4
5
6
7
8
9
10 package edu.uci.ics.jung.visualization;
11
12 import java.awt.Dimension;
13
14 import javax.swing.event.ChangeEvent;
15 import javax.swing.event.ChangeListener;
16 import javax.swing.event.EventListenerList;
17
18 import edu.uci.ics.jung.algorithms.layout.Layout;
19 import edu.uci.ics.jung.algorithms.layout.util.Relaxer;
20 import edu.uci.ics.jung.algorithms.layout.util.VisRunner;
21 import edu.uci.ics.jung.algorithms.util.IterativeContext;
22 import edu.uci.ics.jung.visualization.layout.ObservableCachingLayout;
23 import edu.uci.ics.jung.visualization.util.ChangeEventSupport;
24 import edu.uci.ics.jung.visualization.util.DefaultChangeEventSupport;
25
26
27
28
29
30
31
32
33 public class DefaultVisualizationModel<V, E> implements VisualizationModel<V,E>, ChangeEventSupport {
34
35 ChangeEventSupport changeSupport = new DefaultChangeEventSupport(this);
36
37
38
39
40 protected Relaxer relaxer;
41
42
43
44
45 protected Layout<V,E> layout;
46
47
48
49
50
51 protected ChangeListener changeListener;
52
53
54
55
56
57 public DefaultVisualizationModel(Layout<V,E> layout) {
58 this(layout, null);
59 }
60
61
62
63
64
65
66 public DefaultVisualizationModel(Layout<V,E> layout, Dimension d) {
67 if(changeListener == null) {
68 changeListener = new ChangeListener() {
69 public void stateChanged(ChangeEvent e) {
70 fireStateChanged();
71 }
72 };
73 }
74 setGraphLayout(layout, d);
75 }
76
77
78
79
80
81
82 public void setGraphLayout(Layout<V,E> layout, Dimension viewSize) {
83
84 if(this.layout != null && this.layout instanceof ChangeEventSupport) {
85 ((ChangeEventSupport)this.layout).removeChangeListener(changeListener);
86 }
87
88 if(layout instanceof ChangeEventSupport) {
89 this.layout = layout;
90 } else {
91 this.layout = new ObservableCachingLayout<V,E>(layout);
92 }
93
94 ((ChangeEventSupport)this.layout).addChangeListener(changeListener);
95
96 if(viewSize == null) {
97 viewSize = new Dimension(600,600);
98 }
99 Dimension layoutSize = layout.getSize();
100
101
102 if(layoutSize == null) {
103 layout.setSize(viewSize);
104 }
105 if(relaxer != null) {
106 relaxer.stop();
107 relaxer = null;
108 }
109 if(layout instanceof IterativeContext) {
110 layout.initialize();
111 if(relaxer == null) {
112 relaxer = new VisRunner((IterativeContext)this.layout);
113 relaxer.prerelax();
114 relaxer.relax();
115 }
116 }
117 fireStateChanged();
118 }
119
120
121
122
123
124 public void setGraphLayout(Layout<V,E> layout) {
125 setGraphLayout(layout, null);
126 }
127
128
129
130
131 public Layout<V,E> getGraphLayout() {
132 return layout;
133 }
134
135
136
137
138 public Relaxer getRelaxer() {
139 return relaxer;
140 }
141
142
143
144
145 public void setRelaxer(VisRunner relaxer) {
146 this.relaxer = relaxer;
147 }
148
149
150
151
152
153 public void addChangeListener(ChangeListener l) {
154 changeSupport.addChangeListener(l);
155 }
156
157
158
159
160
161 public void removeChangeListener(ChangeListener l) {
162 changeSupport.removeChangeListener(l);
163 }
164
165
166
167
168
169
170
171
172 public ChangeListener[] getChangeListeners() {
173 return changeSupport.getChangeListeners();
174 }
175
176
177
178
179
180
181
182
183
184 public void fireStateChanged() {
185 changeSupport.fireStateChanged();
186 }
187
188 }