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