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
.lang
.reflect
.Constructor
;
16 import java
.lang
.reflect
.InvocationTargetException
;
19 import org
.eclipse
.core
.resources
.IFile
;
20 import org
.eclipse
.core
.resources
.IFolder
;
21 import org
.eclipse
.core
.resources
.IMarker
;
22 import org
.eclipse
.core
.resources
.IMarkerDelta
;
23 import org
.eclipse
.core
.resources
.IProject
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.core
.resources
.IResourceChangeEvent
;
26 import org
.eclipse
.core
.resources
.IResourceChangeListener
;
27 import org
.eclipse
.core
.resources
.IResourceDelta
;
28 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
29 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
30 import org
.eclipse
.core
.runtime
.CoreException
;
31 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
32 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
33 import org
.eclipse
.core
.runtime
.InvalidRegistryObjectException
;
34 import org
.eclipse
.core
.runtime
.Path
;
35 import org
.eclipse
.core
.runtime
.Platform
;
36 import org
.eclipse
.core
.runtime
.Status
;
37 import org
.eclipse
.core
.runtime
.preferences
.IEclipsePreferences
;
38 import org
.eclipse
.core
.runtime
.preferences
.InstanceScope
;
39 import org
.eclipse
.linuxtools
.tmf
.TmfCorePlugin
;
40 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalHandler
;
41 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfTraceSelectedSignal
;
42 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPlugin
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPreferenceInitializer
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.ParserProviderManager
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.signal
.TmfTraceClosedSignal
;
49 import org
.eclipse
.linuxtools
.tmf
.ui
.signal
.TmfTraceOpenedSignal
;
50 import org
.eclipse
.linuxtools
.tmf
.ui
.signal
.TmfTraceParserUpdatedSignal
;
51 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
52 import org
.eclipse
.swt
.widgets
.Composite
;
53 import org
.eclipse
.swt
.widgets
.Display
;
54 import org
.eclipse
.ui
.IEditorInput
;
55 import org
.eclipse
.ui
.IEditorPart
;
56 import org
.eclipse
.ui
.IEditorSite
;
57 import org
.eclipse
.ui
.IFileEditorInput
;
58 import org
.eclipse
.ui
.IPropertyListener
;
59 import org
.eclipse
.ui
.IReusableEditor
;
60 import org
.eclipse
.ui
.PartInitException
;
61 import org
.eclipse
.ui
.ide
.FileStoreEditorInput
;
62 import org
.eclipse
.ui
.ide
.IGotoMarker
;
63 import org
.osgi
.framework
.Bundle
;
66 * <b><u>TmfEventsEditor</u></b>
68 public class TmfEventsEditor
extends TmfEditor
implements ITmfTraceEditor
, IReusableEditor
, IPropertyListener
, IResourceChangeListener
{
70 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.editors.events"; //$NON-NLS-1$
72 private TmfEventsTable fEventsTable
;
73 private IResource fResource
;
74 private ITmfTrace
<?
> fTrace
;
75 private Composite fParent
;
78 public void doSave(IProgressMonitor monitor
) {
82 public void doSaveAs() {
86 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
87 if (input
instanceof TmfEditorInput
) {
88 fResource
= ((TmfEditorInput
) input
).getResource();
89 fTrace
= ((TmfEditorInput
) input
).getTrace();
90 } else if (input
instanceof IFileEditorInput
) {
91 fResource
= ((IFileEditorInput
) input
).getFile();
92 fTrace
= ParserProviderManager
.getTrace(fResource
);
93 input
= new TmfEditorInput(fResource
, fTrace
);
94 } else if (input
instanceof FileStoreEditorInput
) {
96 FileStoreEditorInput fileStoreEditorInput
= (FileStoreEditorInput
) input
;
97 fResource
= createLink(fileStoreEditorInput
.getURI());
98 fTrace
= ParserProviderManager
.getTrace(fResource
);
99 input
= new TmfEditorInput(fResource
, fTrace
);
100 } catch (CoreException e
) {
101 throw new PartInitException(e
.getMessage());
104 throw new PartInitException("Invalid IEditorInput: " + input
.getClass()); //$NON-NLS-1$
106 if (fTrace
== null) {
107 throw new PartInitException("Invalid IEditorInput: " + fResource
.getName()); //$NON-NLS-1$
110 super.setInput(input
);
113 ///////////////////////////////////////////////////////////////////////////////
114 // FIXME: Duplicated in ManageCustomParsersDialog
115 // From the legacy ProjectView
116 ///////////////////////////////////////////////////////////////////////////////
118 // ------------------------------------------------------------------------
120 // ------------------------------------------------------------------------
122 static public IFolder
getActiveProjectTracesFolder() {
123 @SuppressWarnings("deprecation")
124 IEclipsePreferences node
= new InstanceScope()
125 .getNode(TmfUiPlugin
.PLUGIN_ID
);
126 String activeProjectName
= node
.get(
127 TmfUiPreferenceInitializer
.ACTIVE_PROJECT_PREFERENCE
,
128 TmfUiPreferenceInitializer
.ACTIVE_PROJECT_DEFAULT
);
129 IWorkspaceRoot root
= ResourcesPlugin
.getWorkspace().getRoot();
130 IProject
[] projects
= root
.getProjects();
131 for (IProject project
: projects
) {
132 if (project
.isAccessible()
133 && project
.getName().equals(activeProjectName
)) {
134 return project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
140 static public IFile
createLink(URI uri
) throws CoreException
{
141 IFolder folder
= getActiveProjectTracesFolder();
142 if (folder
== null || !folder
.exists()) {
143 throw new CoreException(new Status(Status
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "No active project set")); //$NON-NLS-1$
145 String path
= uri
.getPath();
146 // TODO: support duplicate file names
147 IFile file
= folder
.getFile(path
.substring(path
148 .lastIndexOf(Path
.SEPARATOR
)));
149 if (!file
.exists()) {
150 file
.createLink(uri
, IResource
.NONE
, null);
154 ///////////////////////////////////////////////////////////////////////////////
157 public boolean isDirty() {
162 public boolean isSaveAsAllowed() {
167 public void setInput(IEditorInput input
) {
168 super.setInput(input
);
169 firePropertyChange(IEditorPart
.PROP_INPUT
);
173 public void propertyChanged(Object source
, int propId
) {
174 if (propId
== IEditorPart
.PROP_INPUT
) {
175 broadcast(new TmfTraceClosedSignal(this, fTrace
));
176 fResource
= ((TmfEditorInput
) getEditorInput()).getResource();
177 fTrace
= ((TmfEditorInput
) getEditorInput()).getTrace();
178 fEventsTable
.dispose();
179 if (fTrace
!= null) {
180 fEventsTable
= createEventsTable(fParent
, fTrace
.getCacheSize());
181 fEventsTable
.setTrace(fTrace
, true);
182 fEventsTable
.refreshBookmarks(fResource
);
183 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fResource
, fEventsTable
));
185 fEventsTable
= new TmfEventsTable(fParent
, 0);
192 public void createPartControl(Composite parent
) {
194 setPartName(getEditorInput().getName());
195 if (fTrace
!= null) {
196 fEventsTable
= createEventsTable(parent
, fTrace
.getCacheSize());
197 fEventsTable
.setTrace(fTrace
, true);
198 fEventsTable
.refreshBookmarks(fResource
);
199 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fResource
, fEventsTable
));
201 fEventsTable
= new TmfEventsTable(parent
, 0);
203 addPropertyListener(this);
204 ResourcesPlugin
.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent
.POST_CHANGE
);
208 public void dispose() {
209 ResourcesPlugin
.getWorkspace().removeResourceChangeListener(this);
210 removePropertyListener(this);
211 if (fTrace
!= null) {
212 broadcast(new TmfTraceClosedSignal(this, fTrace
));
214 if (fEventsTable
!= null) {
215 fEventsTable
.dispose();
220 protected TmfEventsTable
createEventsTable(Composite parent
, int cacheSize
) {
221 TmfEventsTable eventsTable
= getEventsTable(parent
, cacheSize
);
222 if (eventsTable
== null) {
223 eventsTable
= new TmfEventsTable(parent
, cacheSize
);
228 private TmfEventsTable
getEventsTable(Composite parent
, int cacheSize
) {
229 TmfEventsTable eventsTable
= null;
230 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfCorePlugin
.TMF_TRACE_TYPE_ID
);
232 String traceType
= fResource
.getPersistentProperty(TmfTraceElement
.TRACETYPE
);
233 for (IConfigurationElement ce
: config
) {
234 if (ce
.getAttribute(TmfTraceElement
.ID
).equals(traceType
)) {
235 IConfigurationElement
[] eventsTableTypeCE
= ce
.getChildren(TmfTraceElement
.EVENTS_TABLE_TYPE
);
236 if (eventsTableTypeCE
.length
!= 1) {
239 String eventsTableType
= eventsTableTypeCE
[0].getAttribute(TmfTraceElement
.CLASS
);
240 if (eventsTableType
== null || eventsTableType
.length() == 0) {
243 Bundle bundle
= Platform
.getBundle(ce
.getContributor().getName());
244 Class
<?
> c
= bundle
.loadClass(eventsTableType
);
245 Class
<?
>[] constructorArgs
= new Class
[] { Composite
.class, int.class };
246 Constructor
<?
> constructor
= c
.getConstructor(constructorArgs
);
247 Object
[] args
= new Object
[] { parent
, cacheSize
};
248 eventsTable
= (TmfEventsTable
) constructor
.newInstance(args
);
252 } catch (InvalidRegistryObjectException e
) {
254 } catch (CoreException e
) {
256 } catch (ClassNotFoundException e
) {
258 } catch (SecurityException e
) {
260 } catch (NoSuchMethodException e
) {
262 } catch (IllegalArgumentException e
) {
264 } catch (InstantiationException e
) {
266 } catch (IllegalAccessException e
) {
268 } catch (InvocationTargetException e
) {
275 public ITmfTrace
<?
> getTrace() {
280 public IResource
getResource() {
285 public void setFocus() {
286 fEventsTable
.setFocus();
287 if (fTrace
!= null) {
288 broadcast(new TmfTraceSelectedSignal(this, fTrace
));
293 @SuppressWarnings("rawtypes")
294 public Object
getAdapter(Class adapter
) {
295 if (IGotoMarker
.class.equals(adapter
)) {
298 return super.getAdapter(adapter
);
302 public void resourceChanged(IResourceChangeEvent event
) {
303 for (IMarkerDelta delta
: event
.findMarkerDeltas(IMarker
.BOOKMARK
, false)) {
304 if (delta
.getResource().equals(fResource
) && delta
.getKind() == IResourceDelta
.REMOVED
) {
305 final IMarker bookmark
= delta
.getMarker();
306 Display
.getDefault().asyncExec(new Runnable() {
309 fEventsTable
.removeBookmark(bookmark
);
316 // ------------------------------------------------------------------------
318 // ------------------------------------------------------------------------
320 public void addBookmark() {
321 fEventsTable
.addBookmark(fResource
);
325 // ------------------------------------------------------------------------
327 // ------------------------------------------------------------------------
330 public void traceParserUpdated(TmfTraceParserUpdatedSignal signal
) {
331 if (signal
.getTraceResource().equals(fResource
)) {
332 broadcast(new TmfTraceClosedSignal(this, fTrace
));
333 fTrace
= ParserProviderManager
.getTrace(fResource
);
334 fEventsTable
.dispose();
335 if (fTrace
!= null) {
336 fEventsTable
= createEventsTable(fParent
, fTrace
.getCacheSize());
337 fEventsTable
.setTrace(fTrace
, true);
338 broadcast(new TmfTraceOpenedSignal(this, fTrace
, fResource
, fEventsTable
));
340 fEventsTable
= new TmfEventsTable(fParent
, 0);
347 public void traceSelected(TmfTraceSelectedSignal signal
) {
348 if (signal
.getSource() != this && signal
.getTrace().equals(fTrace
)) {
349 getSite().getPage().bringToTop(this);