]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/IconNode.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / IconNode.java
1 package org.simantics.diagram.profile;
2
3 import java.awt.Graphics2D;
4 import java.awt.geom.AffineTransform;
5 import java.awt.geom.Rectangle2D;
6
7 import org.simantics.datatypes.literal.RGB;
8 import org.simantics.datatypes.literal.Vec2d;
9 import org.simantics.diagram.internal.Activator;
10 import org.simantics.diagram.profile.IconResult.A;
11 import org.simantics.diagram.profile.IconResult.B;
12 import org.simantics.scenegraph.g2d.nodes.SVGNode;
13
14 public class IconNode extends IconButtonNode {
15         
16         enum Blink {
17                 NO, INCREASE, DECREASE
18         }
19         
20         private B config;
21         private SVGNode lo = new SVGNode();
22         private SVGNode mid = new SVGNode();
23         private SVGNode hi = new SVGNode();
24
25         private SVGNode current;
26         
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();
31         
32         private void setBlink(IconNode.Blink blink) {
33                 // Was turned off
34                 if(Blink.NO == blink && Blink.NO != this.blink) Updater.getInstance().unregister(this);
35                 // Was turned on
36                 if(Blink.NO != blink && Blink.NO == this.blink) Updater.getInstance().register(this);
37                 // Record state
38                 this.blink = blink;
39         }
40         
41         private void setValue(double value) {
42                 
43                 if(value < config.loValue) {
44                         current = lo;
45                         setBlink(Blink.NO);
46                 } else if (value > config.hiValue) {
47                         current = hi;
48                         setBlink(Blink.NO);
49                 } else {
50                         current = mid;
51                         if(value-previousValue > 1e-6) setBlink(Blink.INCREASE);
52                         else if(previousValue-value > 1e-6) setBlink(Blink.DECREASE);
53                         else setBlink(Blink.NO);
54                 }
55                 previousValue = value;
56                 previousValueTime = System.nanoTime();
57                 
58         }
59         
60         private void setConfig(B config) {
61
62                 if(this.config == config) return;
63                 
64                 if(config.loColor != null) {
65                 String loIcon = createSVG(config.iconName, config.loColor, config.size);
66                 lo.setData(loIcon);
67                 }
68                 if(config.midColor != null) {
69                 String midIcon = createSVG(config.iconName, config.midColor, config.size);
70                 mid.setData(midIcon);
71                 }
72                 if(config.hiColor != null) {
73                 String hiIcon = createSVG(config.iconName, config.hiColor, config.size);
74                 hi.setData(hiIcon);
75                 }
76         
77         this.config = config;
78                 
79         }
80         
81         public void setA(A data) {
82
83                 setConfig(data.config);
84         setValue(data.value);
85                 
86         }
87         
88     @Override
89     public void cleanup() {
90                 Updater.getInstance().unregister(this);
91         super.cleanup();
92     }
93         
94     @Override
95     public void render(Graphics2D g2d) {
96
97                 AffineTransform ot = null;
98                 if (!transform.isIdentity()) {
99                         ot = g2d.getTransform();
100                         g2d.transform(transform);
101                 }
102         
103         if(Blink.NO == blink) {
104                 current.render(g2d);
105         } else {
106                 long time = Updater.getInstance().getTime();
107                 
108                 // Sanity check: turn blink of when no data for 2 seconds.
109                 if(time-previousValueTime > 2000000000L) {
110                         mid.render(g2d);
111                         setBlink(Blink.NO);
112                 } else {
113                         long halfSeconds = time / 500000000L; 
114                         if((halfSeconds & 1) == 0) {
115                                 mid.render(g2d);
116                         } else {
117                                 if(Blink.INCREASE == blink) {
118                                         hi.render(g2d);
119                                 } else {
120                                         lo.render(g2d);
121                                 }
122                         }
123                 }
124                 
125         }
126
127                 if (ot != null)
128                         g2d.setTransform(ot);
129         
130     }
131
132     Rectangle2D EMPTY = new Rectangle2D.Double(0, 0, 0, 0);
133     
134         @Override
135         public Rectangle2D getBoundsInLocal() {
136                 return EMPTY;
137         }
138
139         @Override
140         void setData(IconButtonResult data) {
141                 IconResult ir = (IconResult)data;
142                 setA(ir.getA());
143         }
144         
145     public String createSVG(String iconName, RGB.Integer rgb, Vec2d scale) {
146         return Activator.ICON_PROVIDER.apply(iconName, rgb, scale);
147     }
148         
149 }