1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui;
21
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.event.ActionEvent;
25 import java.beans.PropertyVetoException;
26 import java.util.Map;
27
28 import javax.swing.AbstractAction;
29 import javax.swing.JFrame;
30
31 import com.cosylab.gui.adapters.Converter;
32 import com.cosylab.gui.components.LabelledGauger;
33 import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
34 import com.cosylab.gui.components.range2.TrimValuePolicy;
35 import com.cosylab.gui.components.util.PopupManager;
36 import com.cosylab.gui.displayers.ConvertibleDisplayer;
37 import com.cosylab.gui.displayers.DataConsumer;
38 import com.cosylab.gui.displayers.DataSource;
39 import com.cosylab.gui.displayers.DataState;
40 import com.cosylab.gui.displayers.DisplayerUtilities;
41 import com.cosylab.gui.displayers.DoubleConsumer;
42 import com.cosylab.gui.displayers.DoubleConsumerMulticaster;
43 import com.cosylab.gui.displayers.DoubleDisplayer;
44 import com.cosylab.util.CommonException;
45
46
47
48
49
50
51 public class GaugerDisplayer extends LabelledGauger
52 implements DoubleDisplayer, ConvertibleDisplayer
53 {
54 private static final long serialVersionUID = 1L;
55 private int suspendCount = 0;
56 private double value;
57 private DataState dataState = new DataState(DataState.UNDEFINED);
58 private DataSource dataSource;
59 private Converter converter;
60 private AbstractCustomizerPanel customizer;
61 private InfoDialog infoDialog;
62 private PopupManager popupManager;
63
64
65
66
67
68 public GaugerDisplayer()
69 {
70 super();
71 try {
72 initialize();
73 } catch (PropertyVetoException e) {
74 e.printStackTrace();
75 }
76 }
77
78
79
80
81 public void setCharacteristics(Map characteristics)
82 {
83 if (characteristics == null) {
84 throw new NullPointerException("characteristics");
85 }
86
87 DisplayerUtilities.setCharacteristics(characteristics, this);
88 }
89
90
91
92
93 public DataConsumer getDataConsumer(Class type)
94 {
95 if (type == DoubleConsumer.class) {
96 return this;
97 }
98
99 return DoubleConsumerMulticaster.createDataConsumer(type, this);
100 }
101
102
103
104
105 public DataState getDataState()
106 {
107 return dataState;
108 }
109
110
111
112
113 public DataConsumer getDefaultDataConsumer()
114 {
115 return this;
116 }
117
118
119
120
121 public String[] getSupportedCharacteristics()
122 {
123 return DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_CHARACTERISTICS;
124 }
125
126
127
128
129 @SuppressWarnings("unchecked")
130 public Class[] getSupportedConsumerTypes()
131 {
132 return DoubleConsumerMulticaster.PREFERED_CONSUMER_TYPES;
133 }
134
135
136
137
138 public void setValue(double value)
139 {
140 if (this.value == value) {
141 return;
142 }
143
144 double oldValue = this.value;
145 this.value = value;
146 firePropertyChange("value", oldValue, value);
147
148 if (!isSuspended()) {
149 super.setValue(value);
150 }
151 }
152
153
154
155
156 public void cleanup()
157 {
158 internalCleanup();
159 updateDataState(new DataState(DataState.NOT_INITIALIZED));
160 }
161
162
163
164
165 public void destroy()
166 {
167 suspend();
168 cleanup();
169 dataState = null;
170 }
171
172
173
174
175 public void resume()
176 {
177 if (suspendCount > 0) {
178 suspendCount--;
179 }
180
181 if (suspendCount==0) {
182 setEnabled(true);
183 super.setValue(value);
184 }
185 }
186
187
188
189
190 public void suspend()
191 {
192 suspendCount++;
193 setEnabled(false);
194 }
195
196
197
198
199 public void updateDataState(DataState state)
200 {
201 DataState old = dataState;
202 dataState = state;
203 firePropertyChange(DATA_STATE, old, dataState);
204 }
205
206
207
208
209 public void updateValue(long timestamp, double value)
210 throws CommonException
211 {
212 setValue(value);
213 }
214
215 private void initialize() throws PropertyVetoException
216 {
217
218 internalCleanup();
219 }
220
221 private void internalCleanup()
222 {
223 setTitle("Gauger");
224 setUnits(null);
225 setUnitsVisible(true);
226 setFormat("%3.2f");
227 setResizable(true);
228 setTitleVisible(true);
229 setLogarithmicScale(false);
230 setValuePolicy(new TrimValuePolicy());
231 setPopupEnabled(true);
232 }
233
234
235
236 public boolean isSuspended()
237 {
238 return (suspendCount > 0);
239 }
240
241 public double getValue()
242 {
243 return value;
244 }
245
246
247
248
249 public DataSource getDataSource()
250 {
251 return dataSource;
252 }
253
254
255
256
257 public void setDataSource(DataSource dataSource)
258 throws PropertyVetoException
259 {
260 DisplayerUtilities.prepareNewDataSource(dataSource,this);
261
262 DataSource old= this.dataSource;
263 this.dataSource = dataSource;
264
265 firePropertyChange(DATA_SOURCE,old,dataSource);
266 }
267
268
269
270
271
272
273 public Converter getConverter() {
274 return converter;
275 }
276
277
278
279
280
281
282
283 public void setConverter(Converter converter) throws PropertyVetoException {
284 if (this.converter != null && this.converter.equals(converter) ||
285 (this.converter == null && converter == null)) return;
286 DisplayerUtilities.prepareNewConverter(converter,this);
287
288 Converter old= this.converter;
289 this.converter = converter;
290
291 firePropertyChange(CONVERTER_PROPERTY,old,this.converter);
292 }
293
294 @Override
295 public AbstractCustomizerPanel getCustomizer() {
296 if (customizer == null) {
297 customizer = AbstractCustomizerPanel.findCustomizer(this);
298 }
299
300 return customizer;
301 }
302
303 public InfoDialog getInfoDialog() {
304 if (infoDialog == null) {
305 infoDialog = new InfoDialog(this);
306 }
307 return infoDialog;
308 }
309
310 public PopupManager getPopupManager()
311 {
312 if (popupManager == null) {
313 popupManager = super.getPopupManager();
314 popupManager.addAction(new AbstractAction("Info...") {
315
316 private static final long serialVersionUID = 1L;
317
318 public void actionPerformed(ActionEvent e)
319 {
320 getInfoDialog().setVisible(true);
321 }
322 });
323 }
324
325 return popupManager;
326 }
327
328
329
330
331
332
333 public static void main(String[] args)
334 {
335 JFrame dialog = new JFrame();
336
337 dialog.getContentPane().setLayout(new GridBagLayout());
338
339 GridBagConstraints c = new GridBagConstraints();
340 c.fill = GridBagConstraints.BOTH;
341 c.weightx = 1.0;
342 c.weighty = 1.0;
343
344
345
346 final GaugerDisplayer gd = new GaugerDisplayer();
347 gd.setPopupEnabled(false);
348
349 try {
350 gd.setConverter(new com.cosylab.gui.adapters.MultiplierConverter(2));
351 } catch (PropertyVetoException e) {
352 e.printStackTrace();
353 }
354 dialog.setSize(100, 400);
355
356 dialog.getContentPane().add(gd, c);
357 dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
358 gd.setValue(1.8);
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383 dialog.setVisible(true);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411 }
412 }
413