1
28 package demo.view.networkmonitoring;
29
30 import demo.view.DemoBase;
31 import y.anim.AnimationObject;
32 import y.anim.AnimationPlayer;
33 import y.base.DataMap;
34 import y.util.DefaultMutableValue2D;
35 import y.util.Maps;
36 import y.view.BackgroundRenderer;
37 import y.view.DefaultGraph2DRenderer;
38 import y.view.EditMode;
39 import y.view.NavigationMode;
40 import y.view.ViewAnimationFactory;
41 import y.view.YRenderingHints;
42
43 import javax.swing.AbstractAction;
44 import javax.swing.Action;
45 import javax.swing.JToolBar;
46 import javax.swing.Timer;
47 import java.awt.Color;
48 import java.awt.Dimension;
49 import java.awt.EventQueue;
50 import java.awt.GradientPaint;
51 import java.awt.Graphics2D;
52 import java.awt.Paint;
53 import java.awt.Rectangle;
54 import java.awt.event.ActionEvent;
55 import java.awt.event.ActionListener;
56 import java.beans.PropertyChangeEvent;
57 import java.beans.PropertyChangeListener;
58 import java.util.Locale;
59
60
82 public class NetworkMonitoringDemo extends DemoBase {
83 static final double MAX_ZOOM = 3;
84 static final double MIN_ZOOM = 0.15;
85 static final double PAINT_DETAIL_THRESHOLD = 0.25;
86 static final double LABEL_HIDE_ZOOM_LEVEL = 0.55;
87
88 private NetworkModel model;
89 private DataMap view2model;
90
91 public NetworkMonitoringDemo() {
92 this(null);
93 }
94
95 public NetworkMonitoringDemo(final String helpFilePath) {
96 addHelpPane(helpFilePath);
97 view.setPreferredSize(new Dimension(1200, 900));
99
100 final NetworkView networkView = new NetworkView(model, view, view2model);
101 model.addObserver(networkView);
102
103 addBackgroundRenderer();
104 ((DefaultGraph2DRenderer) view.getGraph2DRenderer()).setDrawEdgesFirst(true);
105 addZoomThreshold();
106
107 final Timer timer = new Timer(500, new ActionListener() {
109 public void actionPerformed(ActionEvent e) {
110 final ViewAnimationFactory factory = new ViewAnimationFactory(view);
111 final Rectangle graphBounds = view.getGraph2D().getBoundingBox();
112 final DefaultMutableValue2D newCenter =
113 DefaultMutableValue2D.create(graphBounds.getWidth() * 0.4, graphBounds.getHeight() * 0.4);
114 final AnimationObject focus = factory.focusView(0.5, newCenter, 1000);
115 final AnimationPlayer player = factory.createConfiguredPlayer();
116 player.animate(focus);
117 }
118 });
119 timer.setRepeats(false);
120 timer.start();
121 }
122
123
127 protected void initialize() {
128 view2model = Maps.createHashedDataMap();
129 model = new NetworkModelImpl(getResource("resource/network.graphml"));
130 }
131
132
135 private void addBackgroundRenderer() {
136 view.setBackgroundRenderer(new BackgroundRenderer() {
137 public void paint(Graphics2D gfx, int x, int y, int w, int h) {
138 final Rectangle visibleRect = view.getVisibleRect();
139 final Paint oldPaint = gfx.getPaint();
141 GradientPaint gradientPaint = new GradientPaint(visibleRect.x, visibleRect.y, Color.WHITE,
143 visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height, new Color(195, 211, 238));
144 gfx.setPaint(gradientPaint);
145 gfx.fillRect(visibleRect.x, visibleRect.y, visibleRect.width, visibleRect.height);
146 gfx.setPaint(oldPaint);
148 }
149 });
150 }
151
152
155 private void addZoomThreshold() {
156 view.setPaintDetailThreshold(PAINT_DETAIL_THRESHOLD);
157 view.getCanvasComponent().addPropertyChangeListener(new PropertyChangeListener() {
158 public void propertyChange(PropertyChangeEvent evt) {
159 if (evt.getPropertyName().equals("Zoom")) {
160 final double zoom = ((Double) evt.getNewValue()).doubleValue();
161 if (zoom <= LABEL_HIDE_ZOOM_LEVEL) {
162 view.getRenderingHints().put(
163 YRenderingHints.KEY_NODE_LABEL_PAINTING,
164 YRenderingHints.VALUE_NODE_LABEL_PAINTING_OFF);
165 } else {
166 view.getRenderingHints().put(
167 YRenderingHints.KEY_NODE_LABEL_PAINTING,
168 YRenderingHints.VALUE_NODE_LABEL_PAINTING_ON);
169 }
170 if (zoom < MIN_ZOOM) {
171 view.setZoom(MIN_ZOOM);
172 } else if (zoom > MAX_ZOOM) {
173 view.setZoom(MAX_ZOOM);
174 }
175 }
176 }
177 });
178 }
179
180
183 protected EditMode createEditMode() {
184 return null;
185 }
186
187
191 protected void registerViewModes() {
192 view.addViewMode(new NavigationMode());
193 view.addViewMode(new NetworkInteractionMode(model, view2model));
194 }
195
196
199 public static void main(String[] args) {
200 EventQueue.invokeLater(new Runnable() {
201 public void run() {
202 Locale.setDefault(Locale.ENGLISH);
203 initLnF();
204 (new NetworkMonitoringDemo("resource/networkhelp.html")).start("Network Monitoring Demo");
205 }
206 });
207 }
208
209
212 protected boolean isDeletionEnabled() {
213 return false;
214 }
215
216
219 protected boolean isUndoRedoEnabled() {
220 return false;
221 }
222
223
226 protected boolean isClipboardEnabled() {
227 return false;
228 }
229
230
233 protected Action createLoadAction() {
234 return null;
235 }
236
237
240 protected Action createSaveAction() {
241 return null;
242 }
243
244
247 protected JToolBar createToolBar() {
248 final JToolBar toolBar = super.createToolBar();
249 toolBar.add(new AbstractAction("Enable Failures") {
250 public void actionPerformed(ActionEvent e) {
251 if (model.isNetworkErrorsEnabled()) {
252 putValue(Action.NAME,"Enable Failures");
253 model.setNetworkErrorsEnabled(false);
254 } else {
255 putValue(Action.NAME,"Disable Failures");
256 model.setNetworkErrorsEnabled(true);
257 }
258 }
259 });
260 return toolBar;
261 }
262 }
263