VisualizationTopologyView.java 25.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.views;

import java.awt.AWTEvent;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
29
import java.awt.Frame;
30
31
32
33
34
35
36
37
38
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
39
import java.awt.event.MouseAdapter;
40
import java.awt.event.MouseEvent;
41
import java.awt.geom.Point2D;
42
import java.util.Collection;
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
55
import javax.swing.JSplitPane;
56
57
58
59
60
61
62
63
64
65
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.Timer;

import com.google.common.collect.Maps;

import de.tud.kom.p2psim.api.linklayer.mac.Link;
import de.tud.kom.p2psim.api.linklayer.mac.MacAddress;
import de.tud.kom.p2psim.api.linklayer.mac.MacLayer;
import de.tud.kom.p2psim.api.linklayer.mac.PhyType;
66
import de.tud.kom.p2psim.api.topology.Topology;
67
68
69
70
71
72
import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.obstacles.ObstacleModel;
import de.tud.kom.p2psim.api.topology.views.TopologyView;
import de.tud.kom.p2psim.api.topology.waypoints.WaypointModel;
import de.tud.kom.p2psim.impl.topology.views.visualization.ComponentVisManager;
import de.tud.kom.p2psim.impl.topology.views.visualization.ComponentVisManager.VisInfo;
73
74
import de.tud.kom.p2psim.impl.topology.views.visualization.ComponentVisManager.VisualizationListener;
import de.tud.kom.p2psim.impl.topology.views.visualization.ui.InteractiveVisualizationComponent;
75
76
import de.tud.kom.p2psim.impl.topology.views.visualization.ui.PlottingView;
import de.tud.kom.p2psim.impl.topology.views.visualization.ui.SimControlPanel;
77
import de.tud.kom.p2psim.impl.topology.views.visualization.ui.VisualizationComponent;
78
79
80
81
82
import de.tud.kom.p2psim.impl.topology.views.visualization.world.NodeVisInteractionListener;
import de.tud.kom.p2psim.impl.topology.views.visualization.world.ObstacleComponentVis;
import de.tud.kom.p2psim.impl.topology.views.visualization.world.StrongWaypointComponentVis;
import de.tud.kom.p2psim.impl.topology.views.visualization.world.WeakWaypointComponentVis;
import de.tud.kom.p2psim.impl.util.NotSupportedException;
83
import de.tudarmstadt.maki.simonstrator.api.Binder;
84
import de.tudarmstadt.maki.simonstrator.api.Host;
Björn Richerzhagen's avatar
Björn Richerzhagen committed
85
import de.tudarmstadt.maki.simonstrator.api.Time;
86
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
87
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
88
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.LocationListener;
89
90
91
92
93
94
95
96
97
98
99
100
101
102

/**
 * A very basic visualization of a Topology (ie. positioning and movement), just
 * to ease further debugging and development of movement and obstacle-related
 * classes.
 * 
 * It is added to the Topology as a TopologyView with no PHY-Type (and therefore
 * it is never used inside a MAC). Lateron an API will be provided that allows
 * other components (analyzers) to display information using this basic
 * Visualization.
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, 19.03.2012
 */
103
public class VisualizationTopologyView extends JFrame
104
		implements TopologyView, Runnable, VisualizationListener {
105
106
107
108
109
110
111
112
113

	protected final int WORLD_X;

	protected final int WORLD_Y;

	protected final WorldPanel worldPanel;

	protected SimControlPanel simControls;

114
	protected JSplitPane splitPane;
115
116
	
	protected boolean showNodes = true;
117

118
119
120
121
122
123
124
	public static final Stroke STROKE_BASIC = new BasicStroke(0.8f);

	public static final Stroke STROKE_THICK = new BasicStroke(2f);

	public static final Font FONT_TINY = new Font("SansSerif", Font.PLAIN, 7);

	public static final Font FONT_MEDIUM = new Font("SansSerif", Font.PLAIN, 9);
Björn Richerzhagen's avatar
Björn Richerzhagen committed
125
126
	
	public static int REDRAW_INTERVAL_MS = 250;
127
128
129
130
131
132
133
134
135
136
137
138

	private ComponentVisManager visManager;

	private WeakWaypointComponentVis weakWaypointComponentVis;

	private ObstacleComponentVis obstacleComponentVis;

	private StrongWaypointComponentVis strongWaypointComponentVis;

	/**
	 * 
	 */
139
	public VisualizationTopologyView() {
140
		Binder.registerComponent(this);
141
142
143
144
		WORLD_X = (int) Binder.getComponentOrNull(Topology.class)
				.getWorldDimensions().getX();
		WORLD_Y = (int) Binder.getComponentOrNull(Topology.class)
				.getWorldDimensions().getY();
145
146
147
148
149
		worldPanel = new WorldPanel();
		visManager = new ComponentVisManager(worldPanel);

		VisualizationInjector.setVariables(this, visManager, worldPanel,
				WORLD_X, WORLD_Y);
150
151
152
153
154
155

		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(1.0);
		splitPane.setResizeWeight(1.0);

156
157
158
		worldPanel.setPreferredSize(
				new Dimension(VisualizationInjector.getWorldX(),
						VisualizationInjector.getWorldY()));
159
		this.setPreferredSize(new Dimension(800, 600));
160
161
		this.setExtendedState(Frame.MAXIMIZED_BOTH);

162
163
164
165
166
167
168
169
170
171
172
		Thread t = new Thread(this);
		t.start();
	}

	@Override
	public void run() {

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new BorderLayout());
		this.setTitle("Topology Visualization");

173
174
		visManager.addVisualizationListener(this);

175
176
177
178
		/*
		 * World-view
		 */
		JScrollPane mapScrollPanel = new JScrollPane(worldPanel,
179
180
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
181
182
183
		mapScrollPanel.getVerticalScrollBar().setUnitIncrement(50);
		mapScrollPanel.getHorizontalScrollBar().setUnitIncrement(50);
		mapScrollPanel.setBackground(Color.DARK_GRAY);
184
185
		splitPane.setLeftComponent(mapScrollPanel);
		getContentPane().add(BorderLayout.CENTER, splitPane);
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

		buildSimControls();

		getRootPane().setDoubleBuffered(true);

		/*
		 * Finally, lights!
		 */
		this.pack();
		this.setVisible(true);

		// for the movement of the line
		final Timer timer = new Timer(250, null);
		timer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				worldPanel.invalidate();
Björn Richerzhagen's avatar
Björn Richerzhagen committed
202
				timer.setDelay(REDRAW_INTERVAL_MS);
203
204
205
206
			}
		});
		timer.start();
	}
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

	@Override
	public void visualizationAdded(VisInfo visInfo) {
		/*
		 * Need demo controls?
		 */
		VisualizationComponent visComp = visInfo.getVisualizationComponent();
		if (visComp != null
				&& visComp instanceof InteractiveVisualizationComponent) {
			JComponent sidebar = ((InteractiveVisualizationComponent) visComp)
					.getSidebarComponent();
			if (sidebar != null) {
				splitPane.setRightComponent(sidebar);
				splitPane.setDividerLocation(0.8);
				splitPane.validate();
			}
		}
	}

	@Override
	public void visualizationRemoved(VisInfo visInfo) {
		// do not care
229
230
231
	}

	private void buildSimControls() {
232
		simControls = new SimControlPanel(visManager);
233
		getContentPane().add(BorderLayout.NORTH, simControls);
234
235
		getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_P, 0), "pause");
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		Action pauseAction = new AbstractAction() {
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				getSimControls().togglePlayPause();
			}
		};
		getRootPane().getActionMap().put("pause", pauseAction);
	}

	public SimControlPanel getSimControls() {
		return simControls;
	}

	@Override
	public void addedComponent(TopologyComponent comp) {
252
		worldPanel.addTopologyComponent(comp);
253
254
255
256
257
258
259
260
261
262
263
264
	}

	@Override
	public void changedWaypointModel(WaypointModel model) {
		if (weakWaypointComponentVis != null) {
			this.visManager.removeComponent(weakWaypointComponentVis);
			this.visManager.removeComponent(strongWaypointComponentVis);
			// worldPanel.remove(waypointComponentVis);
		}

		if (model == null)
			return;
265
266
		weakWaypointComponentVis = new WeakWaypointComponentVis(model);
		strongWaypointComponentVis = new StrongWaypointComponentVis(model);
267
		this.visManager.addComponent("Streets", 0, weakWaypointComponentVis);
268
269
		this.visManager.addComponent("Waypoints", 0,
				strongWaypointComponentVis);
270
271
272
273
274
275
276
277
278
279
280
		// worldPanel.add(waypointComponentVis);
	}

	@Override
	public void changedObstacleModel(ObstacleModel model) {
		if (obstacleComponentVis != null)
			this.visManager.removeComponent(obstacleComponentVis);
		// worldPanel.remove(obstacleComponentVis);

		if (model == null)
			return;
281
		obstacleComponentVis = new ObstacleComponentVis(model);
282
283
284
285
286
287
288
289
290
291
292
		this.visManager.addComponent("Buildings", 0, obstacleComponentVis);
		// worldPanel.add(obstacleComponentVis);
	}

	/*
	 * @Override public void addedObstacle(Obstacle obstacle) { if (obstacle
	 * instanceof PolygonObstacle) { ObstacleComponentVis obsVis = new
	 * ObstacleComponentVis( ((PolygonObstacle) obstacle).getAwtPolygon(),
	 * obstacle); worldPanel.add(obsVis); } }
	 */

Clemens Krug's avatar
Clemens Krug committed
293
294
295
296
297
298
299
300
301
302
303

	/**
	 * Sets a node to clicked or unclicked state
	 * @param id The node which should be affected
	 * @param clicked true if the node should be set as clicked, false otherwise
	 */
	public void toggleNodeClicked(INodeID id, boolean clicked)
	{
		worldPanel.nodeInformation.get(id).clicked = clicked;
	}

304
	@Override
305
	public void onLocationChanged(Host host, Location location) {
306
		// The NodeInformation objects are registered as listener.
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	}

	@Override
	public Link getBestNextLink(MacAddress source, MacAddress lastHop,
			MacAddress currentHop, MacAddress destination) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Link getLinkBetween(MacAddress source, MacAddress destination) {
		throw new UnsupportedOperationException();
	}

	@Override
	public MacLayer getMac(MacAddress address) {
		throw new UnsupportedOperationException();
	}

325
326
327
328
	@Override
	public Collection<MacLayer> getAllMacs() {
		throw new UnsupportedOperationException();
	}
329

330
331
332
333
334
335
336
337
338
339
	@Override
	public List<MacAddress> getNeighbors(MacAddress address) {
		throw new UnsupportedOperationException();
	}

	@Override
	public PhyType getPhyType() {
		return null;
	}

340
341
342
343
344
345
346
347
348
	/**
	 * Set to false to disable default drawing of nodes.
	 * 
	 * @param showNodes
	 */
	public void setShowNodes(boolean showNodes) {
		this.showNodes = showNodes;
	}

349
350
351
352
353
354
	/**
	 * Mini-Container for per-node information
	 * 
	 * @author bjoern
	 * @version 1.0, Jul 5, 2016
	 */
Björn Richerzhagen's avatar
Björn Richerzhagen committed
355
	public static class VisNodeInformation implements LocationListener {
356
357
358
359

		public Point2D position;

		public boolean clicked = false;
Björn Richerzhagen's avatar
Björn Richerzhagen committed
360
361
362
363
364
		
		/**
		 * No longer react to mouse clicks etc.
		 */
		public boolean disableClickListener = false;
365
366
367

		public final long hostId;

Björn Richerzhagen's avatar
Björn Richerzhagen committed
368
		public VisNodeInformation(TopologyComponent comp) {
369
370
371
372
373
374
375
376
377
378
379
380
381
			this.hostId = comp.getHost().getId().value();
			this.position = comp.getRealPosition().asPoint();
		}

		@Override
		public void onLocationChanged(Host host, Location location) {
			this.position.setLocation(
					VisualizationInjector.scaleValue(location.getLongitude()),
					VisualizationInjector.scaleValue(location.getLatitude()));
		}

	}

382
383
384
385
386
387
388
389
	/**
	 * The world
	 * 
	 * @author Bjoern Richerzhagen
	 * @version 1.0, 19.03.2012
	 */
	protected class WorldPanel extends JLayeredPane {

Björn Richerzhagen's avatar
Björn Richerzhagen committed
390
		protected HashMap<INodeID, VisNodeInformation> nodeInformation = new HashMap<INodeID, VisNodeInformation>();
391
392

		protected final static int PADDING = 16;
393
394
		
		protected final static int NODE_PAD = 2;
395

396
397
398
399
		private static final long serialVersionUID = -3023020559483652110L;

		public WorldPanel() {
			this.setLayout(null);
400
401
402
403
404
405
406
407
408
			this.setDoubleBuffered(true);
			this.addMouseListener(new MouseAdapter() {

				/**
				 * Stores the mouse position, if the mouse button is pressed
				 */
				@Override
				public void mousePressed(MouseEvent e) {
					boolean turnedSomeoneOff = false;
Björn Richerzhagen's avatar
Björn Richerzhagen committed
409
					for (VisNodeInformation node : nodeInformation.values()) {
410
411
412
413
414
415
416
417
418
419
420
421
						// Make it easier to turn things off.
						if (node.clicked && node.position
								.distance(e.getPoint()) < PADDING + 2) {
							node.clicked = !node.clicked;
							VisualizationInjector
									.notifyInteractionListenersOnClick(
											node.hostId, node.clicked);
							turnedSomeoneOff = true;
						}
					}
					if (!turnedSomeoneOff) {
						// Turn sth on (limit to one node)
Björn Richerzhagen's avatar
Björn Richerzhagen committed
422
423
424
425
						for (VisNodeInformation node : nodeInformation.values()) {
							if (node.disableClickListener) {
								continue;
							}
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
							if (!node.clicked && node.position
									.distance(e.getPoint()) < PADDING) {
								node.clicked = !node.clicked;
								VisualizationInjector
										.notifyInteractionListenersOnClick(
												node.hostId, node.clicked);
								break;
							}
						}
					}
				}

			});
		}

		public void addTopologyComponent(TopologyComponent comp) {
			if (!nodeInformation.containsKey(comp.getHost().getId())) {
Björn Richerzhagen's avatar
Björn Richerzhagen committed
443
				VisNodeInformation tVis = new VisNodeInformation(comp);
444
445
446
				comp.requestLocationUpdates(null, tVis);
				nodeInformation.put(comp.getHost().getId(), tVis);
			}
447
448
449
450
451
452
453
454
455
456
457
458
459
		}

		@Override
		public void invalidate() {
			super.invalidate();
			repaint();
		}

		@Override
		protected void paintComponent(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
460
			paintNodes(g2);
461
			super.paintComponent(g);
462
463
		}

464
		private void paintNodes(Graphics2D g2) {
Björn Richerzhagen's avatar
Björn Richerzhagen committed
465
			for (VisNodeInformation node : nodeInformation.values()) {
466
467
468
469
470
				if (node.clicked) {
					g2.setColor(Color.MAGENTA);
					g2.fillOval((int) node.position.getX() - PADDING,
							(int) node.position.getY() - PADDING,
							PADDING * 2 + 1, PADDING * 2 + 1);
471
472
473
474
475
476
477
478
				} else {
					if (showNodes) {
						// Draw nodes
						g2.setColor(Color.DARK_GRAY);
						g2.fillOval((int) node.position.getX() - NODE_PAD,
								(int) node.position.getY() - NODE_PAD,
								NODE_PAD * 2 + 1, NODE_PAD * 2 + 1);
					}
479
480
481
482
				}
			}
		}

483
484
	}

485
486
	public void notifyInteractionListenersOnClick(long hostid,
			boolean isActive) {
487
488
489
490
491
492
493
		VisualizationInjector.notifyInteractionListenersOnClick(hostid,
				isActive);
	}

	@Deprecated
	public void setPhy(String phy) {
		// legacy
494
495
		System.err.println(
				"The VisualizationTopologyView does no longer require PHY to be configured!");
496
	}
Björn Richerzhagen's avatar
Björn Richerzhagen committed
497
498
499
500
501
502
503
504
505
	
	/**
	 * Interval between redraw-operations of the visualization. 
	 * Lower interval = smoother visualizations but higher resource demands.
	 * @param redrawInterval
	 */
	public void setRedrawInterval(long redrawInterval) {
		VisualizationTopologyView.REDRAW_INTERVAL_MS = (int)(redrawInterval/Time.MILLISECOND);
	}
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

	/**
	 * This component allows the injection of JComponents into the visualization
	 * from anywhere in the simulator. It is marked deprecated due to its hacky
	 * nature. Integrate the visualization into the topology view instead.
	 * 
	 * TODO: Add checks for every component or plotting view if a graphical
	 * environment is present. This will allow calls to the
	 * VisualizationInjector on servers without failing. e.g. supply dummy a
	 * PlottingView or add additional checks to the plotting view for such
	 * cases.
	 * 
	 * @author Fabio Zöllner
	 * @version 1.0, 03.04.2012
	 */
	public static class VisualizationInjector {
522
523
524
		
		private static boolean isActive = false;
		
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
		protected static WorldPanel worldPanel;

		public static ComponentVisManager visManager;

		protected static ConcurrentLinkedQueue<VisInfo> components = new ConcurrentLinkedQueue<VisInfo>();

		protected static Map<String, VisInfo> nameToVisInfoMap = Maps
				.newConcurrentMap();

		protected static ConcurrentLinkedQueue<DisplayString> displayStrings = new ConcurrentLinkedQueue<DisplayString>();

		protected static List<MouseClickListener> mouseListeners = new CopyOnWriteArrayList<MouseClickListener>();

		protected static VisualizationTopologyView view;

		protected static ConcurrentLinkedQueue<NodeVisInteractionListener> interactionListeners = new ConcurrentLinkedQueue<NodeVisInteractionListener>();

		protected static long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK
				+ AWTEvent.MOUSE_EVENT_MASK;

545
546
547
		private static int WORLD_X = 0;

		private static int WORLD_Y = 0;
548

549
		private static double SCALE = 1;
550
551
552
553
554
555
556
557
558
559

		protected static Map<String, PlottingView> plottingViews = Maps
				.newHashMap();

		public static PlottingView createPlottingView(String name) {
			PlottingView view = new PlottingView(name);
			plottingViews.put(name, view);
			return view;
		}

560
561
		public static PlottingView createPlottingView(String name, int x,
				int y) {
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
			PlottingView view = new PlottingView(name);
			plottingViews.put(name, view);

			view.setBounds(x, y, view.getWidth(), view.getHeight());
			view.setVisible(true);

			return view;
		}

		public static PlottingView getPlottingView(String name) {
			return plottingViews.get(name);
		}

		protected static void notifyInteractionListenersOnClick(long hostid,
				boolean isActive) {
			for (NodeVisInteractionListener listener : VisualizationInjector.interactionListeners) {
				listener.onHostClick(hostid, isActive);
			}
		}
Björn Richerzhagen's avatar
Björn Richerzhagen committed
581
582
583
584
		
		public static VisNodeInformation getNodeInformation(INodeID hostId) {
			return worldPanel.nodeInformation.get(hostId);
		}
585
586
587
588
589
590
591
592
593

		protected static void setVariables(VisualizationTopologyView view,
				ComponentVisManager visManager, WorldPanel worldPanel,
				int WORLD_X, int WORLD_Y) {
			VisualizationInjector.view = view;
			VisualizationInjector.worldPanel = worldPanel;
			VisualizationInjector.visManager = visManager;
			VisualizationInjector.WORLD_X = WORLD_X;
			VisualizationInjector.WORLD_Y = WORLD_Y;
594
			VisualizationInjector.isActive = true;
595
596
597
598
599
600
601
602
603
604

			for (VisInfo visInfo : components) {
				visManager.addComponent(visInfo);
				// worldPanel.add(comp);
			}

			worldPanel.add(new DrawDisplayStrings(WORLD_X, WORLD_Y));

			setupAWTEventListener();
		}
605
606
607
608
609
610
611
612
		
		/**
		 * True if this component is active at all (can be used)
		 * @return
		 */
		public static boolean isActive() {
			return isActive;
		}
613
614
615
616
617
618
619
620
621
622
623
624
625

		public static VisualizationTopologyView getTopologyView() {
			return view;
		}

		private static void setupAWTEventListener() {
			Toolkit tk = Toolkit.getDefaultToolkit();

			tk.addAWTEventListener(new AWTEventListener() {
				@Override
				public void eventDispatched(AWTEvent e) {
					if (e instanceof MouseEvent) {
						MouseEvent me = (MouseEvent) e;
Nils Richerzhagen's avatar
Nils Richerzhagen committed
626
627
628
629
						if (me.getID() == MouseEvent.MOUSE_CLICKED
								|| me.getID() == MouseEvent.MOUSE_DRAGGED
								|| me.getID() == MouseEvent.MOUSE_PRESSED
								|| me.getID() == MouseEvent.MOUSE_RELEASED) {
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
							// Another dirty hack until we have another graphic
							// drawing system
							if (((JFrame) worldPanel.getTopLevelAncestor())
									.isActive()) {
								int x = me.getXOnScreen();
								int y = me.getYOnScreen();

								if (x > (int) worldPanel.getLocationOnScreen()
										.getX()
										&& y > (int) worldPanel
												.getLocationOnScreen().getY()
										&& x < (int) worldPanel
												.getLocationOnScreen().getX()
												+ worldPanel.getWidth()
										&& y < (int) worldPanel
												.getLocationOnScreen().getX()
												+ worldPanel.getHeight()) {

									x -= (int) worldPanel.getLocationOnScreen()
											.getX();
									y -= (int) worldPanel.getLocationOnScreen()
											.getY();

									for (MouseClickListener l : mouseListeners) {
Nils Richerzhagen's avatar
Nils Richerzhagen committed
654
655
656

										if (me.getID() == MouseEvent.MOUSE_CLICKED) {
											l.mouseClicked(x, y);
657
											l.mouseClicked(x, y, me.getModifiers());
Nils Richerzhagen's avatar
Nils Richerzhagen committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
										} else {
											if (l instanceof MouseListener) {
												switch (me.getID()) {
												case MouseEvent.MOUSE_DRAGGED:
													((MouseListener) l)
															.mouseDragged(x, y);
													break;

												case MouseEvent.MOUSE_PRESSED:
													((MouseListener) l)
															.mousePressed(x, y);
													break;

												case MouseEvent.MOUSE_RELEASED:
													((MouseListener) l)
															.mouseReleased(x,
																	y);
													break;
												default:
													break;
												}
											}
										}
681
682
683
684
685
686
687
688
689
690
691
									}
								}

							}
						}
					}
				}
			}, eventMask);
		}

		public static interface MouseClickListener {
692
693
694
695
696
697
698
699

			@Deprecated
			/**
			 * Method is deprecated. Uses mouseClicked(int x, int y, int modifier) instead.
			 */
			default void mouseClicked(int x, int y){}

			default void mouseClicked(int x, int y, int modifier) {}
700
701
		}

702
703
		public interface MouseListener extends MouseClickListener {
			void mouseDragged(int x, int y);
Nils Richerzhagen's avatar
Nils Richerzhagen committed
704

705
			void mousePressed(int x, int y);
Nils Richerzhagen's avatar
Nils Richerzhagen committed
706

707
			void mouseReleased(int x, int y);
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
		}

		public static JComponent getWorldPanel() {
			return worldPanel;
		}

		public static void addMouseListener(MouseClickListener listener) {
			mouseListeners.add(listener);
		}

		public static void removeMouseListener(MouseClickListener listener) {
			mouseListeners.remove(listener);
		}

		/**
		 * Listener is informed, whenever the user clicks on a node
		 * 
		 * @param listener
		 */
		public static void addInteractionListener(
				NodeVisInteractionListener listener) {
			interactionListeners.add(listener);
		}

		public static void removeInteractionListener(
				NodeVisInteractionListener listener) {
			interactionListeners.remove(listener);
		}

		protected static class DrawDisplayStrings extends JComponent {
			public DrawDisplayStrings(int WORLD_X, int WORLD_Y) {
				super();

				setBounds(0, 0, WORLD_X, WORLD_Y);
				setOpaque(false);
				setVisible(true);
			}

			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				Graphics2D g2 = (Graphics2D) g;

				g2.setColor(Color.BLACK);
				g2.setFont(new Font("SansSerif", Font.PLAIN, 9));

				int height = 10;
				for (DisplayString str : displayStrings) {
					g2.drawString(str.getDisplayString(), 0, height);
					height += 10;
				}
			}
		}

762
763
764
765
766
767
		/**
		 * Returns a SCALED x-length of the world size, only to be used by
		 * visualizations.
		 * 
		 * @return
		 */
768
		public static int getWorldX() {
769
770
			// return WORLD_X;
			return (int) (WORLD_X * getScale());
771
772
		}

773
774
775
776
777
778
		/**
		 * Returns a SCALED y-length of the world size, only to be used by
		 * visualizations.
		 * 
		 * @return
		 */
779
		public static int getWorldY() {
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
			// return WORLD_Y;
			return (int) (WORLD_Y * getScale());
		}

		/**
		 * Still needed by OSM-loader (Fabio), however: should not be used
		 * anymore. The coordinates should be final.
		 * 
		 * @param x
		 * @param y
		 */
		@Deprecated
		public static void setWorldCoordinates(int x, int y) {
			WORLD_X = x;
			WORLD_Y = y;
		}

		public static double getScale() {
			return SCALE;
		}

		public static void setScale(double scale) {
			SCALE = scale;
		}

		public static int scaleValue(double value) {
			return (int) (value * getScale());
807
808
		}

809
810
811
812
813
814
		/**
		 * Recommended way to add a custom visualization layer.
		 * 
		 * @param comp
		 */
		public static void injectComponent(VisualizationComponent comp) {
815
816
817
			if (!isActive()) {
				return;
			}
818
819
820
821
822
823
824
825
			visManager.addComponent(comp);
		}

		/**
		 * @deprecated use injectComponent with a {@link VisualizationComponent}
		 *             instead
		 */
		@Deprecated
826
827
828
829
830
		public static void injectComponent(String name, int priority,
				JComponent component) {
			injectComponent(name, priority, component, true);
		}

831
832
833
834
835
		/**
		 * @deprecated use injectComponent with a {@link VisualizationComponent}
		 *             instead
		 */
		@Deprecated
836
837
		public static void injectComponent(String name, int priority,
				JComponent component, boolean active) {
838
839
840
			injectComponent(name, priority, component, active, true);
		}

841
842
843
844
845
		/**
		 * @deprecated use injectComponent with a {@link VisualizationComponent}
		 *             instead
		 */
		@Deprecated
846
847
		public static void injectComponent(String name, int priority,
				JComponent component, boolean active, boolean showInList) {
848
849
			VisInfo visInfo = new VisInfo(name, priority, component);
			visInfo.setActiveByDefault(active);
850
			visInfo.setShowInList(showInList);
851
852
853
854
855
856
857
858
859

			components.add(visInfo);
			nameToVisInfoMap.put(name, visInfo);

			if (visManager != null) {
				visManager.addComponent(visInfo);
			}
		}

860
861
862
863
864
		/**
		 * @deprecated use removeInjectedComponent with a
		 *             {@link VisualizationComponent} instead
		 */
		@Deprecated
865
866
867
868
869
870
871
872
873
874
875
		public static void removeInjectedComponent(String name) {
			if (nameToVisInfoMap.containsKey(name)) {
				VisInfo visInfo = nameToVisInfoMap.get(name);
				nameToVisInfoMap.remove(name);
				components.remove(visInfo);
				visManager.removeComponent(name);
			}
		}

		public static void invalidate() {
			worldPanel.invalidate();
Björn Richerzhagen's avatar
Björn Richerzhagen committed
876
			view.splitPane.revalidate();
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
		}

		public static void addDisplayString(DisplayString str) {
			displayStrings.add(str);
		}

		public static void removeDisplayString(DisplayString str) {
			displayStrings.remove(str);
		}

		public static interface DisplayString {
			public String getDisplayString();
		}
	}

	@Override
893
	public Location getPosition(MacAddress address) {
894
895
896
897
898
899
900
901
		throw new NotSupportedException();
	}

	@Override
	public double getDistance(MacAddress addressA, MacAddress addressB) {
		throw new NotSupportedException();
	}

902
903
904
905
906
	@Override
	public boolean hasRealLinkLayer() {
		throw new NotSupportedException();
	}

907
}