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.TableCell;
23 import com.cosylab.gui.components.util.ColorHelper;
24 import com.cosylab.gui.components.util.PaintHelper;
25
26 import java.awt.Color;
27 import java.awt.Component;
28 import java.awt.Graphics;
29
30 import java.util.GregorianCalendar;
31
32 import javax.swing.JTable;
33
34
35
36
37
38
39
40
41
42 public class DefaultTableCellRenderer
43 extends javax.swing.table.DefaultTableCellRenderer
44 {
45 private static final long serialVersionUID = 1L;
46
47
48
49
50 public DefaultTableCellRenderer()
51 {
52 this(false,true);
53 }
54
55
56
57 public DefaultTableCellRenderer(boolean decorateBorder, boolean decorateBackground)
58 {
59 super();
60 setOpaque(true);
61 this.decorateBorder=decorateBorder;
62 this.decorateBackground=decorateBackground;
63 }
64
65 protected boolean timeout;
66 protected boolean error;
67 protected boolean alarm;
68 protected boolean active;
69 protected String severity;
70 protected boolean decorateBorder=false;
71 protected boolean decorateBackground=true;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public Component getTableCellRendererComponent(JTable table, Object cell,
93 boolean isSelected, boolean hasFocus, int row, int column)
94 {
95 Component comp = super.getTableCellRendererComponent(table, cell,
96 isSelected, hasFocus, row, column);
97
98
99 if (isSelected) {
100 comp.setBackground(table.getSelectionBackground());
101 } else {
102 comp.setBackground(table.getBackground());
103 }
104
105 if (cell instanceof TableCell) {
106 TableCell tc = (TableCell)cell;
107
108 if (tc.isOutOfBounds()) {
109 comp.setBackground(ColorHelper.getWarning());
110 }
111
112 timeout = tc.isTimeout();
113 error = tc.isError();
114 active = !tc.isSuspended();
115 alarm = tc.isAlarm();
116 severity = (String)tc.getCharacteristic(TableCell.SEVERITY);
117
118 String t = tc.getTooltip();
119 setToolTipText(t);
120
121 if (decorateBackground) {
122 if (error || timeout) {
123 setBackground(ColorHelper.getAlarmInvalidBackground());
124 }
125
126 if (alarm) {
127 if (severity == null) {
128 setBackground(Color.WHITE);
129 } else if (severity.equals("MAJOR_ALARM")) {
130 setBackground(ColorHelper.getAlarmMajorBackground());
131 } else if (severity.equals("MINOR_ALARM")) {
132 setBackground(ColorHelper.getAlarmMinorBackground());
133 } else if (severity.equals("INVALID_ALARM")) {
134 setBackground(ColorHelper.getAlarmInvalidBackground());
135 }
136 }
137
138 }
139 }
140
141 return comp;
142 }
143
144
145
146
147 protected void paintComponent(Graphics g)
148 {
149 super.paintComponent(g);
150
151 paintDecorations(g);
152 }
153
154 protected void paintSuperComponent(Graphics g)
155 {
156 super.paintComponent(g);
157 }
158
159 protected void paintDecorations(Graphics g)
160 {
161 if (!active) {
162 PaintHelper.paintDisabled(g, 0, 0, getWidth(), getHeight());
163 }
164
165 if (!decorateBorder) {
166 return;
167 }
168
169 int size = getHeight();
170 int y = 0;
171 int x = (getWidth() - size) / 2;
172
173 if (error && !timeout) {
174 PaintHelper.paintAlarm(g, x, y, size);
175 }
176
177 if (!error && timeout) {
178 PaintHelper.paintTimeout(g, x, y, size, new GregorianCalendar(1900,0,0,3,0).getTime());
179 }
180
181 if (error && timeout) {
182 x = (getWidth() - 2 * size) / 2;
183 PaintHelper.paintAlarm(g, x, y, size);
184 x += size;
185 PaintHelper.paintTimeout(g, x, y, size, new GregorianCalendar(1900,0,0,3,0).getTime());
186 }
187
188 if (alarm) {
189 if (severity == null) {
190 PaintHelper.paintRectangle(g, 0, 0, getWidth(), getHeight(),
191 ColorHelper.getAlarm(), 2);
192 } else if (severity.equals("MAJOR_ALARM")) {
193 PaintHelper.paintRectangle(g, 0, 0, getWidth(), getHeight(),
194 ColorHelper.getAlarmMajor(), 2);
195 } else if (severity.equals("MINOR_ALARM")) {
196 PaintHelper.paintRectangle(g, 0, 0, getWidth(), getHeight(),
197 ColorHelper.getAlarmMinor(), 2);
198 } else if (severity.equals("INVALID_ALARM")) {
199 PaintHelper.paintRectangle(g, 0, 0, getWidth(), getHeight(),
200 ColorHelper.getAlarmInvalid(), 2);
201 }
202 }
203 }
204 public boolean isDecorateBackground() {
205 return decorateBackground;
206 }
207 public void setDecorateBackground(boolean decorateBackground) {
208 this.decorateBackground = decorateBackground;
209 }
210 public boolean isDecorateBorder() {
211 return decorateBorder;
212 }
213 public void setDecorateBorder(boolean decorateBorder) {
214 this.decorateBorder = decorateBorder;
215 }
216 }
217
218