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
.tracecompass
.tmf
.ui
.project
.model
;
19 import java
.io
.IOException
;
20 import java
.util
.List
;
22 import org
.eclipse
.core
.resources
.IFile
;
23 import org
.eclipse
.core
.resources
.IFolder
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.core
.runtime
.CoreException
;
26 import org
.eclipse
.core
.runtime
.IPath
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.core
.runtime
.Path
;
29 import org
.eclipse
.core
.runtime
.Status
;
30 import org
.eclipse
.core
.runtime
.URIUtil
;
31 import org
.eclipse
.jface
.util
.OpenStrategy
;
32 import org
.eclipse
.osgi
.util
.NLS
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.swt
.widgets
.MessageBox
;
35 import org
.eclipse
.swt
.widgets
.Shell
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceImportException
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEditorInput
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
48 import org
.eclipse
.ui
.IEditorInput
;
49 import org
.eclipse
.ui
.IEditorPart
;
50 import org
.eclipse
.ui
.IEditorReference
;
51 import org
.eclipse
.ui
.IReusableEditor
;
52 import org
.eclipse
.ui
.IWorkbench
;
53 import org
.eclipse
.ui
.IWorkbenchPage
;
54 import org
.eclipse
.ui
.PartInitException
;
55 import org
.eclipse
.ui
.PlatformUI
;
56 import org
.eclipse
.ui
.ide
.IDE
;
57 import org
.eclipse
.ui
.part
.FileEditorInput
;
62 * Helper class for opening trace resources and loading them to a tracing
65 * @author Matthew Khouzam
67 public class TmfOpenTraceHelper
{
69 private TmfOpenTraceHelper() {
72 private static final String ENDL
= System
.getProperty("line.separator"); //$NON-NLS-1$
75 * Opens a trace from a path while importing it to the destination folder.
76 * The trace is linked as a resource.
78 * @param destinationFolder
79 * The destination trace folder
83 * the shell to use for dialogs
84 * @return IStatus OK if successful
85 * @throws CoreException
86 * 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
110 public static IStatus
openTraceFromPath(TmfTraceFolder destinationFolder
, String path
, Shell shell
, String tracetypeHint
) throws CoreException
{
111 final String pathToUse
= checkTracePath(path
);
112 TraceTypeHelper traceTypeToSet
= null;
114 traceTypeToSet
= TmfTraceTypeUIUtils
.selectTraceType(pathToUse
, null, tracetypeHint
);
115 } catch (TmfTraceImportException e
) {
116 MessageBox mb
= new MessageBox(shell
);
117 mb
.setMessage(e
.getMessage());
119 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage());
122 IFolder folder
= destinationFolder
.getResource();
123 String traceName
= getTraceName(pathToUse
, folder
);
124 if (traceExists(pathToUse
, folder
)) {
125 return openTraceFromFolder(destinationFolder
, traceName
);
127 final IPath pathString
= Path
.fromOSString(pathToUse
);
128 IResource linkedTrace
= TmfImportHelper
.createLink(folder
, pathString
, traceName
);
130 if (linkedTrace
== null || !linkedTrace
.exists()) {
131 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
132 Messages
.TmfOpenTraceHelper_LinkFailed
);
135 String sourceLocation
= URIUtil
.toUnencodedString(pathString
.toFile().toURI());
136 linkedTrace
.setPersistentProperty(TmfCommonConstants
.SOURCE_LOCATION
, sourceLocation
);
138 // No trace type was determined.
139 if (traceTypeToSet
== null) {
140 return Status
.OK_STATUS
;
143 IStatus ret
= TmfTraceTypeUIUtils
.setTraceType(linkedTrace
, traceTypeToSet
);
145 ret
= openTraceFromFolder(destinationFolder
, traceName
);
151 * Checks whether the parent or grandparent of given path to a file is a
152 * valid directory trace. If it is a directory trace then return the parent
153 * or grandparent path.
157 * @return path to use for trace type validation.
159 private static String
checkTracePath(String path
) {
160 File file
= new File(path
);
161 if (file
.exists() && !file
.isDirectory()) {
162 // First check parent
163 File parent
= file
.getParentFile();
164 String pathToUse
= parent
.getAbsolutePath();
165 if (TmfTraceType
.isDirectoryTrace(pathToUse
)) {
168 // Second check grandparent
169 File grandParent
= parent
.getParentFile();
170 if (grandParent
!= null) {
171 pathToUse
= grandParent
.getAbsolutePath();
172 if (TmfTraceType
.isDirectoryTrace(pathToUse
)) {
180 private static boolean traceExists(String path
, IFolder folder
) {
181 String val
= getTraceName(path
, folder
);
182 return (folder
.findMember(val
) != null);
185 private static boolean isWrongMember(IFolder folder
, String name
, final File traceFile
) {
186 final IResource candidate
= folder
.findMember(name
);
187 if (candidate
!= null) {
188 final IPath rawLocation
= candidate
.getRawLocation();
189 final File file
= rawLocation
.toFile();
190 return !file
.equals(traceFile
);
196 * Gets the display name, either "filename" or "filename(n)" if there is
197 * already a filename existing where n is the next unused integer starting
203 * the folder to import to
204 * @return the filename
206 private static String
getTraceName(String path
, IFolder folder
) {
208 File traceFile
= new File(path
);
210 traceFile
= traceFile
.getCanonicalFile();
211 } catch (IOException e
) {
212 /* just use original file path */
214 name
= traceFile
.getName();
215 for (int i
= 2; isWrongMember(folder
, name
, traceFile
); i
++) {
216 name
= traceFile
.getName() + '(' + i
+ ')';
222 * Open a trace from a trace folder
224 * @param destinationFolder
225 * The destination trace folder
228 * @return success or error
230 private static IStatus
openTraceFromFolder(TmfTraceFolder destinationFolder
, String traceName
) {
231 final List
<ITmfProjectModelElement
> elements
= destinationFolder
.getChildren();
232 TmfTraceElement traceElement
= null;
233 for (ITmfProjectModelElement element
: elements
) {
234 if (element
instanceof TmfTraceElement
&& element
.getName().equals(traceName
)) {
235 traceElement
= (TmfTraceElement
) element
;
238 if (traceElement
== null) {
239 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(Messages
.TmfOpenTraceHelper_TraceNotFound
, traceName
));
241 openTraceFromElement(traceElement
);
242 return Status
.OK_STATUS
;
245 private static ITmfTrace
openTraceElement(final TmfTraceElement traceElement
) {
246 final ITmfTrace trace
= traceElement
.instantiateTrace();
247 final ITmfEvent traceEvent
= traceElement
.instantiateEvent();
248 if ((trace
== null) || (traceEvent
== null)) {
249 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
250 Messages
.TmfOpenTraceHelper_NoTraceType
);
258 trace
.initTrace(traceElement
.getResource(), traceElement
.getResource().getLocation().toOSString(), traceEvent
.getClass(), traceElement
.getElementPath(), traceElement
.getTraceType());
259 } catch (final TmfTraceException e
) {
260 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
261 Messages
.TmfOpenTraceHelper_InitError
+ ENDL
+ ENDL
+ e
);
268 private static ITmfTrace
openExperimentElement(final TmfExperimentElement experimentElement
) {
269 /* Experiment element now has an experiment type associated with it */
270 final TmfExperiment experiment
= experimentElement
.instantiateTrace();
271 if (experiment
== null) {
272 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, experimentElement
.getTypeName()),
273 NLS
.bind(Messages
.TmfOpenTraceHelper_NoTraceOrExperimentType
, experimentElement
.getTypeName()));
277 // Instantiate the experiment's traces
278 final List
<TmfTraceElement
> traceEntries
= experimentElement
.getTraces();
279 int cacheSize
= Integer
.MAX_VALUE
;
280 final ITmfTrace
[] traces
= new ITmfTrace
[traceEntries
.size()];
281 for (int i
= 0; i
< traceEntries
.size(); i
++) {
282 TmfTraceElement element
= traceEntries
.get(i
);
284 // Since trace is under an experiment, use the original trace from
286 element
= element
.getElementUnderTraceFolder();
288 ITmfTrace trace
= openTraceElement(element
);
291 for (int j
= 0; j
< i
; j
++) {
296 cacheSize
= Math
.min(cacheSize
, trace
.getCacheSize());
301 // Create the experiment
302 experiment
.initExperiment(ITmfEvent
.class, experimentElement
.getName(), traces
, cacheSize
, experimentElement
.getResource());
307 private static ITmfTrace
openProjectElement(final TmfCommonProjectElement element
) {
308 ITmfTrace trace
= null;
309 if (element
instanceof TmfTraceElement
) {
310 trace
= openTraceElement((TmfTraceElement
) element
);
311 } else if (element
instanceof TmfExperimentElement
) {
312 trace
= openExperimentElement((TmfExperimentElement
) element
);
318 * Open a trace (or experiment) from a project element. If the trace is already opened, its
319 * editor is activated and brought to top.
321 * @param traceElement
322 * the {@link TmfTraceElement} to open
324 public static void openTraceFromElement(final TmfCommonProjectElement traceElement
) {
328 file
= traceElement
.createBookmarksFile();
329 } catch (final CoreException e
) {
330 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getTypeName()) + ' ' + traceElement
.getName());
331 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
332 NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorElement
, traceElement
.getTypeName()) + ENDL
+ ENDL
+ e
.getMessage());
336 final IWorkbench wb
= PlatformUI
.getWorkbench();
337 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
338 final IEditorPart editor
= findEditor(new FileEditorInput(file
), true);
339 if (editor
!= null) {
340 activePage
.activate(editor
);
344 // If a trace type is not set then delegate it to the eclipse platform
345 if ((traceElement
instanceof TmfTraceElement
) && (traceElement
.getResource() instanceof IFile
) && (traceElement
.getTraceType() == null)) {
347 boolean activate
= OpenStrategy
.activateOnOpen();
348 // only local open is supported
349 IDE
.openEditor(activePage
, file
, activate
);
350 } catch (PartInitException e
) {
351 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
352 NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getElementPath()) + ENDL
+ ENDL
+ e
.getMessage());
357 Thread thread
= new Thread() {
360 final ITmfTrace trace
= openProjectElement(traceElement
);
366 // Get the editor id from the extension point
367 String traceEditorId
= traceElement
.getEditorId();
368 final String editorId
= (traceEditorId
!= null) ? traceEditorId
: TmfEventsEditor
.ID
;
369 final IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
371 Display
.getDefault().asyncExec(new Runnable() {
375 activePage
.openEditor(editorInput
, editorId
);
376 IDE
.setDefaultEditor(file
, editorId
);
377 // editor should dispose the trace on close
378 } catch (final PartInitException e
) {
379 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
380 NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getTypeName()) + ENDL
+ ENDL
+ e
.getMessage());
381 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getTypeName()) + ' ' + traceElement
.getName());
392 * Returns the editor with the specified input. Returns null if there is no
393 * opened editor with that input. If restore is requested, the method finds
394 * and returns the editor even if it is not restored yet after a restart.
399 * true if the editor should be restored
400 * @return an editor with input equals to <code>input</code>
402 private static IEditorPart
findEditor(IEditorInput input
, boolean restore
) {
403 final IWorkbench wb
= PlatformUI
.getWorkbench();
404 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
405 for (IEditorReference editorReference
: activePage
.getEditorReferences()) {
407 IEditorInput editorInput
= editorReference
.getEditorInput();
408 if (editorInput
.equals(input
)) {
409 return editorReference
.getEditor(restore
);
411 } catch (PartInitException e
) {
418 * Reopen a trace or experiment from a project element in the provided
421 * @param traceElement
422 * the {@link TmfTraceElement} to open
424 * the reusable editor
426 public static void reopenTraceFromElement(final TmfCommonProjectElement traceElement
, final IReusableEditor editor
) {
430 file
= traceElement
.createBookmarksFile();
431 } catch (final CoreException e
) {
432 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorOpeningElement
, traceElement
.getTypeName()) + ' ' + traceElement
.getName());
433 TraceUtils
.displayErrorMsg(NLS
.bind(Messages
.TmfOpenTraceHelper_OpenElement
, traceElement
.getTypeName()),
434 NLS
.bind(Messages
.TmfOpenTraceHelper_ErrorElement
, traceElement
.getTypeName()) + ENDL
+ ENDL
+ e
.getMessage());
438 Thread thread
= new Thread() {
442 final ITmfTrace trace
= openProjectElement(traceElement
);
447 final IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
449 Display
.getDefault().asyncExec(new Runnable() {
452 final IWorkbench wb
= PlatformUI
.getWorkbench();
453 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
454 activePage
.reuseEditor(editor
, editorInput
);
455 activePage
.activate(editor
);