-
Notifications
You must be signed in to change notification settings - Fork 104
/
MessagesView.java
374 lines (330 loc) · 10.7 KB
/
MessagesView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*******************************************************************************
* Copyright (c) 2013 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.fusesource.ide.fabric.views;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.fusesource.fon.util.messages.Exchange;
import org.fusesource.fon.util.messages.Exchanges;
import org.fusesource.fon.util.messages.IExchange;
import org.fusesource.fon.util.messages.IExchangeBrowser;
import org.fusesource.fon.util.messages.IMessage;
import org.fusesource.fon.util.messages.ITraceExchangeBrowser;
import org.fusesource.ide.commons.Viewers;
import org.fusesource.ide.commons.ui.config.TableConfiguration;
import org.fusesource.ide.commons.ui.views.TableViewSupport;
import org.fusesource.ide.commons.util.Function1;
import org.fusesource.ide.commons.util.FunctionLong;
import org.fusesource.ide.fabric.FabricPlugin;
/**
* UI for viewing messages
*/
public class MessagesView extends TableViewSupport { // implements ITabbedPropertySheetPageContributor {
public static final String ID = "org.fusesource.ide.fabric.views.MessagesView";
IExchangeBrowser browser;
List<IExchange> exchanges = new ArrayList<IExchange>();
private boolean showBody = true;
private boolean showTimestamp = true;
private boolean showRelativeTime = true;
private boolean showToNode = true;
private boolean showTraceExchangeId = true;
private boolean showElapsedTime = true;
private ISelectionListener selectionListener = new ISelectionListener() {
@Override
public void selectionChanged(IWorkbenchPart part,
ISelection selection) {
// we only want to process selection change events from few selected sources...so filtering here
if (!isRelevantSelectionSource(part, selection)) {
return;
}
IExchangeBrowser browser = ExchangeBrowsers.getSelectedExchangeBrowser(selection);
setExchangeBrowser(browser);
}
};
public MessagesView() {
}
@Override
protected String getHelpID() {
return ID;
}
/* (non-Javadoc)
* @see org.fusesource.ide.commons.ui.IConfigurableColumns#getColumnConfigurationId()
*/
@Override
public String getColumnConfigurationId() {
String key = ID;
if (browser instanceof ITraceExchangeBrowser) {
key += "_tracing";
}
return key;
}
@Override
public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
if (adapter == IPropertySheetPage.class) {
ISelection selection = viewer.getSelection();
MessageDetailView view = new MessageDetailView();
if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
Object o = ((IStructuredSelection)selection).getFirstElement();
if (o instanceof Exchange) {
view.setSelectedExchange((Exchange)o);
}
}
return view;
}
return super.getAdapter(adapter);
}
@Override
public void init(IViewSite site) throws PartInitException {
super.init(site);
if (site != null) {
site.getWorkbenchWindow().getSelectionService().addSelectionListener(selectionListener);
}
}
/* (non-Javadoc)
* @see org.fusesource.ide.commons.ui.views.TableViewSupport#getColumns()
*/
@Override
public List<String> getColumns() {
TableConfiguration tc = getConfiguration();
List<String> cols = new ArrayList<String>();
if (tc == null || tc.hasColumns() == false) return super.getColumns();
tc.reload();
Iterator<String> names = tc.getColumnMap().keySet().iterator();
while (names.hasNext()) {
cols.add(names.next());
}
tc.sortDefaultColumnNames(cols);
return cols;
}
@Override
public void createPartControl(Composite parent) {
super.createPartControl(parent);
aboutToBeShown();
}
@Override
public void dispose() {
IViewSite site = getViewSite();
if (site != null) {
site.getWorkbenchWindow().getSelectionService().removeSelectionListener(selectionListener);
}
super.dispose();
}
private boolean isRelevantSelectionSource(IWorkbenchPart part, ISelection selection) {
boolean process = false;
// we filter for specific selection sources...
if (part.getClass().getName().equals("org.jboss.tools.jmx.ui.internal.views.navigator.JMXNavigator") ||
part.getClass().getName().equals("org.fusesource.ide.fabric.navigator.FabricNavigator") ||
part.getClass().getName().equals("org.fusesource.ide.camel.editor.views.DiagramView") ||
part.getClass().getName().equals("org.eclipse.wst.server.ui.ServersView")
) {
process = true;
}
return process;
}
@Override
protected void createViewer() {
super.createViewer();
// set the comparator to be default ascending sorted so oldest is in the top
// TODO: setDefaultSortColumnIndex(0);
getComparator().setDescending(false);
}
@Override
public void refresh() {
if (browser != null) {
List<IExchange> list = browser.browseExchanges();
if (list != null) {
// FabricPlugin.getLogger().debug("==== Browsed exchanges: " + list);
this.exchanges = list;
viewer.setInput(exchanges);
/*
recreateColumns();
getViewer().refresh();
*/
// only recreate viewer if its valid and visible
//if (Viewers.isValid(viewer)) {
// createViewer();
//}
}
} else {
// create an empty viewer in case its not a browsable node
this.exchanges.clear();
viewer.refresh();
}
setConfiguration(null);
updateColumnConfiguration(getConfiguration());
Viewers.refresh(getViewer());
}
@Override
protected void createColumns() {
int bounds = 100;
int column = 0;
clearColumns();
if (browser instanceof ITraceExchangeBrowser) {
final Function1 function = new Function1() {
@Override
public Object apply(Object element) {
Exchange exchange = Exchanges.asExchange(element);
if (exchange != null) {
return exchange.getExchangeIndex();
}
return null;
}
};
column = addColumnFunction(bounds, column, function, "Trace ID");
}
// TODO add exchange / message ID??
if (browser instanceof ITraceExchangeBrowser) {
if (showTraceExchangeId) {
final Function1 function = new Function1() {
@Override
public Object apply(Object element) {
Exchange exchange = Exchanges.asExchange(element);
if (exchange != null) {
return exchange.getId();
}
return null;
}
};
column = addColumnFunction(bounds, column, function, "Exchange ID");
}
}
if (showBody) {
final Function1 function = new Function1() {
@Override
public Object apply(Object element) {
if (element instanceof IExchange) {
IExchange exchange = (IExchange) element;
String answer = MessageUIHelper.getBody(exchange);
if (answer != null) {
// lets replace newlines
return answer.replace('\n', ' ');
}
}
return null;
}
};
column = addColumnFunction(bounds, column, function, "Message Body");
}
SortedSet<String> headers = new TreeSet<String>();
for (IExchange exchange : exchanges) {
IMessage in = exchange.getIn();
if (in != null) {
headers.addAll(in.getHeaders().keySet());
}
}
for (final String header : headers) {
final Function1 function = new Function1() {
@Override
public Object apply(Object element) {
if (element instanceof IExchange) {
IExchange exchange = (IExchange) element;
IMessage in = exchange.getIn();
if (in != null) {
return in.getHeaders().get(header);
}
}
return null;
}
};
column = addColumnFunction(bounds, column, function, header);
}
if (browser instanceof ITraceExchangeBrowser) {
if (showToNode) {
final Function1 function = new Function1() {
@Override
public Object apply(Object element) {
IMessage message = Exchanges.asMessage(element);
if (message != null) {
return message.getToNode();
}
return null;
}
};
column = addColumnFunction(bounds, column, function, "Trace Node Id");
}
if (showTimestamp) {
final Function1 function = new Function1() {
@Override
public Object apply(Object element) {
IMessage message = Exchanges.asMessage(element);
if (message != null) {
return message.getTimestamp();
}
return null;
}
};
column = addColumnFunction(bounds, column, function, "Trace Timestamp");
}
if (showRelativeTime) {
final Function1 function = new FunctionLong() {
@Override
public Long apply(Object element) {
IMessage message = Exchanges.asMessage(element);
if (message != null) {
return message.getRelativeTime();
}
return null;
}
};
column = addColumnFunction(bounds, column, function, "Relative Time (ms)");
}
if (showElapsedTime) {
final Function1 function = new FunctionLong() {
@Override
public Long apply(Object element) {
IMessage message = Exchanges.asMessage(element);
if (message != null) {
return message.getElapsedTime();
}
return null;
}
};
column = addColumnFunction(bounds, column, function, "Elapsed Time (ms)");
}
}
}
@Override
protected void configureViewer() {
viewer.setInput(exchanges);
new MessageDragListener(viewer).register();
}
@Override
protected IStructuredContentProvider createContentProvider() {
return new IStructuredContentProvider() {
@Override
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
@Override
public void dispose() {
}
@Override
public Object[] getElements(Object parent) {
return exchanges.toArray();
}
};
}
public void setExchangeBrowser(IExchangeBrowser browser) {
this.browser = browser;
refresh();
}
}