1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.adapters;
21
22 import com.cosylab.gui.displayers.DataConsumer;
23 import com.cosylab.gui.displayers.DisplayerUtilities;
24 import com.cosylab.gui.displayers.DoubleConsumer;
25 import com.cosylab.gui.displayers.DoubleSeqConsumer;
26 import com.cosylab.util.CommonException;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public abstract class SimpleConverterSupport extends AbstractConverter
46 implements Converter, DoubleConsumer, DoubleSeqConsumer
47 {
48 public static final String SHORT_NAME = "conv";
49 protected int arrayIndex=0;
50
51
52
53
54
55
56
57 public int getArrayIndex() {
58 return arrayIndex;
59 }
60
61
62
63
64
65
66
67 public void setArrayIndex(int arrayIndex) {
68 this.arrayIndex = arrayIndex;
69 }
70
71
72
73
74 public SimpleConverterSupport()
75 {
76
77 super(new Class[]{ DoubleConsumer.class });
78
79
80 name = "SimpleConverter";
81 }
82
83
84
85
86
87
88
89 public void updateValue(long timestamp, double value)
90 throws CommonException
91 {
92
93 DataConsumer[] delegates = getConsumers();
94
95
96 final double newValue = transform(value);
97
98 for (int i = 0; i < delegates.length; i++) {
99 try {
100 if (delegates[i] instanceof DoubleSeqConsumer) ((DoubleSeqConsumer)delegates[i]).updateValue(timestamp, new double[]{newValue});
101 else if (delegates[i] instanceof DoubleConsumer)((DoubleConsumer)delegates[i]).updateValue(timestamp, newValue);
102 } catch (Exception e) {
103 e.printStackTrace();
104 }
105 }
106 }
107
108 protected NonblockingNumberDispatcher getNonblDisplatecher()
109 {
110 if (nonblDispatcher == null) {
111 nonblDispatcher = new NonblockingNumberDispatcher() {
112 private static final long serialVersionUID = -2136890381501631542L;
113
114 public void updateNonblocking(Number value)
115 {
116
117 super.updateNonblocking(new Double(inverseTransform(
118 value.doubleValue())));
119 }
120 };
121 }
122
123 return nonblDispatcher;
124 }
125
126
127
128
129
130
131
132
133
134 abstract protected double transform(double value);
135
136
137
138
139
140
141
142
143
144
145
146
147 abstract protected double inverseTransform(double value);
148
149
150
151
152 public void updateValue(long timestamp, double[] value) throws CommonException {
153 DataConsumer[] delegates = getConsumers();
154
155 for (int i = 0; i < delegates.length; i++) {
156 try {
157 if (delegates[i] instanceof DoubleSeqConsumer) ((DoubleSeqConsumer)delegates[i]).updateValue(timestamp, transform(value));
158 else if (delegates[i] instanceof DoubleConsumer)((DoubleConsumer)delegates[i]).updateValue(timestamp, transform(DisplayerUtilities.extract(arrayIndex,value)));
159 } catch (Exception e) {
160 e.printStackTrace();
161 }
162 }
163 }
164
165
166 private double[] transform(double[] value) {
167 double[] d = new double[value.length];
168 for (int i = 0; i < value.length; i++) d[i] = transform(value[i]);
169 return d;
170 }
171 }
172
173