Commit | Line | Data |
---|---|---|
76fccfb0 | 1 | /********************************************************************** |
9d2e08b9 | 2 | * Copyright (c) 2013, 2014 Ericsson, École Polytechnique de Montréal |
76fccfb0 MK |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial API and implementation | |
67c53011 | 11 | * Patrick Tasse - Update open trace and add open experiment |
9d2e08b9 | 12 | * Geneviève Bastien - Merge methods to open trace and experiments |
1aec2e92 | 13 | * Bernd Hufmann - Updated handling of directory traces |
76fccfb0 MK |
14 | **********************************************************************/ |
15 | ||
16 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
17 | ||
18 | import java.io.File; | |
19 | import java.util.List; | |
20 | ||
21 | import org.eclipse.core.resources.IFile; | |
22 | import org.eclipse.core.resources.IFolder; | |
76fccfb0 | 23 | import org.eclipse.core.resources.IResource; |
76fccfb0 MK |
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; | |
89730b51 | 29 | import org.eclipse.core.runtime.URIUtil; |
76fccfb0 MK |
30 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
31 | import org.eclipse.linuxtools.internal.tmf.ui.project.model.TmfImportHelper; | |
89730b51 | 32 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
76fccfb0 MK |
33 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
34 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
47aafe74 | 35 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceImportException; |
1aec2e92 | 36 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; |
47aafe74 | 37 | import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper; |
76fccfb0 | 38 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
67c53011 | 39 | import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment; |
76fccfb0 MK |
40 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput; |
41 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor; | |
9d2e08b9 | 42 | import org.eclipse.osgi.util.NLS; |
76fccfb0 MK |
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; | |
371536f0 | 48 | import org.eclipse.ui.IEditorReference; |
eb271b88 | 49 | import org.eclipse.ui.IReusableEditor; |
76fccfb0 MK |
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; | |
56 | ||
57 | /** | |
58 | * Open trace helper | |
59 | * | |
60 | * Helper class for opening trace resources and loading them to a tracing | |
61 | * project. | |
62 | * | |
63 | * @author Matthew Khouzam | |
64 | * @since 2.1 | |
65 | */ | |
66 | public class TmfOpenTraceHelper { | |
67 | ||
977ca87f PT |
68 | private TmfOpenTraceHelper() { |
69 | } | |
70 | ||
76fccfb0 MK |
71 | private static final String ENDL = System.getProperty("line.separator"); //$NON-NLS-1$ |
72 | ||
73 | /** | |
977ca87f PT |
74 | * Opens a trace from a path while importing it to the destination folder. |
75 | * The trace is linked as a resource. | |
76fccfb0 | 76 | * |
977ca87f PT |
77 | * @param destinationFolder |
78 | * The destination trace folder | |
76fccfb0 MK |
79 | * @param path |
80 | * the file to import | |
81 | * @param shell | |
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 | |
86 | * end | |
977ca87f | 87 | * @since 3.0 |
76fccfb0 | 88 | */ |
977ca87f PT |
89 | public static IStatus openTraceFromPath(TmfTraceFolder destinationFolder, String path, Shell shell) throws CoreException { |
90 | return openTraceFromPath(destinationFolder, path, shell, null); | |
4958a213 MK |
91 | } |
92 | ||
93 | /** | |
977ca87f PT |
94 | * Opens a trace from a path while importing it to the destination folder. |
95 | * The trace is linked as a resource. | |
4958a213 | 96 | * |
977ca87f PT |
97 | * @param destinationFolder |
98 | * The destination trace folder | |
4958a213 MK |
99 | * @param path |
100 | * the file to import | |
101 | * @param shell | |
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 | |
108 | * end | |
109 | * | |
977ca87f | 110 | * @since 3.0 |
4958a213 | 111 | */ |
977ca87f | 112 | public static IStatus openTraceFromPath(TmfTraceFolder destinationFolder, String path, Shell shell, String tracetypeHint) throws CoreException { |
1aec2e92 | 113 | final String pathToUse = checkTracePath(path); |
76fccfb0 MK |
114 | TraceTypeHelper traceTypeToSet = null; |
115 | try { | |
1aec2e92 | 116 | traceTypeToSet = TmfTraceTypeUIUtils.selectTraceType(pathToUse, null, tracetypeHint); |
76fccfb0 MK |
117 | } catch (TmfTraceImportException e) { |
118 | MessageBox mb = new MessageBox(shell); | |
119 | mb.setMessage(e.getMessage()); | |
120 | mb.open(); | |
121 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage()); | |
122 | } | |
1aec2e92 | 123 | |
977ca87f | 124 | IFolder folder = destinationFolder.getResource(); |
1aec2e92 BH |
125 | String traceName = getTraceName(pathToUse, folder); |
126 | if (traceExists(pathToUse, folder)) { | |
977ca87f | 127 | return openTraceFromFolder(destinationFolder, traceName); |
76fccfb0 | 128 | } |
1aec2e92 | 129 | final IPath pathString = Path.fromOSString(pathToUse); |
76fccfb0 | 130 | IResource linkedTrace = TmfImportHelper.createLink(folder, pathString, traceName); |
d3e89107 BH |
131 | |
132 | if (linkedTrace == null || !linkedTrace.exists()) { | |
133 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, | |
134 | Messages.TmfOpenTraceHelper_LinkFailed); | |
135 | } | |
136 | ||
1aec2e92 | 137 | String sourceLocation = URIUtil.toUnencodedString(pathString.toFile().toURI()); |
89730b51 PT |
138 | linkedTrace.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); |
139 | ||
d3e89107 BH |
140 | // No trace type was determined. |
141 | if (traceTypeToSet == null) { | |
142 | return Status.OK_STATUS; | |
143 | } | |
144 | ||
a6e37e4c | 145 | IStatus ret = TmfTraceTypeUIUtils.setTraceType(linkedTrace, traceTypeToSet); |
d3e89107 | 146 | if (ret.isOK()) { |
977ca87f | 147 | ret = openTraceFromFolder(destinationFolder, traceName); |
76fccfb0 | 148 | } |
d3e89107 | 149 | return ret; |
76fccfb0 MK |
150 | } |
151 | ||
1aec2e92 BH |
152 | /** |
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. | |
156 | * | |
157 | * @param path | |
158 | * the path to check | |
159 | * @return path to use for trace type validation. | |
160 | */ | |
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(); | |
a4a116c3 | 167 | if (TmfTraceType.isDirectoryTrace(pathToUse)) { |
1aec2e92 BH |
168 | return pathToUse; |
169 | } | |
170 | // Second check grandparent | |
171 | File grandParent = parent.getParentFile(); | |
172 | if (grandParent != null) { | |
173 | pathToUse = grandParent.getAbsolutePath(); | |
a4a116c3 | 174 | if (TmfTraceType.isDirectoryTrace(pathToUse)) { |
1aec2e92 BH |
175 | return pathToUse; |
176 | } | |
177 | } | |
178 | } | |
179 | return path; | |
180 | } | |
181 | ||
977ca87f PT |
182 | private static boolean traceExists(String path, IFolder folder) { |
183 | String val = getTraceName(path, folder); | |
76fccfb0 MK |
184 | return (folder.findMember(val) != null); |
185 | } | |
186 | ||
977ca87f PT |
187 | private static boolean isWrongMember(IFolder folder, String name, final File traceFile) { |
188 | final IResource candidate = folder.findMember(name); | |
76fccfb0 MK |
189 | if (candidate != null) { |
190 | final IPath rawLocation = candidate.getRawLocation(); | |
191 | final File file = rawLocation.toFile(); | |
192 | return !file.equals(traceFile); | |
193 | } | |
194 | return false; | |
195 | } | |
196 | ||
197 | /** | |
198 | * Gets the display name, either "filename" or "filename(n)" if there is | |
977ca87f | 199 | * already a filename existing where n is the next unused integer starting |
76fccfb0 MK |
200 | * from 2 |
201 | * | |
977ca87f PT |
202 | * @param path |
203 | * the file path | |
76fccfb0 MK |
204 | * @param folder |
205 | * the folder to import to | |
206 | * @return the filename | |
207 | */ | |
977ca87f PT |
208 | private static String getTraceName(String path, IFolder folder) { |
209 | String name; | |
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 + ')'; | |
76fccfb0 | 214 | } |
977ca87f | 215 | return name; |
76fccfb0 MK |
216 | } |
217 | ||
218 | /** | |
977ca87f | 219 | * Open a trace from a trace folder |
76fccfb0 | 220 | * |
977ca87f PT |
221 | * @param destinationFolder |
222 | * The destination trace folder | |
76fccfb0 MK |
223 | * @param traceName |
224 | * the trace name | |
225 | * @return success or error | |
977ca87f | 226 | * @since 3.0 |
76fccfb0 | 227 | */ |
977ca87f PT |
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; | |
76fccfb0 MK |
234 | } |
235 | } | |
977ca87f | 236 | if (traceElement == null) { |
9d2e08b9 | 237 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(Messages.TmfOpenTraceHelper_TraceNotFound, traceName)); |
76fccfb0 | 238 | } |
977ca87f | 239 | openTraceFromElement(traceElement); |
67c53011 | 240 | return Status.OK_STATUS; |
76fccfb0 MK |
241 | } |
242 | ||
9d2e08b9 GB |
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()), | |
2d2397a6 | 248 | Messages.TmfOpenTraceHelper_NoTraceType); |
9d2e08b9 GB |
249 | if (trace != null) { |
250 | trace.dispose(); | |
251 | } | |
252 | return null; | |
253 | } | |
67c53011 | 254 | |
67c53011 | 255 | try { |
339d539c | 256 | trace.initTrace(traceElement.getResource(), traceElement.getLocation().getPath(), traceEvent.getClass(), traceElement.getElementPath()); |
9d2e08b9 GB |
257 | } catch (final TmfTraceException e) { |
258 | TraceUtils.displayErrorMsg(NLS.bind(Messages.TmfOpenTraceHelper_OpenElement, traceElement.getTypeName()), | |
259 | Messages.TmfOpenTraceHelper_InitError + ENDL + ENDL + e); | |
260 | trace.dispose(); | |
261 | return null; | |
67c53011 | 262 | } |
9d2e08b9 GB |
263 | return trace; |
264 | } | |
67c53011 | 265 | |
deaae6e1 | 266 | private static ITmfTrace openExperimentElement(final TmfExperimentElement experimentElement) { |
9d2e08b9 GB |
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())); | |
272 | return null; | |
67c53011 PT |
273 | } |
274 | ||
9d2e08b9 GB |
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); | |
76fccfb0 | 281 | |
9d2e08b9 GB |
282 | // Since trace is under an experiment, use the original trace from |
283 | // the traces folder | |
284 | element = element.getElementUnderTraceFolder(); | |
76fccfb0 | 285 | |
9d2e08b9 GB |
286 | ITmfTrace trace = openTraceElement(element); |
287 | ||
288 | if (trace == null) { | |
289 | for (int j = 0; j < i; j++) { | |
290 | traces[j].dispose(); | |
76fccfb0 | 291 | } |
9d2e08b9 GB |
292 | return null; |
293 | } | |
294 | cacheSize = Math.min(cacheSize, trace.getCacheSize()); | |
76fccfb0 | 295 | |
9d2e08b9 GB |
296 | traces[i] = trace; |
297 | } | |
76fccfb0 | 298 | |
9d2e08b9 GB |
299 | // Create the experiment |
300 | experiment.initExperiment(ITmfEvent.class, experimentElement.getName(), traces, cacheSize, experimentElement.getResource()); | |
9d2e08b9 GB |
301 | |
302 | return experiment; | |
303 | } | |
304 | ||
deaae6e1 | 305 | private static ITmfTrace openProjectElement(final TmfCommonProjectElement element) { |
9d2e08b9 GB |
306 | ITmfTrace trace = null; |
307 | if (element instanceof TmfTraceElement) { | |
308 | trace = openTraceElement((TmfTraceElement) element); | |
309 | } else if (element instanceof TmfExperimentElement) { | |
deaae6e1 | 310 | trace = openExperimentElement((TmfExperimentElement) element); |
9d2e08b9 GB |
311 | } |
312 | return trace; | |
67c53011 PT |
313 | } |
314 | ||
315 | /** | |
9d2e08b9 GB |
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. | |
67c53011 | 318 | * |
9d2e08b9 GB |
319 | * @param traceElement |
320 | * the {@link TmfTraceElement} to open | |
321 | * @since 3.0 | |
67c53011 | 322 | */ |
9d2e08b9 | 323 | public static void openTraceFromElement(final TmfCommonProjectElement traceElement) { |
67c53011 PT |
324 | |
325 | final IFile file; | |
326 | try { | |
9d2e08b9 | 327 | file = traceElement.createBookmarksFile(); |
67c53011 | 328 | } catch (final CoreException e) { |
9d2e08b9 GB |
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()); | |
67c53011 PT |
332 | return; |
333 | } | |
334 | ||
335 | final IWorkbench wb = PlatformUI.getWorkbench(); | |
336 | final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
371536f0 | 337 | final IEditorPart editor = findEditor(new FileEditorInput(file), true); |
67c53011 PT |
338 | if (editor != null) { |
339 | activePage.activate(editor); | |
340 | return; | |
341 | } | |
342 | ||
343 | Thread thread = new Thread() { | |
344 | @Override | |
345 | public void run() { | |
346 | ||
deaae6e1 | 347 | final ITmfTrace trace = openProjectElement(traceElement); |
9d2e08b9 GB |
348 | if (trace == null) { |
349 | return; | |
67c53011 PT |
350 | } |
351 | ||
9d2e08b9 GB |
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); | |
67c53011 PT |
356 | |
357 | Display.getDefault().asyncExec(new Runnable() { | |
358 | @Override | |
359 | public void run() { | |
360 | try { | |
361 | activePage.openEditor(editorInput, editorId); | |
362 | IDE.setDefaultEditor(file, editorId); | |
363 | // editor should dispose the trace on close | |
364 | } catch (final PartInitException e) { | |
9d2e08b9 GB |
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()); | |
368 | trace.dispose(); | |
67c53011 PT |
369 | } |
370 | } | |
371 | }); | |
372 | } | |
373 | }; | |
374 | thread.start(); | |
76fccfb0 MK |
375 | } |
376 | ||
371536f0 | 377 | /** |
4958a213 MK |
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. | |
381 | * | |
382 | * @param input | |
383 | * the editor input | |
384 | * @param restore | |
385 | * true if the editor should be restored | |
386 | * @return an editor with input equals to <code>input</code> | |
387 | */ | |
371536f0 PT |
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()) { | |
392 | try { | |
393 | IEditorInput editorInput = editorReference.getEditorInput(); | |
394 | if (editorInput.equals(input)) { | |
395 | return editorReference.getEditor(restore); | |
396 | } | |
397 | } catch (PartInitException e) { | |
398 | } | |
399 | } | |
400 | return null; | |
4958a213 | 401 | } |
371536f0 | 402 | |
eb271b88 | 403 | /** |
9d2e08b9 GB |
404 | * Reopen a trace or experiment from a project element in the provided |
405 | * editor | |
eb271b88 PT |
406 | * |
407 | * @param traceElement | |
408 | * the {@link TmfTraceElement} to open | |
409 | * @param editor | |
410 | * the reusable editor | |
9d2e08b9 | 411 | * @since 3.0 |
eb271b88 | 412 | */ |
9d2e08b9 | 413 | public static void reopenTraceFromElement(final TmfCommonProjectElement traceElement, final IReusableEditor editor) { |
eb271b88 PT |
414 | |
415 | final IFile file; | |
416 | try { | |
417 | file = traceElement.createBookmarksFile(); | |
418 | } catch (final CoreException e) { | |
9d2e08b9 GB |
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()); | |
eb271b88 PT |
422 | return; |
423 | } | |
424 | ||
425 | Thread thread = new Thread() { | |
426 | @Override | |
427 | public void run() { | |
428 | ||
deaae6e1 | 429 | final ITmfTrace trace = openProjectElement(traceElement); |
9d2e08b9 | 430 | if (trace == null) { |
eb271b88 PT |
431 | return; |
432 | } | |
433 | ||
434 | final IEditorInput editorInput = new TmfEditorInput(file, trace); | |
435 | ||
436 | Display.getDefault().asyncExec(new Runnable() { | |
437 | @Override | |
438 | public void run() { | |
439 | final IWorkbench wb = PlatformUI.getWorkbench(); | |
440 | final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
441 | activePage.reuseEditor(editor, editorInput); | |
442 | activePage.activate(editor); | |
443 | } | |
444 | }); | |
445 | } | |
446 | }; | |
447 | thread.start(); | |
448 | } | |
449 | ||
76fccfb0 | 450 | } |