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