1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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
.tmf
.core
.TmfCommonConstants
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTimestampFormatUpdateSignal
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
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
83 * @author Patrick Tasse
86 public class TmfEventsEditor
extends TmfEditor
implements ITmfTraceEditor
, IReusableEditor
, IPropertyListener
, IResourceChangeListener
, ISelectionProvider
, ISelectionChangedListener
, IPartListener
, IGotoMarker
{
88 /** ID for this class */
89 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.editors.events"; //$NON-NLS-1$
91 private TmfEventsTable fEventsTable
;
93 private ITmfTrace fTrace
;
94 private Composite fParent
;
95 private ListenerList fSelectionChangedListeners
= new ListenerList();
96 private boolean fTraceSelected
;
97 private IMarker fPendingGotoMarker
;
100 public void doSave(final IProgressMonitor monitor
) {
104 public void doSaveAs() {
108 public void init(final IEditorSite site
, IEditorInput input
) throws PartInitException
{
109 IFileEditorInput fileEditorInput
;
110 if (input
instanceof TmfEditorInput
) {
111 fFile
= ((TmfEditorInput
) input
).getFile();
112 fTrace
= ((TmfEditorInput
) input
).getTrace();
113 /* change the input to a FileEditorInput to allow open handlers to find this editor */
114 fileEditorInput
= new FileEditorInput(fFile
);
115 } else if (input
instanceof IFileEditorInput
) {
116 fileEditorInput
= (IFileEditorInput
) input
;
117 fFile
= fileEditorInput
.getFile();
119 throw new PartInitException("Invalid IFileEditorInput: " + fileEditorInput
); //$NON-NLS-1$
122 final String traceTypeId
= fFile
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
123 if (traceTypeId
== null) {
124 throw new PartInitException(Messages
.TmfOpenTraceHelper_NoTraceType
);
126 if (traceTypeId
.equals(TmfExperiment
.class.getCanonicalName())) {
127 // Special case: experiment bookmark resource
128 final TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject(), true);
129 if (project
== null) {
130 throw new PartInitException(Messages
.TmfOpenTraceHelper_NoTraceType
);
132 for (final TmfExperimentElement experimentElement
: project
.getExperimentsFolder().getExperiments()) {
133 if (experimentElement
.getResource().equals(fFile
.getParent())) {
134 setPartName(experimentElement
.getName());
136 super.setInput(fileEditorInput
);
137 TmfOpenTraceHelper
.reopenTraceFromElement(experimentElement
, this);
141 } else if (traceTypeId
.equals(TmfTrace
.class.getCanonicalName())) {
142 // Special case: trace bookmark resource
143 final TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject(), true);
144 for (final TmfTraceElement traceElement
: project
.getTracesFolder().getTraces()) {
145 if (traceElement
.getResource().equals(fFile
.getParent())) {
146 setPartName(traceElement
.getName());
148 super.setInput(fileEditorInput
);
149 TmfOpenTraceHelper
.reopenTraceFromElement(traceElement
, this);
154 final TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject(), true);
155 for (final TmfTraceElement traceElement
: project
.getTracesFolder().getTraces()) {
156 if (traceElement
.getResource().equals(fFile
)) {
157 setPartName(traceElement
.getName());
159 super.setInput(fileEditorInput
);
160 TmfOpenTraceHelper
.reopenTraceFromElement(traceElement
, this);
165 } catch (final PartInitException e
) {
167 } catch (final InvalidRegistryObjectException e
) {
168 Activator
.getDefault().logError("Error initializing TmfEventsEditor", e
); //$NON-NLS-1$
169 } catch (final CoreException e
) {
170 Activator
.getDefault().logError("Error initializing TmfEventsEditor", e
); //$NON-NLS-1$
173 throw new PartInitException("Invalid IEditorInput: " + input
.getClass()); //$NON-NLS-1$
175 if (fTrace
== null) {
176 throw new PartInitException("Invalid IEditorInput: " + fFile
.getName()); //$NON-NLS-1$
179 super.setInput(fileEditorInput
);
183 public boolean isDirty() {
188 public boolean isSaveAsAllowed() {
193 public void setInput(final IEditorInput input
) {
194 super.setInput(input
);
195 firePropertyChange(IEditorPart
.PROP_INPUT
);
199 public void propertyChanged(final Object source
, final int propId
) {
200 if (propId
== IEditorPart
.PROP_INPUT
&& getEditorInput() instanceof TmfEditorInput
) {
201 if (fTrace
!= null) {
202 broadcast(new TmfTraceClosedSignal(this, fTrace
));
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 fEventsTable
.dispose();
210 if (fTrace
!= null) {
211 setPartName(fTrace
.getName());
212 fEventsTable
= createEventsTable(fParent
, fTrace
.getCacheSize());
213 fEventsTable
.addSelectionChangedListener(this);
214 fEventsTable
.setTrace(fTrace
, true);
215 fEventsTable
.refreshBookmarks(fFile
);
216 if (fPendingGotoMarker
!= null) {
217 fEventsTable
.gotoMarker(fPendingGotoMarker
);
218 fPendingGotoMarker
= null;
221 /* ensure start time is set */
222 final ITmfContext context
= fTrace
.seekEvent(0);
223 fTrace
.getNext(context
);
226 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fFile
));
228 setPartName(getEditorInput().getName());
229 fEventsTable
= new TmfEventsTable(fParent
, 0);
230 fEventsTable
.addSelectionChangedListener(this);
237 public void createPartControl(final Composite parent
) {
239 if (fTrace
!= null) {
240 setPartName(fTrace
.getName());
241 fEventsTable
= createEventsTable(parent
, fTrace
.getCacheSize());
242 fEventsTable
.addSelectionChangedListener(this);
243 fEventsTable
.setTrace(fTrace
, true);
244 fEventsTable
.refreshBookmarks(fFile
);
246 /* ensure start time is set */
247 final ITmfContext context
= fTrace
.seekEvent(0);
248 fTrace
.getNext(context
);
251 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fFile
));
253 fEventsTable
= new TmfEventsTable(parent
, 0);
254 fEventsTable
.addSelectionChangedListener(this);
256 IStatusLineManager statusLineManager
= getEditorSite().getActionBars().getStatusLineManager();
257 fEventsTable
.setStatusLineManager(statusLineManager
);
258 addPropertyListener(this);
259 ResourcesPlugin
.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent
.POST_CHANGE
);
260 // we need to wrap the ISelectionProvider interface in the editor because
261 // the events table can be replaced later while the selection changed listener
262 // is only added once by the platform to the selection provider set here
263 getSite().setSelectionProvider(this);
264 getSite().getPage().addPartListener(this);
268 public void dispose() {
269 if (getSite() != null) {
270 getSite().getPage().removePartListener(this);
272 ResourcesPlugin
.getWorkspace().removeResourceChangeListener(this);
273 removePropertyListener(this);
274 if (fTrace
!= null) {
275 broadcast(new TmfTraceClosedSignal(this, fTrace
));
277 if (fEventsTable
!= null) {
278 fEventsTable
.dispose();
284 * Create the event table
287 * The parent composite
290 * @return The event table instance
292 protected @NonNull TmfEventsTable
createEventsTable(final Composite parent
, final int cacheSize
) {
293 ITmfTrace trace
= fTrace
;
296 * Check if the trace (or experiment type) defines a specific event
297 * table in its extension point.
299 TmfEventsTable table
= TmfTraceTypeUIUtils
.getEventTable(trace
, parent
, cacheSize
);
305 * Use the aspects defined by the trace type (or each trace type in an
306 * experiment) to build a table consisting of these.
308 Iterable
<ITmfEventAspect
> aspects
= getTraceAspects(trace
);
309 if (Iterables
.isEmpty(aspects
)) {
310 /* Couldn't find any event aspects, use a default table */
311 return new TmfEventsTable(parent
, cacheSize
);
313 return new TmfEventsTable(parent
, cacheSize
, aspects
);
317 * Get the event table for the given trace. It will be of the type defined
318 * by the extension point if applicable, else it will be a default table
319 * with the extension-point-defined columns (if any).
322 * The event table is for this trace
324 * The parent composite of the table
326 * The cache size to use
327 * @return The event table for the trace
329 private static @NonNull Iterable
<ITmfEventAspect
> getTraceAspects(ITmfTrace trace
) {
330 if (trace
instanceof TmfExperiment
) {
331 return getExperimentAspects((TmfExperiment
) trace
);
333 return trace
.getEventAspects();
337 * Get the events table for an experiment. If all traces in the experiment
338 * are of the same type, use the same behavior as if it was one trace of
344 * the parent Composite
346 * the event table cache size
347 * @return An event table of the appropriate type
349 private static @NonNull Iterable
<ITmfEventAspect
> getExperimentAspects(
350 final TmfExperiment experiment
) {
351 List
<ITmfTrace
> traces
= experiment
.getTraces();
352 ImmutableSet
.Builder
<ITmfEventAspect
> builder
= new ImmutableSet
.Builder
<>();
354 /* For experiments, we'll add a "trace name" aspect/column */
355 builder
.add(ITmfEventAspect
.BaseAspects
.TRACE_NAME
);
357 String commonTraceType
= getCommonTraceType(experiment
);
358 if (commonTraceType
!= null) {
360 * All the traces in this experiment are of the same type, let's
361 * just use the normal table for that type.
363 builder
.addAll(traces
.get(0).getEventAspects());
367 * There are different trace types in the experiment, so we are
368 * definitely using a TmfEventsTable. Aggregate the columns from all
371 for (ITmfTrace trace
: traces
) {
372 Iterable
<ITmfEventAspect
> traceAspects
= trace
.getEventAspects();
373 builder
.addAll(traceAspects
);
376 return checkNotNull(builder
.build());
380 * Check if an experiment contains traces of all the same type. If so,
381 * returns this type as a String. If not, returns null.
385 * @return The common trace type if there is one, or 'null' if there are
388 private static @Nullable String
getCommonTraceType(TmfExperiment experiment
) {
389 String commonTraceType
= null;
391 for (final ITmfTrace trace
: experiment
.getTraces()) {
392 final IResource resource
= trace
.getResource();
393 if (resource
== null) {
397 final String traceType
= resource
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
398 if ((commonTraceType
!= null) && !commonTraceType
.equals(traceType
)) {
401 commonTraceType
= traceType
;
403 } catch (CoreException e
) {
405 * One of the traces didn't advertise its type, we can't infer
410 return commonTraceType
;
414 public ITmfTrace
getTrace() {
419 public void setFocus() {
420 fEventsTable
.setFocus();
424 public Object
getAdapter(final Class adapter
) {
425 if (IGotoMarker
.class.equals(adapter
)) {
426 if (fTrace
== null || fEventsTable
== null) {
430 } else if (IPropertySheetPage
.class.equals(adapter
)) {
431 return new UnsortedPropertySheetPage();
433 return super.getAdapter(adapter
);
440 public void gotoMarker(IMarker marker
) {
441 if (fTrace
== null || fEventsTable
== null) {
442 fPendingGotoMarker
= marker
;
444 fEventsTable
.gotoMarker(marker
);
449 public void resourceChanged(final IResourceChangeEvent event
) {
450 for (final IMarkerDelta delta
: event
.findMarkerDeltas(IMarker
.BOOKMARK
, false)) {
451 if (delta
.getResource().equals(fFile
)) {
452 if (delta
.getKind() == IResourceDelta
.REMOVED
) {
453 final IMarker bookmark
= delta
.getMarker();
454 Display
.getDefault().asyncExec(new Runnable() {
457 fEventsTable
.removeBookmark(bookmark
);
460 } else if (delta
.getKind() == IResourceDelta
.CHANGED
) {
461 Display
.getDefault().asyncExec(new Runnable() {
464 fEventsTable
.getTable().refresh();
472 // ------------------------------------------------------------------------
473 // ISelectionProvider
474 // ------------------------------------------------------------------------
480 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
481 fSelectionChangedListeners
.add(listener
);
488 public ISelection
getSelection() {
489 if (fEventsTable
== null) {
490 return StructuredSelection
.EMPTY
;
492 return fEventsTable
.getSelection();
499 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
500 fSelectionChangedListeners
.remove(listener
);
507 public void setSelection(ISelection selection
) {
512 * Notifies any selection changed listeners that the viewer's selection has changed.
513 * Only listeners registered at the time this method is called are notified.
515 * @param event a selection changed event
517 * @see ISelectionChangedListener#selectionChanged
520 protected void fireSelectionChanged(final SelectionChangedEvent event
) {
521 Object
[] listeners
= fSelectionChangedListeners
.getListeners();
522 for (int i
= 0; i
< listeners
.length
; ++i
) {
523 final ISelectionChangedListener l
= (ISelectionChangedListener
) listeners
[i
];
524 SafeRunnable
.run(new SafeRunnable() {
527 l
.selectionChanged(event
);
533 // ------------------------------------------------------------------------
534 // ISelectionChangedListener
535 // ------------------------------------------------------------------------
541 public void selectionChanged(SelectionChangedEvent event
) {
542 fireSelectionChanged(event
);
545 // ------------------------------------------------------------------------
547 // ------------------------------------------------------------------------
553 public void partActivated(IWorkbenchPart part
) {
554 if (part
== this && fTrace
!= null) {
555 if (fTraceSelected
) {
558 fTraceSelected
= true;
559 broadcast(new TmfTraceSelectedSignal(this, fTrace
));
567 public void partBroughtToTop(IWorkbenchPart part
) {
568 if (part
== this && fTrace
!= null) {
569 if (fTraceSelected
) {
572 fTraceSelected
= true;
573 broadcast(new TmfTraceSelectedSignal(this, fTrace
));
581 public void partClosed(IWorkbenchPart part
) {
588 public void partDeactivated(IWorkbenchPart part
) {
595 public void partOpened(IWorkbenchPart part
) {
598 // ------------------------------------------------------------------------
600 // ------------------------------------------------------------------------
605 public void addBookmark() {
606 fEventsTable
.addBookmark(fFile
);
610 // ------------------------------------------------------------------------
612 // ------------------------------------------------------------------------
615 * Handler for the Trace Selected signal
617 * @param signal The incoming signal
620 public void traceSelected(final TmfTraceSelectedSignal signal
) {
621 if ((signal
.getSource() != this)) {
622 if (signal
.getTrace().equals(fTrace
)) {
623 getSite().getPage().bringToTop(this);
625 fTraceSelected
= false;
631 * Update the display to use the updated timestamp format
633 * @param signal the incoming signal
637 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
638 if (fEventsTable
!= null) {
639 fEventsTable
.refresh();