1 package org.simantics.district.network.ui.nodes;
3 import java.awt.AlphaComposite;
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;
12 import java.util.Map.Entry;
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;
30 public class DynamicVisualisationContributionsNode extends G2DNode {
32 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationContributionsNode.class);
34 public static final String ID = "dynamicVisualisationContributionsNode";
36 private static final long serialVersionUID = 7400966702826774761L;
38 protected boolean enabled = true;
40 private Map<String, DynamicColorContribution> dynamicColoringContributions;
41 private Map<String, DynamicSizeContribution> dynamicSizingContributions;
43 private ColorBarOptions colorBarsOptions = ColorBarOptions.useDefault();
44 private SizeBarOptions sizeBarsOptions = SizeBarOptions.useDefault();
47 public void render(Graphics2D g2d) {
51 AffineTransform ot = g2d.getTransform();
52 Color oldColor = g2d.getColor();
55 g2d.transform(transform);
56 g2d.setTransform(new AffineTransform());
58 Rectangle2D bounds = g2d.getClipBounds();
65 g2d.setColor(oldColor);
70 private double colorBarBoxWidth = 300;
71 private double colorBarBoxHeight = 50;
72 private double colorBarBoxPadding = 20;
74 private void renderColors(Graphics2D g2d) {
75 if (colorBarsOptions == null || !colorBarsOptions.isShowColorBars()) {
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);
85 Rectangle2D bounds = g2d.getClipBounds();
91 if (dynamicColoringContributions != null) {
92 for (Entry<String, DynamicColorContribution> object : dynamicColoringContributions.entrySet()) {
93 DynamicColorContribution cc = object.getValue();
98 double min = cc.getDefaultMin();
99 double max = cc.getDefaultMax();
100 String unit = cc.getUnit();
101 String label = cc.getLabel();
103 DynamicColorMap map = cc.getDefaultColorMap();
105 double colorBarBoxTop = (colorBarBoxTopInitial + (colorBarBoxHeight * i));
108 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
109 g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
111 Rectangle2D vertical = new Rectangle2D.Double(colorBarBoxLeft, colorBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
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();
120 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
121 g2d.setFont(rulerFont);
123 double interval = (max - min) / intensities.size();
125 for (int j = 0; j < intensities.size(); j++) {
127 RGBIntensity intensity = intensities.get(j);
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);
133 double value = min + j * interval;
134 String str = Double.toString(value);
135 if (str.length() > 4) {
136 str = str.substring(0, 3);
138 g2d.setColor(Color.BLACK);
139 g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight));
140 colorVerticalLeft = colorVerticalLeft + colorVerticalWidth;
142 g2d.setColor(Color.BLACK);
144 String str = Double.toString(max);
145 g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight));
147 str = object.getKey() + " - " + label + " [" + unit + "]";
148 g2d.drawString(str, (float)colorBarBoxLeft + 5, (float)colorBarBoxTop + 10);
153 private double getColorBarBoxTop(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
154 Rectangle2D bounds = g2d.getClipBounds();
156 throw new IllegalStateException();
159 return colorBarBoxPadding;
162 return bounds.getMaxY() - colorBarBoxPadding;
166 return (bounds.getMaxY() / 2) - (getColorBarBoxHeight(size) / 2);
173 private double getColorBarBoxLeft(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
174 Rectangle2D bounds = g2d.getClipBounds();
176 throw new IllegalStateException();
179 double right = bounds.getMaxX() - colorBarBoxPadding;
180 return right - getColorBarBoxWidth(size);
183 return colorBarBoxPadding;
187 double left = (bounds.getMaxX() / 2) - (getColorBarBoxWidth(size) / 2);
195 private double getColorBarBoxWidth(ColorBarsSize size) {
196 return size.getSize() * colorBarBoxWidth;
199 private double getColorBarBoxHeight(ColorBarsSize size) {
200 return size.getSize() * colorBarBoxHeight;
203 private void renderSizes(Graphics2D g2d) {
204 if (sizeBarsOptions == null || !sizeBarsOptions.isShowSizeBars()) {
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);
214 Rectangle2D bounds = g2d.getClipBounds();
220 for (Entry<String, DynamicSizeContribution> object : dynamicSizingContributions.entrySet()) {
221 DynamicSizeContribution cc = object.getValue();
223 double min = cc.getDefaultMin();
224 double max = cc.getDefaultMax();
225 String unit = cc.getUnit();
226 String label = cc.getLabel();
228 DynamicSizeMap map = cc.getDefaultSizeMap();
230 List<Double> sizes = map.getSizes();
232 double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * i));
235 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
236 g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
238 Rectangle2D vertical = new Rectangle2D.Double(sizeBarBoxLeft, sizeBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
241 double sizeVerticalLeft = sizeBarBoxLeft + 5;
242 double sizeVerticalTop = sizeBarBoxTop + 15;
243 double sizeVerticalHeigth = colorBarBoxHeight - 30;
245 double sizeVerticalWidth = (sizeBarBoxWidth - 10) / sizes.size();
247 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
248 g2d.setFont(rulerFont);
250 double interval = (max - min) / sizes.size();
252 for (int j = 0; j < sizes.size(); j++) {
254 Double size = sizes.get(j);
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);
261 double value = min + j * interval;
262 String str = Double.toString(value);
263 if (str.length() > 4) {
264 str = str.substring(0, 3);
266 g2d.setColor(Color.BLACK);
267 g2d.drawString(str, (float)(sizeVerticalLeft - 8), (float)(sizeBarBoxTop + sizeBarBoxHeight));
269 sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth;
271 g2d.setColor(Color.BLACK);
273 String str = Double.toString(max);
274 g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight));
276 str = object.getKey() + " - " + label + " [" + unit + "]";
277 g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10);
281 private double getSizeBarBoxTop(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
282 Rectangle2D bounds = g2d.getClipBounds();
284 throw new IllegalStateException();
287 return colorBarBoxPadding;
290 return bounds.getMaxY() - colorBarBoxPadding;
294 return (bounds.getMaxY() / 2) - (getSizeBarBoxHeight(size) / 2);
301 private double getSizeBarBoxLeft(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
302 Rectangle2D bounds = g2d.getClipBounds();
304 throw new IllegalStateException();
307 double right = bounds.getMaxX() - colorBarBoxPadding;
308 return right - getSizeBarBoxWidth(size);
311 return colorBarBoxPadding;
315 double left = (bounds.getMaxX() / 2) - (getSizeBarBoxWidth(size) / 2);
323 private double getSizeBarBoxWidth(SizeBarsSize size) {
324 return size.getSize() * colorBarBoxWidth;
327 private double getSizeBarBoxHeight(SizeBarsSize size) {
328 return size.getSize() * colorBarBoxHeight;
332 public Rectangle2D getBoundsInLocal() {
336 public void setEnabled(boolean enabled) {
337 this.enabled = enabled;
340 public void setDynamicColoringObjects(Map<String,DynamicColorContribution> dynamicColoringObjects) {
341 this.dynamicColoringContributions = dynamicColoringObjects;
344 public void setColorBarOptions(ColorBarOptions colorBarsOptions) {
345 this.colorBarsOptions = colorBarsOptions;
348 public void setDynamicSizingObjects(Map<String, DynamicSizeContribution> dynamicSizingObjects) {
349 this.dynamicSizingContributions = dynamicSizingObjects;
352 public void setSizeBarOptions(SizeBarOptions sizeBarOptions) {
353 this.sizeBarsOptions = sizeBarOptions;