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