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 import java.awt.BorderLayout;
22 import java.awt.Color;
23 import java.awt.event.WindowAdapter;
24 import java.awt.event.WindowEvent;
25
26 import javax.swing.JComponent;
27 import javax.swing.JFrame;
28
29 import com.cosylab.application.state.State;
30 import com.cosylab.events.SetListener;
31 import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
32 import com.cosylab.gui.components.util.PopupManageable;
33 import com.cosylab.gui.components.wheelswitch.AbstractWheelswitchFormatter;
34 import com.cosylab.gui.components.wheelswitch.WheelswitchFormatter;
35
36
37
38
39
40
41
42
43
44
45 public class LabelledWheelswitch extends AbstractNumericDisplayerPanel implements PopupManageable
46 {
47 private static final long serialVersionUID = 1L;
48
49 public static final String VALUE = Wheelswitch.VALUE;
50 private Wheelswitch number;
51 private AbstractCustomizerPanel customizer;
52
53
54
55 public LabelledWheelswitch()
56 {
57 this(0.0, "3.2f", null, null);
58 }
59
60
61
62
63
64
65 public LabelledWheelswitch(double value)
66 {
67 this(value, "3.2f" , null, null);
68 }
69
70
71
72
73
74
75 public LabelledWheelswitch(String format)
76 {
77 this(0.0, format, null, null);
78 }
79
80
81
82
83
84
85
86
87
88 public LabelledWheelswitch(double value, String format, String title,
89 String units)
90 {
91 super();
92
93 setValue(value);
94 setFormat(format);
95 setUnits(units);
96 setTitle(title);
97
98 setUnitsVisible(true);
99 setTitleVisible(true);
100 setLayoutOrientation(HORIZONTAL_LAYOUT);
101 setResizable(true);
102 setEnhanced(true);
103 addMouseListener(getPopupManager().getMouseHook());
104
105 getTitleComponent().addMouseListener(getWheelswitch().handler);
106 }
107
108
109
110
111
112 public AbstractCustomizerPanel getCustomizer()
113 {
114 if (customizer == null) {
115 customizer = AbstractCustomizerPanel.findCustomizer(this);
116 }
117
118 return customizer;
119 }
120
121
122
123
124
125
126
127 public void setEditable(boolean newEditable)
128 {
129 if (newEditable == isEditable()) return;
130 getWheelswitch().setEditable(newEditable);
131
132 firePropertyChange("editable",!newEditable,newEditable);
133 }
134
135
136
137
138
139
140 public boolean isEditable()
141 {
142 return getWheelswitch().isEditable();
143 }
144
145
146
147
148
149
150
151
152 public void setMaximum(double newValue)
153 {
154 double oldValue = getMaximum();
155 if (newValue == oldValue) return;
156 getWheelswitch().setGraphMax(newValue);
157 super.setMaximumValue(new Double(getWheelswitch().getGraphMax()));
158 newValue = getMaximum();
159 firePropertyChange("maximum", oldValue, newValue);
160 }
161
162
163
164
165
166
167
168
169 public double getMaximum()
170 {
171 return getWheelswitch().getGraphMax();
172 }
173
174
175
176
177
178
179
180
181 public void setMinimum(double newValue)
182 {
183 double oldValue = getMinimum();
184 if (newValue == oldValue) return;
185 getWheelswitch().setGraphMin(newValue);
186 super.setMinimumValue(new Double(getWheelswitch().getGraphMin()));
187 newValue = getMinimum();
188 firePropertyChange("minimum", oldValue, newValue);
189 }
190
191
192
193
194
195
196
197
198 public double getMinimum()
199 {
200 return getWheelswitch().getGraphMin();
201 }
202
203
204
205
206
207
208
209
210
211 public void setMaxMin(double max, double min)
212 {
213 setMaximum(max);
214 setMinimum(min);
215 }
216
217
218
219
220
221 public void setState(State state)
222 {
223 super.setState(state);
224
225 setTiltingEnabled(state.getBoolean("tiltingEnabled", isTiltingEnabled()));
226 setEditable(state.getBoolean("editable", isEditable()));
227 setMaximum(state.getDouble("maximum", getMaximum()));
228 setMinimum(state.getDouble("minimum", getMinimum()));
229 }
230
231
232
233
234
235 public State getState()
236 {
237 State state = super.getState();
238
239 state.putDouble("maximum", getMaximum());
240 state.putDouble("minimum", getMinimum());
241 state.putBoolean("editable", isEditable());
242 state.putBoolean("tiltingEnabled", isTiltingEnabled());
243
244 return state;
245 }
246
247
248
249
250
251
252
253 public void setTiltingEnabled(boolean b)
254 {
255 if (isTiltingEnabled() == b) return;
256 getWheelswitch().setTiltingEnabled(b);
257 firePropertyChange("tiltingEnabled", !b, b);
258 }
259
260
261
262
263
264
265 public boolean isTiltingEnabled()
266 {
267 return getWheelswitch().isTiltingEnabled();
268 }
269
270
271
272
273
274
275
276
277
278
279 public void setValue(double newValue)
280 {
281 double oldValue = getValue();
282 getWheelswitch().setValue(newValue);
283 firePropertyChange("value", oldValue, newValue);
284 }
285
286
287
288
289
290
291
292 public void setAnimated(boolean animated) {
293 if (isAnimated() == animated) return;
294 getWheelswitch().setAnimated(animated);
295 firePropertyChange("animated", !animated, animated);
296 }
297
298
299
300
301
302
303
304 public boolean isAnimated() {
305 return getWheelswitch().isAnimated();
306 }
307
308
309
310
311
312
313
314
315
316 public double getValue()
317 {
318 return getWheelswitch().getValue();
319 }
320
321
322
323
324
325
326 public void addSetListener(SetListener l)
327 {
328 getWheelswitch().addSetListener(l);
329 }
330
331
332
333
334
335
336 public void removeSetListener(SetListener l)
337 {
338 getWheelswitch().removeSetListener(l);
339 }
340
341 protected Wheelswitch getWheelswitch()
342 {
343 if (number == null) {
344 number = new Wheelswitch();
345
346
347
348
349
350
351
352
353
354
355
356 number.addMouseListener(getPopupManager().getMouseHook());
357 number.setUnitSeparate(true);
358 number.setPopupEnabled(false);
359
360 addMouseListener(number.handler);
361 getUnitsComponent().addMouseListener(number.handler);
362 }
363
364 return number;
365 }
366
367
368
369
370
371 protected JComponent getValueComponent()
372 {
373 return getWheelswitch();
374 }
375
376
377
378
379
380 protected void internalSetEnabled()
381 {
382 super.internalSetEnabled();
383 getWheelswitch().setEnabled(isEnabled());
384 }
385
386
387
388
389
390 protected void internalSetEnhanced()
391 {
392 super.internalSetEnhanced();
393 getWheelswitch().setEnhanced(isEnhanced());
394 }
395
396
397
398
399
400 protected void internalSetFormat()
401 {
402 super.internalSetFormat();
403
404 String tFormat = getFormat();
405
406 if (tFormat == null) {
407 tFormat = NumberField.createDefaultFormat(Double.class);
408 }
409
410 getWheelswitch().setFormat(WheelswitchFormatter.transformFormat(tFormat));
411 }
412
413
414
415
416
417 protected void internalSetResizable()
418 {
419 super.internalSetResizable();
420 getWheelswitch().revalidate();
421 }
422
423
424
425
426 public void setUnitsVisible(boolean b) {
427 if (b) getWheelswitch().setUnit(getUnits());
428 else getWheelswitch().setUnit(null);
429 super.setUnitsVisible(b);
430 }
431
432
433
434
435 public void setUnits(String value) {
436 if (isUnitsVisible()) {
437 getWheelswitch().setUnit(value);
438 }
439 super.setUnits(value);
440 }
441
442
443
444 protected void layoutValueAndTitleAndUnits() {
445 super.layoutValueAndTitle();
446 }
447
448
449
450
451 protected void layoutValueAndTitleAndUnitsAndBounds() {
452 super.layoutValueAndTitleAndBounds();
453 }
454
455
456
457
458 protected void layoutValueAndUnits() {
459 super.layoutValue();
460 }
461
462
463
464
465 protected void layoutValueAndUnitsAndBounds() {
466 super.layoutValueAndBounds();
467 }
468
469
470
471
472 public void setBackground(Color bg) {
473 getWheelswitch().setBackground(bg);
474 super.setBackground(bg);
475 }
476
477
478
479
480
481
482 public void setFormatter(AbstractWheelswitchFormatter formatter) {
483 AbstractWheelswitchFormatter oldValue = getFormatter();
484 getWheelswitch().setFormatter(formatter);
485 firePropertyChange("formatter", oldValue, formatter);
486 }
487
488
489
490
491
492 public AbstractWheelswitchFormatter getFormatter() {
493 return getWheelswitch().getFormatter();
494 }
495
496
497
498
499 public void setTitleMaximumFontSize(int max) {
500 getWheelswitch().setUnitsMaximumFontSize(max);
501 super.setTitleMaximumFontSize(max);
502 }
503
504
505
506
507 public void setTitleMinimumFontSize(int min) {
508 getWheelswitch().setUnitsMinimumFontSize(min);
509 super.setTitleMinimumFontSize(min);
510 }
511
512
513
514
515
516
517
518
519 public void setDigitsTakeUpAllSpace(boolean bool) {
520 if(getWheelswitch().getDigitsTakeUpAllSpace() == bool)
521 return;
522
523 firePropertyChange("digitsTakeUpAllSpace", getWheelswitch().getDigitsTakeUpAllSpace(), bool);
524
525 getWheelswitch().setDigitsTakeUpAllSpace(bool);
526 }
527
528
529
530
531
532
533 public boolean getDigitsTakeUpAllSpace() {
534 return getWheelswitch().getDigitsTakeUpAllSpace();
535 }
536
537
538
539
540
541
542
543
544 public void setNumberOfAllDigits(int numberOfAllDigits) {
545 if(getWheelswitch().getNumberOfAllDigits() == numberOfAllDigits)
546 return;
547
548 firePropertyChange("numberOfAllDigits", getWheelswitch().getNumberOfAllDigits(), numberOfAllDigits);
549 getWheelswitch().setNumberOfAllDigits(numberOfAllDigits);
550 }
551
552
553
554
555 public int getNumberOfAllDigits() {
556 return(getWheelswitch().getNumberOfAllDigits());
557 }
558
559
560
561
562
563 @Override
564 public void setPopupEnabled(boolean enabled) {
565 super.setPopupEnabled(enabled);
566 getWheelswitch().setPopupEnabled(enabled);
567 }
568
569
570
571
572
573
574 public static void main(String[] args)
575 {
576 LabelledWheelswitch ws = new LabelledWheelswitch(2.449, "%2.1f",
577 "Current", "A");
578 ws.setDigitsTakeUpAllSpace(false);
579 ws.setNumberOfAllDigits(5);
580 JFrame frame = new JFrame("Wheelwsitch Testing Applet");
581 frame.getContentPane().setLayout(new BorderLayout(10, 10));
582 frame.getContentPane().add(ws);
583 frame.setSize(300, 100);
584 frame.addWindowListener(new WindowAdapter() {
585 public void windowClosing(WindowEvent e)
586 {
587 System.exit(0);
588 }
589 });
590 frame.setVisible(true);
591 }
592 }
593
594