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