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 java
.util
.LinkedHashSet
;
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
;
76 * Editor for TMF events
79 * @author Patrick Tasse
82 public class TmfEventsEditor
extends TmfEditor
implements ITmfTraceEditor
, IReusableEditor
, IPropertyListener
, IResourceChangeListener
, ISelectionProvider
, ISelectionChangedListener
, IPartListener
, IGotoMarker
{
84 /** ID for this class */
85 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.editors.events"; //$NON-NLS-1$
87 private TmfEventsTable fEventsTable
;
89 private ITmfTrace fTrace
;
90 private Composite fParent
;
91 private ListenerList fSelectionChangedListeners
= new ListenerList();
92 private boolean fTraceSelected
;
93 private IMarker fPendingGotoMarker
;
96 public void doSave(final IProgressMonitor monitor
) {
100 public void doSaveAs() {
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();
115 throw new PartInitException("Invalid IFileEditorInput: " + fileEditorInput
); //$NON-NLS-1$
118 final String traceTypeId
= fFile
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
119 if (traceTypeId
== null) {
120 throw new PartInitException(Messages
.TmfOpenTraceHelper_NoTraceType
);
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
);
128 for (final TmfExperimentElement experimentElement
: project
.getExperimentsFolder().getExperiments()) {
129 if (experimentElement
.getResource().equals(fFile
.getParent())) {
130 setPartName(experimentElement
.getName());
132 super.setInput(fileEditorInput
);
133 TmfOpenTraceHelper
.reopenTraceFromElement(experimentElement
, this);
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());
144 super.setInput(fileEditorInput
);
145 TmfOpenTraceHelper
.reopenTraceFromElement(traceElement
, this);
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());
155 super.setInput(fileEditorInput
);
156 TmfOpenTraceHelper
.reopenTraceFromElement(traceElement
, this);
161 } catch (final PartInitException 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$
169 throw new PartInitException("Invalid IEditorInput: " + input
.getClass()); //$NON-NLS-1$
171 if (fTrace
== null) {
172 throw new PartInitException("Invalid IEditorInput: " + fFile
.getName()); //$NON-NLS-1$
175 super.setInput(fileEditorInput
);
179 public boolean isDirty() {
184 public boolean isSaveAsAllowed() {
189 public void setInput(final IEditorInput input
) {
190 super.setInput(input
);
191 firePropertyChange(IEditorPart
.PROP_INPUT
);
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
));
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;
217 /* ensure start time is set */
218 final ITmfContext context
= fTrace
.seekEvent(0);
219 fTrace
.getNext(context
);
222 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fFile
));
224 setPartName(getEditorInput().getName());
225 fEventsTable
= new TmfEventsTable(fParent
, 0);
226 fEventsTable
.addSelectionChangedListener(this);
233 public void createPartControl(final Composite 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
);
242 /* ensure start time is set */
243 final ITmfContext context
= fTrace
.seekEvent(0);
244 fTrace
.getNext(context
);
247 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fFile
));
249 fEventsTable
= new TmfEventsTable(parent
, 0);
250 fEventsTable
.addSelectionChangedListener(this);
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);
264 public void dispose() {
265 if (getSite() != null) {
266 getSite().getPage().removePartListener(this);
268 ResourcesPlugin
.getWorkspace().removeResourceChangeListener(this);
269 removePropertyListener(this);
270 if (fTrace
!= null) {
271 broadcast(new TmfTraceClosedSignal(this, fTrace
));
273 if (fEventsTable
!= null) {
274 fEventsTable
.dispose();
280 * Create the event table
283 * The parent composite
286 * @return The event table instance
288 protected @NonNull TmfEventsTable
createEventsTable(final Composite parent
, final int cacheSize
) {
289 return getEventTable(fTrace
, parent
, cacheSize
);
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).
298 * The event table is for this trace
300 * The parent composite of the table
302 * The cache size to use
303 * @return The event table for the trace
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
);
311 TmfEventsTable table
= TmfTraceTypeUIUtils
.getEventTable(trace
, parent
, cacheSize
);
314 * The trace type specified an event table type, we will give it to
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).
324 Iterable
<ITmfEventAspect
> aspects
= trace
.getEventAspects();
325 if (aspects
!= null) {
326 return new TmfEventsTable(parent
, cacheSize
, aspects
);
330 * No columns were defined either, use a default table with the default
333 return new TmfEventsTable(parent
, cacheSize
);
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
345 * the parent Composite
347 * the event table cache size
348 * @return An event table of the appropriate type
350 private static @NonNull TmfEventsTable
getExperimentEventTable(
351 final TmfExperiment experiment
, final Composite parent
,
352 final int cacheSize
) {
354 String commonTraceType
= getCommonTraceType(experiment
);
355 if (commonTraceType
!= null) {
357 * All the traces in this experiment are of the same type, let's
358 * just use the normal table for that type.
360 return getEventTable(experiment
.getTraces()[0], parent
, cacheSize
);
364 * There are different trace types in the experiment, so we are
365 * definitely using a TmfEventsTable. Aggregate the columns from all
368 ITmfTrace
[] traces
= experiment
.getTraces();
369 Set
<ITmfEventAspect
> aspects
= new LinkedHashSet
<>();
371 for (ITmfTrace trace
: traces
) {
372 Iterable
<ITmfEventAspect
> traceAspects
= trace
.getEventAspects();
373 if (traceAspects
== null) {
374 aspects
.addAll(TmfTrace
.BASE_ASPECTS
);
376 for (ITmfEventAspect aspect
: traceAspects
) {
382 return new TmfEventsTable(parent
, cacheSize
, aspects
);
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.
391 * @return The common trace type if there is one, or 'null' if there are
394 private static @Nullable String
getCommonTraceType(TmfExperiment experiment
) {
395 String commonTraceType
= null;
397 for (final ITmfTrace trace
: experiment
.getTraces()) {
398 final IResource resource
= trace
.getResource();
399 if (resource
== null) {
403 final String traceType
= resource
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
404 if ((commonTraceType
!= null) && !commonTraceType
.equals(traceType
)) {
407 commonTraceType
= traceType
;
409 } catch (CoreException e
) {
411 * One of the traces didn't advertise its type, we can't infer
416 return commonTraceType
;
420 public ITmfTrace
getTrace() {
425 public void setFocus() {
426 fEventsTable
.setFocus();
430 public Object
getAdapter(final Class adapter
) {
431 if (IGotoMarker
.class.equals(adapter
)) {
432 if (fTrace
== null || fEventsTable
== null) {
436 } else if (IPropertySheetPage
.class.equals(adapter
)) {
437 return new UnsortedPropertySheetPage();
439 return super.getAdapter(adapter
);
446 public void gotoMarker(IMarker marker
) {
447 if (fTrace
== null || fEventsTable
== null) {
448 fPendingGotoMarker
= marker
;
450 fEventsTable
.gotoMarker(marker
);
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() {
463 fEventsTable
.removeBookmark(bookmark
);
466 } else if (delta
.getKind() == IResourceDelta
.CHANGED
) {
467 Display
.getDefault().asyncExec(new Runnable() {
470 fEventsTable
.getTable().refresh();
478 // ------------------------------------------------------------------------
479 // ISelectionProvider
480 // ------------------------------------------------------------------------
486 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
487 fSelectionChangedListeners
.add(listener
);
494 public ISelection
getSelection() {
495 if (fEventsTable
== null) {
496 return StructuredSelection
.EMPTY
;
498 return fEventsTable
.getSelection();
505 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
506 fSelectionChangedListeners
.remove(listener
);
513 public void setSelection(ISelection selection
) {
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.
521 * @param event a selection changed event
523 * @see ISelectionChangedListener#selectionChanged
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() {
533 l
.selectionChanged(event
);
539 // ------------------------------------------------------------------------
540 // ISelectionChangedListener
541 // ------------------------------------------------------------------------
547 public void selectionChanged(SelectionChangedEvent event
) {
548 fireSelectionChanged(event
);
551 // ------------------------------------------------------------------------
553 // ------------------------------------------------------------------------
559 public void partActivated(IWorkbenchPart part
) {
560 if (part
== this && fTrace
!= null) {
561 if (fTraceSelected
) {
564 fTraceSelected
= true;
565 broadcast(new TmfTraceSelectedSignal(this, fTrace
));
573 public void partBroughtToTop(IWorkbenchPart part
) {
574 if (part
== this && fTrace
!= null) {
575 if (fTraceSelected
) {
578 fTraceSelected
= true;
579 broadcast(new TmfTraceSelectedSignal(this, fTrace
));
587 public void partClosed(IWorkbenchPart part
) {
594 public void partDeactivated(IWorkbenchPart part
) {
601 public void partOpened(IWorkbenchPart part
) {
604 // ------------------------------------------------------------------------
606 // ------------------------------------------------------------------------
611 public void addBookmark() {
612 fEventsTable
.addBookmark(fFile
);
616 // ------------------------------------------------------------------------
618 // ------------------------------------------------------------------------
621 * Handler for the Trace Selected signal
623 * @param signal The incoming signal
626 public void traceSelected(final TmfTraceSelectedSignal signal
) {
627 if ((signal
.getSource() != this)) {
628 if (signal
.getTrace().equals(fTrace
)) {
629 getSite().getPage().bringToTop(this);
631 fTraceSelected
= false;
637 * Update the display to use the updated timestamp format
639 * @param signal the incoming signal
643 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
644 if (fEventsTable
!= null) {
645 fEventsTable
.refresh();