tmf: Remove the ITmfEventTableColumns extension point
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / editors / TmfEventsEditor.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson, École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 * Geneviève Bastien - Experiment instantiated with experiment type
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ui.editors;
15
16 import java.util.LinkedHashSet;
17 import java.util.Set;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IMarker;
21 import org.eclipse.core.resources.IMarkerDelta;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IResourceChangeEvent;
24 import org.eclipse.core.resources.IResourceChangeListener;
25 import org.eclipse.core.resources.IResourceDelta;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.InvalidRegistryObjectException;
30 import org.eclipse.core.runtime.ListenerList;
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.eclipse.jface.action.IStatusLineManager;
34 import org.eclipse.jface.util.SafeRunnable;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.ISelectionChangedListener;
37 import org.eclipse.jface.viewers.ISelectionProvider;
38 import org.eclipse.jface.viewers.SelectionChangedEvent;
39 import org.eclipse.jface.viewers.StructuredSelection;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
43 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
44 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
45 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
46 import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
47 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
48 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
49 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
50 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
51 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
52 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
53 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
54 import org.eclipse.tracecompass.tmf.ui.project.model.Messages;
55 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
56 import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
57 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
58 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
59 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
60 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils;
61 import org.eclipse.tracecompass.tmf.ui.viewers.events.TmfEventsTable;
62 import org.eclipse.ui.IEditorInput;
63 import org.eclipse.ui.IEditorPart;
64 import org.eclipse.ui.IEditorSite;
65 import org.eclipse.ui.IFileEditorInput;
66 import org.eclipse.ui.IPartListener;
67 import org.eclipse.ui.IPropertyListener;
68 import org.eclipse.ui.IReusableEditor;
69 import org.eclipse.ui.IWorkbenchPart;
70 import org.eclipse.ui.PartInitException;
71 import org.eclipse.ui.ide.IGotoMarker;
72 import org.eclipse.ui.part.FileEditorInput;
73 import org.eclipse.ui.views.properties.IPropertySheetPage;
74
75 /**
76 * Editor for TMF events
77 *
78 * @version 1.0
79 * @author Patrick Tasse
80 * @since 2.0
81 */
82 public class TmfEventsEditor extends TmfEditor implements ITmfTraceEditor, IReusableEditor, IPropertyListener, IResourceChangeListener, ISelectionProvider, ISelectionChangedListener, IPartListener, IGotoMarker {
83
84 /** ID for this class */
85 public static final String ID = "org.eclipse.linuxtools.tmf.ui.editors.events"; //$NON-NLS-1$
86
87 private TmfEventsTable fEventsTable;
88 private IFile fFile;
89 private ITmfTrace fTrace;
90 private Composite fParent;
91 private ListenerList fSelectionChangedListeners = new ListenerList();
92 private boolean fTraceSelected;
93 private IMarker fPendingGotoMarker;
94
95 @Override
96 public void doSave(final IProgressMonitor monitor) {
97 }
98
99 @Override
100 public void doSaveAs() {
101 }
102
103 @Override
104 public void init(final IEditorSite site, IEditorInput input) throws PartInitException {
105 IFileEditorInput fileEditorInput;
106 if (input instanceof TmfEditorInput) {
107 fFile = ((TmfEditorInput) input).getFile();
108 fTrace = ((TmfEditorInput) input).getTrace();
109 /* change the input to a FileEditorInput to allow open handlers to find this editor */
110 fileEditorInput = new FileEditorInput(fFile);
111 } else if (input instanceof IFileEditorInput) {
112 fileEditorInput = (IFileEditorInput) input;
113 fFile = fileEditorInput.getFile();
114 if (fFile == null) {
115 throw new PartInitException("Invalid IFileEditorInput: " + fileEditorInput); //$NON-NLS-1$
116 }
117 try {
118 final String traceTypeId = fFile.getPersistentProperty(TmfCommonConstants.TRACETYPE);
119 if (traceTypeId == null) {
120 throw new PartInitException(Messages.TmfOpenTraceHelper_NoTraceType);
121 }
122 if (traceTypeId.equals(TmfExperiment.class.getCanonicalName())) {
123 // Special case: experiment bookmark resource
124 final TmfProjectElement project = TmfProjectRegistry.getProject(fFile.getProject(), true);
125 if (project == null) {
126 throw new PartInitException(Messages.TmfOpenTraceHelper_NoTraceType);
127 }
128 for (final TmfExperimentElement experimentElement : project.getExperimentsFolder().getExperiments()) {
129 if (experimentElement.getResource().equals(fFile.getParent())) {
130 setPartName(experimentElement.getName());
131 super.setSite(site);
132 super.setInput(fileEditorInput);
133 TmfOpenTraceHelper.reopenTraceFromElement(experimentElement, this);
134 return;
135 }
136 }
137 } else if (traceTypeId.equals(TmfTrace.class.getCanonicalName())) {
138 // Special case: trace bookmark resource
139 final TmfProjectElement project = TmfProjectRegistry.getProject(fFile.getProject(), true);
140 for (final TmfTraceElement traceElement : project.getTracesFolder().getTraces()) {
141 if (traceElement.getResource().equals(fFile.getParent())) {
142 setPartName(traceElement.getName());
143 super.setSite(site);
144 super.setInput(fileEditorInput);
145 TmfOpenTraceHelper.reopenTraceFromElement(traceElement, this);
146 return;
147 }
148 }
149 } else {
150 final TmfProjectElement project = TmfProjectRegistry.getProject(fFile.getProject(), true);
151 for (final TmfTraceElement traceElement : project.getTracesFolder().getTraces()) {
152 if (traceElement.getResource().equals(fFile)) {
153 setPartName(traceElement.getName());
154 super.setSite(site);
155 super.setInput(fileEditorInput);
156 TmfOpenTraceHelper.reopenTraceFromElement(traceElement, this);
157 return;
158 }
159 }
160 }
161 } catch (final PartInitException e) {
162 throw e;
163 } catch (final InvalidRegistryObjectException e) {
164 Activator.getDefault().logError("Error initializing TmfEventsEditor", e); //$NON-NLS-1$
165 } catch (final CoreException e) {
166 Activator.getDefault().logError("Error initializing TmfEventsEditor", e); //$NON-NLS-1$
167 }
168 } else {
169 throw new PartInitException("Invalid IEditorInput: " + input.getClass()); //$NON-NLS-1$
170 }
171 if (fTrace == null) {
172 throw new PartInitException("Invalid IEditorInput: " + fFile.getName()); //$NON-NLS-1$
173 }
174 super.setSite(site);
175 super.setInput(fileEditorInput);
176 }
177
178 @Override
179 public boolean isDirty() {
180 return false;
181 }
182
183 @Override
184 public boolean isSaveAsAllowed() {
185 return false;
186 }
187
188 @Override
189 public void setInput(final IEditorInput input) {
190 super.setInput(input);
191 firePropertyChange(IEditorPart.PROP_INPUT);
192 }
193
194 @Override
195 public void propertyChanged(final Object source, final int propId) {
196 if (propId == IEditorPart.PROP_INPUT && getEditorInput() instanceof TmfEditorInput) {
197 if (fTrace != null) {
198 broadcast(new TmfTraceClosedSignal(this, fTrace));
199 }
200 fTraceSelected = false;
201 fFile = ((TmfEditorInput) getEditorInput()).getFile();
202 fTrace = ((TmfEditorInput) getEditorInput()).getTrace();
203 /* change the input to a FileEditorInput to allow open handlers to find this editor */
204 super.setInput(new FileEditorInput(fFile));
205 fEventsTable.dispose();
206 if (fTrace != null) {
207 setPartName(fTrace.getName());
208 fEventsTable = createEventsTable(fParent, fTrace.getCacheSize());
209 fEventsTable.addSelectionChangedListener(this);
210 fEventsTable.setTrace(fTrace, true);
211 fEventsTable.refreshBookmarks(fFile);
212 if (fPendingGotoMarker != null) {
213 fEventsTable.gotoMarker(fPendingGotoMarker);
214 fPendingGotoMarker = null;
215 }
216
217 /* ensure start time is set */
218 final ITmfContext context = fTrace.seekEvent(0);
219 fTrace.getNext(context);
220 context.dispose();
221
222 broadcast(new TmfTraceOpenedSignal(this, fTrace, fFile));
223 } else {
224 setPartName(getEditorInput().getName());
225 fEventsTable = new TmfEventsTable(fParent, 0);
226 fEventsTable.addSelectionChangedListener(this);
227 }
228 fParent.layout();
229 }
230 }
231
232 @Override
233 public void createPartControl(final Composite parent) {
234 fParent = parent;
235 if (fTrace != null) {
236 setPartName(fTrace.getName());
237 fEventsTable = createEventsTable(parent, fTrace.getCacheSize());
238 fEventsTable.addSelectionChangedListener(this);
239 fEventsTable.setTrace(fTrace, true);
240 fEventsTable.refreshBookmarks(fFile);
241
242 /* ensure start time is set */
243 final ITmfContext context = fTrace.seekEvent(0);
244 fTrace.getNext(context);
245 context.dispose();
246
247 broadcast(new TmfTraceOpenedSignal(this, fTrace, fFile));
248 } else {
249 fEventsTable = new TmfEventsTable(parent, 0);
250 fEventsTable.addSelectionChangedListener(this);
251 }
252 IStatusLineManager statusLineManager = getEditorSite().getActionBars().getStatusLineManager();
253 fEventsTable.setStatusLineManager(statusLineManager);
254 addPropertyListener(this);
255 ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
256 // we need to wrap the ISelectionProvider interface in the editor because
257 // the events table can be replaced later while the selection changed listener
258 // is only added once by the platform to the selection provider set here
259 getSite().setSelectionProvider(this);
260 getSite().getPage().addPartListener(this);
261 }
262
263 @Override
264 public void dispose() {
265 if (getSite() != null) {
266 getSite().getPage().removePartListener(this);
267 }
268 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
269 removePropertyListener(this);
270 if (fTrace != null) {
271 broadcast(new TmfTraceClosedSignal(this, fTrace));
272 }
273 if (fEventsTable != null) {
274 fEventsTable.dispose();
275 }
276 super.dispose();
277 }
278
279 /**
280 * Create the event table
281 *
282 * @param parent
283 * The parent composite
284 * @param cacheSize
285 * The cache size
286 * @return The event table instance
287 */
288 protected @NonNull TmfEventsTable createEventsTable(final Composite parent, final int cacheSize) {
289 return getEventTable(fTrace, parent, cacheSize);
290 }
291
292 /**
293 * Get the event table for the given trace. It will be of the type defined
294 * by the extension point if applicable, else it will be a default table
295 * with the extension-point-defined columns (if any).
296 *
297 * @param trace
298 * The event table is for this trace
299 * @param parent
300 * The parent composite of the table
301 * @param cacheSize
302 * The cache size to use
303 * @return The event table for the trace
304 */
305 private static @NonNull TmfEventsTable getEventTable(ITmfTrace trace,
306 final Composite parent, final int cacheSize) {
307 if (trace instanceof TmfExperiment) {
308 return getExperimentEventTable((TmfExperiment) trace, parent, cacheSize);
309 }
310
311 TmfEventsTable table = TmfTraceTypeUIUtils.getEventTable(trace, parent, cacheSize);
312 if (table != null) {
313 /*
314 * The trace type specified an event table type, we will give it to
315 * them.
316 */
317 return table;
318 }
319
320 /*
321 * The trace type did not specify an event table, we will use a standard
322 * table with the trace's advertised aspects (if any).
323 */
324 Iterable<ITmfEventAspect> aspects = trace.getEventAspects();
325 if (aspects != null) {
326 return new TmfEventsTable(parent, cacheSize, aspects);
327 }
328
329 /*
330 * No columns were defined either, use a default table with the default
331 * columns.
332 */
333 return new TmfEventsTable(parent, cacheSize);
334
335 }
336
337 /**
338 * Get the events table for an experiment. If all traces in the experiment
339 * are of the same type, use the same behavior as if it was one trace of
340 * that type.
341 *
342 * @param experiment
343 * the experiment
344 * @param parent
345 * the parent Composite
346 * @param cacheSize
347 * the event table cache size
348 * @return An event table of the appropriate type
349 */
350 private static @NonNull TmfEventsTable getExperimentEventTable(
351 final TmfExperiment experiment, final Composite parent,
352 final int cacheSize) {
353
354 String commonTraceType = getCommonTraceType(experiment);
355 if (commonTraceType != null) {
356 /*
357 * All the traces in this experiment are of the same type, let's
358 * just use the normal table for that type.
359 */
360 return getEventTable(experiment.getTraces()[0], parent, cacheSize);
361 }
362
363 /*
364 * There are different trace types in the experiment, so we are
365 * definitely using a TmfEventsTable. Aggregate the columns from all
366 * trace types.
367 */
368 ITmfTrace[] traces = experiment.getTraces();
369 Set<ITmfEventAspect> aspects = new LinkedHashSet<>();
370
371 for (ITmfTrace trace : traces) {
372 Iterable<ITmfEventAspect> traceAspects = trace.getEventAspects();
373 if (traceAspects == null) {
374 aspects.addAll(TmfTrace.BASE_ASPECTS);
375 } else {
376 for (ITmfEventAspect aspect : traceAspects) {
377 aspects.add(aspect);
378 }
379 }
380 }
381
382 return new TmfEventsTable(parent, cacheSize, aspects);
383 }
384
385 /**
386 * Check if an experiment contains traces of all the same type. If so,
387 * returns this type as a String. If not, returns null.
388 *
389 * @param experiment
390 * The experiment
391 * @return The common trace type if there is one, or 'null' if there are
392 * different types.
393 */
394 private static @Nullable String getCommonTraceType(TmfExperiment experiment) {
395 String commonTraceType = null;
396 try {
397 for (final ITmfTrace trace : experiment.getTraces()) {
398 final IResource resource = trace.getResource();
399 if (resource == null) {
400 return null;
401 }
402
403 final String traceType = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
404 if ((commonTraceType != null) && !commonTraceType.equals(traceType)) {
405 return null;
406 }
407 commonTraceType = traceType;
408 }
409 } catch (CoreException e) {
410 /*
411 * One of the traces didn't advertise its type, we can't infer
412 * anything.
413 */
414 return null;
415 }
416 return commonTraceType;
417 }
418
419 @Override
420 public ITmfTrace getTrace() {
421 return fTrace;
422 }
423
424 @Override
425 public void setFocus() {
426 fEventsTable.setFocus();
427 }
428
429 @Override
430 public Object getAdapter(final Class adapter) {
431 if (IGotoMarker.class.equals(adapter)) {
432 if (fTrace == null || fEventsTable == null) {
433 return this;
434 }
435 return fEventsTable;
436 } else if (IPropertySheetPage.class.equals(adapter)) {
437 return new UnsortedPropertySheetPage();
438 }
439 return super.getAdapter(adapter);
440 }
441
442 /**
443 * @since 2.1
444 */
445 @Override
446 public void gotoMarker(IMarker marker) {
447 if (fTrace == null || fEventsTable == null) {
448 fPendingGotoMarker = marker;
449 } else {
450 fEventsTable.gotoMarker(marker);
451 }
452 }
453
454 @Override
455 public void resourceChanged(final IResourceChangeEvent event) {
456 for (final IMarkerDelta delta : event.findMarkerDeltas(IMarker.BOOKMARK, false)) {
457 if (delta.getResource().equals(fFile)) {
458 if (delta.getKind() == IResourceDelta.REMOVED) {
459 final IMarker bookmark = delta.getMarker();
460 Display.getDefault().asyncExec(new Runnable() {
461 @Override
462 public void run() {
463 fEventsTable.removeBookmark(bookmark);
464 }
465 });
466 } else if (delta.getKind() == IResourceDelta.CHANGED) {
467 Display.getDefault().asyncExec(new Runnable() {
468 @Override
469 public void run() {
470 fEventsTable.getTable().refresh();
471 }
472 });
473 }
474 }
475 }
476 }
477
478 // ------------------------------------------------------------------------
479 // ISelectionProvider
480 // ------------------------------------------------------------------------
481
482 /**
483 * @since 2.0
484 */
485 @Override
486 public void addSelectionChangedListener(ISelectionChangedListener listener) {
487 fSelectionChangedListeners.add(listener);
488 }
489
490 /**
491 * @since 2.0
492 */
493 @Override
494 public ISelection getSelection() {
495 if (fEventsTable == null) {
496 return StructuredSelection.EMPTY;
497 }
498 return fEventsTable.getSelection();
499 }
500
501 /**
502 * @since 2.0
503 */
504 @Override
505 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
506 fSelectionChangedListeners.remove(listener);
507 }
508
509 /**
510 * @since 2.0
511 */
512 @Override
513 public void setSelection(ISelection selection) {
514 // not implemented
515 }
516
517 /**
518 * Notifies any selection changed listeners that the viewer's selection has changed.
519 * Only listeners registered at the time this method is called are notified.
520 *
521 * @param event a selection changed event
522 *
523 * @see ISelectionChangedListener#selectionChanged
524 * @since 2.0
525 */
526 protected void fireSelectionChanged(final SelectionChangedEvent event) {
527 Object[] listeners = fSelectionChangedListeners.getListeners();
528 for (int i = 0; i < listeners.length; ++i) {
529 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
530 SafeRunnable.run(new SafeRunnable() {
531 @Override
532 public void run() {
533 l.selectionChanged(event);
534 }
535 });
536 }
537 }
538
539 // ------------------------------------------------------------------------
540 // ISelectionChangedListener
541 // ------------------------------------------------------------------------
542
543 /**
544 * @since 2.0
545 */
546 @Override
547 public void selectionChanged(SelectionChangedEvent event) {
548 fireSelectionChanged(event);
549 }
550
551 // ------------------------------------------------------------------------
552 // IPartListener
553 // ------------------------------------------------------------------------
554
555 /**
556 * @since 2.0
557 */
558 @Override
559 public void partActivated(IWorkbenchPart part) {
560 if (part == this && fTrace != null) {
561 if (fTraceSelected) {
562 return;
563 }
564 fTraceSelected = true;
565 broadcast(new TmfTraceSelectedSignal(this, fTrace));
566 }
567 }
568
569 /**
570 * @since 2.0
571 */
572 @Override
573 public void partBroughtToTop(IWorkbenchPart part) {
574 if (part == this && fTrace != null) {
575 if (fTraceSelected) {
576 return;
577 }
578 fTraceSelected = true;
579 broadcast(new TmfTraceSelectedSignal(this, fTrace));
580 }
581 }
582
583 /**
584 * @since 2.0
585 */
586 @Override
587 public void partClosed(IWorkbenchPart part) {
588 }
589
590 /**
591 * @since 2.0
592 */
593 @Override
594 public void partDeactivated(IWorkbenchPart part) {
595 }
596
597 /**
598 * @since 2.0
599 */
600 @Override
601 public void partOpened(IWorkbenchPart part) {
602 }
603
604 // ------------------------------------------------------------------------
605 // Global commands
606 // ------------------------------------------------------------------------
607
608 /**
609 * Add a bookmark
610 */
611 public void addBookmark() {
612 fEventsTable.addBookmark(fFile);
613 }
614
615
616 // ------------------------------------------------------------------------
617 // Signal handlers
618 // ------------------------------------------------------------------------
619
620 /**
621 * Handler for the Trace Selected signal
622 *
623 * @param signal The incoming signal
624 */
625 @TmfSignalHandler
626 public void traceSelected(final TmfTraceSelectedSignal signal) {
627 if ((signal.getSource() != this)) {
628 if (signal.getTrace().equals(fTrace)) {
629 getSite().getPage().bringToTop(this);
630 } else {
631 fTraceSelected = false;
632 }
633 }
634 }
635
636 /**
637 * Update the display to use the updated timestamp format
638 *
639 * @param signal the incoming signal
640 * @since 2.0
641 */
642 @TmfSignalHandler
643 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) {
644 if (fEventsTable != null) {
645 fEventsTable.refresh();
646 }
647 }
648
649 }
This page took 0.050645 seconds and 5 git commands to generate.