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.table.renderers;
21
22 import com.cosylab.gui.components.table.cells.DoubleCell;
23
24 import com.cosylab.util.FormatCache;
25 import com.cosylab.util.PrintfFormat;
26
27 import java.awt.Component;
28
29 import javax.swing.JTable;
30
31
32
33
34
35
36
37
38
39 public class DoubleCellRenderer extends DefaultTableCellRenderer
40 {
41
42 private static final long serialVersionUID = 1L;
43
44
45
46
47 public DoubleCellRenderer() {
48 this(false,true);
49 }
50
51
52
53
54
55
56
57 public DoubleCellRenderer(boolean decorateBorder, boolean decorateBackground)
58 {
59 super(decorateBorder,decorateBackground);
60 setOpaque(true);
61 }
62
63
64
65
66
67 public Component getTableCellRendererComponent(JTable table, Object cell,
68 boolean isSelected, boolean hasFocus, int row, int column)
69 {
70 super.getTableCellRendererComponent(table, cell, isSelected, hasFocus,
71 row, column);
72
73 if (cell instanceof DoubleCell) {
74 if (((DoubleCell)cell).isSuspended()) {
75 setText("");
76 } else {
77 PrintfFormat formatter = FormatCache.getFormatter(((DoubleCell)cell)
78 .getFormat());
79
80 if (formatter == null) {
81 formatter = FormatCache.getDefaultFormatter();
82 }
83
84 String value = formatter.sprintf(((DoubleCell)cell).getDoubleValue());
85 if (((DoubleCell)cell).isUnitsVisible()) {
86 value += " " + ((DoubleCell)cell).getUnits();
87 }
88 setText(value);
89 }
90 }
91
92 return this;
93 }
94 }
95
96