]> gerrit.simantics Code Review - simantics/district.git/blob
b3149324eb42fb342262a88f1831d194c0c5cdb6
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.AlphaComposite;
4 import java.awt.Color;
5 import java.awt.Font;
6 import java.awt.Graphics2D;
7 import java.awt.geom.AffineTransform;
8 import java.awt.geom.Rectangle2D;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Map.Entry;
12
13 import org.simantics.district.network.visualisations.model.ColorBarOptions;
14 import org.simantics.district.network.visualisations.model.ColorBarOptions.ColorBarsLocation;
15 import org.simantics.district.network.visualisations.model.ColorBarOptions.ColorBarsSize;
16 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
17 import org.simantics.district.network.visualisations.model.DynamicColorMap;
18 import org.simantics.district.network.visualisations.model.DynamicColorMap.RGBIntensity;
19 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
20 import org.simantics.district.network.visualisations.model.DynamicSizeMap;
21 import org.simantics.district.network.visualisations.model.SizeBarOptions;
22 import org.simantics.district.network.visualisations.model.SizeBarOptions.SizeBarsLocation;
23 import org.simantics.district.network.visualisations.model.SizeBarOptions.SizeBarsSize;
24 import org.simantics.scenegraph.g2d.G2DNode;
25 import org.simantics.scenegraph.utils.DPIUtil;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class DynamicVisualisationContributionsNode extends G2DNode {
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationContributionsNode.class);
32
33     public static final String ID = "dynamicVisualisationContributionsNode";
34     
35     private static final long serialVersionUID = 7400966702826774761L;
36
37     protected boolean enabled = true;
38
39     private Map<String, DynamicColorContribution> dynamicColoringContributions;
40     private Map<String, DynamicSizeContribution> dynamicSizingContributions;
41     
42     private ColorBarOptions colorBarsOptions = ColorBarOptions.useDefault();
43     private SizeBarOptions sizeBarsOptions = SizeBarOptions.useDefault();
44
45     @Override
46     public void render(Graphics2D g2d) {
47         if (!enabled)
48             return;
49         
50         AffineTransform ot = g2d.getTransform();
51         Color oldColor = g2d.getColor();
52         
53         try {
54             g2d.transform(transform);
55             g2d.setTransform(new AffineTransform());
56             
57             Rectangle2D bounds = g2d.getClipBounds();
58             if (bounds == null)
59                 return; // FIXME
60             
61             renderColors(g2d);
62             renderSizes(g2d);
63         } finally {
64             g2d.setColor(oldColor);
65             g2d.setTransform(ot);
66         }
67     }
68
69     private double colorBarBoxWidth = 300;
70     private double colorBarBoxHeight = 50;
71     private double colorBarBoxPadding = 20;
72
73     private void renderColors(Graphics2D g2d) {
74         if (colorBarsOptions == null || !colorBarsOptions.isShowColorBars()) {
75             return;
76         }
77         ColorBarsLocation location = colorBarsOptions.getLocation();
78         ColorBarsSize size = colorBarsOptions.getSize();
79         double colorBarBoxLeft = getColorBarBoxLeft(g2d, location, size);
80         double colorBarBoxTopInitial = getColorBarBoxTop(g2d, location, size);
81         double colorBarBoxWidth = getColorBarBoxWidth(size);
82         double colorBarBoxHeight = getColorBarBoxHeight(size);
83         
84         Rectangle2D bounds = g2d.getClipBounds();
85         if (bounds == null)
86             return; // FIXME
87         
88         int i = 0;
89         
90         if (dynamicColoringContributions != null) {
91             for (Entry<String, DynamicColorContribution> object : dynamicColoringContributions.entrySet()) {
92                 DynamicColorContribution cc = object.getValue();
93                 
94                 if (!cc.isUsed())
95                     break;
96                 
97                 double min = cc.getDefaultMin();
98                 double max = cc.getDefaultMax();
99                 String unit = cc.getUnit();
100                 String label = cc.getLabel();
101                 
102                 DynamicColorMap map = cc.getDefaultColorMap();
103                 
104                 double colorBarBoxTop = (colorBarBoxTopInitial + (colorBarBoxHeight * i));
105                 i++;
106
107                 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
108                 g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
109                 
110                 Rectangle2D vertical = new Rectangle2D.Double(colorBarBoxLeft, colorBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
111                 g2d.fill(vertical);
112                 
113                 double colorVerticalLeft = colorBarBoxLeft + 5;
114                 double colorVerticalTop = colorBarBoxTop + 15;
115                 double colorVerticalHeigth = colorBarBoxHeight - 30;
116                 List<RGBIntensity> intensities = map.getIntensities();
117                 double colorVerticalWidth = (colorBarBoxWidth - 10) / intensities.size();
118                 
119                 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
120                 g2d.setFont(rulerFont);
121                 
122                 double interval = (max - min) / intensities.size();
123                 
124                 for (int j = 0; j < intensities.size(); j++) {
125                     
126                     RGBIntensity intensity = intensities.get(j);
127                     
128                     g2d.setColor(new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue(), 1f));
129                     Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft, colorVerticalTop, colorVerticalWidth, colorVerticalHeigth);
130                     g2d.fill(colorVertical);
131                     
132                     double value = min + j * interval;
133                     String str = Double.toString(value);
134                     if (str.length() > 4) {
135                         str = str.substring(0, 3);
136                     }
137                     g2d.setColor(Color.BLACK);
138                     g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight));
139                     colorVerticalLeft = colorVerticalLeft + colorVerticalWidth;
140                 }
141                 g2d.setColor(Color.BLACK);
142                 
143                 String str = Double.toString(max);
144                 g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop  + colorBarBoxHeight));
145                 
146                 str = object.getKey() + " - " + label + " [" + unit + "]";
147                 g2d.drawString(str, (float)colorBarBoxLeft + 5, (float)colorBarBoxTop + 10);
148             }
149         }
150     }
151
152     private double getColorBarBoxTop(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
153         Rectangle2D bounds = g2d.getClipBounds();
154         if (bounds == null)
155             throw new IllegalStateException();
156         switch (location) {
157             case NORTH: {
158                 return colorBarBoxPadding;
159             }
160             case SOUTH: {
161                 return bounds.getMaxY() - colorBarBoxPadding;
162             }
163             case EAST:
164             case WEST: {
165                 return (bounds.getMaxY() / 2) - (getColorBarBoxHeight(size) / 2);
166             }
167             default:
168                 return 0;
169         }
170     }
171     
172     private double getColorBarBoxLeft(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
173         Rectangle2D bounds = g2d.getClipBounds();
174         if (bounds == null)
175             throw new IllegalStateException();
176         switch (location) {
177             case EAST: {
178                 double right = bounds.getMaxX() - colorBarBoxPadding;
179                 return right - getColorBarBoxWidth(size);
180             }
181             case WEST: {
182                 return colorBarBoxPadding;
183             }
184             case NORTH:
185             case SOUTH: {
186                 double left = (bounds.getMaxX() / 2) - (getColorBarBoxWidth(size) / 2);
187                 return left;
188             }
189             default:
190                 return 0;
191         }
192     }
193
194     private double getColorBarBoxWidth(ColorBarsSize size) {
195         return size.getSize() * colorBarBoxWidth;
196     }
197
198     private double getColorBarBoxHeight(ColorBarsSize size) {
199         return size.getSize() * colorBarBoxHeight;
200     }
201
202     private void renderSizes(Graphics2D g2d) {
203         if (sizeBarsOptions == null || !sizeBarsOptions.isShowSizeBars()) {
204             return;
205         }
206         SizeBarsLocation location = sizeBarsOptions.getLocation();
207         SizeBarsSize sizeb = sizeBarsOptions.getSize();
208         double sizeBarBoxLeft = getSizeBarBoxLeft(g2d, location, sizeb);
209         double sizeBarBoxTopInitial = getSizeBarBoxTop(g2d, location, sizeb);
210         double sizeBarBoxWidth = getSizeBarBoxWidth(sizeb);
211         double sizeBarBoxHeight = getSizeBarBoxHeight(sizeb);
212         
213         Rectangle2D bounds = g2d.getClipBounds();
214         if (bounds == null)
215             return; // FIXME
216         
217         int i = 0;
218         
219         if (dynamicSizingContributions != null) {
220             for (Entry<String, DynamicSizeContribution> object : dynamicSizingContributions.entrySet()) {
221                 DynamicSizeContribution cc = object.getValue();
222                 
223                 if (!cc.isUsed())
224                     continue;
225                 
226                 double min = cc.getDefaultMin();
227                 double max = cc.getDefaultMax();
228                 String unit = cc.getUnit();
229                 String label = cc.getLabel();
230                 
231                 DynamicSizeMap map = cc.getDefaultSizeMap();
232                 
233                 List<Double> sizes = map.getSizes();
234                 
235                 double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * i));
236                 i++;
237     
238                 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
239                 g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
240                 
241                 Rectangle2D vertical = new Rectangle2D.Double(sizeBarBoxLeft, sizeBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
242                 g2d.fill(vertical);
243                 
244                 double sizeVerticalLeft = sizeBarBoxLeft + 5;
245                 double sizeVerticalTop = sizeBarBoxTop + 15;
246                 double sizeVerticalHeigth = colorBarBoxHeight - 30;
247     
248                 double sizeVerticalWidth = (sizeBarBoxWidth - 10) / sizes.size();
249                 
250                 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
251                 g2d.setFont(rulerFont);
252     
253                 double interval = (max - min) / sizes.size();
254                 
255                 for (int j = 0; j < sizes.size(); j++) {
256                     
257                     Double size = sizes.get(j);
258                     
259                     g2d.setColor(new Color((float)0, (float)0, (float)0.8, 0.8f));
260                     double sizedWidth = (size / 5) * sizeVerticalHeigth;
261                     Rectangle2D rect = new Rectangle2D.Double(sizeVerticalLeft, (sizeVerticalTop), sizedWidth, sizeVerticalHeigth);
262                     g2d.fill(rect);
263                     
264                     double value = min + j * interval;
265                     String str = Double.toString(value);
266                     if (str.length() > 4) {
267                         str = str.substring(0, 3);
268                     }
269                     g2d.setColor(Color.BLACK);
270                     g2d.drawString(str, (float)(sizeVerticalLeft - 8), (float)(sizeBarBoxTop + sizeBarBoxHeight));
271                     
272                     sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth;
273                 }
274                 g2d.setColor(Color.BLACK);
275                 
276                 String str = Double.toString(max);
277                 g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight));
278                 
279                 str = object.getKey() + " - " + label + " [" + unit + "]";
280                 g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10);
281             }
282         }
283     }
284     
285     private double getSizeBarBoxTop(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
286         Rectangle2D bounds = g2d.getClipBounds();
287         if (bounds == null)
288             throw new IllegalStateException();
289         switch (location) {
290             case NORTH: {
291                 return colorBarBoxPadding;
292             }
293             case SOUTH: {
294                 return bounds.getMaxY() - colorBarBoxPadding;
295             }
296             case EAST:
297             case WEST: {
298                 return (bounds.getMaxY() / 2) - (getSizeBarBoxHeight(size) / 2);
299             }
300             default:
301                 return 0;
302         }
303     }
304     
305     private double getSizeBarBoxLeft(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
306         Rectangle2D bounds = g2d.getClipBounds();
307         if (bounds == null)
308             throw new IllegalStateException();
309         switch (location) {
310             case EAST: {
311                 double right = bounds.getMaxX() - colorBarBoxPadding;
312                 return right - getSizeBarBoxWidth(size);
313             }
314             case WEST: {
315                 return colorBarBoxPadding;
316             }
317             case NORTH:
318             case SOUTH: {
319                 double left = (bounds.getMaxX() / 2) - (getSizeBarBoxWidth(size) / 2);
320                 return left;
321             }
322             default:
323                 return 0;
324         }
325     }
326
327     private double getSizeBarBoxWidth(SizeBarsSize size) {
328         return size.getSize() * colorBarBoxWidth;
329     }
330
331     private double getSizeBarBoxHeight(SizeBarsSize size) {
332         return size.getSize() * colorBarBoxHeight;
333     }
334
335     @Override
336     public Rectangle2D getBoundsInLocal() {
337         return null;
338     }
339
340     public void setEnabled(boolean enabled) {
341         this.enabled = enabled;
342     }
343
344     public void setDynamicColoringObjects(Map<String,DynamicColorContribution> dynamicColoringObjects) {
345         this.dynamicColoringContributions = dynamicColoringObjects;
346     }
347
348     public void setColorBarOptions(ColorBarOptions colorBarsOptions) {
349         this.colorBarsOptions = colorBarsOptions;
350     }
351
352     public void setDynamicSizingObjects(Map<String, DynamicSizeContribution> dynamicSizingObjects) {
353         this.dynamicSizingContributions = dynamicSizingObjects;
354     }
355
356     public void setSizeBarOptions(SizeBarOptions sizeBarOptions) {
357         this.sizeBarsOptions = sizeBarOptions;
358     }
359
360 }