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.Rectangle2D;
11 import java.util.Map.Entry;
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;
29 public class DynamicVisualisationContributionsNode extends G2DNode {
31 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationContributionsNode.class);
33 public static final String ID = "dynamicVisualisationContributionsNode";
35 private static final long serialVersionUID = 7400966702826774761L;
37 protected boolean enabled = true;
39 private Map<String, DynamicColorContribution> dynamicColoringContributions;
40 private Map<String, DynamicSizeContribution> dynamicSizingContributions;
42 private ColorBarOptions colorBarsOptions = ColorBarOptions.useDefault();
43 private SizeBarOptions sizeBarsOptions = SizeBarOptions.useDefault();
46 public void render(Graphics2D g2d) {
50 AffineTransform ot = g2d.getTransform();
51 Color oldColor = g2d.getColor();
54 g2d.transform(transform);
55 g2d.setTransform(new AffineTransform());
57 Rectangle2D bounds = g2d.getClipBounds();
64 g2d.setColor(oldColor);
69 private double colorBarBoxWidth = 300;
70 private double colorBarBoxHeight = 50;
71 private double colorBarBoxPadding = 20;
73 private void renderColors(Graphics2D g2d) {
74 if (colorBarsOptions == null || !colorBarsOptions.isShowColorBars()) {
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);
84 Rectangle2D bounds = g2d.getClipBounds();
90 if (dynamicColoringContributions != null) {
91 for (Entry<String, DynamicColorContribution> object : dynamicColoringContributions.entrySet()) {
92 DynamicColorContribution cc = object.getValue();
97 double min = cc.getDefaultMin();
98 double max = cc.getDefaultMax();
99 String unit = cc.getUnit();
100 String label = cc.getLabel();
102 DynamicColorMap map = cc.getDefaultColorMap();
104 double colorBarBoxTop = (colorBarBoxTopInitial + (colorBarBoxHeight * i));
107 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
108 g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
110 Rectangle2D vertical = new Rectangle2D.Double(colorBarBoxLeft, colorBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
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();
119 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
120 g2d.setFont(rulerFont);
122 double interval = (max - min) / intensities.size();
124 for (int j = 0; j < intensities.size(); j++) {
126 RGBIntensity intensity = intensities.get(j);
128 if (colorBarsOptions.isUseGradients()) {
130 RGBIntensity upperLimitIntensity;
131 if (j + 1 != intensities.size())
132 upperLimitIntensity = intensities.get(j + 1);
134 upperLimitIntensity = intensity;
136 double minRed = intensity.getRed();
137 double minGreen = intensity.getGreen();
138 double minBlue = intensity.getBlue();
140 double maxRed = upperLimitIntensity.getRed();
141 double maxGreen = upperLimitIntensity.getGreen();
142 double maxBlue = upperLimitIntensity.getBlue();
144 double redDelta = (maxRed - minRed) / colorVerticalWidth;
145 double greenDelta = (maxGreen - minGreen) / colorVerticalWidth;
146 double blueDelta = (maxBlue - minBlue) / colorVerticalWidth;
148 for (int k = 0; k < colorVerticalWidth; k++) {
151 g2d.setColor(new Color((float)(minRed + (k * redDelta)), (float)(minGreen + (k * greenDelta)), (float)(minBlue + (k * blueDelta)), 1f));
152 Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft + k, colorVerticalTop, 1, colorVerticalHeigth);
153 g2d.fill(colorVertical);
157 g2d.setColor(new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue(), 1f));
158 Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft, colorVerticalTop, colorVerticalWidth, colorVerticalHeigth);
159 g2d.fill(colorVertical);
164 double value = min + j * interval;
165 String str = Double.toString(value);
166 if (str.length() > 4) {
167 str = str.substring(0, 3);
169 g2d.setColor(Color.BLACK);
170 g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight));
171 colorVerticalLeft = colorVerticalLeft + colorVerticalWidth;
173 g2d.setColor(Color.BLACK);
175 String str = Double.toString(max);
176 g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight));
178 str = object.getKey() + " - " + label + " [" + unit + "]";
179 g2d.drawString(str, (float)colorBarBoxLeft + 5, (float)colorBarBoxTop + 10);
184 private double getColorBarBoxTop(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
185 Rectangle2D bounds = g2d.getClipBounds();
187 throw new IllegalStateException();
190 return colorBarBoxPadding;
193 return bounds.getMaxY() - colorBarBoxPadding;
197 return (bounds.getMaxY() / 2) - (getColorBarBoxHeight(size) / 2);
204 private double getColorBarBoxLeft(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
205 Rectangle2D bounds = g2d.getClipBounds();
207 throw new IllegalStateException();
210 double right = bounds.getMaxX() - colorBarBoxPadding;
211 return right - getColorBarBoxWidth(size);
214 return colorBarBoxPadding;
218 double left = (bounds.getMaxX() / 2) - (getColorBarBoxWidth(size) / 2);
226 private double getColorBarBoxWidth(ColorBarsSize size) {
227 return size.getSize() * colorBarBoxWidth;
230 private double getColorBarBoxHeight(ColorBarsSize size) {
231 return size.getSize() * colorBarBoxHeight;
234 private void renderSizes(Graphics2D g2d) {
235 if (sizeBarsOptions == null || !sizeBarsOptions.isShowSizeBars()) {
238 SizeBarsLocation location = sizeBarsOptions.getLocation();
239 SizeBarsSize sizeb = sizeBarsOptions.getSize();
240 double sizeBarBoxLeft = getSizeBarBoxLeft(g2d, location, sizeb);
241 double sizeBarBoxTopInitial = getSizeBarBoxTop(g2d, location, sizeb);
242 double sizeBarBoxWidth = getSizeBarBoxWidth(sizeb);
243 double sizeBarBoxHeight = getSizeBarBoxHeight(sizeb);
245 Rectangle2D bounds = g2d.getClipBounds();
251 if (dynamicSizingContributions != null) {
252 for (Entry<String, DynamicSizeContribution> object : dynamicSizingContributions.entrySet()) {
253 DynamicSizeContribution cc = object.getValue();
258 double min = cc.getDefaultMin();
259 double max = cc.getDefaultMax();
260 String unit = cc.getUnit();
261 String label = cc.getLabel();
263 DynamicSizeMap map = cc.getDefaultSizeMap();
265 List<Double> sizes = map.getSizes();
267 double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * i));
270 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
271 g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
273 Rectangle2D vertical = new Rectangle2D.Double(sizeBarBoxLeft, sizeBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
276 double sizeVerticalLeft = sizeBarBoxLeft + 5;
277 double sizeVerticalTop = sizeBarBoxTop + 15;
278 double sizeVerticalHeigth = colorBarBoxHeight - 30;
280 double sizeVerticalWidth = (sizeBarBoxWidth - 10) / sizes.size();
282 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
283 g2d.setFont(rulerFont);
285 double interval = (max - min) / sizes.size();
287 for (int j = 0; j < sizes.size(); j++) {
289 Double size = sizes.get(j);
291 g2d.setColor(new Color((float)0, (float)0, (float)0.8, 0.8f));
292 double sizedWidth = (size / 5) * sizeVerticalHeigth;
293 Rectangle2D rect = new Rectangle2D.Double(sizeVerticalLeft, (sizeVerticalTop), sizedWidth, sizeVerticalHeigth);
296 double value = min + j * interval;
297 String str = Double.toString(value);
298 if (str.length() > 4) {
299 str = str.substring(0, 3);
301 g2d.setColor(Color.BLACK);
302 g2d.drawString(str, (float)(sizeVerticalLeft - 8), (float)(sizeBarBoxTop + sizeBarBoxHeight));
304 sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth;
306 g2d.setColor(Color.BLACK);
308 String str = Double.toString(max);
309 g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight));
311 str = object.getKey() + " - " + label + " [" + unit + "]";
312 g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10);
317 private double getSizeBarBoxTop(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
318 Rectangle2D bounds = g2d.getClipBounds();
320 throw new IllegalStateException();
323 return colorBarBoxPadding;
326 return bounds.getMaxY() - colorBarBoxPadding;
330 return (bounds.getMaxY() / 2) - (getSizeBarBoxHeight(size) / 2);
337 private double getSizeBarBoxLeft(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
338 Rectangle2D bounds = g2d.getClipBounds();
340 throw new IllegalStateException();
343 double right = bounds.getMaxX() - colorBarBoxPadding;
344 return right - getSizeBarBoxWidth(size);
347 return colorBarBoxPadding;
351 double left = (bounds.getMaxX() / 2) - (getSizeBarBoxWidth(size) / 2);
359 private double getSizeBarBoxWidth(SizeBarsSize size) {
360 return size.getSize() * colorBarBoxWidth;
363 private double getSizeBarBoxHeight(SizeBarsSize size) {
364 return size.getSize() * colorBarBoxHeight;
368 public Rectangle2D getBoundsInLocal() {
372 public void setEnabled(boolean enabled) {
373 this.enabled = enabled;
376 public void setDynamicColoringObjects(Map<String,DynamicColorContribution> dynamicColoringObjects) {
377 this.dynamicColoringContributions = dynamicColoringObjects;
380 public void setColorBarOptions(ColorBarOptions colorBarsOptions) {
381 this.colorBarsOptions = colorBarsOptions;
384 public void setDynamicSizingObjects(Map<String, DynamicSizeContribution> dynamicSizingObjects) {
385 this.dynamicSizingContributions = dynamicSizingObjects;
388 public void setSizeBarOptions(SizeBarOptions sizeBarOptions) {
389 this.sizeBarsOptions = sizeBarOptions;