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.Graphics;
37
38 import javax.swing.JPanel;
39
40 import lombok.extern.slf4j.Slf4j;
41 import net.sf.jtreemap.swing.JTreeMap;
42 import net.sf.jtreemap.swing.TreeMapNode;
43 import net.sf.jtreemap.swing.Value;
44
45
46
47
48
49
50
51 @Slf4j
52 public class RedGreenColorProvider extends ColorProvider {
53 private static final long serialVersionUID = 5030306338780462810L;
54 private static final int COLOUR_MAX_VALUE = 255;
55 private final JTreeMap jTreeMap;
56 private JPanel legend;
57 private Value maxAbsValue;
58 private Value minVal;
59 private static final int[] TAB_COLOR = { 0, 60, 102, 153, 204, COLOUR_MAX_VALUE };
60 private static final int[] TAB_LIMIT_VALUE = { 25, 76, 123, 179, 230, COLOUR_MAX_VALUE };
61
62
63
64
65
66
67
68 public RedGreenColorProvider(final JTreeMap jTreeMap) {
69 this.jTreeMap = jTreeMap;
70 }
71
72 @Override
73 public Color getColor(final Value value) {
74
75 if (this.maxAbsValue == null) {
76 setMaxValue(this.jTreeMap.getRoot());
77 }
78
79 final double dValeur = value != null ? value.getValue() : 0.00;
80
81 int colorIndex = (int) (COLOUR_MAX_VALUE * Math.abs(dValeur) / this.maxAbsValue.getValue());
82
83 if (colorIndex > COLOUR_MAX_VALUE) {
84 colorIndex = COLOUR_MAX_VALUE;
85 }
86
87 for (int i = 0; i < TAB_LIMIT_VALUE.length; i++) {
88 if (colorIndex <= TAB_LIMIT_VALUE[i]) {
89 colorIndex = TAB_COLOR[i];
90 break;
91 }
92 }
93
94 if (dValeur >= 0) {
95 return new Color(0, colorIndex, 0);
96 }
97 return new Color(colorIndex, 0, 0);
98 }
99
100 @Override
101 public JPanel getLegendPanel() {
102 if (this.legend == null) {
103
104 if (this.maxAbsValue == null) {
105 setMaxValue(this.jTreeMap.getRoot());
106 }
107 this.legend = new Legend();
108 }
109 return this.legend;
110 }
111
112
113
114
115
116
117
118 private void setMaxValue(final TreeMapNode root) {
119 if (root.isLeaf()) {
120 final Value value = root.getValue();
121 if (value != null && (this.maxAbsValue == null || Math.abs(value.getValue()) > this.maxAbsValue.getValue())) {
122 try {
123 final Class<?> c = value.getClass();
124 if (this.maxAbsValue == null || this.minVal == null) {
125 this.maxAbsValue = (Value) c.newInstance();
126 this.minVal = (Value) c.newInstance();
127 }
128 this.minVal.setValue(-Math.abs(value.getValue()));
129 this.maxAbsValue.setValue(Math.abs(value.getValue()));
130 } catch (final IllegalAccessException | InstantiationException ie) {
131
132 log.error("Issue with max value", ie);
133 }
134 }
135 } else {
136 for (final TreeMapNode node : root.getChildren()) {
137 setMaxValue(node);
138 }
139 }
140 }
141
142
143
144
145
146
147 private class Legend extends JPanel {
148 private static final int Y_INSET = 7;
149 private static final int X_INSET = 15;
150 private static final long serialVersionUID = -536198802533900214L;
151 private static final int HEIGHT = 20;
152 private static final int WIDTH = 10;
153 private static final int X = 20;
154 private static final int Y = 25;
155
156
157
158
159 public Legend() {
160 this.setPreferredSize(
161 new java.awt.Dimension(2 * (Legend.X + RedGreenColorProvider.TAB_COLOR.length * Legend.WIDTH), 2 * (Legend.Y + Legend.HEIGHT)));
162 }
163
164 @Override
165 public void paintComponent(final Graphics g) {
166 super.paintComponent(g);
167 if (RedGreenColorProvider.this.minVal == null || RedGreenColorProvider.this.maxAbsValue == null) {
168 return;
169 }
170
171 int xCursor = 0;
172
173 for (int i = RedGreenColorProvider.TAB_COLOR.length - 1; i > 0; i--) {
174 g.setColor(new Color(RedGreenColorProvider.TAB_COLOR[i], 0, 0));
175 g.fillRect(Legend.X + xCursor * Legend.WIDTH, Legend.Y, Legend.WIDTH, Legend.HEIGHT);
176 xCursor++;
177 }
178
179 g.setColor(Color.black);
180 g.drawString(RedGreenColorProvider.this.minVal.getLabel(), Legend.X - X_INSET, Legend.Y - Y_INSET);
181 g.drawString("0", Legend.X + xCursor * Legend.WIDTH, Legend.Y - Y_INSET);
182
183 for (final int element : RedGreenColorProvider.TAB_COLOR) {
184 g.setColor(new Color(0, element, 0));
185 g.fillRect(Legend.X + xCursor * Legend.WIDTH, Legend.Y, Legend.WIDTH, Legend.HEIGHT);
186 xCursor++;
187 }
188
189 g.setColor(Color.black);
190 g.drawString(RedGreenColorProvider.this.maxAbsValue.getLabel(), Legend.X + (xCursor - 2) * Legend.WIDTH, Legend.Y - Y_INSET);
191 }
192 }
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209