SocialViewComponentVis.java 6.12 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
29
30
31
32
33
34
35
36
37
/*
 * 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.visualization.world;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;

import javax.swing.JCheckBox;
38
import javax.swing.JCheckBoxMenuItem;
39
import javax.swing.JComponent;
40
import javax.swing.JMenu;
41
42
43
44
45
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.topology.social.SocialView;
46
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
47
48
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector.MouseClickListener;
49
import de.tud.kom.p2psim.impl.topology.views.visualization.ui.VisualizationComponent;
50
51
52
53
54
55
56

/**
 * This component draw the Social connections.
 * 
 * @author Christoph Muenker
 * @version 1.0, 22.06.2013
 */
57
58
public class SocialViewComponentVis extends JComponent
		implements MouseClickListener, VisualizationComponent {
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

	private static Vector<Color> CLUSTER_COLOR = new Vector<Color>();
	static {
		CLUSTER_COLOR.add(Color.BLUE);
		CLUSTER_COLOR.add(Color.GREEN);
		CLUSTER_COLOR.add(new Color(1f, 0.27f, 0f)); // OrangeRed
		CLUSTER_COLOR.add(Color.CYAN);
		CLUSTER_COLOR.add(Color.RED);
		CLUSTER_COLOR.add(Color.YELLOW);
	}

	private SocialView view;

	private Map<SimHost, PositionVector> posVecs = new HashMap<SimHost, PositionVector>();

	private Map<Color, Set<SimHost>> clusterMap = new HashMap<Color, Set<SimHost>>();

76
	private JMenu customMenu;
77
78
79
80
81
82
83
84
85
86
87
88
89

	private SimHost selectedHost = null;

	private boolean showCluster = false;

	private boolean showRelationship = false;

	public SocialViewComponentVis(SocialView view) {
		this.view = view;

		int i = 0;
		for (Set<SimHost> cluster : view.getClusters()) {
			for (SimHost host : cluster) {
90
91
				posVecs.put(host,
						host.getTopologyComponent().getRealPosition());
92
93
94
95
96
97
98
			}
			if (i < CLUSTER_COLOR.size()) {
				clusterMap.put(CLUSTER_COLOR.get(i), cluster);
				i++;
			}
		}

99
100
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
101
102
103
104
105
		setOpaque(true);
		setVisible(true);

		VisualizationInjector.addMouseListener(this);

106
107
108
		customMenu = new JMenu("Social View");
		customMenu.add(createRelationshipCheckBox());
		customMenu.add(createClusterCheckBox());
109
110
111

	}

112
113
	private JCheckBoxMenuItem createRelationshipCheckBox() {
		final JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem("Connections",
114
115
116
117
118
119
120
121
122
123
124
125
				showRelationship);
		checkBox.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				showRelationship = checkBox.isSelected();
				SocialViewComponentVis.this.repaint();
			}
		});
		return checkBox;
	}

126
127
128
	private JCheckBoxMenuItem createClusterCheckBox() {
		final JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem("Cluster",
				showCluster);
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
		checkBox.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				showCluster = checkBox.isSelected();
				SocialViewComponentVis.this.repaint();
			}
		});
		return checkBox;
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		if (showRelationship) {
			for (Entry<SimHost, PositionVector> entry : posVecs.entrySet()) {
				SimHost source = entry.getKey();
				Point sourcePos = entry.getValue().asPoint();
				// neighbors
				for (SimHost dest : view.getNeighbors(source)) {
					if (selectedHost == null || source.equals(selectedHost)
							|| dest.equals(selectedHost)) {
						g2.setColor(new Color(0, 0, 0));
					} else {
						g2.setColor(new Color(0, 0, 0, 32));
					}
					Point destPos = posVecs.get(dest).asPoint();
					g2.drawLine(sourcePos.x, sourcePos.y, destPos.x, destPos.y);
				}
			}
		}

		if (showCluster) {
			for (Entry<Color, Set<SimHost>> clusters : clusterMap.entrySet()) {

				for (SimHost host : clusters.getValue()) {
					Point center = posVecs.get(host).asPoint();
					// for a black border around the circle
					g2.setColor(Color.BLACK);
					g2.setStroke(new BasicStroke(4.5f));
					g2.drawOval(center.x - 7, center.y - 7, 14, 14);

					// draw the circle
					g2.setColor(clusters.getKey());
					g2.setStroke(new BasicStroke(3.5f));
					g2.drawOval(center.x - 7, center.y - 7, 14, 14);
				}
			}
		}
	}

	@Override
	public void mouseClicked(int x, int y) {
		List<SimHost> inNear = new Vector<SimHost>();
		for (Entry<SimHost, PositionVector> e : posVecs.entrySet()) {
187
			if (e.getValue().distanceTo(new PositionVector(x, y)) < 4) {
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
				inNear.add(e.getKey());
			}
		}

		if (inNear.size() == 1) {
			selectedHost = inNear.iterator().next();
		} else if (inNear.size() > 1) {
			Collections.shuffle(inNear);
			selectedHost = inNear.iterator().next();
		} else {
			selectedHost = null;
		}
		this.repaint();

	}

	@Override
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	public JComponent getComponent() {
		return this;
	}

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

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

	@Override
	public String getDisplayName() {
		return view.getIdentifier();
222
223
	}
}