Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0c5a0fa96a928d8f2900d297e9bb0b17b2e6b435 (plain) (blame)
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are 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:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.internal.debug.views;

import org.eclipse.net4j.internal.debug.RemoteTraceManager;
import org.eclipse.net4j.util.ObjectUtil;
import org.eclipse.net4j.util.ReflectUtil;
import org.eclipse.net4j.util.om.trace.RemoteTraceServer.Event;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

/**
 * @author Eike Stepper
 */
public class RemoteTraceView extends ViewPart
{
  private static RemoteTraceView instance;

  private TableViewer viewer;

  private Action clearAction;

  private Action doubleClickAction;

  public RemoteTraceView()
  {
  }

  @Override
  public void dispose()
  {
    instance = null;
    super.dispose();
  }

  @Override
  public void createPartControl(Composite parent)
  {
    viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.getTable().setHeaderVisible(true);
    createColmuns(viewer);
    viewer.setContentProvider(new ViewContentProvider());
    viewer.setLabelProvider(new ViewLabelProvider());
    // viewer.setSorter(new NameSorter());
    viewer.setInput(getViewSite());

    // final ToolTipHandler tooltip = new ToolTipHandler(getSite().getShell());
    // tooltip.activateHoverHelp(viewer.getTable());

    makeActions();
    hookContextMenu();
    hookDoubleClickAction();
    contributeToActionBars();
    instance = this;
  }

  protected void createColmuns(TableViewer viewer)
  {
    final String[] columnNames = { "ID", "Time Stamp", "Agent ID", "Bundle ID", "Tracer Name", "Context", "Message",
        "Throwable" };
    final int[] columnWidths = { 60, 170, 80, 160, 120, 120, 400, 200 };
    TableColumn[] columns = new TableColumn[columnNames.length];
    for (int i = 0; i < columns.length; i++)
    {
      TableColumn column = new TableColumn(viewer.getTable(), SWT.LEFT, i);
      column.setText(columnNames[i]);
      column.setWidth(columnWidths[i]);
      column.setMoveable(true);
      column.setResizable(true);
    }
  }

  public static void notifyNewTrace()
  {
    if (instance != null)
    {
      instance.refreshViewer();
    }
  }

  public void refreshViewer()
  {
    if (isViewerAlive())
    {
      getSite().getShell().getDisplay().asyncExec(new Runnable()
      {
        public void run()
        {
          if (isViewerAlive())
          {
            viewer.refresh();
          }
        }
      });
    }
  }

  private boolean isViewerAlive()
  {
    return viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed();
  }

  private void hookContextMenu()
  {
    MenuManager menuMgr = new MenuManager("#PopupMenu");
    menuMgr.setRemoveAllWhenShown(true);
    menuMgr.addMenuListener(new IMenuListener()
    {
      public void menuAboutToShow(IMenuManager manager)
      {
        RemoteTraceView.this.fillContextMenu(manager);
      }
    });
    Menu menu = menuMgr.createContextMenu(viewer.getControl());
    viewer.getControl().setMenu(menu);
    getSite().registerContextMenu(menuMgr, viewer);
  }

  private void contributeToActionBars()
  {
    IActionBars bars = getViewSite().getActionBars();
    fillLocalPullDown(bars.getMenuManager());
    fillLocalToolBar(bars.getToolBarManager());
  }

  private void fillLocalPullDown(IMenuManager manager)
  {
    manager.add(clearAction);
  }

  private void fillContextMenu(IMenuManager manager)
  {
    manager.add(clearAction);
    // Other plug-ins can contribute there actions here
    manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
  }

  private void fillLocalToolBar(IToolBarManager manager)
  {
    manager.add(clearAction);
  }

  private void makeActions()
  {
    clearAction = new Action()
    {
      @Override
      public void run()
      {
        RemoteTraceManager.INSTANCE.clearEvents();
        refreshViewer();
      }
    };
    clearAction.setText("Clear");
    clearAction.setToolTipText("Clear");
    clearAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
        ISharedImages.IMG_TOOL_DELETE));

    doubleClickAction = new Action()
    {
      @Override
      public void run()
      {
        ISelection selection = viewer.getSelection();
        Object obj = ((IStructuredSelection)selection).getFirstElement();
        showMessage("Double-click detected on " + obj.toString());
      }
    };
  }

  private void hookDoubleClickAction()
  {
    viewer.addDoubleClickListener(new IDoubleClickListener()
    {
      public void doubleClick(DoubleClickEvent event)
      {
        doubleClickAction.run();
      }
    });
  }

  private void showMessage(String message)
  {
    MessageDialog.openInformation(viewer.getControl().getShell(), "Remote Traces", message);
  }

  /**
   * Passing the focus request to the viewer's control.
   */
  @Override
  public void setFocus()
  {
    viewer.getControl().setFocus();
  }

  /**
   * @author Eike Stepper
   */
  class ViewContentProvider implements IStructuredContentProvider
  {
    public void inputChanged(Viewer v, Object oldInput, Object newInput)
    {
    }

    public void dispose()
    {
    }

    public Object[] getElements(Object parent)
    {
      return RemoteTraceManager.INSTANCE.getEvents();
    }
  }

  /**
   * @author Eike Stepper
   */
  class ViewLabelProvider extends LabelProvider implements ITableLabelProvider
  {
    private Image info;

    private Image error;

    private Image text;

    public ViewLabelProvider()
    {
      ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
      info = sharedImages.getImage(ISharedImages.IMG_OBJS_INFO_TSK);
      error = sharedImages.getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
      text = sharedImages.getImage(ISharedImages.IMG_OBJ_FILE);
    }

    public String getColumnText(Object obj, int index)
    {
      if (obj instanceof Event)
      {
        Event event = (Event)obj;
        String text = event.getText(index);
        switch (index)
        {
        case 5:
          return ReflectUtil.getSimpleClassName(text);
        case 6:
          int at = text.indexOf('@');
          if (at == -1)
          {
            return text;
          }

          String context = ReflectUtil.getSimpleClassName(event.getText(5));
          String id = text.substring(at + 1);
          String className = text.substring(0, at);
          if (ObjectUtil.equals(context, className))
          {
            return id;
          }

          return id + " (" + className + ")";

        case 7:
          return getFirstLine(text);
        }

        return text;
      }

      return getText(obj);
    }

    public Image getColumnImage(Object obj, int index)
    {
      if (obj instanceof Event)
      {
        Event event = (Event)obj;
        switch (index)
        {
        case 0:
          return event.hasError() ? error : info;
        case 7:
          return hasNL(event.getMessage()) ? text : null;
        case 8:
          return event.hasError() ? error : null;
        }
      }

      return null;
    }

    @Override
    public Image getImage(Object obj)
    {
      return null;
    }

    private boolean hasNL(String str)
    {
      return str.indexOf('\n') != -1;
    }

    private String getFirstLine(String str)
    {
      int nl = str.indexOf('\n');
      if (nl != -1)
      {
        str = str.substring(0, nl);
      }

      return str.replaceAll("\r", "");
    }
  }

  /**
   * @author Eike Stepper
   */
  class NameSorter extends ViewerSorter
  {
  }

  // static class ToolTipHandler
  // {
  // private Shell tipShell;
  //
  // private Label tipLabel;
  //
  // private TableItem tipItem;
  //
  // private Point tipPosition;
  //
  // public ToolTipHandler(Shell parent)
  // {
  // final Display display = parent.getDisplay();
  //
  // tipShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL);
  // GridLayout gridLayout = new GridLayout();
  // gridLayout.numColumns = 2;
  // gridLayout.marginWidth = 2;
  // gridLayout.marginHeight = 2;
  // tipShell.setLayout(gridLayout);
  //
  // tipShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
  //
  // tipLabel = new Label(tipShell, SWT.NONE);
  // tipLabel.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
  // tipLabel.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
  // tipLabel
  // .setLayoutData(new GridData(GridData.FILL_HORIZONTAL |
  // GridData.VERTICAL_ALIGN_CENTER));
  // }
  //
  // public void activateHoverHelp(final Control control)
  // {
  // control.addMouseListener(new MouseAdapter()
  // {
  // @Override
  // public void mouseDown(MouseEvent e)
  // {
  // if (tipShell.isVisible())
  // {
  // tipShell.setVisible(false);
  // }
  // }
  // });
  //
  // control.addMouseTrackListener(new MouseTrackAdapter()
  // {
  // @Override
  // public void mouseExit(MouseEvent e)
  // {
  // if (tipShell.isVisible())
  // {
  // tipShell.setVisible(false);
  // }
  //
  // tipItem = null;
  // }
  //
  // @Override
  // public void mouseHover(MouseEvent event)
  // {
  // Point pt = new Point(event.x, event.y);
  // Widget widget = event.widget;
  // if (widget instanceof Table)
  // {
  // Table table = (Table)widget;
  // TableItem item = table.getItem(pt);
  // if (widget == null)
  // {
  // tipShell.setVisible(false);
  // tipItem = null;
  // return;
  // }
  //
  // if (item == tipItem)
  // {
  // return;
  // }
  //
  // Object data = item.getData();
  // if (data instanceof Event)
  // {
  // Event e = (Event)data;
  // }
  //
  // tipItem = item;
  // tipPosition = control.toDisplay(pt);
  // String text = (String)item.getData("TIP_TEXT");
  // tipLabel.setText(text != null ? text : "");
  // tipShell.pack();
  // setHoverLocation(tipShell, tipPosition);
  // tipShell.setVisible(true);
  // }
  // }
  // });
  // }
  //
  // private void setHoverLocation(Shell shell, Point position)
  // {
  // Rectangle displayBounds = shell.getDisplay().getBounds();
  // Rectangle shellBounds = shell.getBounds();
  // shellBounds.x = Math.max(Math.min(position.x, displayBounds.width -
  // shellBounds.width), 0);
  // shellBounds.y = Math.max(
  // Math.min(position.y + 16, displayBounds.height - shellBounds.height), 0);
  // shell.setBounds(shellBounds);
  // }
  // }
}

Back to the top