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 package net.sf.jtreemap.swing.provider;
34
35 import java.awt.Color;
36 import java.awt.Dimension;
37 import java.awt.FontMetrics;
38 import java.awt.Graphics;
39 import java.util.Enumeration;
40 import java.util.TreeMap;
41
42 import javax.swing.JPanel;
43
44 import net.sf.jtreemap.swing.JTreeMap;
45 import net.sf.jtreemap.swing.TreeMapNode;
46 import net.sf.jtreemap.swing.Value;
47
48
49
50
51
52
53
54
55
56
57
58
59 public class RandomColorProvider extends ColorProvider {
60 private static final long serialVersionUID = -8184356270950978553L;
61
62 private static final Color[] COLOURS = new Color[] { new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255), new Color(255, 255, 0),
63 new Color(255, 0, 255), new Color(0, 255, 255), new Color(102, 102, 51), new Color(255, 51, 153), new Color(255, 153, 51),
64 new Color(204, 204, 51), new Color(205, 102, 204), new Color(51, 153, 255), new Color(153, 102, 0) };
65
66 private int cursor = 0;
67 private final TreeMap<Value, Color> mapping = new TreeMap<>();
68 private JPanel legend;
69 private final JTreeMap jTreeMap;
70
71
72
73
74
75
76
77 public RandomColorProvider(final JTreeMap jTreeMap) {
78 this.jTreeMap = jTreeMap;
79 }
80
81
82
83
84
85
86 @Override
87 public Color getColor(final Value value) {
88 if (!this.mapping.containsKey(value)) {
89 mapping.put(value, COLOURS[this.cursor]);
90 cursor++;
91 if (this.cursor == COLOURS.length) {
92 cursor = 0;
93 }
94 }
95 return mapping.get(value);
96 }
97
98 void setValues(final TreeMapNode root) {
99 if (root.isLeaf()) {
100 final Value value = root.getValue();
101 getColor(value);
102 } else {
103 for (final Enumeration e = root.children(); e.hasMoreElements();) {
104 final TreeMapNode node = (TreeMapNode) e.nextElement();
105 setValues(node);
106 }
107 }
108 }
109
110
111
112
113
114
115 @Override
116 public JPanel getLegendPanel() {
117 if (this.legend == null) {
118 legend = new Legend();
119 }
120 return legend;
121 }
122
123
124
125
126
127
128 protected class Legend extends JPanel {
129 private static final int OFFSET = 3;
130 private static final int X_OFFSET = 15;
131 private static final int INITIAL_X_POS = 20;
132 private static final long serialVersionUID = 4652239358357480113L;
133 private static final int Y = 25;
134 private static final int WIDTH = 10;
135 private static final int HEIGHT = 20;
136
137 @Override
138 public void paintComponent(final Graphics g) {
139 super.paintComponent(g);
140 if (RandomColorProvider.this.mapping.isEmpty()) {
141 RandomColorProvider.this.setValues(jTreeMap.getRoot());
142 }
143 final FontMetrics fm = g.getFontMetrics();
144 final int yString = Legend.Y + (Legend.HEIGHT + fm.getAscent() - fm.getDescent()) / 2;
145
146 int xPosition = INITIAL_X_POS;
147 for (final Value value : RandomColorProvider.this.mapping.keySet()) {
148 final Color color = RandomColorProvider.this.mapping.get(value);
149 g.setColor(color);
150 g.fillRect(xPosition, Legend.Y, Legend.WIDTH, Legend.HEIGHT);
151 g.setColor(Color.black);
152 xPosition = xPosition + Legend.WIDTH + OFFSET;
153 g.drawString(value.getLabel(), xPosition, yString);
154 xPosition = xPosition + fm.stringWidth(value.getLabel()) + X_OFFSET;
155 }
156
157 setPreferredSize(new Dimension(xPosition, 2 * Legend.Y + Legend.HEIGHT));
158 setSize(this.getPreferredSize());
159 }
160 }
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177