1 /**********************************************************************
2 * Copyright (c) 2013, 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 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Update open trace and add open experiment
12 * Geneviève Bastien - Merge methods to open trace and experiments
13 * Bernd Hufmann - Updated handling of directory traces
14 **********************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
19 import java
.util
.List
;
21 import org
.eclipse
.core
.resources
.IFile
;
22 import org
.eclipse
.core
.resources
.IFolder
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IPath
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.core
.runtime
.Path
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.core
.runtime
.URIUtil
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
31 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceImportException
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TraceTypeHelper
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
42 import org
.eclipse
.osgi
.util
.NLS
;
43 import org
.eclipse
.swt
.widgets
.Display
;
44 import org
.eclipse
.swt
.widgets
.MessageBox
;
45 import org
.eclipse
.swt
.widgets
.Shell
;
46 import org
.eclipse
.ui
.IEditorInput
;
47 import org
.eclipse
.ui
.IEditorPart
;
48 import org
.eclipse
.ui
.IEditorReference
;
49 import org
.eclipse
.ui
.IReusableEditor
;
50 import org
.eclipse
.ui
.IWorkbench
;
51 import org
.eclipse
.ui
.IWorkbenchPage
;
52 import org
.eclipse
.ui
.PartInitException
;
53 import org
.eclipse
.ui
.PlatformUI
;
54 import org
.eclipse
.ui
.ide
.IDE
;
55 import org
.eclipse
.ui
.part
.FileEditorInput
;
60 * Helper class for opening trace resources and loading them to a tracing
63 * @author Matthew Khouzam
66 public class TmfOpenTraceHelper
{
68 private TmfOpenTraceHelper() {
71 private static final String ENDL
= System
.getProperty("line.separator"); //$NON-NLS-1$
74 * Opens a trace from a path while importing it to the destination folder.
75 * The trace is linked as a resource.
77 * @param destinationFolder
78 * The destination trace folder
82 * the shell to use for dialogs
83 * @return IStatus OK if successful
84 * @throws CoreException
85 * core exceptions if something is not well set up in the back
89 public static IStatus
openTraceFromPath(TmfTraceFolder destinationFolder
, String path
, Shell shell
) throws CoreException
{
90 return openTraceFromPath(destinationFolder
, path
, shell
, null);
94 * Opens a trace from a path while importing it to the destination folder.
95 * The trace is linked as a resource.
97 * @param destinationFolder
98 * The destination trace folder
102 * the shell to use for dialogs
103 * @param tracetypeHint
104 * The trace type id, can be null
105 * @return IStatus OK if successful
106 * @throws CoreException
107 * core exceptions if something is not well set up in the back
112 public static IStatus
openTraceFromPath(TmfTraceFolder destinationFolder
, String path
, Shell shell
, String tracetypeHint
) throws CoreException
{
113 final String pathToUse
= checkTracePath(path
);
114 TraceTypeHelper traceTypeToSet
= null;
116 traceTypeToSet
= TmfTraceTypeUIUtils
.selectTraceType(pathToUse
, null, tracetypeHint
);
117 } catch (TmfTraceImportException e
) {
118 MessageBox mb
= new MessageBox(shell
);
119 mb
.setMessage(e
.getMessage());
121 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage());
124 IFolder folder
= destinationFolder
.getResource();
125 String traceName
= getTraceName(pathToUse
, folder
);
126 if (traceExists(pathToUse
, folder
)) {
127 return openTraceFromFolder(destinationFolder
, traceName
);
129 final IPath pathString
= Path
.fromOSString(pathToUse
);
130 IResource linkedTrace
= TmfImportHelper
.createLink(folder
, pathString
, traceName
);
132 if (linkedTrace
== null || !linkedTrace
.exists()) {
133 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
134 Messages
.TmfOpenTraceHelper_LinkFailed
);
137 String sourceLocation
= URIUtil
.toUnencodedString(pathString
.toFile().toURI());
138 linkedTrace
.setPersistentProperty(TmfCommonConstants
.SOURCE_LOCATION
, sourceLocation
);
140 // No trace type was determined.
141 if (traceTypeToSet
== null) {
142 return Status
.OK_STATUS
;
145 IStatus ret
= TmfTraceTypeUIUtils
.setTraceType(linkedTrace
, traceTypeToSet
);
147 ret
= openTraceFromFolder(destinationFolder
, traceName
);
153 * Checks whether the parent or grandparent of given path to a file is a
154 * valid directory trace. If it is a directory trace then return the parent
155 * or grandparent path.
159 * @return path to use for trace type validation.
161 private static String
checkTracePath(String path
) {
162 File file
= new File(path
);
163 if (file
.exists() && !file
.isDirectory()) {
164 // First check parent
165 File parent
= file
.getParentFile();
166 String pathToUse
= parent
.getAbsolutePath();
167 if (TmfTraceType
.getInstance().isDirectoryTrace(pathToUse
)) {
170 // Second check grandparent
171 File grandParent
= parent
.getParentFile();
172 if (grandParent
!= null) {
173 pathToUse
= grandParent
.getAbsolutePath();
174 if (TmfTraceType
.getInstance().isDirectoryTrace(pathToUse
)) {
182 private static boolean traceExists(String path
, IFolder folder
) {
183 String val
= getTraceName(path
, folder
);
184 return (folder
.findMember(val
) != null);
187 private static boolean isWrongMember(IFolder folder
, String name
, final File traceFile
) {
188 final IResource candidate
= folder
.findMember(name
);
189 if (candidate
!= null) {
190 final IPath rawLocation
= candidate
.getRawLocation();
191 final File file
= rawLocation
.toFile();
192 return !file
.equals(traceFile
);
198 * Gets the display name, either "filename" or "filename(n)" if there is
199 * already a filename existing where n is the next unused integer starting
205 * the folder to import to
206 * @return the filename
208 private static String
getTraceName(String path
, IFolder folder
) {
210 final File traceFile
= new File(path
);
211 name
= traceFile
.getName();
212 for (int i
= 2; isWrongMember(folder
, name
, traceFile
); i
++) {
213 name
= traceFile
.getName() + '(' + i
+ ')';
219 * Open a trace from a trace folder
221 * @param destinationFolder
222 * The destination trace folder
225 * @return success or error
228 private static IStatus
openTraceFromFolder(TmfTraceFolder destinationFolder
, String traceName
) {
229 final List
<ITmfProjectModelElement
> elements
= destinationFolder
.getChildren();
230 TmfTraceElement traceElement
= null;
231 for (ITmfProjectModelElement element
: elements
) {
232 if (element
instanceof TmfTraceElement
&& element
.getName().equals(traceName
)) {
233 traceElement
= (TmfTraceElement
) element
;
236 if (traceElement
== null) {
237 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(Messages
.TmfOpenTraceHelper_TraceNotFound
, traceName
));
239 openTraceFromElement(traceElement
);
240 return Status
.OK_STATUS
;
243 private static ITmfTrace
openTraceElement(final TmfTraceElement traceElement
) {
244 final ITmfTrace trace
= traceElement
.instantiateTrace();
245 final ITmfEvent traceEvent
= traceElement
.instantiateEvent();
246 if ((trace
== null) || (traceEvent
== null)) {
247 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
248 NLS
.bind(Messages
.TmfOpenTraceHelper_NoTraceOrExperimentType
, traceElement
.getTypeName()));
256 trace
.initTrace(traceElement
.getResource(), traceElement
.getLocation().getPath(), traceEvent
.getClass(), traceElement
.getElementPath());
257 } catch (final TmfTraceException e
) {
258 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
259 Messages
.TmfOpenTraceHelper_InitError
+ ENDL
+ ENDL
+ e
);
266 private static ITmfTrace
openExperimentElement(final TmfExperimentElement experimentElement
) {
267 /* Experiment element now has an experiment type associated with it */
268 final TmfExperiment experiment
= experimentElement
.instantiateTrace();
269 if (experiment
== null) {
270 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, experimentElement
.getTypeName()),
271 NLS
.bind(Messages
.TmfOpenTraceHelper_NoTraceOrExperimentType
, experimentElement
.getTypeName()));
275 // Instantiate the experiment's traces
276 final List
<TmfTraceElement
> traceEntries
= experimentElement
.getTraces();
277 int cacheSize
= Integer
.MAX_VALUE
;
278 final ITmfTrace
[] traces
= new ITmfTrace
[traceEntries
.size()];
279 for (int i
= 0; i
< traceEntries
.size(); i
++) {
280 TmfTraceElement element
= traceEntries
.get(i
);
282 // Since trace is under an experiment, use the original trace from
284 element
= element
.getElementUnderTraceFolder();
286 ITmfTrace trace
= openTraceElement(element
);
289 for (int j
= 0; j
< i
; j
++) {
294 cacheSize
= Math
.min(cacheSize
, trace
.getCacheSize());
299 // Create the experiment
300 experiment
.initExperiment(ITmfEvent
.class, experimentElement
.getName(), traces
, cacheSize
, experimentElement
.getResource());
305 private static ITmfTrace
openProjectElement(final TmfCommonProjectElement element
) {
306 ITmfTrace trace
= null;
307 if (element
instanceof TmfTraceElement
) {
308 trace
= openTraceElement((TmfTraceElement
) element
);
309 } else if (element
instanceof TmfExperimentElement
) {
310 trace
= openExperimentElement((TmfExperimentElement
) element
);
316 * Open a trace (or experiment) from a project element. If the trace is already opened, its
317 * editor is activated and brought to top.
319 * @param traceElement
320 * the {@link TmfTraceElement} to open
323 public static void openTraceFromElement(final TmfCommonProjectElement traceElement
) {
327 file
= traceElement
.createBookmarksFile();
328 } catch (final CoreException e
) {
329 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getTypeName()) + ' ' + traceElement
.getName());
330 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
331 NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorElement
, traceElement
.getTypeName()) + ENDL
+ ENDL
+ e
.getMessage());
335 final IWorkbench wb
= PlatformUI
.getWorkbench();
336 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
337 final IEditorPart editor
= findEditor(new FileEditorInput(file
), true);
338 if (editor
!= null) {
339 activePage
.activate(editor
);
343 Thread thread
= new Thread() {
347 final ITmfTrace trace
= openProjectElement(traceElement
);
352 // Get the editor id from the extension point
353 String traceEditorId
= traceElement
.getEditorId();
354 final String editorId
= (traceEditorId
!= null) ? traceEditorId
: TmfEventsEditor
.ID
;
355 final IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
357 Display
.getDefault().asyncExec(new Runnable() {
361 activePage
.openEditor(editorInput
, editorId
);
362 IDE
.setDefaultEditor(file
, editorId
);
363 // editor should dispose the trace on close
364 } catch (final PartInitException e
) {
365 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
366 NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getTypeName()) + ENDL
+ ENDL
+ e
.getMessage());
367 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getTypeName()) + ' ' + traceElement
.getName());
378 * Returns the editor with the specified input. Returns null if there is no
379 * opened editor with that input. If restore is requested, the method finds
380 * and returns the editor even if it is not restored yet after a restart.
385 * true if the editor should be restored
386 * @return an editor with input equals to <code>input</code>
388 private static IEditorPart
findEditor(IEditorInput input
, boolean restore
) {
389 final IWorkbench wb
= PlatformUI
.getWorkbench();
390 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
391 for (IEditorReference editorReference
: activePage
.getEditorReferences()) {
393 IEditorInput editorInput
= editorReference
.getEditorInput();
394 if (editorInput
.equals(input
)) {
395 return editorReference
.getEditor(restore
);
397 } catch (PartInitException e
) {
404 * Reopen a trace or experiment from a project element in the provided
407 * @param traceElement
408 * the {@link TmfTraceElement} to open
410 * the reusable editor
413 public static void reopenTraceFromElement(final TmfCommonProjectElement traceElement
, final IReusableEditor editor
) {
417 file
= traceElement
.createBookmarksFile();
418 } catch (final CoreException e
) {
419 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getTypeName()) + ' ' + traceElement
.getName());
420 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
421 NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorElement
, traceElement
.getTypeName()) + ENDL
+ ENDL
+ e
.getMessage());
425 Thread thread
= new Thread() {
429 final ITmfTrace trace
= openProjectElement(traceElement
);
434 final IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
436 Display
.getDefault().asyncExec(new Runnable() {
439 final IWorkbench wb
= PlatformUI
.getWorkbench();
440 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
441 activePage
.reuseEditor(editor
, editorInput
);
442 activePage
.activate(editor
);