| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2014 Ericsson |
| 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 | * Patrick Tasse - Initial API and implementation |
| 11 | * Patrick Tasse - Add support for DROP_LINK and rename prompt on name clash |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.internal.tmf.ui.project.handlers; |
| 15 | |
| 16 | import java.io.File; |
| 17 | import java.io.FileInputStream; |
| 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | import java.util.Arrays; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.eclipse.core.resources.IContainer; |
| 25 | import org.eclipse.core.resources.IFile; |
| 26 | import org.eclipse.core.resources.IFolder; |
| 27 | import org.eclipse.core.resources.IProject; |
| 28 | import org.eclipse.core.resources.IResource; |
| 29 | import org.eclipse.core.resources.IWorkspace; |
| 30 | import org.eclipse.core.resources.ResourcesPlugin; |
| 31 | import org.eclipse.core.runtime.CoreException; |
| 32 | import org.eclipse.core.runtime.IPath; |
| 33 | import org.eclipse.core.runtime.IProgressMonitor; |
| 34 | import org.eclipse.core.runtime.IStatus; |
| 35 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 36 | import org.eclipse.core.runtime.Path; |
| 37 | import org.eclipse.core.runtime.Status; |
| 38 | import org.eclipse.core.runtime.URIUtil; |
| 39 | import org.eclipse.jface.operation.IRunnableWithProgress; |
| 40 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 41 | import org.eclipse.osgi.util.NLS; |
| 42 | import org.eclipse.swt.SWT; |
| 43 | import org.eclipse.swt.dnd.DND; |
| 44 | import org.eclipse.swt.dnd.DropTargetEvent; |
| 45 | import org.eclipse.swt.dnd.FileTransfer; |
| 46 | import org.eclipse.swt.dnd.TransferData; |
| 47 | import org.eclipse.swt.widgets.MessageBox; |
| 48 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
| 49 | import org.eclipse.tracecompass.internal.tmf.ui.editors.ITmfEventsEditorConstants; |
| 50 | import org.eclipse.tracecompass.tmf.core.TmfCommonConstants; |
| 51 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceImportException; |
| 52 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType; |
| 53 | import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper; |
| 54 | import org.eclipse.tracecompass.tmf.ui.project.model.ITmfProjectModelElement; |
| 55 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement; |
| 56 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement; |
| 57 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry; |
| 58 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement; |
| 59 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; |
| 60 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils; |
| 61 | import org.eclipse.ui.PlatformUI; |
| 62 | import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation; |
| 63 | import org.eclipse.ui.dialogs.IOverwriteQuery; |
| 64 | import org.eclipse.ui.navigator.CommonDropAdapter; |
| 65 | import org.eclipse.ui.navigator.CommonDropAdapterAssistant; |
| 66 | import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; |
| 67 | import org.eclipse.ui.wizards.datatransfer.ImportOperation; |
| 68 | |
| 69 | /** |
| 70 | * Drop adapter assistant for project explorer |
| 71 | */ |
| 72 | public class DropAdapterAssistant extends CommonDropAdapterAssistant { |
| 73 | |
| 74 | /** |
| 75 | * Default constructor |
| 76 | */ |
| 77 | public DropAdapterAssistant() { |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean isSupportedType(TransferData aTransferType) { |
| 82 | return super.isSupportedType(aTransferType) || FileTransfer.getInstance().isSupportedType(aTransferType); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public IStatus validateDrop(Object target, int operation, TransferData transferType) { |
| 87 | if (target instanceof TmfTraceFolder) { |
| 88 | return Status.OK_STATUS; |
| 89 | } |
| 90 | if (target instanceof TmfExperimentElement) { |
| 91 | return Status.OK_STATUS; |
| 92 | } |
| 93 | if (target instanceof TmfTraceElement) { |
| 94 | ITmfProjectModelElement parent = ((TmfTraceElement) target).getParent(); |
| 95 | if (parent instanceof TmfTraceFolder) { |
| 96 | return Status.OK_STATUS; |
| 97 | } |
| 98 | if (parent instanceof TmfExperimentElement) { |
| 99 | return Status.OK_STATUS; |
| 100 | } |
| 101 | } |
| 102 | if (target instanceof IProject) { |
| 103 | return Status.OK_STATUS; |
| 104 | } |
| 105 | return Status.CANCEL_STATUS; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public IStatus handleDrop(CommonDropAdapter aDropAdapter, DropTargetEvent aDropTargetEvent, Object aTarget) { |
| 110 | boolean ok = false; |
| 111 | |
| 112 | // Use local variable to avoid parameter assignment |
| 113 | Object targetToUse = aTarget; |
| 114 | |
| 115 | int operation = aDropTargetEvent.detail; |
| 116 | if (operation != DND.DROP_LINK) { |
| 117 | operation = DND.DROP_COPY; |
| 118 | } |
| 119 | |
| 120 | // If target is a trace, use its parent (either trace folder or experiment) |
| 121 | if (targetToUse instanceof TmfTraceElement) { |
| 122 | targetToUse = ((TmfTraceElement) targetToUse).getParent(); |
| 123 | } |
| 124 | |
| 125 | // If target is a project, use its trace folder |
| 126 | if (targetToUse instanceof IProject) { |
| 127 | TmfProjectElement projectElement = TmfProjectRegistry.getProject((IProject) targetToUse, true); |
| 128 | if (projectElement != null) { |
| 129 | targetToUse = projectElement.getTracesFolder(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (aDropTargetEvent.data instanceof IStructuredSelection) { |
| 134 | IStructuredSelection selection = (IStructuredSelection) aDropTargetEvent.data; |
| 135 | for (Object source : selection.toArray()) { |
| 136 | if (source instanceof IResource) { |
| 137 | // If source resource is a trace, use the trace element |
| 138 | IResource sourceResource = (IResource) source; |
| 139 | TmfProjectElement projectElement = TmfProjectRegistry.getProject(sourceResource.getProject()); |
| 140 | if (projectElement != null && projectElement.getTracesFolder() != null) { |
| 141 | for (TmfTraceElement trace : projectElement.getTracesFolder().getTraces()) { |
| 142 | if (trace.getResource().equals(sourceResource)) { |
| 143 | source = trace; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | if (source instanceof TmfTraceElement) { |
| 150 | TmfTraceElement sourceTrace = (TmfTraceElement) source; |
| 151 | // If source trace is under an experiment, use the original trace from the traces folder |
| 152 | sourceTrace = sourceTrace.getElementUnderTraceFolder(); |
| 153 | if (targetToUse instanceof TmfExperimentElement) { |
| 154 | TmfExperimentElement targetExperiment = (TmfExperimentElement) targetToUse; |
| 155 | ok |= drop(sourceTrace, targetExperiment, operation); |
| 156 | } else if (targetToUse instanceof TmfTraceFolder) { |
| 157 | TmfTraceFolder traceFolder = (TmfTraceFolder) targetToUse; |
| 158 | ok |= drop(sourceTrace, traceFolder, operation); |
| 159 | } |
| 160 | } else if (source instanceof IResource) { |
| 161 | IResource sourceResource = (IResource) source; |
| 162 | if (sourceResource.getType() != IResource.FILE && sourceResource.getType() != IResource.FOLDER) { |
| 163 | continue; |
| 164 | } |
| 165 | if (targetToUse instanceof TmfExperimentElement) { |
| 166 | TmfExperimentElement targetExperiment = (TmfExperimentElement) targetToUse; |
| 167 | ok |= (drop(sourceResource, targetExperiment, operation) != null); |
| 168 | } else if (targetToUse instanceof TmfTraceFolder) { |
| 169 | TmfTraceFolder traceFolder = (TmfTraceFolder) targetToUse; |
| 170 | ok |= (drop(sourceResource, traceFolder, operation) != null); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } else if (aDropTargetEvent.data instanceof String[]) { |
| 175 | String[] sources = (String[]) aDropTargetEvent.data; |
| 176 | for (String source : sources) { |
| 177 | Path path = new Path(source); |
| 178 | if (targetToUse instanceof TmfExperimentElement) { |
| 179 | TmfExperimentElement targetExperiment = (TmfExperimentElement) targetToUse; |
| 180 | ok |= drop(path, targetExperiment, operation); |
| 181 | } else if (targetToUse instanceof TmfTraceFolder) { |
| 182 | TmfTraceFolder traceFolder = (TmfTraceFolder) targetToUse; |
| 183 | ok |= drop(path, traceFolder, operation); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | return (ok ? Status.OK_STATUS : Status.CANCEL_STATUS); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Drop a trace by copying/linking a trace element in a target experiment |
| 193 | * |
| 194 | * @param sourceTrace the source trace element to copy |
| 195 | * @param targetExperiment the target experiment |
| 196 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
| 197 | * @return true if successful |
| 198 | */ |
| 199 | private static boolean drop(TmfTraceElement sourceTrace, |
| 200 | TmfExperimentElement targetExperiment, |
| 201 | int operation) { |
| 202 | |
| 203 | IResource sourceResource = sourceTrace.getResource(); |
| 204 | IResource targetResource = drop(sourceResource, targetExperiment, operation); |
| 205 | |
| 206 | if (targetResource != null) { |
| 207 | if (! sourceTrace.getProject().equals(targetExperiment.getProject())) { |
| 208 | IFolder destinationSupplementaryFolder = targetExperiment.getTraceSupplementaryFolder(targetResource.getName()); |
| 209 | sourceTrace.copySupplementaryFolder(destinationSupplementaryFolder); |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Drop a trace by copying/linking a resource in a target experiment |
| 218 | * |
| 219 | * @param sourceResource the source resource |
| 220 | * @param targetExperiment the target experiment |
| 221 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
| 222 | * @return the target resource or null if unsuccessful |
| 223 | */ |
| 224 | private static IResource drop(IResource sourceResource, |
| 225 | TmfExperimentElement targetExperiment, |
| 226 | int operation) { |
| 227 | |
| 228 | IResource traceResource = sourceResource; |
| 229 | |
| 230 | IPath tracesFolderPath = targetExperiment.getProject().getTracesFolder().getPath(); |
| 231 | if (tracesFolderPath.isPrefixOf(sourceResource.getFullPath())) { |
| 232 | String elementPath = sourceResource.getFullPath().makeRelativeTo(tracesFolderPath).toString(); |
| 233 | for (TmfTraceElement trace : targetExperiment.getTraces()) { |
| 234 | if (trace.getElementPath().equals(elementPath)) { |
| 235 | return null; |
| 236 | } |
| 237 | } |
| 238 | } else { |
| 239 | String targetName = sourceResource.getName(); |
| 240 | for (ITmfProjectModelElement element : targetExperiment.getProject().getTracesFolder().getChildren()) { |
| 241 | if (element.getName().equals(targetName)) { |
| 242 | targetName = promptRename(element); |
| 243 | if (targetName == null) { |
| 244 | return null; |
| 245 | } |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | try { |
| 250 | if (operation == DND.DROP_COPY && !sourceResource.isLinked()) { |
| 251 | IPath destination = targetExperiment.getProject().getTracesFolder().getResource().getFullPath().addTrailingSeparator().append(targetName); |
| 252 | sourceResource.copy(destination, false, null); |
| 253 | cleanupBookmarks(destination); |
| 254 | } else { |
| 255 | createLink(targetExperiment.getProject().getTracesFolder().getResource(), sourceResource, targetName); |
| 256 | } |
| 257 | // use the copied resource for the experiment |
| 258 | if (sourceResource.getType() == IResource.FILE) { |
| 259 | traceResource = targetExperiment.getProject().getTracesFolder().getResource().getFile(targetName); |
| 260 | } else if (sourceResource.getType() == IResource.FOLDER) { |
| 261 | traceResource = targetExperiment.getProject().getTracesFolder().getResource().getFolder(targetName); |
| 262 | } |
| 263 | String sourceLocation = sourceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION); |
| 264 | if (sourceLocation == null) { |
| 265 | sourceLocation = URIUtil.toUnencodedString(new File(sourceResource.getLocationURI()).toURI()); |
| 266 | } |
| 267 | traceResource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); |
| 268 | } catch (CoreException e) { |
| 269 | displayException(e); |
| 270 | return null; |
| 271 | } |
| 272 | } |
| 273 | if (traceResource != null && traceResource.exists()) { |
| 274 | setTraceType(traceResource); |
| 275 | for (TmfTraceElement trace : targetExperiment.getProject().getTracesFolder().getTraces()) { |
| 276 | if (trace.getResource().equals(traceResource)) { |
| 277 | targetExperiment.addTrace(trace); |
| 278 | targetExperiment.closeEditors(); |
| 279 | targetExperiment.deleteSupplementaryResources(); |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | return traceResource; |
| 284 | } |
| 285 | return null; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Drop a trace by copying/linking a trace element in a trace folder |
| 290 | * |
| 291 | * @param sourceTrace the source trace |
| 292 | * @param traceFolder the target trace folder |
| 293 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
| 294 | * @return true if successful |
| 295 | */ |
| 296 | private static boolean drop(TmfTraceElement sourceTrace, |
| 297 | TmfTraceFolder traceFolder, |
| 298 | int operation) { |
| 299 | |
| 300 | IResource sourceResource = sourceTrace.getResource(); |
| 301 | IResource targetResource = drop(sourceResource, traceFolder, operation); |
| 302 | |
| 303 | if (targetResource != null) { |
| 304 | String elementPath = targetResource.getFullPath().makeRelativeTo(traceFolder.getProject().getTracesFolder().getPath()).toString(); |
| 305 | IFolder destinationSupplementaryFolder = traceFolder.getTraceSupplementaryFolder(elementPath); |
| 306 | sourceTrace.copySupplementaryFolder(destinationSupplementaryFolder); |
| 307 | return true; |
| 308 | } |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Drop a trace by copying/linking a resource in a trace folder |
| 314 | * |
| 315 | * @param sourceResource the source resource |
| 316 | * @param traceFolder the target trace folder |
| 317 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
| 318 | * @return the target resource or null if unsuccessful |
| 319 | */ |
| 320 | private static IResource drop(IResource sourceResource, |
| 321 | TmfTraceFolder traceFolder, |
| 322 | int operation) { |
| 323 | |
| 324 | if (sourceResource.getParent().equals(traceFolder.getResource())) { |
| 325 | return null; |
| 326 | } |
| 327 | String targetName = sourceResource.getName(); |
| 328 | for (ITmfProjectModelElement element : traceFolder.getChildren()) { |
| 329 | if (element.getName().equals(targetName)) { |
| 330 | targetName = promptRename(element); |
| 331 | if (targetName == null) { |
| 332 | return null; |
| 333 | } |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | try { |
| 338 | if (operation == DND.DROP_COPY && !sourceResource.isLinked()) { |
| 339 | IPath destination = traceFolder.getResource().getFullPath().addTrailingSeparator().append(targetName); |
| 340 | sourceResource.copy(destination, false, null); |
| 341 | cleanupBookmarks(destination); |
| 342 | } else { |
| 343 | createLink(traceFolder.getResource(), sourceResource, targetName); |
| 344 | } |
| 345 | IResource traceResource = traceFolder.getResource().findMember(targetName); |
| 346 | if (traceResource != null && traceResource.exists()) { |
| 347 | String sourceLocation = sourceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION); |
| 348 | if (sourceLocation == null) { |
| 349 | sourceLocation = URIUtil.toUnencodedString(new File(sourceResource.getLocationURI()).toURI()); |
| 350 | } |
| 351 | traceResource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); |
| 352 | setTraceType(traceResource); |
| 353 | } |
| 354 | return traceResource; |
| 355 | } catch (CoreException e) { |
| 356 | displayException(e); |
| 357 | } |
| 358 | return null; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Drop a trace by importing/linking a path in a target experiment |
| 363 | * |
| 364 | * @param path the source path |
| 365 | * @param targetExperiment the target experiment |
| 366 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
| 367 | * @return true if successful |
| 368 | */ |
| 369 | private static boolean drop(Path path, |
| 370 | TmfExperimentElement targetExperiment, |
| 371 | int operation) { |
| 372 | |
| 373 | IPath tracesFolderPath = targetExperiment.getProject().getTracesFolder().getResource().getLocation(); |
| 374 | IResource traceResource = null; |
| 375 | if (tracesFolderPath.isPrefixOf(path)) { |
| 376 | String elementPath = path.makeRelativeTo(tracesFolderPath).toString(); |
| 377 | for (TmfTraceElement trace : targetExperiment.getTraces()) { |
| 378 | if (trace.getElementPath().equals(elementPath)) { |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | traceResource = targetExperiment.getProject().getTracesFolder().getResource().findMember(elementPath); |
| 383 | } else { |
| 384 | String targetName = path.lastSegment(); |
| 385 | for (ITmfProjectModelElement element : targetExperiment.getProject().getTracesFolder().getChildren()) { |
| 386 | if (element.getName().equals(targetName)) { |
| 387 | targetName = promptRename(element); |
| 388 | if (targetName == null) { |
| 389 | return false; |
| 390 | } |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | if (operation == DND.DROP_COPY) { |
| 395 | importTrace(targetExperiment.getProject().getTracesFolder().getResource(), path, targetName); |
| 396 | } else { |
| 397 | createLink(targetExperiment.getProject().getTracesFolder().getResource(), path, targetName); |
| 398 | } |
| 399 | // use the copied resource for the experiment |
| 400 | File file = new File(path.toString()); |
| 401 | if (file.exists() && file.isFile()) { |
| 402 | traceResource = targetExperiment.getProject().getTracesFolder().getResource().getFile(targetName); |
| 403 | } else if (file.exists() && file.isDirectory()) { |
| 404 | traceResource = targetExperiment.getProject().getTracesFolder().getResource().getFolder(targetName); |
| 405 | } |
| 406 | } |
| 407 | if (traceResource != null && traceResource.exists()) { |
| 408 | try { |
| 409 | String sourceLocation = URIUtil.toUnencodedString(path.toFile().toURI()); |
| 410 | traceResource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); |
| 411 | } catch (CoreException e) { |
| 412 | displayException(e); |
| 413 | } |
| 414 | setTraceType(traceResource); |
| 415 | for (TmfTraceElement trace : targetExperiment.getProject().getTracesFolder().getTraces()) { |
| 416 | if (trace.getResource().equals(traceResource)) { |
| 417 | targetExperiment.addTrace(trace); |
| 418 | targetExperiment.closeEditors(); |
| 419 | targetExperiment.deleteSupplementaryResources(); |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Drop a trace by importing/linking a path in a trace folder |
| 430 | * |
| 431 | * @param path the source path |
| 432 | * @param traceFolder the target trace folder |
| 433 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
| 434 | * @return true if successful |
| 435 | */ |
| 436 | private static boolean drop(Path path, |
| 437 | TmfTraceFolder traceFolder, |
| 438 | int operation) { |
| 439 | |
| 440 | String targetName = path.lastSegment(); |
| 441 | for (ITmfProjectModelElement element : traceFolder.getChildren()) { |
| 442 | if (element.getName().equals(targetName)) { |
| 443 | targetName = promptRename(element); |
| 444 | if (targetName == null) { |
| 445 | return false; |
| 446 | } |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | if (operation == DND.DROP_COPY) { |
| 451 | importTrace(traceFolder.getResource(), path, targetName); |
| 452 | } else { |
| 453 | createLink(traceFolder.getResource(), path, targetName); |
| 454 | } |
| 455 | IResource traceResource = traceFolder.getResource().findMember(targetName); |
| 456 | if (traceResource != null && traceResource.exists()) { |
| 457 | try { |
| 458 | String sourceLocation = URIUtil.toUnencodedString(path.toFile().toURI()); |
| 459 | traceResource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); |
| 460 | } catch (CoreException e) { |
| 461 | displayException(e); |
| 462 | } |
| 463 | setTraceType(traceResource); |
| 464 | } |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Import a trace to the trace folder |
| 470 | * |
| 471 | * @param folder the trace folder resource |
| 472 | * @param path the path to the trace to import |
| 473 | * @param targetName the target name |
| 474 | */ |
| 475 | private static void importTrace(final IFolder folder, final Path path, final String targetName) { |
| 476 | final File source = new File(path.toString()); |
| 477 | if (source.isDirectory()) { |
| 478 | IPath containerPath = folder.getFullPath().addTrailingSeparator().append(targetName); |
| 479 | IOverwriteQuery overwriteImplementor = new IOverwriteQuery() { |
| 480 | @Override |
| 481 | public String queryOverwrite(String pathString) { |
| 482 | return IOverwriteQuery.NO_ALL; |
| 483 | } |
| 484 | }; |
| 485 | List<File> filesToImport = Arrays.asList(source.listFiles()); |
| 486 | ImportOperation operation = new ImportOperation( |
| 487 | containerPath, |
| 488 | source, |
| 489 | FileSystemStructureProvider.INSTANCE, |
| 490 | overwriteImplementor, |
| 491 | filesToImport); |
| 492 | operation.setCreateContainerStructure(false); |
| 493 | try { |
| 494 | operation.run(new NullProgressMonitor()); |
| 495 | } catch (InvocationTargetException e) { |
| 496 | displayException(e); |
| 497 | } catch (InterruptedException e) { |
| 498 | displayException(e); |
| 499 | } |
| 500 | } else { |
| 501 | IRunnableWithProgress runnable = new IRunnableWithProgress() { |
| 502 | @Override |
| 503 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
| 504 | try (InputStream inputStream = new FileInputStream(source);) { |
| 505 | IFile targetFile = folder.getFile(targetName); |
| 506 | targetFile.create(inputStream, IResource.NONE, monitor); |
| 507 | } catch (CoreException | IOException e) { |
| 508 | displayException(e); |
| 509 | } |
| 510 | } |
| 511 | }; |
| 512 | WorkspaceModifyDelegatingOperation operation = new WorkspaceModifyDelegatingOperation(runnable); |
| 513 | try { |
| 514 | operation.run(new NullProgressMonitor()); |
| 515 | } catch (InvocationTargetException e) { |
| 516 | displayException(e); |
| 517 | } catch (InterruptedException e) { |
| 518 | displayException(e); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Create a link to the actual trace and set the trace type |
| 525 | * |
| 526 | * @param parentFolder the parent folder |
| 527 | * @param resource the resource |
| 528 | * @param targetName the target name |
| 529 | */ |
| 530 | private static void createLink(IFolder parentFolder, IResource resource, String targetName) { |
| 531 | IPath location = resource.getLocation(); |
| 532 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 533 | try { |
| 534 | String traceType = TmfTraceType.getTraceTypeId(resource); |
| 535 | TraceTypeHelper traceTypeHelper = TmfTraceType.getTraceType(traceType); |
| 536 | |
| 537 | if (resource instanceof IFolder) { |
| 538 | IFolder folder = parentFolder.getFolder(targetName); |
| 539 | IStatus result = workspace.validateLinkLocation(folder, location); |
| 540 | if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) { |
| 541 | folder.createLink(location, IResource.REPLACE, null); |
| 542 | if (traceTypeHelper != null) { |
| 543 | TmfTraceTypeUIUtils.setTraceType(folder, traceTypeHelper); |
| 544 | } |
| 545 | } else { |
| 546 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ |
| 547 | } |
| 548 | } else { |
| 549 | IFile file = parentFolder.getFile(targetName); |
| 550 | IStatus result = workspace.validateLinkLocation(file, location); |
| 551 | if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) { |
| 552 | file.createLink(location, IResource.REPLACE, null); |
| 553 | if (traceTypeHelper != null) { |
| 554 | TmfTraceTypeUIUtils.setTraceType(file, traceTypeHelper); |
| 555 | } |
| 556 | } else { |
| 557 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ |
| 558 | } |
| 559 | } |
| 560 | } catch (CoreException e) { |
| 561 | displayException(e); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Create a link to a file or folder |
| 567 | * |
| 568 | * @param parentFolder the parent folder |
| 569 | * @param source the file or folder |
| 570 | * @param targetName the target name |
| 571 | */ |
| 572 | private static void createLink(IFolder parentFolder, IPath location, String targetName) { |
| 573 | File source = new File(location.toString()); |
| 574 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 575 | try { |
| 576 | |
| 577 | if (source.isDirectory()) { |
| 578 | IFolder folder = parentFolder.getFolder(targetName); |
| 579 | IStatus result = workspace.validateLinkLocation(folder, location); |
| 580 | if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) { |
| 581 | folder.createLink(location, IResource.REPLACE, null); |
| 582 | } else { |
| 583 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ |
| 584 | } |
| 585 | } else { |
| 586 | IFile file = parentFolder.getFile(targetName); |
| 587 | IStatus result = workspace.validateLinkLocation(file, location); |
| 588 | if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) { |
| 589 | file.createLink(location, IResource.REPLACE, null); |
| 590 | } else { |
| 591 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ |
| 592 | } |
| 593 | } |
| 594 | } catch (CoreException e) { |
| 595 | displayException(e); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Prompts the user to rename a trace |
| 601 | * |
| 602 | * @param element the conflicting element |
| 603 | * @return the new name to use or null if rename is canceled |
| 604 | */ |
| 605 | private static String promptRename(ITmfProjectModelElement element) { |
| 606 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); |
| 607 | mb.setText(Messages.DropAdapterAssistant_RenameTraceTitle); |
| 608 | mb.setMessage(NLS.bind(Messages.DropAdapterAssistant_RenameTraceMessage, element.getName())); |
| 609 | if (mb.open() != SWT.OK) { |
| 610 | return null; |
| 611 | } |
| 612 | IContainer folder = element.getResource().getParent(); |
| 613 | int i = 2; |
| 614 | while (true) { |
| 615 | String name = element.getName() + '(' + Integer.toString(i++) + ')'; |
| 616 | IResource resource = folder.findMember(name); |
| 617 | if (resource == null) { |
| 618 | return name; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Cleanup bookmarks file in copied trace |
| 625 | */ |
| 626 | private static void cleanupBookmarks(IPath path) { |
| 627 | IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(path); |
| 628 | if (folder.exists()) { |
| 629 | try { |
| 630 | for (IResource member : folder.members()) { |
| 631 | if (ITmfEventsEditorConstants.TRACE_INPUT_TYPE_CONSTANTS.contains(TmfTraceType.getTraceTypeId(member))) { |
| 632 | member.delete(true, null); |
| 633 | } |
| 634 | } |
| 635 | } catch (CoreException e) { |
| 636 | displayException(e); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | private static void setTraceType(IResource traceResource) { |
| 642 | try { |
| 643 | String traceType = TmfTraceType.getTraceTypeId(traceResource); |
| 644 | TraceTypeHelper traceTypeHelper = TmfTraceType.getTraceType(traceType); |
| 645 | if (traceTypeHelper == null) { |
| 646 | traceTypeHelper = TmfTraceTypeUIUtils.selectTraceType(traceResource.getLocation().toOSString(), null, null); |
| 647 | } |
| 648 | if (traceTypeHelper != null) { |
| 649 | TmfTraceTypeUIUtils.setTraceType(traceResource, traceTypeHelper); |
| 650 | } |
| 651 | } catch (TmfTraceImportException e) { |
| 652 | } catch (CoreException e) { |
| 653 | displayException(e); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Display an exception in a message box |
| 659 | * |
| 660 | * @param e the exception |
| 661 | */ |
| 662 | private static void displayException(Exception e) { |
| 663 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
| 664 | mb.setText(e.getClass().getName()); |
| 665 | mb.setMessage(e.getMessage()); |
| 666 | mb.open(); |
| 667 | } |
| 668 | |
| 669 | } |