1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components;
21
22 import java.awt.Color;
23 import java.awt.Dimension;
24 import java.awt.Font;
25
26 import javax.swing.JComponent;
27 import javax.swing.JFrame;
28 import javax.swing.TransferHandler;
29
30 import com.cosylab.application.state.State;
31 import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
32 import com.cosylab.gui.components.range2.RangedValuePolicy;
33 import com.cosylab.gui.components.util.CosyTransferHandler;
34 import com.cosylab.gui.components.util.PopupManager;
35
36
37
38
39
40
41
42
43
44
45 public class LabelledGauger extends AbstractDisplayerPanel
46 {
47 private static final long serialVersionUID = 1L;
48 private Gauger gauger = null;
49 private AbstractCustomizerPanel customizer;
50
51
52
53
54 public LabelledGauger(String title)
55 {
56 super();
57 setTitle(title);
58 setTitleVisible(true);
59 setLayoutOrientation(VERTICAL_LAYOUT);
60 setResizable(true);
61 setEnhanced(true);
62 setValue(5);
63 setFormat("%1.1f");
64 setUnits("");
65 }
66
67
68
69
70 public LabelledGauger()
71 {
72 this(null);
73 }
74
75
76
77
78 public boolean isEditable()
79 {
80 return false;
81 }
82
83
84
85
86 public void setFormat(String value)
87 {
88 String oldValue = getFormat();
89 getGauger().setFormat(value);
90 firePropertyChange("format", oldValue, value);
91 }
92
93
94
95
96 public String getFormat()
97 {
98 return getGauger().getFormat();
99 }
100
101
102
103
104
105
106 public void setLogarithmicScale(boolean b)
107 {
108 if (b) {
109 setLogarithmicScale();
110 } else {
111 setLinearScale();
112 }
113 }
114
115
116
117
118
119
120 public boolean isLogarithmicScale()
121 {
122 return getGauger().isLogarithmicScale();
123 }
124
125
126
127
128
129
130 public void setMaximum(double value)
131 {
132 double oldValue = getMaximum();
133 getGauger().setMaximum(value);
134 firePropertyChange("maximum", oldValue, value);
135 }
136
137
138
139
140
141
142 public double getMaximum()
143 {
144 return getGauger().getMaximum();
145 }
146
147
148
149
150
151
152 public void setMinimum(double value)
153 {
154 double oldValue = getMinimum();
155 getGauger().setMinimum(value);
156 firePropertyChange("minimum", oldValue, value);
157 }
158
159
160
161
162
163
164 public double getMinimum()
165 {
166 return getGauger().getMinimum();
167 }
168
169
170
171
172
173
174
175
176 public void setScaleMode(int policy)
177 {
178 int oldValue = getScaleMode();
179 getGauger().setScaleMode(policy);
180 firePropertyChange("scaleMode", oldValue, policy);
181 }
182
183
184
185
186
187
188
189
190 public int getScaleMode()
191 {
192 return getGauger().getScaleMode();
193 }
194
195
196
197
198
199
200 public void setValuePolicy(RangedValuePolicy policy) {
201 RangedValuePolicy oldValue = getValuePolicy();
202 getGauger().setValuePolicy(policy);
203 firePropertyChange("valuePolicy", oldValue, policy);
204 }
205
206
207
208
209
210
211 public RangedValuePolicy getValuePolicy() {
212 return getGauger().getValuePolicy();
213 }
214
215
216
217
218 public void setState(State state)
219 {
220 super.setState(state);
221 getGauger().setState(state.getState("gauger"));
222 }
223
224
225
226
227 public State getState()
228 {
229 State state = super.getState();
230 state.putState("gauger",getGauger().getState());
231 return state;
232 }
233
234
235
236
237
238
239 public void setUnitsVisible(boolean b) {
240 boolean oldB = isUnitsVisible();
241
242 if (oldB == b) return;
243
244 getGauger().setUnitsVisible(b);
245
246 firePropertyChange("unitsVisible",oldB,b);
247 }
248
249
250
251
252
253
254 public boolean isUnitsVisible() {
255 return getGauger().isUnitsVisible();
256 }
257
258
259
260
261
262
263 protected Gauger getGauger() {
264 if (gauger==null) {
265 gauger = new Gauger(){
266 private static final long serialVersionUID = 1L;
267
268 public AbstractCustomizerPanel getCustomizer()
269 {
270 if (customizer == null) {
271 customizer = AbstractCustomizerPanel.findCustomizer(LabelledGauger.this);
272 }
273
274 return customizer;
275 }
276 };
277 Dimension size = new Dimension(250, 150);
278 gauger.setPreferredSize(size);
279 gauger.setSize(size);
280 gauger.setMinimumSize(new Dimension(120,85));
281 }
282 return gauger;
283 }
284
285
286
287
288
289 @Override
290 protected JComponent getValueComponent() {
291 return getGauger();
292 }
293
294
295
296
297
298
299 public double getValue() {
300 return getGauger().getValue();
301 }
302
303
304
305
306
307
308 public void setValue(double d) {
309 double oldValue = getValue();
310 getGauger().setValue(d);
311 firePropertyChange("value", oldValue, d);
312 }
313
314
315
316
317
318 @Override
319 protected void layoutDisplayer() {
320 super.layoutDisplayer();
321 getGauger().doLayout();
322 }
323
324
325
326
327
328
329 public String getUnits() {
330 return getGauger().getUnits();
331 }
332
333
334
335
336
337
338 public void setUnits(String u) {
339 String oldValue = getUnits();
340 getGauger().setUnits(u);
341 firePropertyChange("units", oldValue, u);
342 }
343
344
345
346
347
348 public AbstractCustomizerPanel getCustomizer()
349 {
350 if (customizer == null) {
351 customizer = AbstractCustomizerPanel.findCustomizer(this);
352 }
353
354 return customizer;
355 }
356
357
358
359
360
361 public PopupManager getPopupManager()
362 {
363 return getGauger().getPopupManager();
364 }
365
366
367
368
369
370 @Override
371 public void setPopupEnabled(boolean enabled) {
372 getGauger().setPopupEnabled(enabled);
373 super.setPopupEnabled(enabled);
374 }
375
376
377
378
379
380
381 public Font getValueLabelFont() {
382 return gauger.getValueLabelFont();
383 }
384
385
386
387
388
389
390 public void setValueLabelFont(Font font) {
391 Font oldValue = getValueLabelFont();
392 gauger.setValueLabelFont(font);
393 firePropertyChange("valueLabelFont", oldValue, font);
394 }
395
396
397
398
399
400 public void setLogarithmicScale() {
401 boolean oldValue = isLogarithmicScale();
402 gauger.setLogarithmicScale();
403 firePropertyChange("logarithmicScale", oldValue, true);
404 }
405
406
407
408
409
410 public void setLinearScale() {
411 boolean oldValue = isLinearScale();
412 gauger.setLinearScale();
413 firePropertyChange("linearScale", oldValue, true);
414 }
415
416
417
418
419
420
421 public boolean isLinearScale() {
422 return gauger.isLinearScale();
423 }
424
425
426
427
428
429
430 public void setOutOfBoundsColor(Color color) {
431 Color oldValue = gauger.getOutOfBoundsColor();
432 gauger.setOutOfBoundsColor(color);
433 firePropertyChange("outOfBoundsColor", oldValue, color);
434 }
435
436
437
438
439
440
441 public Color getOutOfBoundsColor() {
442 return gauger.getOutOfBoundsColor();
443 }
444
445
446
447
448
449
450 public Color getWarningColor() {
451 return gauger.getWarningColor();
452 }
453
454
455
456
457
458
459 public void setWarningColor(Color color) {
460 Color oldValue = gauger.getWarningColor();
461 gauger.setWarningColor(color);
462 firePropertyChange("warningColor", oldValue, color);
463 }
464
465
466
467
468
469
470
471
472 public void setHighWarningLimit(double value) {
473 double oldValue = gauger.getHighWarningLimit();
474 gauger.setHighWarningLimit(value);
475 firePropertyChange("highWarningLimit", oldValue, value);
476 }
477
478
479
480
481
482
483 public double getHighWarningLimit() {
484 return gauger.getHighWarningLimit();
485 }
486
487
488
489
490
491
492
493
494 public void setLowWarningLimit(double value) {
495 double oldValue = gauger.getLowWarningLimit();
496 gauger.setLowWarningLimit(value);
497 firePropertyChange("lowWarningLimit", oldValue, value);
498 }
499
500
501
502
503
504
505 public double getLowWarningLimit() {
506 return gauger.getLowWarningLimit();
507 }
508
509
510
511
512
513
514
515
516 public void setValue(double newMin, double newMax, double newValue) {
517 double oldValue = getValue();
518 gauger.setValue(newMin, newMax, newValue);
519 firePropertyChange("value", oldValue, newValue);
520 }
521
522
523
524
525 @Override
526 public void setTransferHandler(TransferHandler newHandler) {
527 super.setTransferHandler(newHandler);
528 CosyTransferHandler.registerTransferHandler(this,newHandler,new JComponent[]{getValueComponent()});
529 }
530
531
532
533
534
535
536 public static void main(String[] args)
537 {
538 Thread t = new Thread(){
539 public void run(){
540 JFrame f= new JFrame();
541 f.setSize(400,400);
542 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
543
544 LabelledGauger g= new LabelledGauger("Labelled Gauger");
545
546
547
548 g.setResizable(true);
549 g.setUnits("V");
550 g.setFormat("%1.2f");
551 f.getContentPane().add(g);
552
553 f.setVisible(true);
554
555
556
557
558
559
560
561
562
563 }
564 };
565 t.run();
566 }
567
568 }
569
570