1 package net.sf.panoptes.module.log4j;
2
3 import java.util.Date;
4 import java.util.HashMap;
5
6 import net.sf.panoptes.controller.MainController;
7 import net.sf.panoptes.model.node.Node;
8 import net.sf.panoptes.model.node.NodeUpdateEvent;
9 import net.sf.panoptes.view.configurator.NodeConfigurator;
10
11 import org.apache.log4j.Level;
12 import org.apache.log4j.spi.LoggingEvent;
13 import org.eclipse.jface.viewers.ILabelProviderListener;
14 import org.eclipse.jface.viewers.ISelectionChangedListener;
15 import org.eclipse.jface.viewers.IStructuredContentProvider;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.ITableLabelProvider;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.SashForm;
23 import org.eclipse.swt.events.DisposeEvent;
24 import org.eclipse.swt.events.DisposeListener;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.FillLayout;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Combo;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Event;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Listener;
34 import org.eclipse.swt.widgets.TableColumn;
35 import org.eclipse.swt.widgets.Text;
36
37 /***
38 * LoggerPanel.java
39 *
40 * NodeConfigurator for <code>LoggerComponent</code>s.
41 *
42 * @author Dag Liodden
43 * @version 0.1
44 */
45 public class LoggerPanel extends NodeConfigurator {
46
47 TableViewer table;
48 Combo comboLevel;
49 Text details;
50 final Text filter;
51
52 LoggerRepository repository;
53 private LoggerComponent loggerComponent = null;
54
55 /***
56 * Constructor for LoggerPanel.
57 * @param parent
58 * @param style
59 */
60 public LoggerPanel(MainController controller, Composite parent, int style, HashMap attributes) {
61 super(controller, parent, style, attributes);
62
63 this.repository = controller.getRootNode().getLoggerRepository();
64
65 setLayout(new FillLayout());
66
67 SashForm sash = new SashForm(this, SWT.VERTICAL);
68
69 Composite top = new Composite(sash, SWT.NULL);
70 top.setLayout(new GridLayout(4, false));
71
72 new Label(top, SWT.NULL).setText("Threshold:");
73 comboLevel = new Combo(top, SWT.DROP_DOWN | SWT.READ_ONLY);
74
75 comboLevel.add(Level.ERROR.toString());
76 comboLevel.add(Level.WARN.toString());
77 comboLevel.add(Level.INFO.toString());
78 comboLevel.add(Level.DEBUG.toString());
79
80 comboLevel.addListener(SWT.Selection, new Listener() {
81 public void handleEvent(Event arg0) {
82 Level l = Level.toLevel(comboLevel.getText());
83 setLevel(l);
84 refresh();
85 }
86 });
87
88 new Label(top, SWT.NULL).setText("Filter:");
89 filter = new Text(top, SWT.BORDER);
90 filter.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
91
92 filter.setText("");
93 filter.addListener(SWT.Modify, new Listener() {
94 public void handleEvent(Event event) {
95 loggerComponent.getFilter().setPrefix(filter.getText());
96 refresh();
97 }
98 });
99
100 table = new TableViewer(top, SWT.FULL_SELECTION | SWT.BORDER);
101 GridData gd = new GridData(GridData.FILL_BOTH);
102 gd.horizontalSpan = 4;
103 table.getTable().setLayoutData(gd);
104 table.getTable().setHeaderVisible(true);
105 table.getTable().setLinesVisible(true);
106
107 Composite bottom = new Composite(sash, SWT.NULL);
108 bottom.setLayout(new GridLayout());
109
110 Label tmp = new Label(bottom, SWT.NULL);
111 tmp.setText("Details:");
112 tmp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
113
114 details = new Text(bottom, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
115 details.setLayoutData(new GridData(GridData.FILL_BOTH));
116 details.setEditable(false);
117
118 table.setContentProvider(new IStructuredContentProvider() {
119 public Object[] getElements(Object inputElement) {
120
121 Object[] kids = null;
122 if (loggerComponent != null)
123 kids = loggerComponent.listEvents();
124 return kids == null ? new Object[0] : kids;
125 }
126
127 public void dispose() {
128 }
129
130 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
131 }
132 });
133
134 table.setLabelProvider(new ITableLabelProvider() {
135 public Image getColumnImage(Object element, int columnIndex) {
136 if (columnIndex == 0) {
137 LoggingEvent m = ((LoggingEventInfo) element).getLoggingEvent();
138 Image icon;
139
140 switch (m.getLevel().toInt()) {
141 case Level.ERROR_INT :
142 {
143 icon = Node.ICON_ERROR;
144 break;
145 }
146 case Level.WARN_INT :
147 {
148 icon = Node.ICON_WARNING;
149 break;
150 }
151 case Level.INFO_INT :
152 case Level.DEBUG_INT :
153 {
154 icon = Node.ICON_INFO;
155 break;
156 }
157 default :
158 {
159 icon = null;
160 }
161 }
162 return icon == null ? null : icon;
163 } else
164 return null;
165 }
166
167 public String getColumnText(Object element, int columnIndex) {
168 LoggingEvent m = ((LoggingEventInfo) element).getLoggingEvent();
169 switch (columnIndex) {
170 case 0 :
171 {
172 return "";
173 }
174 case 1 :
175 {
176 return (new Date(m.timeStamp)).toString();
177 }
178 case 2 :
179 {
180 return m.getRenderedMessage() == null ? "-" : m.getRenderedMessage().toString();
181 }
182 case 3 :
183 {
184 return m.getLocationInformation().fullInfo == null
185 ? "-"
186 : m.getLocationInformation().fullInfo;
187 }
188 case 4 :
189 {
190 return m.getNDC() == null ? "-" : m.getNDC();
191 }
192 default :
193 {
194 return "Unknown";
195 }
196 }
197 }
198
199 public void addListener(ILabelProviderListener listener) {
200 }
201
202 public void dispose() {
203 }
204
205 public boolean isLabelProperty(Object element, String property) {
206 return false;
207 }
208
209 public void removeListener(ILabelProviderListener listener) {
210 }
211 });
212
213 table.addSelectionChangedListener(new ISelectionChangedListener() {
214 public void selectionChanged(SelectionChangedEvent event) {
215 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
216 LoggingEvent m = ((LoggingEventInfo) selection.getFirstElement()).getLoggingEvent();
217 String message = m.getMessage().toString();
218 String cause = "No stacktrace available";
219
220 if (m.getThrowableInformation() != null) {
221 String[] trace = m.getThrowableInformation().getThrowableStrRep();
222 StringBuffer b = new StringBuffer();
223 for (int i = 0; i < trace.length; i++) {
224 b.append(trace[i] + "\n");
225 }
226 cause = b.toString();
227 }
228 details.setText(
229 "Message:\n"
230 + message
231 + "\n\nLocation:\n"
232 + m.getLocationInformation().fullInfo
233 + "\n\nNDC:\n"
234 + m.getNDC()
235 + "\n\nCause:\n"
236 + cause);
237 }
238 });
239
240 TableColumn tc;
241
242 tc = new TableColumn(table.getTable(), SWT.NULL);
243 tc.setWidth(20);
244 tc.setText("!");
245
246 tc = new TableColumn(table.getTable(), SWT.NULL);
247 tc.setWidth(200);
248 tc.setText("Time");
249
250 tc = new TableColumn(table.getTable(), SWT.NULL);
251 tc.setWidth(200);
252 tc.setText("Message");
253
254 tc = new TableColumn(table.getTable(), SWT.NULL);
255 tc.setWidth(200);
256 tc.setText("Location");
257
258 tc = new TableColumn(table.getTable(), SWT.NULL);
259 tc.setWidth(200);
260 tc.setText("NDC");
261
262 table.setInput("");
263
264 addDisposeListener(new DisposeListener() {
265 public void widgetDisposed(DisposeEvent e) {
266 }
267 });
268 }
269
270 private void revealLastItem() {
271 if (table.getTable().getItemCount() > 0)
272 table.reveal(table.getElementAt(table.getTable().getItemCount() - 1));
273 }
274
275 public void refresh() {
276 long start = System.currentTimeMillis();
277 table.refresh(true);
278 revealLastItem();
279 }
280
281 public void setLoggerComponent(LoggerComponent component) {
282 loggerComponent = component;
283 if (component == null)
284 return;
285 setLevel(component.getFilter().getLevel());
286 table.refresh(true);
287 revealLastItem();
288 }
289
290 public void setNode(Node component) {
291 setLoggerComponent((LoggerComponent) component);
292 comboLevel.setText(loggerComponent.getFilter().getLevel().toString());
293 }
294
295 protected void setLevel(Level level) {
296 loggerComponent.getFilter().setLevel(level);
297 comboLevel.setText(level.toString());
298 }
299
300 public LoggerComponent getLoggerComponent() {
301 return loggerComponent;
302 }
303
304 /* (non-Javadoc)
305 * @see net.sf.panoptes.model.node.NodeUpdateListener#nodeUpdated(net.sf.panoptes.model.node.NodeUpdateEvent)
306 */
307 public void nodeUpdated(NodeUpdateEvent event) {
308 if (event instanceof LogComponentUpdateEvent) {
309 table.add(((LogComponentUpdateEvent) event).getNewEvents().toArray());
310 table.reveal(table.getElementAt(table.getTable().getItemCount() - 1));
311 }
312 }
313
314 }
This page was automatically generated by Maven