1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.editors
;
15 import java
.io
.FileNotFoundException
;
16 import java
.lang
.reflect
.Constructor
;
17 import java
.lang
.reflect
.InvocationTargetException
;
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
.IConfigurationElement
;
30 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
31 import org
.eclipse
.core
.runtime
.InvalidRegistryObjectException
;
32 import org
.eclipse
.core
.runtime
.Platform
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomEventsTable
;
34 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTrace
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
36 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
.Messages
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.experiment
.TmfExperiment
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfNavigatorContentProvider
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
49 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
50 import org
.eclipse
.linuxtools
.tmf
.ui
.signal
.TmfTraceClosedSignal
;
51 import org
.eclipse
.linuxtools
.tmf
.ui
.signal
.TmfTraceOpenedSignal
;
52 import org
.eclipse
.linuxtools
.tmf
.ui
.signal
.TmfTraceParserUpdatedSignal
;
53 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
54 import org
.eclipse
.swt
.widgets
.Composite
;
55 import org
.eclipse
.swt
.widgets
.Display
;
56 import org
.eclipse
.ui
.IEditorInput
;
57 import org
.eclipse
.ui
.IEditorPart
;
58 import org
.eclipse
.ui
.IEditorSite
;
59 import org
.eclipse
.ui
.IFileEditorInput
;
60 import org
.eclipse
.ui
.IPropertyListener
;
61 import org
.eclipse
.ui
.IReusableEditor
;
62 import org
.eclipse
.ui
.PartInitException
;
63 import org
.eclipse
.ui
.ide
.IGotoMarker
;
64 import org
.eclipse
.ui
.part
.FileEditorInput
;
65 import org
.osgi
.framework
.Bundle
;
68 * <b><u>TmfEventsEditor</u></b>
70 public class TmfEventsEditor
extends TmfEditor
implements ITmfTraceEditor
, IReusableEditor
, IPropertyListener
, IResourceChangeListener
{
72 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.editors.events"; //$NON-NLS-1$
74 private TmfEventsTable fEventsTable
;
76 @SuppressWarnings("rawtypes")
77 private ITmfTrace fTrace
;
78 private Composite fParent
;
81 public void doSave(IProgressMonitor monitor
) {
85 public void doSaveAs() {
88 @SuppressWarnings({ "unchecked", "rawtypes" })
90 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
91 if (input
instanceof TmfEditorInput
) {
92 fFile
= ((TmfEditorInput
) input
).getFile();
93 fTrace
= ((TmfEditorInput
) input
).getTrace();
94 input
= new FileEditorInput(fFile
);
95 } else if (input
instanceof IFileEditorInput
) {
96 fFile
= ((IFileEditorInput
) input
).getFile();
98 throw new PartInitException("Invalid IFileEditorInput: " + input
); //$NON-NLS-1$
101 String traceTypeId
= fFile
.getPersistentProperty(TmfTraceElement
.TRACETYPE
);
102 if (traceTypeId
== null) {
103 throw new PartInitException(Messages
.OpenTraceHandler_NoTraceType
);
105 if (traceTypeId
.equals(TmfExperiment
.class.getCanonicalName())) {
106 // Special case: experiment bookmark resource
107 TmfNavigatorContentProvider ncp
= new TmfNavigatorContentProvider();
108 ncp
.getChildren(fFile
.getProject()); // force the model to be populated
109 TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject());
110 if (project
== null) {
111 throw new PartInitException(Messages
.OpenExperimentHandler_NoTraceType
);
113 for (ITmfProjectModelElement projectElement
: project
.getExperimentsFolder().getChildren()) {
114 String traceName
= fFile
.getParent().getName();
115 if (projectElement
.getName().equals(traceName
)) {
116 TmfExperimentElement experimentElement
= (TmfExperimentElement
) projectElement
;
117 // Instantiate the experiment's traces
118 List
<TmfTraceElement
> traceEntries
= experimentElement
.getTraces();
119 int nbTraces
= traceEntries
.size();
120 int cacheSize
= Integer
.MAX_VALUE
;
121 ITmfTrace
<?
>[] traces
= new ITmfTrace
[nbTraces
];
122 for (int i
= 0; i
< nbTraces
; i
++) {
123 TmfTraceElement traceElement
= traceEntries
.get(i
);
124 ITmfTrace trace
= traceElement
.instantiateTrace();
125 TmfEvent traceEvent
= traceElement
.instantiateEvent();
126 if (trace
== null || traceEvent
== null) {
127 for (int j
= 0; j
< i
; j
++) {
130 throw new PartInitException(Messages
.OpenExperimentHandler_NoTraceType
);
133 trace
.initTrace(traceElement
.getName(), traceElement
.getLocation().getPath(), traceEvent
.getClass());
134 } catch (FileNotFoundException e
) {
136 trace
.setResource(traceElement
.getResource());
137 cacheSize
= Math
.min(cacheSize
, trace
.getIndexPageSize());
140 TmfExperiment experiment
= new TmfExperiment(TmfEvent
.class, experimentElement
.getName(), traces
, cacheSize
);
141 experiment
.setBookmarksFile(fFile
);
143 experiment
.initTrace(null, null, null);
144 experiment
.indexTrace(true);
148 } else if (traceTypeId
.equals(TmfTrace
.class.getCanonicalName())) {
149 // Special case: trace bookmark resource
150 TmfNavigatorContentProvider ncp
= new TmfNavigatorContentProvider();
151 ncp
.getChildren(fFile
.getProject()); // force the model to be populated
152 TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject());
153 for (ITmfProjectModelElement projectElement
: project
.getTracesFolder().getChildren()) {
154 String traceName
= fFile
.getParent().getName();
155 if (projectElement
.getName().equals(traceName
)) {
156 TmfTraceElement traceElement
= (TmfTraceElement
) projectElement
;
157 // Instantiate the trace
158 ITmfTrace trace
= traceElement
.instantiateTrace();
159 TmfEvent traceEvent
= traceElement
.instantiateEvent();
160 if (trace
== null || traceEvent
== null) {
161 throw new PartInitException(Messages
.OpenTraceHandler_NoTraceType
);
164 trace
.initTrace(traceElement
.getName(), traceElement
.getLocation().getPath(), traceEvent
.getClass());
165 trace
.indexTrace(false);
166 } catch (FileNotFoundException e
) {
168 trace
.setResource(traceElement
.getResource());
174 TmfNavigatorContentProvider ncp
= new TmfNavigatorContentProvider();
175 ncp
.getChildren(fFile
.getProject()); // force the model to be populated
176 TmfProjectElement project
= TmfProjectRegistry
.getProject(fFile
.getProject());
177 for (ITmfProjectModelElement projectElement
: project
.getTracesFolder().getChildren()) {
178 if (projectElement
.getResource().equals(fFile
)) {
179 TmfTraceElement traceElement
= (TmfTraceElement
) projectElement
;
180 // Instantiate the trace
181 ITmfTrace trace
= traceElement
.instantiateTrace();
182 TmfEvent traceEvent
= traceElement
.instantiateEvent();
183 if (trace
== null || traceEvent
== null) {
184 throw new PartInitException(Messages
.OpenTraceHandler_NoTraceType
);
187 trace
.initTrace(traceElement
.getName(), traceElement
.getLocation().getPath(), traceEvent
.getClass());
188 trace
.indexTrace(false);
189 } catch (FileNotFoundException e
) {
191 if (trace
instanceof TmfTrace
) {
192 ((TmfTrace
) trace
).setResource(traceElement
.getResource());
199 } catch (InvalidRegistryObjectException e
) {
201 } catch (PartInitException e
) {
203 } catch (CoreException e
) {
207 throw new PartInitException("Invalid IEditorInput: " + input
.getClass()); //$NON-NLS-1$
209 if (fTrace
== null) {
210 throw new PartInitException("Invalid IEditorInput: " + fFile
.getName()); //$NON-NLS-1$
213 super.setInput(input
);
217 public boolean isDirty() {
222 public boolean isSaveAsAllowed() {
227 public void setInput(IEditorInput input
) {
228 super.setInput(input
);
229 firePropertyChange(IEditorPart
.PROP_INPUT
);
233 public void propertyChanged(Object source
, int propId
) {
234 if (propId
== IEditorPart
.PROP_INPUT
) {
235 broadcast(new TmfTraceClosedSignal(this, fTrace
));
236 fFile
= ((TmfEditorInput
) getEditorInput()).getFile();
237 fTrace
= ((TmfEditorInput
) getEditorInput()).getTrace();
238 super.setInput(new FileEditorInput(fFile
));
239 fEventsTable
.dispose();
240 if (fTrace
!= null) {
241 fEventsTable
= createEventsTable(fParent
, fTrace
.getIndexPageSize());
242 fEventsTable
.setTrace(fTrace
, true);
243 fEventsTable
.refreshBookmarks(fFile
);
244 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fFile
, fEventsTable
));
246 fEventsTable
= new TmfEventsTable(fParent
, 0);
253 public void createPartControl(Composite parent
) {
255 if (fTrace
!= null) {
256 setPartName(fTrace
.getName());
257 fEventsTable
= createEventsTable(parent
, fTrace
.getIndexPageSize());
258 fEventsTable
.setTrace(fTrace
, true);
259 fEventsTable
.refreshBookmarks(fFile
);
260 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fFile
, fEventsTable
));
262 setPartName(getEditorInput().getName());
263 fEventsTable
= new TmfEventsTable(parent
, 0);
265 addPropertyListener(this);
266 ResourcesPlugin
.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent
.POST_CHANGE
);
270 public void dispose() {
271 ResourcesPlugin
.getWorkspace().removeResourceChangeListener(this);
272 removePropertyListener(this);
273 if (fTrace
!= null) {
274 broadcast(new TmfTraceClosedSignal(this, fTrace
));
276 if (fEventsTable
!= null) {
277 fEventsTable
.dispose();
282 protected TmfEventsTable
createEventsTable(Composite parent
, int cacheSize
) {
283 TmfEventsTable eventsTable
= getEventsTable(parent
, cacheSize
);
284 if (eventsTable
== null) {
285 eventsTable
= new TmfEventsTable(parent
, cacheSize
);
290 private TmfEventsTable
getEventsTable(Composite parent
, int cacheSize
) {
291 if (fTrace
instanceof TmfExperiment
) {
292 return getExperimentEventsTable((TmfExperiment
<?
>) fTrace
, parent
, cacheSize
);
294 TmfEventsTable eventsTable
= null;
296 if (fTrace
.getResource() == null) {
299 String traceType
= fTrace
.getResource().getPersistentProperty(TmfTraceElement
.TRACETYPE
);
300 if (traceType
== null) {
303 if (traceType
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
304 return new CustomEventsTable(((CustomTxtTrace
) fTrace
).getDefinition(), parent
, cacheSize
);
306 if (traceType
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
307 return new CustomEventsTable(((CustomXmlTrace
) fTrace
).getDefinition(), parent
, cacheSize
);
309 for (IConfigurationElement ce
: TmfTraceType
.getTypeElements()) {
310 if (ce
.getAttribute(TmfTraceType
.ID_ATTR
).equals(traceType
)) {
311 IConfigurationElement
[] eventsTableTypeCE
= ce
.getChildren(TmfTraceType
.EVENTS_TABLE_TYPE_ELEM
);
312 if (eventsTableTypeCE
.length
!= 1) {
315 String eventsTableType
= eventsTableTypeCE
[0].getAttribute(TmfTraceType
.CLASS_ATTR
);
316 if (eventsTableType
== null || eventsTableType
.length() == 0) {
319 Bundle bundle
= Platform
.getBundle(ce
.getContributor().getName());
320 Class
<?
> c
= bundle
.loadClass(eventsTableType
);
321 Class
<?
>[] constructorArgs
= new Class
[] { Composite
.class, int.class };
322 Constructor
<?
> constructor
= c
.getConstructor(constructorArgs
);
323 Object
[] args
= new Object
[] { parent
, cacheSize
};
324 eventsTable
= (TmfEventsTable
) constructor
.newInstance(args
);
328 } catch (InvalidRegistryObjectException e
) {
330 } catch (CoreException e
) {
332 } catch (ClassNotFoundException e
) {
334 } catch (SecurityException e
) {
336 } catch (NoSuchMethodException e
) {
338 } catch (IllegalArgumentException e
) {
340 } catch (InstantiationException e
) {
342 } catch (IllegalAccessException e
) {
344 } catch (InvocationTargetException e
) {
351 * Get the events table for an experiment.
352 * If all traces in the experiment are of the same type,
353 * use the extension point specified event table
354 * @param experiment the experiment
355 * @param parent the parent Composite
356 * @param cacheSize the event table cache size
357 * @return an events table of the appropriate type
359 private TmfEventsTable
getExperimentEventsTable(TmfExperiment
<?
> experiment
, Composite parent
, int cacheSize
) {
360 TmfEventsTable eventsTable
= null;
361 String commonTraceType
= null;
363 for (ITmfTrace
<?
> trace
: experiment
.getTraces()) {
364 IResource resource
= trace
.getResource();
365 if (resource
== null) {
368 String traceType
= resource
.getPersistentProperty(TmfTraceElement
.TRACETYPE
);
369 if (commonTraceType
!= null && !commonTraceType
.equals(traceType
)) {
372 commonTraceType
= traceType
;
374 if (commonTraceType
== null) {
377 if (commonTraceType
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
378 return new CustomEventsTable(((CustomTxtTrace
) experiment
.getTraces()[0]).getDefinition(), parent
, cacheSize
);
380 if (commonTraceType
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
381 return new CustomEventsTable(((CustomXmlTrace
) experiment
.getTraces()[0]).getDefinition(), parent
, cacheSize
);
383 for (IConfigurationElement ce
: TmfTraceType
.getTypeElements()) {
384 if (ce
.getAttribute(TmfTraceType
.ID_ATTR
).equals(commonTraceType
)) {
385 IConfigurationElement
[] eventsTableTypeCE
= ce
.getChildren(TmfTraceType
.EVENTS_TABLE_TYPE_ELEM
);
386 if (eventsTableTypeCE
.length
!= 1) {
389 String eventsTableType
= eventsTableTypeCE
[0].getAttribute(TmfTraceType
.CLASS_ATTR
);
390 if (eventsTableType
== null || eventsTableType
.length() == 0) {
393 Bundle bundle
= Platform
.getBundle(ce
.getContributor().getName());
394 Class
<?
> c
= bundle
.loadClass(eventsTableType
);
395 Class
<?
>[] constructorArgs
= new Class
[] { Composite
.class, int.class };
396 Constructor
<?
> constructor
= c
.getConstructor(constructorArgs
);
397 Object
[] args
= new Object
[] { parent
, cacheSize
};
398 eventsTable
= (TmfEventsTable
) constructor
.newInstance(args
);
402 } catch (CoreException e
) {
404 } catch (InvalidRegistryObjectException e
) {
406 } catch (SecurityException e
) {
408 } catch (IllegalArgumentException e
) {
410 } catch (ClassNotFoundException e
) {
412 } catch (NoSuchMethodException e
) {
414 } catch (InstantiationException e
) {
416 } catch (IllegalAccessException e
) {
418 } catch (InvocationTargetException e
) {
425 public ITmfTrace
<?
> getTrace() {
430 public IFile
getBookmarksFile() {
435 public void setFocus() {
436 fEventsTable
.setFocus();
437 if (fTrace
!= null) {
438 broadcast(new TmfTraceSelectedSignal(this, fTrace
));
443 @SuppressWarnings("rawtypes")
444 public Object
getAdapter(Class adapter
) {
445 if (IGotoMarker
.class.equals(adapter
)) {
448 return super.getAdapter(adapter
);
452 public void resourceChanged(IResourceChangeEvent event
) {
453 for (IMarkerDelta delta
: event
.findMarkerDeltas(IMarker
.BOOKMARK
, false)) {
454 if (delta
.getResource().equals(fFile
)) {
455 if (delta
.getKind() == IResourceDelta
.REMOVED
) {
456 final IMarker bookmark
= delta
.getMarker();
457 Display
.getDefault().asyncExec(new Runnable() {
460 fEventsTable
.removeBookmark(bookmark
);
463 } else if (delta
.getKind() == IResourceDelta
.CHANGED
) {
464 Display
.getDefault().asyncExec(new Runnable() {
467 fEventsTable
.getTable().refresh();
475 // ------------------------------------------------------------------------
477 // ------------------------------------------------------------------------
479 public void addBookmark() {
480 fEventsTable
.addBookmark(fFile
);
484 // ------------------------------------------------------------------------
486 // ------------------------------------------------------------------------
488 @SuppressWarnings("unchecked")
490 public void traceParserUpdated(TmfTraceParserUpdatedSignal signal
) {
491 if (signal
.getTraceResource().equals(fFile
)) {
492 broadcast(new TmfTraceClosedSignal(this, fTrace
));
494 String name
= fTrace
.getName();
496 String traceTypeId
= fFile
.getPersistentProperty(TmfTraceElement
.TRACETYPE
);
497 if (traceTypeId
!= null) {
498 for (IConfigurationElement ce
: TmfTraceType
.getTypeElements()) {
499 if (traceTypeId
.equals(ce
.getAttribute(TmfTraceType
.ID_ATTR
))) {
500 fTrace
= (ITmfTrace
<?
>) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
501 TmfEvent event
= (TmfEvent
) ce
.createExecutableExtension(TmfTraceType
.EVENT_TYPE_ATTR
);
502 String path
= fFile
.getLocationURI().getPath();
503 fTrace
.initTrace(name
, path
, event
.getClass());
504 fTrace
.indexTrace(false);
509 } catch (InvalidRegistryObjectException e
) {
511 } catch (FileNotFoundException e
) {
513 } catch (CoreException e
) {
516 fEventsTable
.dispose();
517 if (fTrace
!= null) {
518 fEventsTable
= createEventsTable(fParent
, fTrace
.getIndexPageSize());
519 fEventsTable
.setTrace(fTrace
, true);
520 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fFile
, fEventsTable
));
522 fEventsTable
= new TmfEventsTable(fParent
, 0);
529 public void traceSelected(TmfTraceSelectedSignal signal
) {
530 if (signal
.getSource() != this && signal
.getTrace().equals(fTrace
)) {
531 getSite().getPage().bringToTop(this);