tmf: Log the exception when error occurs while opening a trace
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TmfOpenTraceHelper.java
CommitLineData
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
2bdf0193 16package org.eclipse.tracecompass.tmf.ui.project.model;
76fccfb0
MK
17
18import java.io.File;
b1f91ef7 19import java.io.IOException;
76fccfb0
MK
20import java.util.List;
21
22import org.eclipse.core.resources.IFile;
23import org.eclipse.core.resources.IFolder;
76fccfb0 24import org.eclipse.core.resources.IResource;
76fccfb0
MK
25import org.eclipse.core.runtime.CoreException;
26import org.eclipse.core.runtime.IPath;
27import org.eclipse.core.runtime.IStatus;
28import org.eclipse.core.runtime.Path;
29import org.eclipse.core.runtime.Status;
89730b51 30import org.eclipse.core.runtime.URIUtil;
09b9832c 31import org.eclipse.jface.util.OpenStrategy;
9d2e08b9 32import org.eclipse.osgi.util.NLS;
76fccfb0
MK
33import org.eclipse.swt.widgets.Display;
34import org.eclipse.swt.widgets.MessageBox;
35import org.eclipse.swt.widgets.Shell;
2bdf0193
AM
36import org.eclipse.tracecompass.internal.tmf.ui.Activator;
37import org.eclipse.tracecompass.internal.tmf.ui.project.model.TmfImportHelper;
38import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
39import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
40import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
41import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceImportException;
42import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
43import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
44import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
5c5fa260 45import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
2bdf0193
AM
46import org.eclipse.tracecompass.tmf.ui.editors.TmfEditorInput;
47import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
76fccfb0
MK
48import org.eclipse.ui.IEditorInput;
49import org.eclipse.ui.IEditorPart;
371536f0 50import org.eclipse.ui.IEditorReference;
eb271b88 51import org.eclipse.ui.IReusableEditor;
76fccfb0
MK
52import org.eclipse.ui.IWorkbench;
53import org.eclipse.ui.IWorkbenchPage;
54import org.eclipse.ui.PartInitException;
55import org.eclipse.ui.PlatformUI;
56import org.eclipse.ui.ide.IDE;
57import org.eclipse.ui.part.FileEditorInput;
58
59/**
60 * Open trace helper
61 *
62 * Helper class for opening trace resources and loading them to a tracing
63 * project.
64 *
65 * @author Matthew Khouzam
76fccfb0
MK
66 */
67public class TmfOpenTraceHelper {
68
977ca87f
PT
69 private TmfOpenTraceHelper() {
70 }
71
76fccfb0
MK
72 private static final String ENDL = System.getProperty("line.separator"); //$NON-NLS-1$
73
74 /**
977ca87f
PT
75 * Opens a trace from a path while importing it to the destination folder.
76 * The trace is linked as a resource.
76fccfb0 77 *
977ca87f
PT
78 * @param destinationFolder
79 * The destination trace folder
76fccfb0
MK
80 * @param path
81 * the file to import
82 * @param shell
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
87 * end
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
4958a213 109 */
977ca87f 110 public static IStatus openTraceFromPath(TmfTraceFolder destinationFolder, String path, Shell shell, String tracetypeHint) throws CoreException {
1aec2e92 111 final String pathToUse = checkTracePath(path);
76fccfb0
MK
112 TraceTypeHelper traceTypeToSet = null;
113 try {
1aec2e92 114 traceTypeToSet = TmfTraceTypeUIUtils.selectTraceType(pathToUse, null, tracetypeHint);
76fccfb0
MK
115 } catch (TmfTraceImportException e) {
116 MessageBox mb = new MessageBox(shell);
117 mb.setMessage(e.getMessage());
118 mb.open();
119 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage());
120 }
1aec2e92 121
977ca87f 122 IFolder folder = destinationFolder.getResource();
1aec2e92
BH
123 String traceName = getTraceName(pathToUse, folder);
124 if (traceExists(pathToUse, folder)) {
977ca87f 125 return openTraceFromFolder(destinationFolder, traceName);
76fccfb0 126 }
1aec2e92 127 final IPath pathString = Path.fromOSString(pathToUse);
76fccfb0 128 IResource linkedTrace = TmfImportHelper.createLink(folder, pathString, traceName);
d3e89107
BH
129
130 if (linkedTrace == null || !linkedTrace.exists()) {
131 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
132 Messages.TmfOpenTraceHelper_LinkFailed);
133 }
134
1aec2e92 135 String sourceLocation = URIUtil.toUnencodedString(pathString.toFile().toURI());
89730b51
PT
136 linkedTrace.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation);
137
d3e89107
BH
138 // No trace type was determined.
139 if (traceTypeToSet == null) {
140 return Status.OK_STATUS;
141 }
142
a6e37e4c 143 IStatus ret = TmfTraceTypeUIUtils.setTraceType(linkedTrace, traceTypeToSet);
d3e89107 144 if (ret.isOK()) {
977ca87f 145 ret = openTraceFromFolder(destinationFolder, traceName);
76fccfb0 146 }
d3e89107 147 return ret;
76fccfb0
MK
148 }
149
1aec2e92
BH
150 /**
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.
154 *
155 * @param path
156 * the path to check
157 * @return path to use for trace type validation.
158 */
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();
a4a116c3 165 if (TmfTraceType.isDirectoryTrace(pathToUse)) {
1aec2e92
BH
166 return pathToUse;
167 }
168 // Second check grandparent
169 File grandParent = parent.getParentFile();
170 if (grandParent != null) {
171 pathToUse = grandParent.getAbsolutePath();
a4a116c3 172 if (TmfTraceType.isDirectoryTrace(pathToUse)) {
1aec2e92
BH
173 return pathToUse;
174 }
175 }
176 }
177 return path;
178 }
179
977ca87f
PT
180 private static boolean traceExists(String path, IFolder folder) {
181 String val = getTraceName(path, folder);
76fccfb0
MK
182 return (folder.findMember(val) != null);
183 }
184
977ca87f
PT
185 private static boolean isWrongMember(IFolder folder, String name, final File traceFile) {
186 final IResource candidate = folder.findMember(name);
76fccfb0
MK
187 if (candidate != null) {
188 final IPath rawLocation = candidate.getRawLocation();
f278fbd4
MAL
189 File file = rawLocation.toFile();
190 try {
8f786919 191 file = file.getCanonicalFile();
f278fbd4
MAL
192 } catch (IOException e) {
193 /* just use original file path */
194 }
76fccfb0
MK
195 return !file.equals(traceFile);
196 }
197 return false;
198 }
199
200 /**
201 * Gets the display name, either "filename" or "filename(n)" if there is
977ca87f 202 * already a filename existing where n is the next unused integer starting
76fccfb0
MK
203 * from 2
204 *
977ca87f
PT
205 * @param path
206 * the file path
76fccfb0
MK
207 * @param folder
208 * the folder to import to
209 * @return the filename
210 */
977ca87f
PT
211 private static String getTraceName(String path, IFolder folder) {
212 String name;
b1f91ef7
PT
213 File traceFile = new File(path);
214 try {
215 traceFile = traceFile.getCanonicalFile();
216 } catch (IOException e) {
217 /* just use original file path */
218 }
977ca87f
PT
219 name = traceFile.getName();
220 for (int i = 2; isWrongMember(folder, name, traceFile); i++) {
221 name = traceFile.getName() + '(' + i + ')';
76fccfb0 222 }
977ca87f 223 return name;
76fccfb0
MK
224 }
225
226 /**
977ca87f 227 * Open a trace from a trace folder
76fccfb0 228 *
977ca87f
PT
229 * @param destinationFolder
230 * The destination trace folder
76fccfb0
MK
231 * @param traceName
232 * the trace name
233 * @return success or error
234 */
977ca87f
PT
235 private static IStatus openTraceFromFolder(TmfTraceFolder destinationFolder, String traceName) {
236 final List<ITmfProjectModelElement> elements = destinationFolder.getChildren();
237 TmfTraceElement traceElement = null;
238 for (ITmfProjectModelElement element : elements) {
239 if (element instanceof TmfTraceElement && element.getName().equals(traceName)) {
240 traceElement = (TmfTraceElement) element;
76fccfb0
MK
241 }
242 }
977ca87f 243 if (traceElement == null) {
9d2e08b9 244 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(Messages.TmfOpenTraceHelper_TraceNotFound, traceName));
76fccfb0 245 }
977ca87f 246 openTraceFromElement(traceElement);
67c53011 247 return Status.OK_STATUS;
76fccfb0
MK
248 }
249
9d2e08b9
GB
250 private static ITmfTrace openTraceElement(final TmfTraceElement traceElement) {
251 final ITmfTrace trace = traceElement.instantiateTrace();
252 final ITmfEvent traceEvent = traceElement.instantiateEvent();
253 if ((trace == null) || (traceEvent == null)) {
254 TraceUtils.displayErrorMsg(NLS.bind(Messages.TmfOpenTraceHelper_OpenElement, traceElement.getTypeName()),
2d2397a6 255 Messages.TmfOpenTraceHelper_NoTraceType);
9d2e08b9
GB
256 if (trace != null) {
257 trace.dispose();
258 }
259 return null;
260 }
67c53011 261
67c53011 262 try {
2b0005f0 263 trace.initTrace(traceElement.getResource(), traceElement.getResource().getLocation().toOSString(), traceEvent.getClass(), traceElement.getElementPath(), traceElement.getTraceType());
9d2e08b9
GB
264 } catch (final TmfTraceException e) {
265 TraceUtils.displayErrorMsg(NLS.bind(Messages.TmfOpenTraceHelper_OpenElement, traceElement.getTypeName()),
c0550011 266 Messages.TmfOpenTraceHelper_InitError + ENDL + ENDL + e, e);
9d2e08b9
GB
267 trace.dispose();
268 return null;
67c53011 269 }
9d2e08b9
GB
270 return trace;
271 }
67c53011 272
deaae6e1 273 private static ITmfTrace openExperimentElement(final TmfExperimentElement experimentElement) {
9d2e08b9
GB
274 /* Experiment element now has an experiment type associated with it */
275 final TmfExperiment experiment = experimentElement.instantiateTrace();
276 if (experiment == null) {
277 TraceUtils.displayErrorMsg(NLS.bind(Messages.TmfOpenTraceHelper_OpenElement, experimentElement.getTypeName()),
278 NLS.bind(Messages.TmfOpenTraceHelper_NoTraceOrExperimentType, experimentElement.getTypeName()));
279 return null;
67c53011
PT
280 }
281
9d2e08b9
GB
282 // Instantiate the experiment's traces
283 final List<TmfTraceElement> traceEntries = experimentElement.getTraces();
284 int cacheSize = Integer.MAX_VALUE;
285 final ITmfTrace[] traces = new ITmfTrace[traceEntries.size()];
286 for (int i = 0; i < traceEntries.size(); i++) {
287 TmfTraceElement element = traceEntries.get(i);
76fccfb0 288
9d2e08b9
GB
289 // Since trace is under an experiment, use the original trace from
290 // the traces folder
291 element = element.getElementUnderTraceFolder();
76fccfb0 292
9d2e08b9
GB
293 ITmfTrace trace = openTraceElement(element);
294
295 if (trace == null) {
296 for (int j = 0; j < i; j++) {
297 traces[j].dispose();
76fccfb0 298 }
9d2e08b9
GB
299 return null;
300 }
301 cacheSize = Math.min(cacheSize, trace.getCacheSize());
76fccfb0 302
9d2e08b9
GB
303 traces[i] = trace;
304 }
76fccfb0 305
9d2e08b9
GB
306 // Create the experiment
307 experiment.initExperiment(ITmfEvent.class, experimentElement.getName(), traces, cacheSize, experimentElement.getResource());
9d2e08b9
GB
308
309 return experiment;
310 }
311
deaae6e1 312 private static ITmfTrace openProjectElement(final TmfCommonProjectElement element) {
9d2e08b9
GB
313 ITmfTrace trace = null;
314 if (element instanceof TmfTraceElement) {
315 trace = openTraceElement((TmfTraceElement) element);
316 } else if (element instanceof TmfExperimentElement) {
deaae6e1 317 trace = openExperimentElement((TmfExperimentElement) element);
9d2e08b9
GB
318 }
319 return trace;
67c53011
PT
320 }
321
322 /**
9d2e08b9
GB
323 * Open a trace (or experiment) from a project element. If the trace is already opened, its
324 * editor is activated and brought to top.
67c53011 325 *
9d2e08b9
GB
326 * @param traceElement
327 * the {@link TmfTraceElement} to open
67c53011 328 */
9d2e08b9 329 public static void openTraceFromElement(final TmfCommonProjectElement traceElement) {
67c53011
PT
330
331 final IFile file;
332 try {
9d2e08b9 333 file = traceElement.createBookmarksFile();
67c53011 334 } catch (final CoreException e) {
9d2e08b9
GB
335 Activator.getDefault().logError(NLS.bind(Messages.TmfOpenTraceHelper_ErrorOpeningElement, traceElement.getTypeName()) + ' ' + traceElement.getName());
336 TraceUtils.displayErrorMsg(NLS.bind(Messages.TmfOpenTraceHelper_OpenElement, traceElement.getTypeName()),
337 NLS.bind(Messages.TmfOpenTraceHelper_ErrorElement, traceElement.getTypeName()) + ENDL + ENDL + e.getMessage());
67c53011
PT
338 return;
339 }
340
341 final IWorkbench wb = PlatformUI.getWorkbench();
342 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
371536f0 343 final IEditorPart editor = findEditor(new FileEditorInput(file), true);
67c53011
PT
344 if (editor != null) {
345 activePage.activate(editor);
346 return;
347 }
348
09b9832c
BH
349 // If a trace type is not set then delegate it to the eclipse platform
350 if ((traceElement instanceof TmfTraceElement) && (traceElement.getResource() instanceof IFile) && (traceElement.getTraceType() == null)) {
351 try {
352 boolean activate = OpenStrategy.activateOnOpen();
353 // only local open is supported
354 IDE.openEditor(activePage, file, activate);
355 } catch (PartInitException e) {
356 TraceUtils.displayErrorMsg(NLS.bind(Messages.TmfOpenTraceHelper_OpenElement, traceElement.getTypeName()),
357 NLS.bind(Messages.TmfOpenTraceHelper_ErrorOpeningElement, traceElement.getElementPath()) + ENDL + ENDL + e.getMessage());
358 }
359 return;
360 }
361
67c53011
PT
362 Thread thread = new Thread() {
363 @Override
364 public void run() {
deaae6e1 365 final ITmfTrace trace = openProjectElement(traceElement);
09b9832c 366
9d2e08b9
GB
367 if (trace == null) {
368 return;
67c53011
PT
369 }
370
9d2e08b9
GB
371 // Get the editor id from the extension point
372 String traceEditorId = traceElement.getEditorId();
373 final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID;
374 final IEditorInput editorInput = new TmfEditorInput(file, trace);
67c53011
PT
375
376 Display.getDefault().asyncExec(new Runnable() {
377 @Override
378 public void run() {
379 try {
380 activePage.openEditor(editorInput, editorId);
381 IDE.setDefaultEditor(file, editorId);
382 // editor should dispose the trace on close
383 } catch (final PartInitException e) {
9d2e08b9
GB
384 TraceUtils.displayErrorMsg(NLS.bind(Messages.TmfOpenTraceHelper_OpenElement, traceElement.getTypeName()),
385 NLS.bind(Messages.TmfOpenTraceHelper_ErrorOpeningElement, traceElement.getTypeName()) + ENDL + ENDL + e.getMessage());
386 Activator.getDefault().logError(NLS.bind(Messages.TmfOpenTraceHelper_ErrorOpeningElement, traceElement.getTypeName()) + ' ' + traceElement.getName());
387 trace.dispose();
67c53011
PT
388 }
389 }
390 });
391 }
392 };
393 thread.start();
76fccfb0
MK
394 }
395
371536f0 396 /**
4958a213
MK
397 * Returns the editor with the specified input. Returns null if there is no
398 * opened editor with that input. If restore is requested, the method finds
399 * and returns the editor even if it is not restored yet after a restart.
400 *
401 * @param input
402 * the editor input
403 * @param restore
404 * true if the editor should be restored
405 * @return an editor with input equals to <code>input</code>
406 */
371536f0
PT
407 private static IEditorPart findEditor(IEditorInput input, boolean restore) {
408 final IWorkbench wb = PlatformUI.getWorkbench();
409 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
410 for (IEditorReference editorReference : activePage.getEditorReferences()) {
411 try {
412 IEditorInput editorInput = editorReference.getEditorInput();
413 if (editorInput.equals(input)) {
414 return editorReference.getEditor(restore);
415 }
416 } catch (PartInitException e) {
417 }
418 }
419 return null;
4958a213 420 }
371536f0 421
eb271b88 422 /**
9d2e08b9
GB
423 * Reopen a trace or experiment from a project element in the provided
424 * editor
eb271b88
PT
425 *
426 * @param traceElement
427 * the {@link TmfTraceElement} to open
428 * @param editor
429 * the reusable editor
430 */
9d2e08b9 431 public static void reopenTraceFromElement(final TmfCommonProjectElement traceElement, final IReusableEditor editor) {
eb271b88
PT
432
433 final IFile file;
434 try {
435 file = traceElement.createBookmarksFile();
436 } catch (final CoreException e) {
9d2e08b9
GB
437 Activator.getDefault().logError(NLS.bind(Messages.TmfOpenTraceHelper_ErrorOpeningElement, traceElement.getTypeName()) + ' ' + traceElement.getName());
438 TraceUtils.displayErrorMsg(NLS.bind(Messages.TmfOpenTraceHelper_OpenElement, traceElement.getTypeName()),
439 NLS.bind(Messages.TmfOpenTraceHelper_ErrorElement, traceElement.getTypeName()) + ENDL + ENDL + e.getMessage());
eb271b88
PT
440 return;
441 }
442
443 Thread thread = new Thread() {
444 @Override
445 public void run() {
446
deaae6e1 447 final ITmfTrace trace = openProjectElement(traceElement);
9d2e08b9 448 if (trace == null) {
eb271b88
PT
449 return;
450 }
451
452 final IEditorInput editorInput = new TmfEditorInput(file, trace);
453
454 Display.getDefault().asyncExec(new Runnable() {
455 @Override
456 public void run() {
457 final IWorkbench wb = PlatformUI.getWorkbench();
458 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
459 activePage.reuseEditor(editor, editorInput);
460 activePage.activate(editor);
461 }
462 });
463 }
464 };
465 thread.start();
466 }
467
76fccfb0 468}
This page took 0.108658 seconds and 5 git commands to generate.