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.awt.Color;
23 import java.awt.Dimension;
24 import java.awt.event.ActionEvent;
25 import java.beans.PropertyVetoException;
26
27 import javax.swing.AbstractAction;
28 import javax.swing.JLabel;
29 import javax.swing.JPanel;
30
31 import com.cosylab.gui.adapters.Converter;
32 import com.cosylab.gui.components.AbstractDisplayerPanelCustomizer;
33 import com.cosylab.gui.components.ResizableTextLabel;
34 import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
35 import com.cosylab.gui.components.util.CosyUIElements;
36 import com.cosylab.gui.components.util.PopupManageable;
37 import com.cosylab.gui.components.util.PopupManager;
38 import com.cosylab.gui.components.util.ScreenCapturer;
39
40
41
42
43
44
45
46
47
48 public abstract class AbstractDisplayerPanel extends JPanel implements Displayer,
49 PopupManageable, ConvertibleDisplayer
50 {
51 private DataState dataState = new DataState(DataState.UNDEFINED);
52 private int suspendCount = 0;
53 private PopupManager popupManager;
54 private boolean popupEnabled;
55 private DataSource dataSource;
56 private Converter converter;
57
58 private ResizableTextLabel titleLabel;
59 private AbstractCustomizerPanel customizer;
60
61
62
63
64 public AbstractDisplayerPanel()
65 {
66 super();
67 setBorder(CosyUIElements.getPlainBorder(true));
68
69 Dimension size = new Dimension(200, 50);
70 setPreferredSize(size);
71 setPopupEnabled(true);
72 }
73
74
75
76
77
78
79 public AbstractCustomizerPanel getCustomizer()
80 {
81 if (customizer == null) {
82 customizer = AbstractCustomizerPanel.findCustomizer(this);
83 }
84
85 return customizer;
86 }
87
88 protected ResizableTextLabel getTitleLabel() {
89 if (titleLabel == null) {
90 titleLabel = new ResizableTextLabel();
91 titleLabel.setHorizontalAlignment(JLabel.CENTER);
92 titleLabel.addMouseListener(getPopupManager().getMouseHook());
93 titleLabel.setEnabled(isEnabled());
94 }
95
96 return titleLabel;
97 }
98
99
100
101
102 public DataConsumer getDefaultDataConsumer()
103 {
104 return this;
105 }
106
107
108
109
110 public String[] getSupportedCharacteristics()
111 {
112 return DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_CHARACTERISTICS;
113 }
114
115
116
117
118 public void updateDataState(DataState state)
119 {
120 DataState old = dataState;
121 dataState = state;
122 firePropertyChange(DATA_STATE, old, dataState);
123 }
124
125
126
127
128 public void cleanup()
129 {
130 updateDataState(new DataState(DataState.NOT_INITIALIZED));
131 }
132
133
134
135
136 public DataState getDataState()
137 {
138 return dataState;
139 }
140
141
142
143
144 public boolean isSuspended()
145 {
146 return suspendCount > 0;
147 }
148
149
150
151
152 public void resume()
153 {
154 suspendCount--;
155 }
156
157
158
159
160 public void suspend()
161 {
162 suspendCount++;
163 }
164
165
166
167
168
169
170 public PopupManager getPopupManager()
171 {
172 if (popupManager == null) {
173 popupManager = new PopupManager(this, false);
174 popupManager.addAction(new AbstractAction("Preferences...") {
175 private static final long serialVersionUID = 1L;
176 public void actionPerformed(ActionEvent e)
177 {
178 getCustomizer().showDialog();
179 }
180 });
181 popupManager.addAction(new AbstractAction("Capture screen..."){
182 private static final long serialVersionUID = 1L;
183
184 public void actionPerformed(ActionEvent e){
185 ScreenCapturer sc = new ScreenCapturer(AbstractDisplayerPanel.this);
186 sc.showScreenDialog();
187 }
188 });
189 }
190
191 return popupManager;
192 }
193
194
195
196
197
198
199 public boolean isPopupEnabled() {
200 return popupEnabled;
201 }
202
203
204
205
206
207
208 public void setPopupEnabled(boolean enabled) {
209 if (popupEnabled == enabled) return;
210 popupEnabled = enabled;
211 if (enabled) {
212 addMouseListener(getPopupManager().getMouseHook());
213 } else {
214 removeMouseListener(getPopupManager().getMouseHook());
215 }
216 firePropertyChange("popupEnabled",!popupEnabled,popupEnabled);
217 }
218
219
220
221
222 public void destroy()
223 {
224 popupManager = null;
225 dataState = null;
226 }
227
228
229
230
231 public DataSource getDataSource() {
232 return dataSource;
233 }
234
235
236
237
238 public void setDataSource(DataSource dataSource)
239 throws PropertyVetoException
240 {
241 DisplayerUtilities.prepareNewDataSource(dataSource,this);
242
243 DataSource old= this.dataSource;
244 this.dataSource = dataSource;
245
246 firePropertyChange(DATA_SOURCE,old,dataSource);
247 }
248
249
250
251
252
253 public Converter getConverter() {
254 return converter;
255 }
256
257
258
259
260
261
262 public void setConverter(Converter converter) throws PropertyVetoException {
263 DisplayerUtilities.prepareNewConverter(converter,this);
264
265 Converter old= this.converter;
266 this.converter = converter;
267
268 firePropertyChange(CONVERTER_PROPERTY,old,dataSource);
269 }
270
271
272
273
274
275
276 public int getTitleMaximumFontSize()
277 {
278 return getTitleLabel().getMaximumFontSize();
279 }
280
281
282
283
284
285
286 public int getTitleMinimumFontSize()
287 {
288 return getTitleLabel().getMinimumFontSize();
289 }
290
291
292
293
294
295
296 public void setTitleMaximumFontSize(int max)
297 {
298 int old= getTitleMaximumFontSize();
299 getTitleLabel().setMaximumFontSize(max);
300 firePropertyChange(AbstractDisplayerPanelCustomizer.MAX_TITLE_FONT_SIZE,
301 old, max);
302 }
303
304
305
306
307
308
309 public void setTitleMinimumFontSize(int min)
310 {
311 int old= getTitleMinimumFontSize();
312 getTitleLabel().setMaximumFontSize(min);
313 firePropertyChange(AbstractDisplayerPanelCustomizer.MIN_TITLE_FONT_SIZE,
314 old, min);
315 }
316
317
318
319
320
321
322 public void setTitleVisible(boolean value) {
323 getTitleLabel().setVisible(value);
324 }
325
326
327
328
329
330
331 public boolean isTitleVisible() {
332 return getTitleLabel().isVisible();
333 }
334
335
336
337
338
339
340 public String getTitle() {
341 return getTitleLabel().getText();
342 }
343
344
345
346
347
348
349 public void setTitle(String value) {
350 getTitleLabel().setText(value);
351 }
352
353
354
355
356
357 @Override
358 public void setBackground(Color bg) {
359 super.setBackground(bg);
360 getTitleLabel().setBackground(bg);
361
362 }
363
364
365
366
367
368 @Override
369 public void setForeground(Color fg) {
370 super.setForeground(fg);
371 getTitleLabel().setForeground(fg);
372 }
373
374
375 }
376
377