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);
122 g2d.setColor(Color.BLACK);
124 String str = Double.toString(max);
125 g2d.drawString(str, (int)(colorBarBoxLeft + colorBarBoxWidth - 30), (int)(colorBarBoxTop + colorBarBoxHeight));
127 str = Double.toString(min);
128 g2d.drawString(str, (int)colorBarBoxLeft + 1, (int)(colorBarBoxTop + colorBarBoxHeight));
131 for (RGBIntensity intensity : intensities) {
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;
138 g2d.setColor(Color.BLACK);
139 str = object.getKey() + " - " + label + " [" + unit + "]";
140 g2d.drawString(str, (int)colorBarBoxLeft + 5, (int)colorBarBoxTop + 10);
145 private double getColorBarBoxTop(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
146 Rectangle2D bounds = g2d.getClipBounds();
148 throw new IllegalStateException();
151 return colorBarBoxPadding;
154 return bounds.getMaxY() - colorBarBoxPadding;
158 return (bounds.getMaxY() / 2) - (getColorBarBoxHeight(size) / 2);
165 private double getColorBarBoxLeft(Graphics2D g2d, ColorBarsLocation location, ColorBarsSize size) {
166 Rectangle2D bounds = g2d.getClipBounds();
168 throw new IllegalStateException();
171 double right = bounds.getMaxX() - colorBarBoxPadding;
172 return right - getColorBarBoxWidth(size);
175 return colorBarBoxPadding;
179 double left = (bounds.getMaxX() / 2) - (getColorBarBoxWidth(size) / 2);
187 private double getColorBarBoxWidth(ColorBarsSize size) {
188 return size.getSize() * colorBarBoxWidth;
191 private double getColorBarBoxHeight(ColorBarsSize size) {
192 return size.getSize() * colorBarBoxHeight;
195 private void renderSizes(Graphics2D g2d) {
196 if (sizeBarsOptions == null || !sizeBarsOptions.isShowSizeBars()) {
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);
206 Rectangle2D bounds = g2d.getClipBounds();
212 for (Entry<String, DynamicSizeContribution> object : dynamicSizingContributions.entrySet()) {
213 DynamicSizeContribution cc = object.getValue();
215 double min = cc.getDefaultMin();
216 double max = cc.getDefaultMax();
217 String unit = cc.getUnit();
218 String label = cc.getLabel();
220 DynamicSizeMap map = cc.getDefaultSizeMap();
222 List<Double> sizes = map.getSizes();
224 double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * 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));
235 Rectangle2D vertical = new Rectangle2D.Double(sizeBarBoxLeft, sizeBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
238 double sizeVerticalLeft = sizeBarBoxLeft + 5;
239 double sizeVerticalTop = sizeBarBoxTop + 15;
240 double sizeVerticalHeigth = colorBarBoxHeight - 30;
242 double sizeVerticalWidth = (sizeBarBoxWidth - 10) / sizes.size();
244 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
245 g2d.setFont(rulerFont);
246 g2d.setColor(Color.BLACK);
248 String str = Double.toString(max);
249 g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight));
251 str = Double.toString(min);
252 g2d.drawString(str, (int)sizeBarBoxLeft + 1, (int)(sizeBarBoxTop + sizeBarBoxHeight));
255 g2d.setColor(new Color((float)0, (float)0, (float)0.8, 0.8f));
256 for (Double size: sizes) {
258 Ellipse2D ellipse = new Ellipse2D.Double(sizeVerticalLeft + sizeVerticalWidth / 2, (sizeVerticalTop), size * sizeVerticalHeigth, size * sizeVerticalHeigth);
261 sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth;
263 g2d.setColor(Color.BLACK);
264 str = object.getKey() + " - " + label + " [" + unit + "]";
265 g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10);
269 private double getSizeBarBoxTop(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
270 Rectangle2D bounds = g2d.getClipBounds();
272 throw new IllegalStateException();
275 return colorBarBoxPadding;
278 return bounds.getMaxY() - colorBarBoxPadding;
282 return (bounds.getMaxY() / 2) - (getSizeBarBoxHeight(size) / 2);
289 private double getSizeBarBoxLeft(Graphics2D g2d, SizeBarsLocation location, SizeBarsSize size) {
290 Rectangle2D bounds = g2d.getClipBounds();
292 throw new IllegalStateException();
295 double right = bounds.getMaxX() - colorBarBoxPadding;
296 return right - getSizeBarBoxWidth(size);
299 return colorBarBoxPadding;
303 double left = (bounds.getMaxX() / 2) - (getSizeBarBoxWidth(size) / 2);
311 private double getSizeBarBoxWidth(SizeBarsSize size) {
312 return size.getSize() * colorBarBoxWidth;
315 private double getSizeBarBoxHeight(SizeBarsSize size) {
316 return size.getSize() * colorBarBoxHeight;
320 public Rectangle2D getBoundsInLocal() {
324 public void setEnabled(boolean enabled) {
325 this.enabled = enabled;
328 public void setDynamicColoringObjects(Map<String,DynamicColorContribution> dynamicColoringObjects) {
329 this.dynamicColoringContributions = dynamicColoringObjects;
332 public void setColorBarOptions(ColorBarOptions colorBarsOptions) {
333 this.colorBarsOptions = colorBarsOptions;
336 public void setDynamicSizingObjects(Map<String, DynamicSizeContribution> dynamicSizingObjects) {
337 this.dynamicSizingContributions = dynamicSizingObjects;
340 public void setSizeBarOptions(SizeBarOptions sizeBarOptions) {
341 this.sizeBarsOptions = sizeBarOptions;