1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson, École Polytechnique de Montréal
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
10 * Patrick Tasse - Initial API and implementation
11 * Geneviève Bastien - Experiment instantiated with experiment type
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.editors
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.resources
.IFile
;
21 import org
.eclipse
.core
.resources
.IMarker
;
22 import org
.eclipse
.core
.resources
.IMarkerDelta
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.resources
.IResourceChangeEvent
;
25 import org
.eclipse
.core
.resources
.IResourceChangeListener
;
26 import org
.eclipse
.core
.resources
.IResourceDelta
;
27 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
28 import org
.eclipse
.core
.runtime
.CoreException
;
29 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
30 import org
.eclipse
.core
.runtime
.InvalidRegistryObjectException
;
31 import org
.eclipse
.core
.runtime
.ListenerList
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.jdt
.annotation
.Nullable
;
34 import org
.eclipse
.jface
.action
.IStatusLineManager
;
35 import org
.eclipse
.jface
.util
.SafeRunnable
;
36 import org
.eclipse
.jface
.viewers
.ISelection
;
37 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
38 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
39 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
40 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.Display
;
43 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
44 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.editors
.ITmfEventsEditorConstants
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTimestampFormatUpdateSignal
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.Messages
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceTypeUIUtils
;
62 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
63 import org
.eclipse
.ui
.IEditorInput
;
64 import org
.eclipse
.ui
.IEditorPart
;
65 import org
.eclipse
.ui
.IEditorSite
;
66 import org
.eclipse
.ui
.IFileEditorInput
;
67 import org
.eclipse
.ui
.IPartListener
;
68 import org
.eclipse
.ui
.IPropertyListener
;
69 import org
.eclipse
.ui
.IReusableEditor
;
70 import org
.eclipse
.ui
.IWorkbenchPart
;
71 import org
.eclipse
.ui
.PartInitException
;
72 import org
.eclipse
.ui
.ide
.IGotoMarker
;
73 import org
.eclipse
.ui
.part
.FileEditorInput
;
74 import org
.eclipse
.ui
.views
.properties
.IPropertySheetPage
;
76 import com
.google
.common
.collect
.ImmutableSet
;
77 import com
.google
.common
.collect
.Iterables
;
80 * Editor for TMF events
82 * @author Patrick Tasse
84 public class TmfEventsEditor
extends TmfEditor
implements ITmfTraceEditor
, IReusableEditor
, IPropertyListener
, IResourceChangeListener
, ISelectionProvider
, ISelectionChangedListener
, IPartListener
, IGotoMarker
{
86 /** ID for this class */
87 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.editors.events"; //$NON-NLS-1$
89 private TmfEventsTable fEventsTable
;
91 private ITmfTrace fTrace
;
92 private Composite fParent
;
93 private ListenerList fSelectionChangedListeners
= new ListenerList();
94 private boolean fTraceSelected
;
95 private IMarker fPendingGotoMarker
;
98 public void doSave(final IProgressMonitor monitor
) {
102 public void doSaveAs() {
106 public void init(final IEditorSite site
, IEditorInput input
) throws PartInitException
{
107 IFileEditorInput fileEditorInput
;
108 if (input
instanceof TmfEditorInput
) {
109 fFile
= ((TmfEditorInput
) input
).getFile();
110 fTrace
= ((TmfEditorInput
) input
).getTrace();
111 /* change the input to a FileEditorInput to allow open handlers to find this editor */
112 fileEditorInput
= new FileEditorInput(fFile
);
113 } else if (input
instanceof IFileEditorInput
) {
114 fileEditorInput
= (IFileEditorInput
) input
;
115 fFile
= fileEditorInput
.getFile();
117 throw new PartInitException("Invalid IFileEditorInput: " + fileEditorInput
); //$NON-NLS-1$
120 final String traceTypeId
= fFile
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
121 if (traceTypeId
== null) {
122 throw new PartInitException(Messages
.TmfOpenTraceHelper_NoTraceType
);
124 if (ITmfEventsEditorConstants
.EXPERIMENT_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
125 // Special case: experiment bookmark resource
126 final TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject(), true);
127 if (project
== null) {
128 throw new PartInitException(Messages
.TmfOpenTraceHelper_NoTraceType
);
130 for (final TmfExperimentElement experimentElement
: project
.getExperimentsFolder().getExperiments()) {
131 if (experimentElement
.getResource().equals(fFile
.getParent())) {
132 setPartName(experimentElement
.getName());
134 super.setInput(fileEditorInput
);
135 TmfOpenTraceHelper
.reopenTraceFromElement(experimentElement
, this);
139 } else if (ITmfEventsEditorConstants
.TRACE_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
140 // Special case: trace bookmark resource
141 final TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject(), true);
142 for (final TmfTraceElement traceElement
: project
.getTracesFolder().getTraces()) {
143 if (traceElement
.getResource().equals(fFile
.getParent())) {
144 setPartName(traceElement
.getElementPath());
146 super.setInput(fileEditorInput
);
147 TmfOpenTraceHelper
.reopenTraceFromElement(traceElement
, this);
152 final TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject(), true);
153 for (final TmfTraceElement traceElement
: project
.getTracesFolder().getTraces()) {
154 if (traceElement
.getResource().equals(fFile
)) {
155 setPartName(traceElement
.getElementPath());
157 super.setInput(fileEditorInput
);
158 TmfOpenTraceHelper
.reopenTraceFromElement(traceElement
, this);
163 } catch (final PartInitException e
) {
165 } catch (final InvalidRegistryObjectException e
) {
166 Activator
.getDefault().logError("Error initializing TmfEventsEditor", e
); //$NON-NLS-1$
167 } catch (final CoreException e
) {
168 Activator
.getDefault().logError("Error initializing TmfEventsEditor", e
); //$NON-NLS-1$
171 throw new PartInitException("Invalid IEditorInput: " + input
.getClass()); //$NON-NLS-1$
173 if (fTrace
== null) {
174 throw new PartInitException("Invalid IEditorInput: " + fFile
.getName()); //$NON-NLS-1$
177 super.setInput(fileEditorInput
);
181 public boolean isDirty() {
186 public boolean isSaveAsAllowed() {
191 public void setInput(final IEditorInput input
) {
192 super.setInput(input
);
193 firePropertyChange(IEditorPart
.PROP_INPUT
);
197 public void propertyChanged(final Object source
, final int propId
) {
198 if (propId
== IEditorPart
.PROP_INPUT
&& getEditorInput() instanceof TmfEditorInput
) {
199 if (fTrace
!= null) {
200 broadcast(new TmfTraceClosedSignal(this, fTrace
));
201 TmfTraceColumnManager
.saveColumnOrder(fTrace
.getTraceTypeId(), fEventsTable
.getColumnOrder());
203 fEventsTable
.dispose();
204 fTraceSelected
= false;
205 fFile
= ((TmfEditorInput
) getEditorInput()).getFile();
206 fTrace
= ((TmfEditorInput
) getEditorInput()).getTrace();
207 /* change the input to a FileEditorInput to allow open handlers to find this editor */
208 super.setInput(new FileEditorInput(fFile
));
209 createAndInitializeTable();
215 public void createPartControl(final Composite parent
) {
217 createAndInitializeTable();
218 addPropertyListener(this);
219 ResourcesPlugin
.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent
.POST_CHANGE
);
220 // we need to wrap the ISelectionProvider interface in the editor because
221 // the events table can be replaced later while the selection changed listener
222 // is only added once by the platform to the selection provider set here
223 getSite().setSelectionProvider(this);
224 getSite().getPage().addPartListener(this);
227 private void createAndInitializeTable() {
228 if (fTrace
!= null) {
229 setPartName(fTrace
.getName());
230 fEventsTable
= createEventsTable(fParent
, fTrace
.getCacheSize());
231 fEventsTable
.setColumnOrder(TmfTraceColumnManager
.loadColumnOrder(fTrace
.getTraceTypeId()));
232 fEventsTable
.addSelectionChangedListener(this);
233 fEventsTable
.setTrace(fTrace
, true);
234 fEventsTable
.refreshBookmarks(fFile
);
235 if (fPendingGotoMarker
!= null) {
236 fEventsTable
.gotoMarker(fPendingGotoMarker
);
237 fPendingGotoMarker
= null;
240 /* ensure start time is set */
241 final ITmfContext context
= fTrace
.seekEvent(0);
242 fTrace
.getNext(context
);
245 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fFile
));
247 fEventsTable
= new TmfEventsTable(fParent
, 0);
248 fEventsTable
.addSelectionChangedListener(this);
250 IStatusLineManager statusLineManager
= getEditorSite().getActionBars().getStatusLineManager();
251 fEventsTable
.setStatusLineManager(statusLineManager
);
255 public void dispose() {
256 if (getSite() != null) {
257 getSite().getPage().removePartListener(this);
259 ResourcesPlugin
.getWorkspace().removeResourceChangeListener(this);
260 removePropertyListener(this);
261 if (fTrace
!= null) {
262 broadcast(new TmfTraceClosedSignal(this, fTrace
));
263 if (fEventsTable
!= null) {
264 TmfTraceColumnManager
.saveColumnOrder(fTrace
.getTraceTypeId(), fEventsTable
.getColumnOrder());
267 if (fEventsTable
!= null) {
268 fEventsTable
.dispose();
274 * Create the event table
277 * The parent composite
280 * @return The event table instance
282 protected @NonNull TmfEventsTable
createEventsTable(final Composite parent
, final int cacheSize
) {
283 ITmfTrace trace
= fTrace
;
286 * Check if the trace (or experiment type) defines a specific event
287 * table in its extension point.
289 TmfEventsTable table
= TmfTraceTypeUIUtils
.getEventTable(trace
, parent
, cacheSize
);
295 * Use the aspects defined by the trace type (or each trace type in an
296 * experiment) to build a table consisting of these.
298 Iterable
<ITmfEventAspect
> aspects
= getTraceAspects(trace
);
299 if (Iterables
.isEmpty(aspects
)) {
300 /* Couldn't find any event aspects, use a default table */
301 return new TmfEventsTable(parent
, cacheSize
);
303 return new TmfEventsTable(parent
, cacheSize
, aspects
);
307 * Get the event table for the given trace. It will be of the type defined
308 * by the extension point if applicable, else it will be a default table
309 * with the extension-point-defined columns (if any).
312 * The event table is for this trace
314 * The parent composite of the table
316 * The cache size to use
317 * @return The event table for the trace
319 private static @NonNull Iterable
<ITmfEventAspect
> getTraceAspects(ITmfTrace trace
) {
320 if (trace
instanceof TmfExperiment
) {
321 return getExperimentAspects((TmfExperiment
) trace
);
323 return trace
.getEventAspects();
327 * Get the events table for an experiment. If all traces in the experiment
328 * are of the same type, use the same behavior as if it was one trace of
334 * the parent Composite
336 * the event table cache size
337 * @return An event table of the appropriate type
339 private static @NonNull Iterable
<ITmfEventAspect
> getExperimentAspects(
340 final TmfExperiment experiment
) {
341 List
<ITmfTrace
> traces
= experiment
.getTraces();
342 ImmutableSet
.Builder
<ITmfEventAspect
> builder
= new ImmutableSet
.Builder
<>();
344 /* For experiments, we'll add a "trace name" aspect/column */
345 builder
.add(ITmfEventAspect
.BaseAspects
.TRACE_NAME
);
347 String commonTraceType
= getCommonTraceType(experiment
);
348 if (commonTraceType
!= null) {
350 * All the traces in this experiment are of the same type, let's
351 * just use the normal table for that type.
353 builder
.addAll(traces
.get(0).getEventAspects());
357 * There are different trace types in the experiment, so we are
358 * definitely using a TmfEventsTable. Aggregate the columns from all
361 for (ITmfTrace trace
: traces
) {
362 Iterable
<ITmfEventAspect
> traceAspects
= trace
.getEventAspects();
363 builder
.addAll(traceAspects
);
366 return checkNotNull(builder
.build());
370 * Check if an experiment contains traces of all the same type. If so,
371 * returns this type as a String. If not, returns null.
375 * @return The common trace type if there is one, or 'null' if there are
378 private static @Nullable String
getCommonTraceType(TmfExperiment experiment
) {
379 String commonTraceType
= null;
381 for (final ITmfTrace trace
: experiment
.getTraces()) {
382 final IResource resource
= trace
.getResource();
383 if (resource
== null) {
387 final String traceType
= resource
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
388 if ((commonTraceType
!= null) && !commonTraceType
.equals(traceType
)) {
391 commonTraceType
= traceType
;
393 } catch (CoreException e
) {
395 * One of the traces didn't advertise its type, we can't infer
400 return commonTraceType
;
404 public ITmfTrace
getTrace() {
409 public void setFocus() {
410 fEventsTable
.setFocus();
414 public <T
> T
getAdapter(final Class
<T
> adapter
) {
415 if (IGotoMarker
.class.equals(adapter
)) {
416 if (fTrace
== null || fEventsTable
== null) {
417 return adapter
.cast(this);
419 return adapter
.cast(fEventsTable
);
420 } else if (IPropertySheetPage
.class.equals(adapter
)) {
421 return adapter
.cast(new UnsortedPropertySheetPage());
423 return super.getAdapter(adapter
);
427 public void gotoMarker(IMarker marker
) {
428 if (fTrace
== null || fEventsTable
== null) {
429 fPendingGotoMarker
= marker
;
431 fEventsTable
.gotoMarker(marker
);
436 public void resourceChanged(final IResourceChangeEvent event
) {
437 for (final IMarkerDelta delta
: event
.findMarkerDeltas(IMarker
.BOOKMARK
, false)) {
438 if (delta
.getResource().equals(fFile
)) {
439 if (delta
.getKind() == IResourceDelta
.REMOVED
) {
440 final IMarker bookmark
= delta
.getMarker();
441 Display
.getDefault().asyncExec(new Runnable() {
444 fEventsTable
.removeBookmark(bookmark
);
447 } else if (delta
.getKind() == IResourceDelta
.CHANGED
) {
448 Display
.getDefault().asyncExec(new Runnable() {
451 fEventsTable
.getTable().refresh();
459 // ------------------------------------------------------------------------
460 // ISelectionProvider
461 // ------------------------------------------------------------------------
464 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
465 fSelectionChangedListeners
.add(listener
);
469 public ISelection
getSelection() {
470 if (fEventsTable
== null) {
471 return StructuredSelection
.EMPTY
;
473 return fEventsTable
.getSelection();
477 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
478 fSelectionChangedListeners
.remove(listener
);
482 public void setSelection(ISelection selection
) {
487 * Notifies any selection changed listeners that the viewer's selection has changed.
488 * Only listeners registered at the time this method is called are notified.
490 * @param event a selection changed event
492 * @see ISelectionChangedListener#selectionChanged
494 protected void fireSelectionChanged(final SelectionChangedEvent event
) {
495 Object
[] listeners
= fSelectionChangedListeners
.getListeners();
496 for (int i
= 0; i
< listeners
.length
; ++i
) {
497 final ISelectionChangedListener l
= (ISelectionChangedListener
) listeners
[i
];
498 SafeRunnable
.run(new SafeRunnable() {
501 l
.selectionChanged(event
);
507 // ------------------------------------------------------------------------
508 // ISelectionChangedListener
509 // ------------------------------------------------------------------------
512 public void selectionChanged(SelectionChangedEvent event
) {
513 fireSelectionChanged(event
);
516 // ------------------------------------------------------------------------
518 // ------------------------------------------------------------------------
521 public void partActivated(IWorkbenchPart part
) {
522 if (part
== this && fTrace
!= null) {
523 if (fTraceSelected
) {
526 fTraceSelected
= true;
527 broadcast(new TmfTraceSelectedSignal(this, fTrace
));
532 public void partBroughtToTop(IWorkbenchPart part
) {
533 if (part
== this && fTrace
!= null) {
534 if (fTraceSelected
) {
537 fTraceSelected
= true;
538 broadcast(new TmfTraceSelectedSignal(this, fTrace
));
543 public void partClosed(IWorkbenchPart part
) {
547 public void partDeactivated(IWorkbenchPart part
) {
551 public void partOpened(IWorkbenchPart part
) {
554 // ------------------------------------------------------------------------
556 // ------------------------------------------------------------------------
561 public void addBookmark() {
562 fEventsTable
.addBookmark(fFile
);
566 // ------------------------------------------------------------------------
568 // ------------------------------------------------------------------------
571 * Handler for the Trace Selected signal
573 * @param signal The incoming signal
576 public void traceSelected(final TmfTraceSelectedSignal signal
) {
577 if ((signal
.getSource() != this)) {
578 if (signal
.getTrace().equals(fTrace
)) {
579 getSite().getPage().bringToTop(this);
581 fTraceSelected
= false;
587 * Update the display to use the updated timestamp format
589 * @param signal the incoming signal
592 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
593 if (fEventsTable
!= null) {
594 fEventsTable
.refresh();