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.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.Font;
25 import java.awt.FontMetrics;
26 import java.awt.GridBagConstraints;
27 import java.awt.GridBagLayout;
28 import java.awt.Insets;
29 import java.awt.Rectangle;
30 import java.awt.event.ActionEvent;
31 import java.util.Map;
32
33 import javax.swing.AbstractAction;
34 import javax.swing.Icon;
35 import javax.swing.JFrame;
36 import javax.swing.JLabel;
37 import javax.swing.JScrollPane;
38 import javax.swing.JTable;
39 import javax.swing.SwingConstants;
40 import javax.swing.plaf.metal.MetalLabelUI;
41 import javax.swing.table.DefaultTableColumnModel;
42 import javax.swing.table.TableColumn;
43
44 import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
45 import com.cosylab.gui.components.introspection.MapTableModel;
46 import com.cosylab.gui.components.introspection.PropertiesTableModel;
47 import com.cosylab.gui.components.util.CosyUIElements;
48 import com.cosylab.gui.components.util.PopupManager;
49 import com.cosylab.gui.displayers.AbstractDisplayerPanel;
50 import com.cosylab.gui.displayers.DataConsumer;
51 import com.cosylab.gui.displayers.DataState;
52 import com.cosylab.gui.displayers.ObjectConsumer;
53 import com.cosylab.gui.displayers.ObjectConsumerMulticaster;
54 import com.cosylab.gui.displayers.ObjectDisplayer;
55 import com.cosylab.util.ArrayHelper;
56 import com.cosylab.util.CommonException;
57
58
59
60
61
62
63
64
65
66
67 public class CharacteristicsMapPanel extends AbstractDisplayerPanel
68 implements ObjectDisplayer
69 {
70 private static final long serialVersionUID = 1L;
71 protected PopupManager popupManager;
72 private boolean initialized = false;
73 public static MapTableModel tableModel = null;
74 private JLabel title = null;
75 public static JTable table;
76 private AbstractCustomizerPanel customizer;
77 private int suspendCount = 0;
78 private Object lastValue=null;
79
80
81
82
83 public CharacteristicsMapPanel()
84 {
85 super();
86 initialize();
87 }
88
89
90
91
92 public DataConsumer getDataConsumer(Class type)
93 {
94 if (type == ObjectConsumer.class) {
95 return this;
96 }
97
98 return ObjectConsumerMulticaster.createDataConsumer(type, this);
99 }
100
101 public void resume()
102 {
103 if (suspendCount > 0) {
104 suspendCount--;
105 }
106
107 if (suspendCount==0) {
108 setEnabled(true);
109 setValue(lastValue);
110 }
111 }
112
113 public void setEnabled(boolean enabled){
114 table.setEnabled(enabled);
115 title.setEnabled(enabled);
116 }
117
118
119 public void suspend()
120 {
121 suspendCount++;
122 setEnabled(false);
123 }
124
125
126
127
128
129 public DataConsumer getDefaultDataConsumer()
130 {
131 return this;
132 }
133
134
135
136
137 public String getName()
138 {
139 return "CharacteristicsMapPanel";
140 }
141
142
143
144
145 public String[] getSupportedCharacteristics()
146 {
147 return null;
148 }
149
150
151
152
153 public Class[] getSupportedConsumerTypes()
154 {
155 return ObjectConsumerMulticaster.SUPPORTED_CONSUMER_TYPES;
156 }
157
158
159
160
161 public String getTitle()
162 {
163 return title.getText();
164 }
165
166
167
168
169 public Object getValue()
170 {
171 return null;
172 }
173
174
175
176
177
178 public AbstractCustomizerPanel getCustomizer()
179 {
180 if (customizer == null) {
181 customizer = AbstractCustomizerPanel.findCustomizer(this);
182 }
183
184 return customizer;
185 }
186
187
188 private void initialize()
189 {
190 if (initialized) {
191 return;
192 }
193
194
195 initialized = true;
196
197 setLayout(new GridBagLayout());
198 setBorder(CosyUIElements.getPlainBorder(true));
199
200 GridBagConstraints c = new GridBagConstraints();
201
202 getPopupManager().addAction(new AbstractAction("Preferences...") {
203 public void actionPerformed(ActionEvent e)
204 {
205 getCustomizer().showDialog();
206 }
207
208 });
209 this.addMouseListener(getPopupManager().getMouseHook());
210
211
212
213 title = new JLabel();
214 title.setHorizontalAlignment(SwingConstants.CENTER);
215 title.setHorizontalTextPosition(SwingConstants.CENTER);
216 title.setFont(title.getFont().deriveFont(Font.BOLD));
217
218
219 title.setUI(new MetalLabelUI() {
220 protected String layoutCL(JLabel label,
221 FontMetrics fontMetrics, String text, Icon icon,
222 Rectangle viewR, Rectangle iconR, Rectangle textR)
223 {
224 char[] chars = text.toCharArray();
225 ArrayHelper.flip(chars);
226
227 String prevText = String.copyValueOf(chars);
228 String modText = super.layoutCL(label, fontMetrics,
229 prevText, icon, viewR, iconR, textR);
230 chars = modText.toCharArray();
231 ArrayHelper.flip(chars);
232
233 return String.copyValueOf(chars);
234 }
235 });
236
237 c.anchor = GridBagConstraints.NORTH;
238 c.weightx = 1.0;
239 c.weighty = 0.0;
240 c.fill = GridBagConstraints.BOTH;
241 c.insets = new Insets(4, 4, 2, 4);
242 add(title, c);
243
244
245 tableModel = new MapTableModel(new String[]{ "property", "value"});
246 tableModel.setValueEditable(false);
247 tableModel.setKeyEditable(false);
248
249 final JScrollPane scroll = new JScrollPane();
250 table = new JTable(tableModel);
251
252
253 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
254 table.setSize(new Dimension(200,200));
255
256 scroll.setMinimumSize(new Dimension(100,50));
257 scroll.setViewportView(table);
258
259 table.addMouseListener(getPopupManager().getMouseHook());
260 scroll.addMouseListener(getPopupManager().getMouseHook());
261
262 c.gridy = 1;
263 c.weighty = 1.0;
264 c.insets = new Insets(0, 4, 4, 4);
265 add(scroll, c);
266 revalidate();
267
268
269
270
271 setRowWidthMultiplayer(1.5);
272
273
274 }
275
276 private void setRowWidthMultiplayer(double i){
277
278 DefaultTableColumnModel colModel = (DefaultTableColumnModel)table.getColumnModel();
279 for(int j=0;j<colModel.getColumnCount();j++){
280 TableColumn col = colModel.getColumn(j);
281
282 col.setPreferredWidth((int)(col.getWidth()*i));
283 }
284
285 }
286
287
288
289
290 public boolean isEditable()
291 {
292 return false;
293 }
294
295
296
297
298 public void setCharacteristics(Map characteristics)
299 {
300 tableModel.setMap(characteristics);
301 Object o= characteristics.get(C_DISPLAY_NAME);
302 if (o==null) {
303 o="N/A";
304 }
305 String t= o.toString();
306 setTitle(t);
307 revalidate();
308 repaint();
309 }
310
311
312
313
314 public void setTitle(String label)
315 {
316
317 title.setText(label);
318 }
319
320
321
322
323 public void setValue(Object value)
324 {
325 tableModel.put("VALUE",value);
326 }
327
328
329
330
331 public void updateValue(long timestamp, Object value)
332 throws CommonException
333 {
334 lastValue=value;
335 if(!isSuspended()){
336 setValue(value);
337 }
338 }
339
340
341
342 public void updateDataState(DataState state) {
343 super.updateDataState(state);
344 tableModel.put("DATA_STATE",state);
345 }
346
347
348
349
350
351 public static void main(String[] arg0) {
352 try {
353 JFrame bla = new JFrame("CharacteristicsMapPanel");
354 CharacteristicsMapPanel cmp = new CharacteristicsMapPanel();
355 bla.getContentPane().setLayout(new BorderLayout());
356 bla.getContentPane().add(cmp);
357 bla.setSize(350,150);
358 cmp.setTitle("aha");
359
360 bla.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
361
362 bla.setVisible(true);
363
364
365
366
367
368 String[] names = new String[2];
369 Class[] types = new Class[2];
370 Object[] values = new Object[2];
371
372 names[0] = C_DISPLAY_NAME;
373 types[0] = String.class;
374 values[0] = "SOME/DUMMY/DATA/SOURCE";
375
376 names[1] = C_DESCRIPTION;
377 types[1] = String.class;
378 values[1] = "This is simple DataSource implementation.";
379
380
381
382 PropertiesTableModel model = null;
383 model = new PropertiesTableModel(names, types, values);
384
385 table.setModel(model);
386
387 for(int i = 0 ; i<15; i++){
388
389 values[0] = new Integer(i);
390 values[1] = "This "+i;
391
392 cmp.updateValue(1,values);
393 Thread.sleep(1000);
394 if(i==5){
395 cmp.suspend();
396 }
397 if (i==10){
398 cmp.resume();
399 }
400 }
401 } catch (Exception e) {
402 e.printStackTrace();
403 }
404
405 }
406
407
408 private boolean resizable, titleVisible;
409 private int titleMinimumFontSize, titleMaximumFontSize;
410 public boolean isResizable() {
411 return resizable;
412 }
413
414 public void setResizable(boolean resizable) {
415 this.resizable = resizable;
416 }
417
418 public int getTitleMaximumFontSize() {
419 return titleMaximumFontSize;
420 }
421
422 public void setTitleMaximumFontSize(int titleMaximumFontSize) {
423 this.titleMaximumFontSize = titleMaximumFontSize;
424 }
425
426 public int getTitleMinimumFontSize() {
427 return titleMinimumFontSize;
428 }
429
430 public void setTitleMinimumFontSize(int titleMinimumFontSize) {
431 this.titleMinimumFontSize = titleMinimumFontSize;
432 }
433
434 public boolean isTitleVisible() {
435 return titleVisible;
436 }
437
438 public void setTitleVisible(boolean titleVisible) {
439 this.titleVisible = titleVisible;
440 }
441
442
443 }
444
445