summaryrefslogtreecommitdiffstats
path: root/DataStructure/PhoneContainer.java
blob: efcc24c3e4b831329728ee4df1e6512664c47e40 (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
package DataStructure;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Random;

import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.painter.Painter;

public class PhoneContainer implements Painter<JXMapViewer> {

	private ArrayList<MobilePhone> phones = new ArrayList<MobilePhone>();
	private ArrayList<ImsiColor> colors = new ArrayList<ImsiColor>();

	public PhoneContainer() {

	}

	/**
	 * Adds the phone to the JXMAP-Painter. Assigns a random Color if phone is
	 * new. Make sure phone is already initialized. That means, locate method is
	 * already called. This container takes MobilEPhone Objects and draws them
	 * all!
	 * 
	 * @param phone
	 */
	public void add(MobilePhone phone) {
		// look if Phone is already inside
		int index = findIMSI(phone);
		if (index > -1) {
			// phone is there at position index
			Color intermediate = phones.get(index).color;
			phone.color = intermediate;
			phones.set(index, phone);
		} else {

			Random r = new Random();
			phone.color = new Color(r.nextInt(256), r.nextInt(256),
					r.nextInt(256));
			phones.add(phone);
		}

		// oder Farbe durch einen Hash auf die IMSI machen...

	}

	private int findIMSI(MobilePhone phone) {
		for (int i = 0; i < phones.size(); i++) {
			if (phones.get(i).IMSI == phone.IMSI) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * Removes all Phones from Painter list. Colors for each phone (IMSI) is
	 * remembered
	 */
	public void removeAll() {
		phones.clear();
	}

	@Override
	/**
	 * HACK: Outputs only two phones now! For Journal. Remove test for IMSI afterwards
	 */
	public void paint(Graphics2D g, JXMapViewer map, int w, int h) {
		for (MobilePhone phone : phones) {
			if (phone.IMSI == 262230000000003L
					|| phone.IMSI == 262230000000013L) {
				// g.setColor(phone.color);
				g.setColor(getColor(phone));

				phone.paint(g, map, w, h);
			}
		}
		g.dispose();

	}

	private Color getColor(MobilePhone phone) {
		// search if IMSI already in list
		for (int i = 0; i < colors.size(); i++) {
			if (colors.get(i).IMSI == phone.IMSI) {
				return colors.get(i).c;
			}
		}
		Random r = new Random();
		Color result = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
		colors.add(new ImsiColor(phone.IMSI, result));
		return result;
	}

}

class ImsiColor {
	public long IMSI;
	public Color c;

	public ImsiColor(long IMSI, Color c) {
		this.IMSI = IMSI;
		this.c = c;
	}
}