1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.gui.renderers;
25
26 import java.awt.Color;
27 import java.awt.Component;
28 import java.awt.Graphics;
29
30 import javax.swing.Icon;
31 import javax.swing.JComponent;
32 import javax.swing.JTable;
33 import javax.swing.UIManager;
34 import javax.swing.border.LineBorder;
35 import javax.swing.table.DefaultTableCellRenderer;
36
37 import com.mindtree.techworks.insight.InsightConstants;
38 import com.mindtree.techworks.insight.model.LogEventTableModel;
39 import com.mindtree.techworks.insight.receiver.RuntimeNamespaceContainer;
40 import com.mindtree.techworks.insight.spi.LogEvent;
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class LogEventTableCellRenderer extends DefaultTableCellRenderer implements Icon {
55
56
57
58
59 private static final long serialVersionUID = 4519016975132589236L;
60
61
62
63
64 private static final Color INFO_COLOR = Color.white;
65 private static final Color DEBUG_COLOR = Color.green;
66 private static final Color WARN_COLOR = Color.yellow;
67 private static final Color ERROR_COLOR = Color.orange;
68 private static final Color FATAL_COLOR = Color.red;
69
70
71
72
73 private static final char INFO_CHAR = 'I';
74 private static final char DEBUG_CHAR = 'D';
75 private static final char WARN_CHAR = 'W';
76 private static final char ERROR_CHAR = 'E';
77 private static final char FATAL_CHAR = 'F';
78
79
80
81
82 private static final int LEGEND_WIDTH = 8;
83 private static final int LEGEND_HEIGHT = 8;
84
85
86
87
88 private Object value;
89
90
91
92
93 private int selectedRow = -1;
94
95
96
97
98
99 public Component getTableCellRendererComponent(JTable table,
100 Object value,
101 boolean isSelected,
102 boolean hasFocus,
103 int row,
104 int column) {
105 this.value = value;
106 int columnIdentifier = ((Integer)table.getColumnModel().getColumn(column).getIdentifier()).intValue();
107 switch(columnIdentifier) {
108 case LogEventTableModel.PRIORITY_INDEX:
109 this.setIcon(this);
110 break;
111 default :
112 this.setIcon(null);
113 break;
114 }
115 JComponent renderingComponent = (JComponent)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
116 if (isSelected) {
117 if (selectedRow >= 0) {
118 JComponent oldRenderingComponent = (JComponent)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,selectedRow,column);
119 oldRenderingComponent.setBorder(DefaultTableCellRenderer.noFocusBorder);
120 }
121 selectedRow = row;
122
123 renderingComponent.setBorder( new TableCellBorder(UIManager.getDefaults().getColor(InsightConstants.TEXT_HIGHLIGHT).darker(),
124 column==0, column==table.getModel().getColumnCount() - 1));
125 }
126 LogEvent event = ((LogEventTableModel)table.getModel()).getLoggingEvent(row);
127 Color namespaceColor = RuntimeNamespaceContainer.getNamespaceColor(event.getNamespace());
128 renderingComponent.setBackground(namespaceColor);
129 return renderingComponent;
130 }
131
132
133
134
135
136 public int getIconHeight() {
137 return LEGEND_HEIGHT;
138 }
139
140
141
142
143
144 public int getIconWidth() {
145 return LEGEND_WIDTH;
146 }
147
148
149
150
151
152 public void paintIcon(Component c, Graphics g, int x, int y) {
153 char startChar = this.value.toString().charAt(0);
154 switch(startChar) {
155 case INFO_CHAR:
156 g.setColor(INFO_COLOR);
157 break;
158 case DEBUG_CHAR:
159 g.setColor(DEBUG_COLOR);
160 break;
161 case WARN_CHAR:
162 g.setColor(WARN_COLOR);
163 break;
164 case ERROR_CHAR:
165 g.setColor(ERROR_COLOR);
166 break;
167 case FATAL_CHAR:
168 g.setColor(FATAL_COLOR);
169 break;
170 }
171 g.fill3DRect(x,y,LEGEND_WIDTH,LEGEND_HEIGHT,true);
172 }
173
174
175
176
177
178 private class TableCellBorder extends LineBorder {
179
180
181
182
183 private static final long serialVersionUID = 1582741766574751785L;
184
185 private Color color;
186 private boolean isLeftBoundary;
187 private boolean isRightBoundary;
188
189 public TableCellBorder(Color color, boolean isLeftBoundary, boolean isRightBoundary) {
190 super(color);
191 this.color = color;
192 this.isLeftBoundary = isLeftBoundary;
193 this.isRightBoundary = isRightBoundary;
194 }
195
196 public void paintBorder(Component c,
197 Graphics g,
198 int x,
199 int y,
200 int width,
201 int height) {
202 g.setColor(color);
203 g.drawLine(0,0,width,0);
204 g.drawLine(0,height - 1,width,height - 1);
205 if (isLeftBoundary) {
206 g.drawLine(0,0,0,height);
207 } else if (isRightBoundary) {
208 g.drawLine(width - 1,0,width - 1,height);
209 }
210 }
211 }
212
213 }