SocialViewComponentVis.java 5.79 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * 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;
import javax.swing.JComponent;
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;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector.MouseClickListener;
import de.tud.kom.p2psim.impl.topology.views.visualization.ui.ComponentOptions;

/**
 * This component draw the Social connections.
 * 
 * @author Christoph Muenker
 * @version 1.0, 22.06.2013
 */
public class SocialViewComponentVis extends JComponent implements
		MouseClickListener, ComponentOptions {

	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>>();

	private List<JCheckBox> optionBoxes = new Vector<JCheckBox>();

	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) {
				posVecs.put(host, host.getTopologyComponent().getRealPosition());
			}
			if (i < CLUSTER_COLOR.size()) {
				clusterMap.put(CLUSTER_COLOR.get(i), cluster);
				i++;
			}
		}

96
97
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
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
		setOpaque(true);
		setVisible(true);

		VisualizationInjector.addMouseListener(this);

		optionBoxes.add(createRelationshipCheckBox());
		optionBoxes.add(createClusterCheckBox());

	}

	private JCheckBox createRelationshipCheckBox() {
		final JCheckBox checkBox = new JCheckBox("Connections",
				showRelationship);
		checkBox.addChangeListener(new ChangeListener() {

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

	private JCheckBox createClusterCheckBox() {
		final JCheckBox checkBox = new JCheckBox("Cluster", showCluster);
		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()) {
182
			if (e.getValue().distanceTo(new PositionVector(x, y)) < 4) {
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
				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
	public List<JCheckBox> getCheckBoxes() {
		return optionBoxes;
	}
}