1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.desy.acop.displayers.tools;
24
25 import java.awt.Color;
26 import java.beans.Beans;
27 import java.util.Map;
28
29 import com.cosylab.gui.displayers.CommonDisplayer;
30 import com.cosylab.gui.displayers.DataConsumer;
31 import com.cosylab.gui.displayers.DataState;
32 import com.cosylab.gui.displayers.Displayer;
33 import com.cosylab.gui.displayers.DisplayerUtilities;
34 import com.cosylab.gui.displayers.DoubleSeqConsumer;
35 import com.cosylab.util.CommonException;
36
37 import de.desy.acop.chart.AcopGraphStyleEnum;
38 import de.desy.acop.displayers.AcopChart;
39
40
41
42
43
44
45
46
47
48
49
50 public class AcopChartConsumer implements DoubleSeqConsumer {
51
52 @SuppressWarnings("unchecked")
53 public static final Class[] SUPPORTED_CONSUMER_TYPES = {
54 DoubleSeqConsumer.class
55 };
56 public static final String[] SUPPORTED_CHARACTERISTICS = {
57 CommonDisplayer.C_GRAPH_MAX, CommonDisplayer.C_GRAPH_MIN
58 };
59
60 protected String name;
61 protected AcopChart acop;
62
63 protected double[] xData;
64 protected double[] yData;
65 protected int displayHandle = -1;
66 protected int textHandle = -1;
67 protected long lastTimestamp;
68 protected Color color= Color.RED;
69 protected String[] xLabels;
70 protected Object disable;
71 protected int arraySize;
72 protected int maxNumber;
73
74 private boolean isTrendChart = false;
75
76 private double preferredYMax;
77 private double preferredYMin;
78
79 private AcopGraphParameters displayerParameters;
80
81
82
83
84
85
86
87
88 public AcopChartConsumer(AcopChart acop, String name) {
89 super();
90 this.name=name;
91 this.acop=acop;
92 preferredYMax = acop.getYMax();
93 preferredYMin = acop.getYMin();
94
95
96 }
97
98
99
100
101 @SuppressWarnings("unchecked")
102 public DataConsumer getDataConsumer(Class type) {
103 if (type.isAssignableFrom(getClass())) {
104 return this;
105 }
106 return null;
107 }
108
109
110
111
112 public DataConsumer getDefaultDataConsumer() {
113 return this;
114 }
115
116
117
118
119 public void updateDataState(DataState state) {
120
121
122 }
123
124
125
126
127 public void setCharacteristics(Map characteristics) {
128
129 if (acop == null) {
130 return;
131 }
132
133 Object o1 = null;
134 Object o2 = null;
135
136 o1 = characteristics.get(CommonDisplayer.C_GRAPH_MAX);
137
138 if (o1 == null) {
139 o1 = characteristics.get(CommonDisplayer.C_MAXIMUM);
140 }
141
142 o2 = characteristics.get(CommonDisplayer.C_GRAPH_MIN);
143
144 if (o2 == null) {
145 o2 = characteristics.get(CommonDisplayer.C_MINIMUM);
146 }
147
148 if (o1 != null || o2 != null) {
149 double max = o1 != null ? ((Number)o1).doubleValue()
150 : acop.getYMax();
151 double min = o2 != null ? ((Number)o2).doubleValue()
152 : acop.getYMin();
153
154 setPreferredYMax(max);
155 setPreferredYMin(min);
156 acop.updateYScale(this);
157 }
158
159 o1 = characteristics.get(CommonDisplayer.C_DISPLAY_NAME);
160
161 String newName = (String) o1;
162 if (newName != null && !newName.equals(acop.getName())) {
163 acop.setName(newName);
164 acop.setCaption(newName);
165 }
166
167 o1 = characteristics.get(Displayer.C_UNITS);
168 if (o1 != null) {
169 acop.setYAxisLabel((String)o1);
170 }
171
172
173
174 o1 = characteristics.get(Displayer.C_COLOR);
175
176 if (o1 != null) {
177 color = (Color) o1;
178 acop.setForeground(color);
179 }
180
181 o1 = characteristics.get("xLabels");
182 if (o1 != null && !isTrendChart) {
183 setXLabels((String[])o1);
184 }
185
186 o1 = characteristics.get(CommonDisplayer.C_SEQUENCE_LENGTH);
187 if (arraySize == 0 || arraySize == -1 && o1 != null) {
188 setArraySize((Integer)o1);
189 }
190
191 if (!Beans.isDesignTime()) {
192 acop.updateXScale(this);
193 }
194 acop.repaint();
195
196 }
197
198
199
200
201 public String getName() {
202 return name;
203 }
204
205
206
207
208 public String[] getSupportedCharacteristics() {
209 return DisplayerUtilities.combineCharacteristics(DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_CHARACTERISTICS,
210 SUPPORTED_CHARACTERISTICS);
211 }
212
213
214
215
216 @SuppressWarnings("unchecked")
217 public Class<DataConsumer>[] getSupportedConsumerTypes() {
218 return SUPPORTED_CONSUMER_TYPES;
219 }
220
221 private void updateChart() {
222 if (acop==null) {
223 return;
224 }
225
226 synchronized (acop) {
227
228
229
230
231
232
233 if (displayHandle < 0)
234 {
235 applyParameters();
236 displayHandle = acop.draw(yData,xData,disable,xLabels,arraySize,maxNumber);
237
238 }
239 else
240 {
241 if (!isTrendChart)
242 {
243 acop.refreshScreen(yData, displayHandle, arraySize, 0, xData);
244
245 }
246 else
247 {
248 double[] yValue = new double[1];
249 yValue[0] = yData[0];
250 double[] xValue = new double[1];
251 xValue[0] = lastTimestamp/1000.;
252
253
254 acop.appendScreen(yData, displayHandle, xValue);
255
256
257
258
259
260
261 }
262 }
263
264
265
266
267
268 }
269
270 }
271
272
273
274
275
276 public double[][] dumpData() {
277
278 double[][] array= new double[2][arraySize];
279
280 acop.getDrawnData(array[0], array[1], arraySize, 0, displayHandle);
281
282 return array;
283 }
284
285
286
287
288
289
290 public void destroy() {
291 acop.clearScreen(displayHandle);
292 acop=null;
293 }
294
295
296
297
298 public void updateValue(long timestamp, double[] value) throws CommonException {
299 lastTimestamp=timestamp;
300 yData=value;
301 updateChart();
302 }
303
304
305
306
307
308
309 public double[] getXData() {
310 return xData;
311 }
312
313
314
315
316
317
318 public void setXData(double[] xdata) {
319 this.xData = xdata;
320 }
321
322
323
324
325
326
327 public double[] getYData() {
328 return yData;
329 }
330
331
332
333
334
335
336 public void setYData(double[] data) {
337 yData = data;
338 }
339
340
341
342
343
344
345 public AcopChart getAcop() {
346 return acop;
347 }
348
349
350
351
352
353
354 public long getLastTimestamp() {
355 return lastTimestamp;
356 }
357
358
359
360
361
362
363 public Color getColor() {
364 return color;
365 }
366
367
368
369
370
371
372
373 public void setColor(Color color) {
374 this.color = color;
375 }
376
377
378
379
380
381
382 public String[] getXLabels() {
383 return xLabels;
384 }
385
386
387
388
389
390
391
392 public void setXLabels(String[] labels) {
393 xLabels = labels;
394 }
395
396
397
398
399
400
401 public int getArraySize() {
402 return arraySize;
403 }
404
405
406
407
408
409
410
411 public void setArraySize(int arraySize) {
412 this.arraySize = arraySize;
413 }
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448 public double getPreferredYMax() {
449 return preferredYMax;
450 }
451
452
453
454
455
456
457 public void setPreferredYMax(double preferredYMax) {
458 this.preferredYMax = preferredYMax;
459 }
460
461
462
463
464
465 public double getPreferredYMin() {
466 return preferredYMin;
467 }
468
469
470
471
472
473 public void setPreferredYMin(double preferredYMin) {
474 this.preferredYMin = preferredYMin;
475 }
476
477 private void applyParameters() {
478 if (displayerParameters != null) {
479 acop.setForeground(displayerParameters.getColor());
480 acop.setDrawStyle(displayerParameters.getDrawStyle());
481 acop.setFFT(displayerParameters.getFFT());
482 acop.setDisplayMode(displayerParameters.getMode());
483 acop.setDrawWidth(displayerParameters.getWidth());
484 if (displayerParameters.isTrend()) {
485 acop.setGraphStyle(AcopGraphStyleEnum.TimeLin.ordinal());
486 double[] data = new double[displayerParameters.getTrendLength()];
487 if (yData != null) {
488 for (int i = 0; i < yData.length && i < data.length; i++) {
489 data[i] = yData[i];
490 }
491 }
492 yData = data;
493 maxNumber = data.length;
494 }
495 } else if (color != null) {
496 acop.setForeground(color);
497 }
498 }
499
500
501
502
503
504
505 public AcopGraphParameters getDisplayerParameters() {
506 return displayerParameters;
507 }
508
509
510
511
512
513
514
515
516 public void setDisplayerParameters(AcopGraphParameters displayerParameters) {
517 this.displayerParameters = displayerParameters;
518 this.color = displayerParameters.getColor() != null ? displayerParameters.getColor() : this.color;
519 setTrendChart(displayerParameters.isTrend());
520 if (isTrendChart()) {
521 setArraySize(displayerParameters.getTrendLength());
522 } else {
523 setArraySize(displayerParameters.getConnectionParameters().getPropertySize());
524 }
525 }
526
527
528
529
530
531
532 public boolean isTrendChart() {
533 return isTrendChart;
534 }
535
536
537
538
539
540
541
542 public void setTrendChart(boolean isTrendChart) {
543 this.isTrendChart = isTrendChart;
544 }
545
546 }