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