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 java.util.HashMap;
23 import java.util.Map;
24
25 import com.cosylab.gui.displayers.CommonDisplayer;
26 import com.cosylab.gui.displayers.DoubleConsumer;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 @SuppressWarnings("unchecked")
48 public class LinearConverter extends SimpleConverterSupport implements DoubleConsumer
49 {
50
51 private static final long serialVersionUID = -9041321592219301979L;
52
53 public static final String SHORT_NAME = "Linear";
54
55
56 private double multiplicationFactor = 1.0;
57
58
59 private double shift = 0.0;
60
61
62
63
64 public LinearConverter()
65 {
66 this(1.0, 0.0);
67 }
68
69
70
71
72
73
74
75 public LinearConverter(double factor, double shift)
76 {
77
78 setMultiplicationFactor(factor);
79 setShift(shift);
80
81
82 name = "LinearConverter";
83 }
84
85
86
87
88
89
90
91 public void setCharacteristics(Map characteristics)
92 {
93
94
95 cacheLastCharacteristics(characteristics);
96
97 HashMap map = new HashMap(characteristics);
98
99 double min;
100 double max;
101
102 if (map.containsKey(CommonDisplayer.C_GRAPH_MIN)
103 && map.containsKey(CommonDisplayer.C_GRAPH_MAX)) {
104 min = ((Number)map.get(CommonDisplayer.C_GRAPH_MIN)).doubleValue();
105 max = ((Number)map.get(CommonDisplayer.C_GRAPH_MAX)).doubleValue();
106
107 if (multiplicationFactor < 0) {
108 double a = max;
109 max = min;
110 min = a;
111 }
112
113 map.put(CommonDisplayer.C_GRAPH_MIN, new Double(transform(min)));
114 map.put(CommonDisplayer.C_GRAPH_MAX, new Double(transform(max)));
115 }
116
117 if (map.containsKey(CommonDisplayer.C_MINIMUM)
118 && map.containsKey(CommonDisplayer.C_MAXIMUM)) {
119 min = ((Number)map.get(CommonDisplayer.C_MINIMUM)).doubleValue();
120 max = ((Number)map.get(CommonDisplayer.C_MAXIMUM)).doubleValue();
121
122 if (multiplicationFactor < 0) {
123 double a = max;
124 max = min;
125 min = a;
126 }
127
128 map.put(CommonDisplayer.C_MINIMUM, new Double(transform(min)));
129 map.put(CommonDisplayer.C_MAXIMUM, new Double(transform(max)));
130 }
131
132 map.put(CommonDisplayer.C_UNITS,
133 "1/" + multiplicationFactor + "*" + map.get(CommonDisplayer.C_UNITS));
134
135 super.setCharacteristics(map);
136 }
137
138
139
140
141
142
143 public double getMultiplicationFactor()
144 {
145 return multiplicationFactor;
146 }
147
148
149
150
151
152
153 public void setMultiplicationFactor(double d)
154 {
155 multiplicationFactor = d;
156
157 if (getLastCharacteristics() != null) {
158
159 setCharacteristics(getLastCharacteristics());
160 }
161 }
162
163
164
165
166
167
168 protected double transform(double value)
169 {
170 return value * multiplicationFactor + shift;
171 }
172
173
174
175
176
177
178 protected double inverseTransform(double value)
179 {
180 return (value - shift) / multiplicationFactor;
181 }
182
183
184
185
186
187
188 public double getShift()
189 {
190 return shift;
191 }
192
193
194
195
196
197
198 public void setShift(double d)
199 {
200 shift = d;
201
202 if (getLastCharacteristics() != null) {
203
204 setCharacteristics(getLastCharacteristics());
205 }
206 }
207
208
209
210
211
212 public String getName(){
213 return SHORT_NAME;
214 }
215
216
217
218
219
220 public String toString(){
221 return SHORT_NAME + ": " + getMultiplicationFactor() + ", " + getShift();
222 }
223
224
225
226
227
228 @Override
229 public boolean equals(Object obj) {
230 if (!(obj instanceof LinearConverter)) return false;
231 LinearConverter c = (LinearConverter)obj;
232 return c.getMultiplicationFactor() == multiplicationFactor && c.getShift() == shift;
233 }
234
235 }
236
237