1 package org.simantics.diagram.profile;
3 import java.awt.Graphics2D;
4 import java.awt.geom.AffineTransform;
5 import java.awt.geom.Rectangle2D;
7 import org.simantics.datatypes.literal.RGB;
8 import org.simantics.datatypes.literal.Vec2d;
9 import org.simantics.diagram.elements.SVGNode;
10 import org.simantics.diagram.internal.Activator;
11 import org.simantics.diagram.profile.IconResult.A;
12 import org.simantics.diagram.profile.IconResult.B;
14 public class IconNode extends IconButtonNode {
17 NO, INCREASE, DECREASE
21 private SVGNode lo = new SVGNode();
22 private SVGNode mid = new SVGNode();
23 private SVGNode hi = new SVGNode();
25 private SVGNode current;
27 private static final long serialVersionUID = -7850188341977866325L;
28 private IconNode.Blink blink = Blink.NO;
29 private double previousValue = Double.NaN;
30 private long previousValueTime = System.nanoTime();
32 private void setBlink(IconNode.Blink blink) {
34 if(Blink.NO == blink && Blink.NO != this.blink) Updater.getInstance().unregister(this);
36 if(Blink.NO != blink && Blink.NO == this.blink) Updater.getInstance().register(this);
41 private void setValue(double value) {
43 if(value < config.loValue) {
46 } else if (value > config.hiValue) {
51 if(value-previousValue > 1e-6) setBlink(Blink.INCREASE);
52 else if(previousValue-value > 1e-6) setBlink(Blink.DECREASE);
53 else setBlink(Blink.NO);
55 previousValue = value;
56 previousValueTime = System.nanoTime();
60 private void setConfig(B config) {
62 if(this.config == config) return;
64 if(config.loColor != null) {
65 String loIcon = createSVG(config.iconName, config.loColor, config.size);
68 if(config.midColor != null) {
69 String midIcon = createSVG(config.iconName, config.midColor, config.size);
72 if(config.hiColor != null) {
73 String hiIcon = createSVG(config.iconName, config.hiColor, config.size);
81 public void setA(A data) {
83 setConfig(data.config);
89 public void cleanup() {
90 Updater.getInstance().unregister(this);
95 public void render(Graphics2D g2d) {
97 AffineTransform ot = null;
98 if (!transform.isIdentity()) {
99 ot = g2d.getTransform();
100 g2d.transform(transform);
103 if(Blink.NO == blink) {
106 long time = Updater.getInstance().getTime();
108 // Sanity check: turn blink of when no data for 2 seconds.
109 if(time-previousValueTime > 2000000000L) {
113 long halfSeconds = time / 500000000L;
114 if((halfSeconds & 1) == 0) {
117 if(Blink.INCREASE == blink) {
128 g2d.setTransform(ot);
132 Rectangle2D EMPTY = new Rectangle2D.Double(0, 0, 0, 0);
135 public Rectangle2D getBoundsInLocal() {
140 void setData(IconButtonResult data) {
141 IconResult ir = (IconResult)data;
145 public String createSVG(String iconName, RGB.Integer rgb, Vec2d scale) {
146 return Activator.ICON_PROVIDER.apply(iconName, rgb, scale);