NodeInfoComponentVis.java 9.38 KB
Newer Older
1
2
3
4
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
5
 *
6
7
8
9
 * 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.
10
 *
11
12
13
14
 * 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.
15
 *
16
17
18
19
20
21
22
 * 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.visualization.world;

23
24
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
25
26
27
28
29
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
30
import java.util.ArrayList;
31
32
33
import java.util.Collection;
import java.util.LinkedList;

34
import javax.swing.JCheckBoxMenuItem;
35
import javax.swing.JComponent;
36
37
38
import javax.swing.JMenu;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
39
40

import de.tud.kom.p2psim.api.common.SimHost;
41
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
Björn Richerzhagen's avatar
Björn Richerzhagen committed
42
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisNodeInformation;
43
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
44
import de.tud.kom.p2psim.impl.topology.views.visualization.ui.VisualizationComponent;
45
46
47
48
49
50
51
52
53
54
55
56
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Oracle;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponent;
import de.tudarmstadt.maki.simonstrator.api.component.overlay.NodeInformation;

/**
 * Generic component that visualizes information from nodes implementing the
 * {@link NodeInformation} interface.
57
 *
58
59
60
 * @author Bjoern Richerzhagen
 * @version 1.0, Jul 9, 2015
 */
61
public class NodeInfoComponentVis extends JComponent
62
implements VisualizationComponent {
63
64
65

	protected Collection<NodeVis> nodes = new LinkedList<>();

66
	private JMenu menu = new JMenu("Node Info");
67

68
69
	protected boolean[] activeLayers = new boolean[0];

Björn Richerzhagen's avatar
Björn Richerzhagen committed
70
	boolean hideInactiveNodes = false;
71

72
	private final String name;
73

74
75
	public <T extends HostComponent> NodeInfoComponentVis(
			final Class<T> componentClass) {
76
77
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
78
79
		setOpaque(true);
		setVisible(true);
80
81
		this.name = componentClass.getSimpleName();
		menu.setText("Info: "+name);
82

83
84
85
86
87
88
89
90
91
92
93
94
95
		Event.scheduleWithDelay(1 * Time.MICROSECOND, new EventHandler() {
			@Override
			public void eventOccurred(Object content, int type) {
				for (Host host : Oracle.getAllHosts()) {
					try {
						HostComponent c = host.getComponent(componentClass);
						if (c instanceof NodeInformation) {
							nodes.add(new NodeVis(host, (NodeInformation) c));
						}
					} catch (ComponentNotAvailableException e) {
						// don't care
					}
				}
96
				initializeMenu();
97
98
99
			}
		}, null, 0);
	}
100

Björn Richerzhagen's avatar
Björn Richerzhagen committed
101
102
103
104
105
106
107
	/**
	 * Hide rings for inactive nodes.
	 * @param hideOfflineNodes
	 */
	public void setHideInactiveNodes(boolean hideInactiveNodes) {
		this.hideInactiveNodes = hideInactiveNodes;
	}
108
109
110
111
112
113
114

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
115
		boolean first = true;
116
		for (NodeVis vis : new ArrayList<>(nodes)) {
117
			vis.draw(g2);
118
119
120
121
			if (first) {
				first = false;
				vis.drawLegend(g2);
			}
122
123
124
		}
	}

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	protected void initializeMenu() {
		for (NodeVis vis : nodes) {
			String[] dimensions = vis.nodeInfo
					.getNodeColorDimensionDescriptions();
			activeLayers = new boolean[dimensions.length];
			for (int dim = 0; dim < dimensions.length; dim++) {
				JCheckBoxMenuItem item = new JCheckBoxMenuItem(
						dimensions[dim]);
				item.setSelected(true);
				item.addChangeListener(new ChangeListenerImpl(dim));
				menu.add(item);
				activeLayers[dim] = true;
			}
			break;
		}
	}

	private class ChangeListenerImpl implements ChangeListener {

		private final int dim;

		public ChangeListenerImpl(int dim) {
			this.dim = dim;
		}

		@Override
		public void stateChanged(ChangeEvent e) {
			activeLayers[dim] = !activeLayers[dim];
		}

	}

157
158
	/**
	 * Visualization-fragments for Node-centric visualiation-information.
159
	 *
160
161
162
163
	 * @author Bjoern Richerzhagen
	 * @version 1.0, Sep 22, 2013
	 */
	private class NodeVis {
164

Björn Richerzhagen's avatar
Björn Richerzhagen committed
165
		public final VisNodeInformation visNodeInfo;
166

167
		public final NodeInformation nodeInfo;
168
169
170
171

		private final SimHost host;

		private final PositionVector loc;
172

173
		private final Color activeGreen = new Color(0, 200, 0);
174

175
176
177
		private final Color[] baseColors = { Color.ORANGE, Color.BLUE, Color.RED,
				Color.PINK, Color.GRAY, Color.GREEN, Color.CYAN,
				Color.WHITE };
178
179

		private final Color[] tuColors = {
180
181
				new Color(93, 133, 195), // 1a
				new Color(80, 182, 149), // 3a
182
				//				new Color(221,223,72), // 5a
183
184
185
186
187
				new Color(248,186,60), // 7a
				new Color(233,80,62), // 9a
				new Color(128, 69, 151), // 11a
				new Color(0, 78, 138), // 1c
				new Color(0, 136, 119), // 3c
188
				//				new Color(177, 189, 0), // 5c
189
190
191
				new Color(210, 135, 0), // 7c
				new Color(185, 15, 34), // 9c
				new Color(97, 28, 115), // 11c
192
193
		};

194
		private Color[][] colors = null;
195
196
197
198
199

		public NodeVis(Host host, NodeInformation nodeInfo) {
			this.nodeInfo = nodeInfo;
			this.host = (SimHost) host;
			this.loc = this.host.getTopologyComponent().getRealPosition();
Björn Richerzhagen's avatar
Björn Richerzhagen committed
200
			this.visNodeInfo = VisualizationInjector.getNodeInformation(host.getId());
201

202
203
204
205
206
207
208
209
210
211
212
213
214
			/*
			 * Create per-info-option colors by deriving the color from the base color
			 */
			colors = new Color[nodeInfo.getNodeColorDimensions()][];
			for (int dim = 0; dim < colors.length; dim++) {
				Color baseColor = baseColors[dim];
				int dimensionSize = nodeInfo
						.getNodeColorDescriptions(dim).length;
				colors[dim] = new Color[dimensionSize];
				/*
				 * http://stackoverflow.com/questions/2355157/dynamically-
				 * creating-colors-with-different-brightness
				 */
215
216
				//				float hsbVals[] = Color.RGBtoHSB(baseColor.getRed(),
				//						baseColor.getGreen(), baseColor.getBlue(), null);
217
218
				for (int i = 0; i < dimensionSize; i++) {
					float hue = i / (float) dimensionSize;
219
220
					//					colors[dim][i] = Color.getHSBColor(hue, hsbVals[1],
					//							hsbVals[2]);
221
222
223
					colors[dim][i] = tuColors[i];
				}
			}
224

225
226
227
228
229
		}

		/**
		 * Called on one of the nodes to draw global objects such as a legend.
		 * Called before draw.
230
		 *
231
232
233
234
		 * @param g2
		 */
		public void drawLegend(Graphics2D g2) {
			String[] dimensions = nodeInfo.getNodeColorDimensionDescriptions();
235

236
237
238
			int segments = dimensions.length;
			int segmentDegrees = (int) (360 / (double) segments);
			int arcSize = 8;
239

240
241
			g2.setStroke(new BasicStroke(3));
			for (int color = 0; color < segments; color++) {
242
				if (activeLayers.length <= color || !activeLayers[color]) {
243
244
					continue;
				}
245
				g2.setColor(Color.DARK_GRAY);
246
247
				g2.drawArc(10, 20 * (color + 1)+10, 2*arcSize, 2*arcSize, color*segmentDegrees, segmentDegrees);
				String[] colorDescs = nodeInfo.getNodeColorDescriptions(color);
248
				for (int i = 0; i < colorDescs.length; i++) {
249
250
					g2.setColor(colors[color][i]);
					g2.fillRect(40 + i * 90, 20 * (color + 1)+10, 8, 8);
251
					g2.setColor(Color.DARK_GRAY);
252
253
					g2.drawString(colorDescs[i], 50 + i * 90,
							20 * (color + 2));
254
255
				}
			}
256
			g2.setStroke(new BasicStroke(1));
257
258
259
		}

		public void draw(Graphics2D g2) {
260

Björn Richerzhagen's avatar
Björn Richerzhagen committed
261
262
263
264
265
			if (hideInactiveNodes && !nodeInfo.isActive()) {
				visNodeInfo.disableClickListener = true;
				return;
			}
			visNodeInfo.disableClickListener = false;
266

267
			Point center = loc.asPoint();
268
269
270
			// Draw active (green) over underlay vis.
			g2.setColor(nodeInfo.isActive() ? activeGreen : Color.LIGHT_GRAY);
			int radius = 3;
271
			g2.drawOval(VisualizationInjector.scaleValue(center.x) - radius, VisualizationInjector.scaleValue(center.y) - radius, radius * 2,
272
					radius * 2);
273

274
275
276
			if (!nodeInfo.isActive()) {
				return;
			}
277

278
279
280
281
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
					1.0f));


282
283
284
			int segments = nodeInfo.getNodeColorDimensions();
			int segmentDegrees = (int) (360 / (double) segments);
			int arcSize = 8;
285

286
287
288
289
			g2.setStroke(new BasicStroke(8, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
			for (int dim = 0; dim < segments; dim++) {
				int value = nodeInfo.getNodeColor(dim);
				if (value < 0 || !activeLayers[dim]) {
290
291
					continue;
				}
292
				g2.setColor(colors[dim][value]);
293
				g2.drawArc(VisualizationInjector.scaleValue(center.x)-arcSize, VisualizationInjector.scaleValue(center.y)-arcSize, 2*arcSize, 2*arcSize, dim*segmentDegrees, segmentDegrees);
294
			}
295
			g2.setStroke(new BasicStroke(1));
296

297
298
299
300
301
302
			String nodeDesc = nodeInfo.getNodeDescription();
			g2.drawString(nodeDesc, center.x + 4, center.y + 4);
		}

	}

303
304
305
306
307
308
309
310
311
312
313
314
315
316
	@Override
	public JComponent getComponent() {
		return this;
	}

	@Override
	public JMenu getCustomMenu() {
		return menu;
	}

	@Override
	public boolean isHidden() {
		return false;
	}
317

318
319
	@Override
	public String getDisplayName() {
320
		return "Info: "+name;
321
322
	}

323
}