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