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