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 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);
132 double value = min + j * interval;
133 String str = Double.toString(value);
134 if (str.length() > 4) {
135 str = str.substring(0, 3);
137 g2d.setColor(Color.BLACK);
138 g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight));
139 colorVerticalLeft = colorVerticalLeft + colorVerticalWidth;
141 g2d.setColor(Color.BLACK);
143 String str = Double.toString(max);
144 g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight));
146 str = object.getKey() + " - " + label + " [" + unit + "]";
147 g2d.drawString(str, (float)colorBarBoxLeft + 5, (float)colorBarBoxTop + 10);
152 private double getColorBarBoxTop(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
153 Rectangle2D bounds = g2d.getClipBounds();
155 throw new IllegalStateException();
158 return colorBarBoxPadding;
161 return bounds.getMaxY() - colorBarBoxPadding;
165 return (bounds.getMaxY() / 2) - (getColorBarBoxHeight(size) / 2);
172 private double getColorBarBoxLeft(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
173 Rectangle2D bounds = g2d.getClipBounds();
175 throw new IllegalStateException();
178 double right = bounds.getMaxX() - colorBarBoxPadding;
179 return right - getColorBarBoxWidth(size);
182 return colorBarBoxPadding;
186 double left = (bounds.getMaxX() / 2) - (getColorBarBoxWidth(size) / 2);
194 private double getColorBarBoxWidth(ColorBarsSize size) {
195 return size.getSize() * colorBarBoxWidth;
198 private double getColorBarBoxHeight(ColorBarsSize size) {
199 return size.getSize() * colorBarBoxHeight;
202 private void renderSizes(Graphics2D g2d) {
203 if (sizeBarsOptions == null || !sizeBarsOptions.isShowSizeBars()) {
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);
213 Rectangle2D bounds = g2d.getClipBounds();
219 if (dynamicSizingContributions != null) {
220 for (Entry<String, DynamicSizeContribution> object : dynamicSizingContributions.entrySet()) {
221 DynamicSizeContribution cc = object.getValue();
226 double min = cc.getDefaultMin();
227 double max = cc.getDefaultMax();
228 String unit = cc.getUnit();
229 String label = cc.getLabel();
231 DynamicSizeMap map = cc.getDefaultSizeMap();
233 List<Double> sizes = map.getSizes();
235 double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * i));
238 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
239 g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
241 Rectangle2D vertical = new Rectangle2D.Double(sizeBarBoxLeft, sizeBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
244 double sizeVerticalLeft = sizeBarBoxLeft + 5;
245 double sizeVerticalTop = sizeBarBoxTop + 15;
246 double sizeVerticalHeigth = colorBarBoxHeight - 30;
248 double sizeVerticalWidth = (sizeBarBoxWidth - 10) / sizes.size();
250 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
251 g2d.setFont(rulerFont);
253 double interval = (max - min) / sizes.size();
255 for (int j = 0; j < sizes.size(); j++) {
257 Double size = sizes.get(j);
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);
264 double value = min + j * interval;
265 String str = Double.toString(value);
266 if (str.length() > 4) {
267 str = str.substring(0, 3);
269 g2d.setColor(Color.BLACK);
270 g2d.drawString(str, (float)(sizeVerticalLeft - 8), (float)(sizeBarBoxTop + sizeBarBoxHeight));
272 sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth;
274 g2d.setColor(Color.BLACK);
276 String str = Double.toString(max);
277 g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight));
279 str = object.getKey() + " - " + label + " [" + unit + "]";
280 g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10);
285 private double getSizeBarBoxTop(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
286 Rectangle2D bounds = g2d.getClipBounds();
288 throw new IllegalStateException();
291 return colorBarBoxPadding;
294 return bounds.getMaxY() - colorBarBoxPadding;
298 return (bounds.getMaxY() / 2) - (getSizeBarBoxHeight(size) / 2);
305 private double getSizeBarBoxLeft(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
306 Rectangle2D bounds = g2d.getClipBounds();
308 throw new IllegalStateException();
311 double right = bounds.getMaxX() - colorBarBoxPadding;
312 return right - getSizeBarBoxWidth(size);
315 return colorBarBoxPadding;
319 double left = (bounds.getMaxX() / 2) - (getSizeBarBoxWidth(size) / 2);
327 private double getSizeBarBoxWidth(SizeBarsSize size) {
328 return size.getSize() * colorBarBoxWidth;
331 private double getSizeBarBoxHeight(SizeBarsSize size) {
332 return size.getSize() * colorBarBoxHeight;
336 public Rectangle2D getBoundsInLocal() {
340 public void setEnabled(boolean enabled) {
341 this.enabled = enabled;
344 public void setDynamicColoringObjects(Map<String,DynamicColorContribution> dynamicColoringObjects) {
345 this.dynamicColoringContributions = dynamicColoringObjects;
348 public void setColorBarOptions(ColorBarOptions colorBarsOptions) {
349 this.colorBarsOptions = colorBarsOptions;
352 public void setDynamicSizingObjects(Map<String, DynamicSizeContribution> dynamicSizingObjects) {
353 this.dynamicSizingContributions = dynamicSizingObjects;
356 public void setSizeBarOptions(SizeBarOptions sizeBarOptions) {
357 this.sizeBarsOptions = sizeBarOptions;