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