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.Color;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.WindowAdapter;
25 import java.awt.event.WindowEvent;
26 import java.beans.Beans;
27 import java.beans.PropertyVetoException;
28 import java.util.Map;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import javax.swing.AbstractAction;
33 import javax.swing.JComponent;
34 import javax.swing.JFrame;
35 import javax.swing.SwingConstants;
36 import javax.swing.TransferHandler;
37
38 import com.cosylab.gui.components.AbstractDisplayerPanel;
39 import com.cosylab.gui.components.ResizableTextLabel;
40 import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
41 import com.cosylab.gui.components.util.CosyTransferHandler;
42 import com.cosylab.gui.components.util.CosyUIElements;
43 import com.cosylab.gui.components.util.PopupManageable;
44 import com.cosylab.gui.components.util.PopupManager;
45 import com.cosylab.gui.displayers.DataConsumer;
46 import com.cosylab.gui.displayers.DataSource;
47 import com.cosylab.gui.displayers.DataState;
48 import com.cosylab.gui.displayers.StringConsumerMulticaster;
49 import com.cosylab.gui.displayers.StringDisplayer;
50 import com.cosylab.gui.util.UserSettingsProtection;
51 import com.cosylab.logging.DebugLogger;
52 import com.cosylab.util.CommonException;
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class LabelDisplayer extends AbstractDisplayerPanel
67 implements StringDisplayer, PopupManageable
68 {
69 private static final long serialVersionUID = 1L;
70 private DataSource dataSource;
71 private DataState dataState = new DataState(DataState.UNDEFINED);
72 private ResizableTextLabel label = null;
73 private int suspendCount;
74 private String value;
75 private final Logger logger = DebugLogger.getLogger("LabelDisplayer",
76 Level.OFF);
77 private PopupManager popupManager;
78 private AbstractCustomizerPanel customizer;
79 private InfoDialog infoDialog;
80
81
82
83
84 public LabelDisplayer()
85 {
86 super();
87 internalCleanup();
88 }
89
90
91
92
93 public void cleanup()
94 {
95 internalCleanup();
96 updateDataState(new DataState(DataState.NOT_INITIALIZED));
97 }
98
99
100
101
102 public void destroy()
103 {
104 internalCleanup();
105 }
106
107
108
109
110
111
112 public int getColumns()
113 {
114 return getValueLabel().getColumns();
115 }
116
117
118
119
120 public DataConsumer getDataConsumer(Class type)
121 {
122 return StringConsumerMulticaster.createDataConsumer(type, this);
123 }
124
125
126
127
128 public DataSource getDataSource()
129 {
130 return dataSource;
131 }
132
133
134
135
136 public DataState getDataState()
137 {
138 return dataState;
139 }
140
141
142
143
144 public DataConsumer getDefaultDataConsumer()
145 {
146 return this;
147 }
148
149
150
151
152
153
154
155
156
157 protected ResizableTextLabel getValueLabel()
158 {
159 if (label == null) {
160 label = new ResizableTextLabel();
161 label.setHorizontalAlignment(SwingConstants.CENTER);
162 label.setEnabled(isEnabled());
163 label.setEnhanced(isEnhanced());
164 label.setResizable(isResizable());
165 internalSetValue();
166 label.setOpaque(true);
167 label.setBorder(CosyUIElements.getFlushBorder());
168 label.setBackground(Color.LIGHT_GRAY);
169 }
170
171 return label;
172 }
173
174 @Override
175 public void setBackground(Color bg) {
176 Color labelBg = getValueLabel().getBackground();
177 super.setBackground(bg);
178 getValueLabel().setBackground(labelBg);
179 }
180
181
182
183
184 public String[] getSupportedCharacteristics()
185 {
186 return new String[]{ C_DISPLAY_NAME };
187 }
188
189
190
191
192 @SuppressWarnings("unchecked")
193 public Class[] getSupportedConsumerTypes()
194 {
195 return StringConsumerMulticaster.SUPPORTED_CONSUMER_TYPES;
196 }
197
198
199
200
201
202
203 public String getValue()
204 {
205 return value;
206 }
207
208
209
210
211 protected JComponent getValueComponent()
212 {
213 return getValueLabel();
214 }
215
216 private void internalCleanup()
217 {
218 logger.info("Cleanup");
219
220
221
222
223
224
225
226 setEnhanced(true);
227 setResizable(true);
228 setValue("value");
229 setTitle("Label Displayer");
230 setLayoutOrientation(AbstractDisplayerPanel.DYNAMIC_LAYOUT);
231 setTitleVisible(true);
232 setColumns(5);
233 setPopupEnabled(true);
234 }
235
236
237
238
239 protected void internalSetEnabled()
240 {
241 super.internalSetEnabled();
242 getValueLabel().setEnabled(isEnabled());
243 }
244
245
246
247
248 protected void internalSetEnhanced()
249 {
250 super.internalSetEnhanced();
251 getValueLabel().setEnhanced(isEnhanced());
252 }
253
254
255
256
257 protected void internalSetResizable()
258 {
259 super.internalSetResizable();
260 getValueLabel().setResizable(isResizable());
261 }
262
263 private void internalSetValue()
264 {
265 String tValue = value;
266
267 if (tValue == null && Beans.isDesignTime()) {
268 tValue = "<value>";
269 }
270
271 if (getValueLabel().isEnabled()) {
272 if (logger!=null) logger.fine("'" + tValue + "'");
273 getValueLabel().setText(tValue);
274 }
275 }
276
277
278
279
280 public boolean isEditable()
281 {
282 return false;
283 }
284
285
286
287
288 public boolean isSuspended()
289 {
290 return suspendCount > 0;
291 }
292
293
294
295
296 public void resume()
297 {
298 if (suspendCount > 0) {
299 suspendCount--;
300 }
301
302 if (suspendCount == 0) {
303 setEnabled(true);
304 }
305 }
306
307
308
309
310 public void setCharacteristics(Map characteristics)
311 {
312 Object o = characteristics.get(C_DISPLAY_NAME);
313
314 if (o != null) {
315 try {
316 UserSettingsProtection.setUnprotected(this,"title",o.toString());
317 } catch (Exception e) {
318 e.printStackTrace();
319 }
320 }
321 }
322
323
324
325
326
327
328 public void setColumns(int c)
329 {
330 getValueLabel().setColumns(c);
331 }
332
333
334
335
336 public void setDataSource(DataSource dataSource)
337 throws PropertyVetoException
338 {
339 if (this.dataSource != null) {
340 this.dataSource.removeConsumer(this);
341 }
342
343 DataSource old = this.dataSource;
344 this.dataSource = dataSource;
345
346 if (this.dataSource != null) {
347 this.dataSource.addConsumer(this);
348 }
349
350 firePropertyChange(DATA_SOURCE, old, dataSource);
351 }
352
353
354
355
356 public void setValue(String value)
357 {
358 logger.info("'" + value + "'");
359
360 if (value == null && this.value == null
361 || value != null && value.equals(this.value)) {
362 return;
363 }
364
365 String oldValue = this.value;
366 this.value = value;
367 internalSetValue();
368 firePropertyChange("value", oldValue, value);
369 }
370
371
372
373
374 public void suspend()
375 {
376 setEnabled(false);
377 suspendCount++;
378 }
379
380
381
382
383 public void updateDataState(DataState state)
384 {
385 DataState old = dataState;
386 dataState = state;
387 firePropertyChange(DATA_STATE, old, dataState);
388 }
389
390
391
392
393 public void updateValue(long timestamp, String value)
394 throws CommonException
395 {
396 logger.info(timestamp + ",'" + value + "'");
397 setValue(value);
398 }
399
400
401
402
403
404 @Override
405 public PopupManager getPopupManager()
406 {
407 if (popupManager == null) {
408 popupManager = super.getPopupManager();
409 popupManager.addAction(new AbstractAction("Info...") {
410 private static final long serialVersionUID = 1L;
411
412 public void actionPerformed(ActionEvent e)
413 {
414 getInfoDialog().setVisible(true);
415 }
416 });
417 }
418
419 return popupManager;
420 }
421
422 public InfoDialog getInfoDialog() {
423 if (infoDialog == null) {
424 infoDialog = new InfoDialog(this);
425 }
426 return infoDialog;
427 }
428
429
430
431
432
433
434
435
436 public AbstractCustomizerPanel getCustomizer()
437 {
438 if (customizer == null) {
439 customizer = AbstractCustomizerPanel.findCustomizer(this);
440 }
441
442 return customizer;
443 }
444
445 public static void main(String[] args) {
446 try {
447 JFrame frame= new JFrame();
448 frame.addWindowListener(new WindowAdapter() {
449 public void windowClosing(WindowEvent e) {
450 System.exit(0);
451 }
452 });
453 frame.setSize(200,200);
454
455 LabelDisplayer d= new LabelDisplayer();
456 d.setPopupEnabled(true);
457 frame.setContentPane(d);
458
459
460 frame.setVisible(true);
461
462 } catch (Exception e) {
463 e.printStackTrace();
464 }
465 }
466
467
468
469 @Override
470 public void setTransferHandler(TransferHandler newHandler) {
471 super.setTransferHandler(newHandler);
472 CosyTransferHandler.registerTransferHandler(this,newHandler,new JComponent[]{getValueComponent()});
473 }
474
475 }
476
477