]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java
First version of district visualisations
[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                 g2d.setColor(Color.BLACK);
123                 
124                 String str = Double.toString(max);
125                 g2d.drawString(str, (int)(colorBarBoxLeft + colorBarBoxWidth - 30), (int)(colorBarBoxTop  + colorBarBoxHeight));
126                 
127                 str = Double.toString(min);
128                 g2d.drawString(str, (int)colorBarBoxLeft + 1, (int)(colorBarBoxTop + colorBarBoxHeight));
129                 
130                 
131                 for (RGBIntensity intensity : intensities) {
132                     
133                     g2d.setColor(new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue(), 1f));
134                     Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft, colorVerticalTop, colorVerticalWidth, colorVerticalHeigth);
135                     g2d.fill(colorVertical);
136                     colorVerticalLeft = colorVerticalLeft + colorVerticalWidth;
137                 }
138                 g2d.setColor(Color.BLACK);
139                 str = object.getKey() + " - " + label + " [" + unit + "]";
140                 g2d.drawString(str, (int)colorBarBoxLeft + 5, (int)colorBarBoxTop + 10);
141             }
142         }
143     }
144
145     private double getColorBarBoxTop(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
146         Rectangle2D bounds = g2d.getClipBounds();
147         if (bounds == null)
148             throw new IllegalStateException();
149         switch (location) {
150             case NORTH: {
151                 return colorBarBoxPadding;
152             }
153             case SOUTH: {
154                 return bounds.getMaxY() - colorBarBoxPadding;
155             }
156             case EAST:
157             case WEST: {
158                 return (bounds.getMaxY() / 2) - (getColorBarBoxHeight(size) / 2);
159             }
160             default:
161                 return 0;
162         }
163     }
164     
165     private double getColorBarBoxLeft(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
166         Rectangle2D bounds = g2d.getClipBounds();
167         if (bounds == null)
168             throw new IllegalStateException();
169         switch (location) {
170             case EAST: {
171                 double right = bounds.getMaxX() - colorBarBoxPadding;
172                 return right - getColorBarBoxWidth(size);
173             }
174             case WEST: {
175                 return colorBarBoxPadding;
176             }
177             case NORTH:
178             case SOUTH: {
179                 double left = (bounds.getMaxX() / 2) - (getColorBarBoxWidth(size) / 2);
180                 return left;
181             }
182             default:
183                 return 0;
184         }
185     }
186
187     private double getColorBarBoxWidth(ColorBarsSize size) {
188         return size.getSize() * colorBarBoxWidth;
189     }
190
191     private double getColorBarBoxHeight(ColorBarsSize size) {
192         return size.getSize() * colorBarBoxHeight;
193     }
194
195     private void renderSizes(Graphics2D g2d) {
196         if (sizeBarsOptions == null || !sizeBarsOptions.isShowSizeBars()) {
197             return;
198         }
199         SizeBarsLocation location = sizeBarsOptions.getLocation();
200         SizeBarsSize sizeb = sizeBarsOptions.getSize();
201         double sizeBarBoxLeft = getSizeBarBoxLeft(g2d, location, sizeb);
202         double sizeBarBoxTopInitial = getSizeBarBoxTop(g2d, location, sizeb);
203         double sizeBarBoxWidth = getSizeBarBoxWidth(sizeb);
204         double sizeBarBoxHeight = getSizeBarBoxHeight(sizeb);
205         
206         Rectangle2D bounds = g2d.getClipBounds();
207         if (bounds == null)
208             return; // FIXME
209         
210         int i = 0;
211         
212         for (Entry<String, DynamicSizeContribution> object : dynamicSizingContributions.entrySet()) {
213             DynamicSizeContribution cc = object.getValue();
214             
215             double min = cc.getDefaultMin();
216             double max = cc.getDefaultMax();
217             String unit = cc.getUnit();
218             String label = cc.getLabel();
219             
220             DynamicSizeMap map = cc.getDefaultSizeMap();
221             
222             List<Double> sizes = map.getSizes();
223             
224             double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * i));
225             i++;
226 //                double backgroundBoxPaddingRight = 20;
227 //                double backgroundBoxHeight = 50;
228 //                double backgroundBoxWidth = 300;
229 //                double backgroundBoxRight = bounds.getMaxX() - backgroundBoxPaddingRight;
230 //                double backgroundBoxLeft = backgroundBoxRight - backgroundBoxWidth;
231 //                double backgroundBoxTop = bounds.getMaxY();// + (initialPosition + (position * i));
232             g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
233             g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
234             
235             Rectangle2D vertical = new Rectangle2D.Double(sizeBarBoxLeft, sizeBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
236             g2d.fill(vertical);
237             
238             double sizeVerticalLeft = sizeBarBoxLeft + 5;
239             double sizeVerticalTop = sizeBarBoxTop + 15;
240             double sizeVerticalHeigth = colorBarBoxHeight - 30;
241
242             double sizeVerticalWidth = (sizeBarBoxWidth - 10) / sizes.size();
243             
244             Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
245             g2d.setFont(rulerFont);
246             g2d.setColor(Color.BLACK);
247             
248             String str = Double.toString(max);
249             g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight));
250             
251             str = Double.toString(min);
252             g2d.drawString(str, (int)sizeBarBoxLeft + 1, (int)(sizeBarBoxTop + sizeBarBoxHeight));
253             
254             
255             g2d.setColor(new Color((float)0, (float)0, (float)0.8, 0.8f));
256             for (Double size: sizes) {
257                 
258                 Ellipse2D ellipse = new Ellipse2D.Double(sizeVerticalLeft + sizeVerticalWidth / 2, (sizeVerticalTop), size * sizeVerticalHeigth, size * sizeVerticalHeigth);
259                 g2d.fill(ellipse);
260                 
261                 sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth;
262             }
263             g2d.setColor(Color.BLACK);
264             str = object.getKey() + " - " + label + " [" + unit + "]";
265             g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10);
266         }
267     }
268     
269     private double getSizeBarBoxTop(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
270         Rectangle2D bounds = g2d.getClipBounds();
271         if (bounds == null)
272             throw new IllegalStateException();
273         switch (location) {
274             case NORTH: {
275                 return colorBarBoxPadding;
276             }
277             case SOUTH: {
278                 return bounds.getMaxY() - colorBarBoxPadding;
279             }
280             case EAST:
281             case WEST: {
282                 return (bounds.getMaxY() / 2) - (getSizeBarBoxHeight(size) / 2);
283             }
284             default:
285                 return 0;
286         }
287     }
288     
289     private double getSizeBarBoxLeft(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
290         Rectangle2D bounds = g2d.getClipBounds();
291         if (bounds == null)
292             throw new IllegalStateException();
293         switch (location) {
294             case EAST: {
295                 double right = bounds.getMaxX() - colorBarBoxPadding;
296                 return right - getSizeBarBoxWidth(size);
297             }
298             case WEST: {
299                 return colorBarBoxPadding;
300             }
301             case NORTH:
302             case SOUTH: {
303                 double left = (bounds.getMaxX() / 2) - (getSizeBarBoxWidth(size) / 2);
304                 return left;
305             }
306             default:
307                 return 0;
308         }
309     }
310
311     private double getSizeBarBoxWidth(SizeBarsSize size) {
312         return size.getSize() * colorBarBoxWidth;
313     }
314
315     private double getSizeBarBoxHeight(SizeBarsSize size) {
316         return size.getSize() * colorBarBoxHeight;
317     }
318
319     @Override
320     public Rectangle2D getBoundsInLocal() {
321         return null;
322     }
323
324     public void setEnabled(boolean enabled) {
325         this.enabled = enabled;
326     }
327
328     public void setDynamicColoringObjects(Map<String,DynamicColorContribution> dynamicColoringObjects) {
329         this.dynamicColoringContributions = dynamicColoringObjects;
330     }
331
332     public void setColorBarOptions(ColorBarOptions colorBarsOptions) {
333         this.colorBarsOptions = colorBarsOptions;
334     }
335
336     public void setDynamicSizingObjects(Map<String, DynamicSizeContribution> dynamicSizingObjects) {
337         this.dynamicSizingContributions = dynamicSizingObjects;
338     }
339
340     public void setSizeBarOptions(SizeBarOptions sizeBarOptions) {
341         this.sizeBarsOptions = sizeBarOptions;
342     }
343
344 }