1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.displayers;
21
22 import java.beans.PropertyVetoException;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.ArrayList;
25 import java.util.BitSet;
26 import java.util.Map;
27
28 import javax.swing.JComponent;
29
30 import com.cosylab.gui.adapters.Converter;
31 import com.cosylab.gui.util.UserSettingsProtection;
32 import com.cosylab.util.BitCondition;
33
34
35
36
37
38
39
40
41
42
43 public final class DisplayerUtilities
44 {
45 private final static boolean debug = false;
46
47
48
49
50
51 public static final String[] COMMON_NUMERIC_DISPLAYER_CHARACTERISTICS = {
52 CommonDisplayer.C_MINIMUM, CommonDisplayer.C_MAXIMUM,
53 CommonDisplayer.C_FORMAT, CommonDisplayer.C_UNITS,
54 CommonDisplayer.C_DISPLAY_NAME, Displayer.C_EDITABLE
55 };
56
57
58
59
60
61 public static final String[] COMMON_NUMERIC_DISPLAYER_PROPERTIES= {
62 CommonDisplayer.C_MINIMUM, CommonDisplayer.C_MAXIMUM,
63 CommonDisplayer.C_FORMAT, CommonDisplayer.C_UNITS,
64 "title", Displayer.C_EDITABLE
65 };
66
67
68
69
70
71 public static final String[] PATTERN_DISPLAYER_CHARACTERISTICS = {
72 CommonDisplayer.C_DISPLAY_NAME,
73 CommonDisplayer.C_BIT_DESCRIPTIONS,
74 CommonDisplayer.C_BIT_MASK,
75 CommonDisplayer.C_CONDITION_WHEN_CLEARED,
76 CommonDisplayer.C_CONDITION_WHEN_SET
77 };
78
79
80 public static final String[] COMMON_OBJECT_DISPLAYER_CHARACTERISTICS = { };
81
82
83
84
85
86
87
88
89
90 public static final String[] combineCharacteristics(String[] char1,
91 String[] char2)
92 {
93 ArrayList<String> l = new ArrayList<String>((char1 != null ? char1.length : 0)
94 + (char2 != null ? char2.length : 0));
95
96 if (char1 != null) {
97 for (int i = 0; i < char1.length; i++) {
98 l.add(char1[i]);
99 }
100 }
101
102 if (char2 != null) {
103 for (int i = 0; i < char2.length; i++) {
104 l.add(char2[i]);
105 }
106 }
107
108 return (String[])l.toArray(new String[l.size()]);
109 }
110
111
112
113
114
115
116
117
118
119
120 public static final String[] combineCharacteristics(String[] char1,
121 String charName)
122 {
123 ArrayList<String> l = new ArrayList<String>((char1 != null ? char1.length : 0)
124 + (charName != null ? 1 : 0));
125
126 if (char1 != null) {
127 for (int i = 0; i < char1.length; i++) {
128 l.add(char1[i]);
129 }
130 }
131
132 if (charName != null) {
133 l.add(charName);
134 }
135
136 return (String[])l.toArray(new String[l.size()]);
137 }
138
139 private DisplayerUtilities()
140 {
141 super();
142 }
143
144
145
146
147
148
149
150
151
152
153 public static final void setCharacteristics(Map characteristics,
154 DoubleDisplayer disp)
155 {
156 if (debug) {
157 System.out.println("[DispUtilities::setChar] setting to '" + disp
158 + "' characteristics " + characteristics);
159 }
160
161 disp.suspend();
162
163 Double min = 0.0;
164 Double max = 1.0;
165
166 Object oMin = characteristics.get(CommonDisplayer.C_MINIMUM);
167
168 if (oMin != null) {
169 min = (((Number)oMin).doubleValue());
170 }
171
172 Object oMax = characteristics.get(CommonDisplayer.C_MAXIMUM);
173
174 if (oMax != null) {
175 max = (((Number)oMax).doubleValue());
176 }
177
178 if (oMax != null && max<disp.getMinimum()) {
179 if (oMin != null) {
180 try {
181 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MINIMUM,min);
182 } catch (Exception e) {
183 e.printStackTrace();
184 }
185 disp.setValue(min);
186 } else {
187 try {
188 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MINIMUM,max);
189 } catch (Exception e) {
190 e.printStackTrace();
191 }
192 disp.setValue(max);
193 }
194 try {
195 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MAXIMUM,max);
196 } catch (Exception e) {
197 e.printStackTrace();
198 }
199 } else if (oMin != null && min>disp.getMaximum()) {
200 if (oMax != null) {
201 try {
202 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MAXIMUM,max);
203 } catch (Exception e) {
204 e.printStackTrace();
205 }
206 } else {
207 try {
208 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MAXIMUM,min);
209 } catch (Exception e) {
210 e.printStackTrace();
211 }
212 }
213 disp.setValue(min);
214 try {
215 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MINIMUM,min);
216 } catch (Exception e) {
217 e.printStackTrace();
218 }
219 } else {
220 if (oMin != null) {
221 try {
222 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MINIMUM,min);
223 } catch (Exception e) {
224 e.printStackTrace();
225 }
226 }
227 if (oMax != null) {
228 try {
229 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MAXIMUM,max);
230 } catch (Exception e) {
231 e.printStackTrace();
232 }
233 }
234 }
235
236 Object o = characteristics.get(CommonDisplayer.C_FORMAT);
237
238 if (o != null) {
239 try {
240 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_FORMAT,o.toString());
241 } catch (Exception e) {
242 e.printStackTrace();
243 }
244 }
245
246 o = characteristics.get(CommonDisplayer.C_UNITS);
247
248 if (o != null) {
249 try {
250 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_UNITS,o.toString());
251 } catch (Exception e) {
252 e.printStackTrace();
253 }
254 }
255
256 o = characteristics.get(CommonDisplayer.C_DISPLAY_NAME);
257
258 if (o != null) {
259 try {
260 UserSettingsProtection.setUnprotected((JComponent)disp,"title",o.toString());
261 } catch (Exception e) {
262 e.printStackTrace();
263 }
264 }
265
266
267
268 o = characteristics.get(Displayer.C_EDITABLE);
269
270 if (o != null) {
271 try {
272 UserSettingsProtection.setUnprotected((JComponent)disp,"editable",o);
273 } catch (NoSuchMethodException ex) {
274
275 } catch (IllegalAccessException ex) {
276 ex.printStackTrace();
277 } catch (InvocationTargetException ex) {
278 ex.printStackTrace();
279 }
280 }
281
282 disp.resume();
283
284 if (debug) {
285 System.out.println("[DispUtilities::setChar] test: min= "
286 + disp.getMinimum() + " max= " + disp.getMaximum() + " title= "
287 + disp.getTitle());
288 }
289 }
290
291
292
293
294
295
296
297
298
299
300 public static final void setCharacteristics(Map characteristics,
301 LongDisplayer disp)
302 {
303 if (debug) {
304 System.out.println("[DispUtilities::setChar] setting to '" + disp
305 + "' characteristics " + characteristics);
306 }
307
308 disp.suspend();
309
310 long min = 0;
311 long max = 1;
312
313 Object oMin = characteristics.get(CommonDisplayer.C_MINIMUM);
314
315 if (oMin != null) {
316 min = (((Number)oMin).longValue());
317 }
318
319 Object oMax = characteristics.get(CommonDisplayer.C_MAXIMUM);
320
321 if (oMax != null) {
322 max = (((Number)oMax).longValue());
323 }
324
325 if (oMax != null && max<disp.getMinimum()) {
326 if (oMin != null) {
327 try {
328 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MINIMUM,min);
329 } catch (Exception e) {
330 e.printStackTrace();
331 }
332 disp.setValue(min);
333 } else {
334 try {
335 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MINIMUM,max);
336 } catch (Exception e) {
337 e.printStackTrace();
338 }
339 disp.setValue(max);
340 }
341 try {
342 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MAXIMUM,max);
343 } catch (Exception e) {
344 e.printStackTrace();
345 }
346 } else if (oMin != null && min>disp.getMaximum()) {
347 if (oMax != null) {
348 try {
349 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MAXIMUM,max);
350 } catch (Exception e) {
351 e.printStackTrace();
352 }
353 } else {
354 try {
355 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MAXIMUM,min);
356 } catch (Exception e) {
357 e.printStackTrace();
358 }
359 }
360 disp.setValue(min);
361 try {
362 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MINIMUM,min);
363 } catch (Exception e) {
364 e.printStackTrace();
365 }
366 } else {
367 if (oMin != null) {
368 try {
369 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MINIMUM,min);
370 } catch (Exception e) {
371 e.printStackTrace();
372 }
373 }
374 if (oMax != null) {
375 try {
376 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_MAXIMUM,max);
377 } catch (Exception e) {
378 e.printStackTrace();
379 }
380 }
381 }
382
383 Object o = characteristics.get(CommonDisplayer.C_FORMAT);
384
385 if (o != null) {
386 try {
387 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_FORMAT,o.toString());
388 } catch (Exception e) {
389 e.printStackTrace();
390 }
391 }
392
393 o = characteristics.get(CommonDisplayer.C_UNITS);
394
395 if (o != null) {
396 try {
397 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_UNITS,o.toString());
398 } catch (Exception e) {
399 e.printStackTrace();
400 }
401 }
402
403 o = characteristics.get(CommonDisplayer.C_DISPLAY_NAME);
404
405 if (o != null) {
406 try {
407 UserSettingsProtection.setUnprotected((JComponent)disp,"title",o.toString());
408 } catch (Exception e) {
409 e.printStackTrace();
410 }
411 }
412
413
414
415 o = characteristics.get(Displayer.C_EDITABLE);
416
417 if (o != null) {
418 try {
419 UserSettingsProtection.setUnprotected((JComponent)disp,"editable",o);
420 } catch (NoSuchMethodException ex) {
421
422 } catch (IllegalAccessException ex) {
423 ex.printStackTrace();
424 } catch (InvocationTargetException ex) {
425 ex.printStackTrace();
426 }
427 }
428
429 disp.resume();
430
431 if (debug) {
432 System.out.println("[DispUtilities::setChar] test: min= "
433 + disp.getMinimum() + " max= " + disp.getMaximum() + " title= "
434 + disp.getTitle());
435 }
436 }
437
438
439
440
441
442
443
444
445
446
447 public static final void setCharacteristics(Map characteristics,
448 PatternDisplayer disp)
449 {
450 if (debug) {
451 System.out.println("[DispUtilities::setChar] setting to '" + disp
452 + "' characteristics " + characteristics);
453 }
454
455 disp.suspend();
456
457 Object o = characteristics.get(CommonDisplayer.C_FORMAT);
458
459 if (o != null) {
460 try {
461 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_FORMAT,o.toString());
462 } catch (Exception e) {
463 e.printStackTrace();
464 }
465 }
466
467 o = characteristics.get(CommonDisplayer.C_UNITS);
468
469 if (o != null) {
470 try {
471 UserSettingsProtection.setUnprotected((JComponent)disp,CommonDisplayer.C_UNITS,o.toString());
472 } catch (Exception e) {
473 e.printStackTrace();
474 }
475 }
476
477 o = characteristics.get(CommonDisplayer.C_DISPLAY_NAME);
478
479 if (o != null) {
480 try {
481 UserSettingsProtection.setUnprotected((JComponent)disp,"title",o.toString());
482 } catch (Exception e) {
483 e.printStackTrace();
484 }
485 }
486
487
488
489 o = characteristics.get(Displayer.C_EDITABLE);
490
491 if (o != null) {
492 try {
493 UserSettingsProtection.setUnprotected((JComponent)disp,"editable",o);
494 } catch (NoSuchMethodException ex) {
495
496 } catch (IllegalAccessException ex) {
497 ex.printStackTrace();
498 } catch (InvocationTargetException ex) {
499 ex.printStackTrace();
500 }
501 }
502
503 o = characteristics.get(CommonDisplayer.C_BIT_MASK);
504
505 if (o != null) {
506 try {
507 UserSettingsProtection.setUnprotected((JComponent)disp,"bitMask",(BitSet)o);
508 } catch (Exception e) {
509 e.printStackTrace();
510 }
511 }
512
513 o = characteristics.get(CommonDisplayer.C_CONDITION_WHEN_CLEARED);
514
515 if (o != null) {
516 try {
517 UserSettingsProtection.setUnprotected((JComponent)disp,"conditionWhenCleared",(BitCondition[])o);
518 } catch (Exception e) {
519 e.printStackTrace();
520 }
521 }
522
523 o = characteristics.get(CommonDisplayer.C_CONDITION_WHEN_SET);
524
525 if (o != null) {
526 try {
527 UserSettingsProtection.setUnprotected((JComponent)disp,"conditionWhenSet",(BitCondition[])o);
528 } catch (Exception e) {
529 e.printStackTrace();
530 }
531 }
532
533 o = characteristics.get(CommonDisplayer.C_BIT_DESCRIPTIONS);
534
535 if (o != null) {
536 try {
537 UserSettingsProtection.setUnprotected((JComponent)disp,"bitDescriptions",(String[])o);
538 } catch (Exception e) {
539 e.printStackTrace();
540 }
541 }
542
543 disp.resume();
544 }
545
546
547
548
549
550
551
552
553 public static String toLongFormat(String format)
554 {
555 if (format != null && (format.indexOf('f') > -1)) {
556 format = format.replace('f', 'd');
557
558 int dot = format.indexOf('.');
559
560 if (dot > -1) {
561 format = format.substring(0, dot)
562 + format.substring(format.indexOf('d'));
563 }
564 }
565
566 return format;
567 }
568
569
570
571
572
573
574
575
576
577
578 public static void prepareNewConverter(Converter converter, ConvertibleDisplayer disp) throws PropertyVetoException {
579 reattachDataTransport(disp.getConverter(), converter, disp.getDataSource(), disp.getDataSource(), disp);
580 }
581
582
583
584
585
586
587
588
589 private static void reattachDataTransport(Converter oldConverter, Converter newConverter, DataSource oldDataSource, DataSource newDataSource, Displayer displayer) throws PropertyVetoException {
590
591 if (oldDataSource!=null) {
592 oldDataSource.removeConsumer(displayer);
593 if (oldConverter!=null) {
594 oldDataSource.removeConsumer(oldConverter);
595 }
596 }
597
598 if (oldConverter!=null) {
599 oldConverter.removeConsumer(displayer);
600 }
601
602
603 if (newDataSource!=null) {
604 if (newConverter!=null) {
605
606 newConverter.addConsumer(displayer);
607 newDataSource.addConsumer(newConverter);
608 } else {
609
610 newDataSource.addConsumer(displayer);
611 }
612 }
613 }
614
615
616
617
618
619
620
621
622
623
624 public static void prepareNewDataSource(DataSource dataSource, ConvertibleDisplayer disp) throws PropertyVetoException {
625 reattachDataTransport(disp.getConverter(), disp.getConverter(), disp.getDataSource(), dataSource, disp);
626 }
627
628
629
630
631
632
633
634
635
636
637 public static void prepareNewDataSource(DataSource dataSource, Displayer disp) throws PropertyVetoException {
638 reattachDataTransport(null, null, disp.getDataSource(), dataSource, disp);
639 }
640
641
642
643
644
645
646
647
648
649 public static double extract(int index, double[] array) {
650 if (index<0) {
651 return array[0];
652 }
653 if (index>=array.length) {
654 return array[array.length-1];
655 }
656 return array[index];
657 }
658
659
660
661
662
663
664
665
666
667 public static String extract(int index, String[] array) {
668 if (index<0) {
669 return array[0];
670 }
671 if (index>array.length) {
672 return array[array.length];
673 }
674 return array[index];
675 }
676
677
678
679
680
681
682
683
684 public static String toString(Object[] value) {
685 StringBuilder s= new StringBuilder(16*value.length);
686 s.append('[');
687 if (value.length>0) {
688 s.append(value[0]);
689 }
690 for (int i = 1; i < value.length; i++) {
691 s.append(',');
692 s.append(value[i]);
693 }
694 s.append(']');
695 return s.toString();
696 }
697
698 }
699
700