1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
;
16 import java
.io
.InputStream
;
17 import java
.lang
.reflect
.InvocationTargetException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.HashMap
;
21 import java
.util
.Iterator
;
22 import java
.util
.LinkedList
;
23 import java
.util
.List
;
24 import java
.util
.ListIterator
;
27 import org
.eclipse
.core
.filesystem
.EFS
;
28 import org
.eclipse
.core
.filesystem
.IFileInfo
;
29 import org
.eclipse
.core
.resources
.IFile
;
30 import org
.eclipse
.core
.resources
.IFolder
;
31 import org
.eclipse
.core
.resources
.IResource
;
32 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
33 import org
.eclipse
.core
.runtime
.CoreException
;
34 import org
.eclipse
.core
.runtime
.IPath
;
35 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
36 import org
.eclipse
.core
.runtime
.IStatus
;
37 import org
.eclipse
.core
.runtime
.Path
;
38 import org
.eclipse
.core
.runtime
.Status
;
39 import org
.eclipse
.core
.runtime
.SubMonitor
;
40 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
41 import org
.eclipse
.core
.runtime
.URIUtil
;
42 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
43 import org
.eclipse
.jface
.operation
.ModalContext
;
44 import org
.eclipse
.swt
.widgets
.Shell
;
45 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceImportException
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.util
.Pair
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceTypeUIUtils
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TraceUtils
;
54 import org
.eclipse
.ui
.dialogs
.FileSystemElement
;
55 import org
.eclipse
.ui
.dialogs
.IOverwriteQuery
;
56 import org
.eclipse
.ui
.wizards
.datatransfer
.FileSystemStructureProvider
;
57 import org
.eclipse
.ui
.wizards
.datatransfer
.IImportStructureProvider
;
58 import org
.eclipse
.ui
.wizards
.datatransfer
.ImportOperation
;
61 * An operation that performs validation and importing of traces. Its primary
62 * inputs are a collection of TraceFileSystemElement and several flags that
66 public class TraceValidateAndImportOperation
implements IRunnableWithProgress
{
68 private static final String TRACE_IMPORT_TEMP_FOLDER
= ".traceImport"; //$NON-NLS-1$
70 private String fTraceType
;
71 private IPath fDestinationContainerPath
;
72 private IPath fBaseSourceContainerPath
;
73 private boolean fImportFromArchive
;
74 private int fImportOptionFlags
;
76 private TmfTraceFolder fTraceFolderElement
;
77 private List
<TraceFileSystemElement
> fSelectedFileSystemElements
;
79 private IStatus fStatus
;
80 private ImportConflictHandler fConflictHandler
;
81 private String fCurrentPath
;
83 private List
<IResource
> fImportedResources
;
86 * Constructs a new validate and import operation.
89 * the parent shell to use for possible error messages
90 * @param traceFileSystemElements
91 * the trace file elements to import
93 * the trace type to import the traces as (can be set to null for
94 * automatic detection)
95 * @param baseSourceContainerPath
96 * the path to the container of the source. This is used as a
97 * "base" to generate the folder structure for the
98 * "preserve folder structure" option.
99 * @param destinationContainerPath
100 * the destination path of the import operation, typically a
102 * @param importFromArchive
103 * whether or not the source is an archive
104 * @param importOptionFlags
105 * bit-wise 'or' of import option flag constants (
106 * {@link ImportTraceWizardPage#OPTION_PRESERVE_FOLDER_STRUCTURE}
108 * {@link ImportTraceWizardPage#OPTION_CREATE_LINKS_IN_WORKSPACE}
110 * {@link ImportTraceWizardPage#OPTION_IMPORT_UNRECOGNIZED_TRACES}
112 * {@link ImportTraceWizardPage#OPTION_OVERWRITE_EXISTING_RESOURCES}
114 * @param traceFolderElement
115 * the destination trace folder of the import operation.
117 public TraceValidateAndImportOperation(Shell shell
, List
<TraceFileSystemElement
> traceFileSystemElements
, String traceId
, IPath baseSourceContainerPath
, IPath destinationContainerPath
, boolean importFromArchive
, int importOptionFlags
,
118 TmfTraceFolder traceFolderElement
) {
119 fTraceType
= traceId
;
120 fBaseSourceContainerPath
= baseSourceContainerPath
;
121 fDestinationContainerPath
= destinationContainerPath
;
122 fImportOptionFlags
= importOptionFlags
;
123 fImportFromArchive
= importFromArchive
;
125 fTraceFolderElement
= traceFolderElement
;
127 boolean overwriteExistingResources
= (importOptionFlags
& ImportTraceWizardPage
.OPTION_OVERWRITE_EXISTING_RESOURCES
) != 0;
128 if (overwriteExistingResources
) {
129 setConflictHandler(new ImportConflictHandler(fShell
, fTraceFolderElement
, ImportConfirmation
.OVERWRITE_ALL
));
131 setConflictHandler(new ImportConflictHandler(fShell
, fTraceFolderElement
, ImportConfirmation
.SKIP
));
133 fImportedResources
= new ArrayList
<>();
134 fSelectedFileSystemElements
= traceFileSystemElements
;
138 public void run(IProgressMonitor progressMonitor
) {
141 final List
<TraceFileSystemElement
> selectedFileSystemElements
= fSelectedFileSystemElements
;
143 // List fileSystemElements will be filled using the
145 SubMonitor subMonitor
= SubMonitor
.convert(progressMonitor
, 1);
147 // Check if operation was cancelled.
148 ModalContext
.checkCanceled(subMonitor
);
150 // Temporary directory to contain any extracted files
151 IFolder destTempFolder
= fTraceFolderElement
.getProject().getResource().getFolder(TRACE_IMPORT_TEMP_FOLDER
);
152 if (destTempFolder
.exists()) {
153 SubProgressMonitor monitor
= new SubProgressMonitor(subMonitor
, 1, SubProgressMonitor
.PREPEND_MAIN_LABEL_TO_SUBTASK
);
154 destTempFolder
.delete(true, monitor
);
156 SubProgressMonitor monitor
= new SubProgressMonitor(subMonitor
, 1, SubProgressMonitor
.PREPEND_MAIN_LABEL_TO_SUBTASK
);
157 destTempFolder
.create(IResource
.HIDDEN
, true, monitor
);
159 subMonitor
= SubMonitor
.convert(progressMonitor
, 2);
160 String baseSourceLocation
= null;
161 if (fImportFromArchive
) {
162 // When importing from archive, we first extract the
163 // *selected* files to a temporary folder then create new
164 // TraceFileSystemElements
166 SubMonitor archiveMonitor
= SubMonitor
.convert(subMonitor
.newChild(1), 2);
168 // Extract selected files from source archive to temporary
170 extractArchiveContent(selectedFileSystemElements
.iterator(), destTempFolder
, archiveMonitor
.newChild(1));
172 if (!selectedFileSystemElements
.isEmpty()) {
173 // Even if the files were extracted to temporary folder, they
174 // have to look like they originate from the source archive
175 baseSourceLocation
= getRootElement(selectedFileSystemElements
.get(0)).getSourceLocation();
176 // Extract additional archives contained in the extracted files
177 // (archives in archives)
178 List
<TraceFileSystemElement
> tempFolderFileSystemElements
= createElementsForFolder(destTempFolder
);
179 extractAllArchiveFiles(tempFolderFileSystemElements
, destTempFolder
, destTempFolder
.getLocation(), archiveMonitor
.newChild(1));
182 SubMonitor directoryMonitor
= SubMonitor
.convert(subMonitor
.newChild(1), 2);
183 // Import selected files, excluding archives (done in a later step)
184 importFileSystemElements(directoryMonitor
.newChild(1), selectedFileSystemElements
);
186 // Extract archives in selected files (if any) to temporary folder
187 extractAllArchiveFiles(selectedFileSystemElements
, destTempFolder
, fBaseSourceContainerPath
, directoryMonitor
.newChild(1));
188 // Even if the files were extracted to temporary folder, they
189 // have to look like they originate from the source folder
190 baseSourceLocation
= URIUtil
.toUnencodedString(fBaseSourceContainerPath
.toFile().getCanonicalFile().toURI());
194 * Import extracted files that are now in the temporary folder, if any
197 // We need to update the source container path because the
198 // "preserve folder structure" option would create the
199 // wrong trace folders otherwise.
200 fBaseSourceContainerPath
= destTempFolder
.getLocation();
201 List
<TraceFileSystemElement
> tempFolderFileSystemElements
= createElementsForFolder(destTempFolder
);
202 if (!tempFolderFileSystemElements
.isEmpty()) {
203 calculateSourceLocations(tempFolderFileSystemElements
, baseSourceLocation
);
204 // Never import extracted files as links, they would link to the
205 // temporary directory that will be deleted
206 fImportOptionFlags
= fImportOptionFlags
& ~ImportTraceWizardPage
.OPTION_CREATE_LINKS_IN_WORKSPACE
;
207 SubMonitor importTempMonitor
= subMonitor
.newChild(1);
208 importFileSystemElements(importTempMonitor
, tempFolderFileSystemElements
);
211 if (destTempFolder
.exists()) {
212 destTempFolder
.delete(true, progressMonitor
);
215 setStatus(Status
.OK_STATUS
);
216 } catch (InterruptedException e
) {
217 setStatus(Status
.CANCEL_STATUS
);
218 } catch (Exception e
) {
219 String errorMessage
= Messages
.ImportTraceWizard_ImportProblem
+ ": " + //$NON-NLS-1$
220 (fCurrentPath
!= null ? fCurrentPath
: ""); //$NON-NLS-1$
221 Activator
.getDefault().logError(errorMessage
, e
);
222 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, errorMessage
, e
));
227 * Get the list of resources that were imported by this operation. An
228 * example use case would be to use this to open traces that were imported
231 * @return the resources that were imported
233 public List
<IResource
> getImportedResources() {
234 return fImportedResources
;
238 * Import a collection of file system elements into the workspace.
240 private void importFileSystemElements(IProgressMonitor monitor
, List
<TraceFileSystemElement
> fileSystemElements
)
241 throws InterruptedException
, TmfTraceImportException
, CoreException
, InvocationTargetException
{
242 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, fileSystemElements
.size());
244 ListIterator
<TraceFileSystemElement
> fileSystemElementsIter
= fileSystemElements
.listIterator();
246 // Map to remember already imported directory traces
247 final Map
<String
, TraceFileSystemElement
> directoryTraces
= new HashMap
<>();
248 while (fileSystemElementsIter
.hasNext()) {
249 ModalContext
.checkCanceled(monitor
);
251 TraceFileSystemElement element
= fileSystemElementsIter
.next();
252 IFileSystemObject fileSystemObject
= element
.getFileSystemObject();
253 String resourcePath
= element
.getFileSystemObject().getAbsolutePath();
254 element
.setDestinationContainerPath(computeDestinationContainerPath(new Path(resourcePath
)));
256 fCurrentPath
= resourcePath
;
257 SubMonitor sub
= subMonitor
.newChild(1);
258 if (element
.isDirectory()) {
259 if (!directoryTraces
.containsKey(resourcePath
) && isDirectoryTrace(element
)) {
260 directoryTraces
.put(resourcePath
, element
);
261 validateAndImportTrace(element
, sub
);
264 TraceFileSystemElement parentElement
= (TraceFileSystemElement
) element
.getParent();
265 String parentPath
= parentElement
.getFileSystemObject().getAbsolutePath();
266 parentElement
.setDestinationContainerPath(computeDestinationContainerPath(new Path(parentPath
)));
267 fCurrentPath
= parentPath
;
268 if (!directoryTraces
.containsKey(parentPath
)) {
269 if (isDirectoryTrace(parentElement
)) {
270 directoryTraces
.put(parentPath
, parentElement
);
271 validateAndImportTrace(parentElement
, sub
);
273 boolean validateFile
= true;
274 TraceFileSystemElement grandParentElement
= (TraceFileSystemElement
) parentElement
.getParent();
275 // Special case for LTTng trace that may contain index
276 // directory and files
277 if (grandParentElement
!= null) {
278 String grandParentPath
= grandParentElement
.getFileSystemObject().getAbsolutePath();
279 grandParentElement
.setDestinationContainerPath(computeDestinationContainerPath(new Path(parentPath
)));
280 fCurrentPath
= grandParentPath
;
281 if (directoryTraces
.containsKey(grandParentPath
)) {
282 validateFile
= false;
283 } else if (isDirectoryTrace(grandParentElement
)) {
284 directoryTraces
.put(grandParentPath
, grandParentElement
);
285 validateAndImportTrace(grandParentElement
, sub
);
286 validateFile
= false;
289 if (validateFile
&& (fileSystemObject
.exists())) {
290 validateAndImportTrace(element
, sub
);
299 * Generate a new list of file system elements for the specified folder.
301 private static List
<TraceFileSystemElement
> createElementsForFolder(IFolder folder
) {
302 // Create the new import provider and root element based on the
304 FileSystemObjectImportStructureProvider importStructureProvider
= new FileSystemObjectImportStructureProvider(FileSystemStructureProvider
.INSTANCE
, null);
305 IFileSystemObject rootElement
= importStructureProvider
.getIFileSystemObject(new File(folder
.getLocation().toOSString()));
306 TraceFileSystemElement createRootElement
= TraceFileSystemElement
.createRootTraceFileElement(rootElement
, importStructureProvider
);
307 List
<TraceFileSystemElement
> list
= new LinkedList
<>();
308 createRootElement
.getAllChildren(list
);
313 * Extract all file system elements (File) to destination folder (typically
314 * workspace/TraceProject/.traceImport)
316 private void extractAllArchiveFiles(List
<TraceFileSystemElement
> fileSystemElements
, IFolder destFolder
, IPath baseSourceContainerPath
, IProgressMonitor progressMonitor
) throws InterruptedException
, CoreException
, InvocationTargetException
{
317 SubMonitor subMonitor
= SubMonitor
.convert(progressMonitor
, fileSystemElements
.size());
318 ListIterator
<TraceFileSystemElement
> fileSystemElementsIter
= fileSystemElements
.listIterator();
319 while (fileSystemElementsIter
.hasNext()) {
320 ModalContext
.checkCanceled(subMonitor
);
322 SubMonitor elementProgress
= subMonitor
.newChild(1);
323 TraceFileSystemElement element
= fileSystemElementsIter
.next();
324 File archiveFile
= (File
) element
.getFileSystemObject().getRawFileSystemObject();
325 boolean isArchiveFileElement
= element
.getFileSystemObject() instanceof FileFileSystemObject
&& ArchiveUtil
.isArchiveFile(archiveFile
);
326 if (isArchiveFileElement
) {
327 elementProgress
= SubMonitor
.convert(elementProgress
, 4);
328 IPath makeAbsolute
= baseSourceContainerPath
.makeAbsolute();
329 IPath relativeToSourceContainer
= new Path(element
.getFileSystemObject().getAbsolutePath()).makeRelativeTo(makeAbsolute
);
330 IFolder folder
= safeCreateExtractedFolder(destFolder
, relativeToSourceContainer
, elementProgress
.newChild(1));
331 extractArchiveToFolder(archiveFile
, folder
, elementProgress
.newChild(1));
333 // Delete original archive, we don't want to import this, just
334 // the extracted content
335 IFile fileRes
= destFolder
.getFile(relativeToSourceContainer
);
336 fileRes
.delete(true, elementProgress
.newChild(1));
337 IPath newPath
= destFolder
.getFullPath().append(relativeToSourceContainer
);
338 // Rename extracted folder (.extract) to original archive name
339 folder
.move(newPath
, true, elementProgress
.newChild(1));
340 folder
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(newPath
);
342 // Create the new import provider and root element based on
343 // the newly extracted temporary folder
344 FileSystemObjectImportStructureProvider importStructureProvider
= new FileSystemObjectImportStructureProvider(FileSystemStructureProvider
.INSTANCE
, null);
345 IFileSystemObject rootElement
= importStructureProvider
.getIFileSystemObject(new File(folder
.getLocation().toOSString()));
346 TraceFileSystemElement newElement
= TraceFileSystemElement
.createRootTraceFileElement(rootElement
, importStructureProvider
);
347 List
<TraceFileSystemElement
> extractedChildren
= new ArrayList
<>();
348 newElement
.getAllChildren(extractedChildren
);
349 extractAllArchiveFiles(extractedChildren
, folder
, folder
.getLocation(), progressMonitor
);
355 * Extract a file (File) to a destination folder
357 private void extractArchiveToFolder(File sourceFile
, IFolder destinationFolder
, IProgressMonitor progressMonitor
) throws InvocationTargetException
, InterruptedException
{
358 Pair
<IFileSystemObject
, FileSystemObjectImportStructureProvider
> rootObjectAndProvider
= ArchiveUtil
.getRootObjectAndProvider(sourceFile
, fShell
);
359 TraceFileSystemElement rootElement
= TraceFileSystemElement
.createRootTraceFileElement(rootObjectAndProvider
.getFirst(), rootObjectAndProvider
.getSecond());
360 List
<TraceFileSystemElement
> fileSystemElements
= new ArrayList
<>();
361 rootElement
.getAllChildren(fileSystemElements
);
362 extractArchiveContent(fileSystemElements
.listIterator(), destinationFolder
, progressMonitor
);
363 rootObjectAndProvider
.getSecond().dispose();
367 * Safely create a folder meant to receive extracted content by making sure
368 * there is no name clash.
370 private static IFolder
safeCreateExtractedFolder(IFolder destinationFolder
, IPath relativeContainerRelativePath
, IProgressMonitor monitor
) throws CoreException
{
371 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, 2);
372 IFolder extractedFolder
;
373 String suffix
= ""; //$NON-NLS-1$
376 IPath fullPath
= destinationFolder
.getFullPath().append(relativeContainerRelativePath
+ ".extract" + suffix
); //$NON-NLS-1$
377 IFolder folder
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(fullPath
);
378 if (!folder
.exists()) {
379 extractedFolder
= folder
;
382 suffix
= "(" + i
+ ")"; //$NON-NLS-1$//$NON-NLS-2$
385 subMonitor
.worked(1);
387 TraceUtils
.createFolder(extractedFolder
, subMonitor
.newChild(1));
388 return extractedFolder
;
391 private void calculateSourceLocations(List
<TraceFileSystemElement
> fileSystemElements
, String baseSourceLocation
) {
392 for (TraceFileSystemElement element
: fileSystemElements
) {
393 IPath tempRelative
= new Path(element
.getFileSystemObject().getAbsolutePath()).makeRelativeTo(fBaseSourceContainerPath
);
394 String sourceLocation
= baseSourceLocation
+ tempRelative
;
395 element
.setSourceLocation(sourceLocation
);
397 TraceFileSystemElement parentElement
= (TraceFileSystemElement
) element
.getParent();
398 tempRelative
= new Path(parentElement
.getFileSystemObject().getAbsolutePath()).makeRelativeTo(fBaseSourceContainerPath
);
399 sourceLocation
= baseSourceLocation
+ tempRelative
+ '/';
400 parentElement
.setSourceLocation(sourceLocation
);
405 * Extract all file system elements (Tar, Zip elements) to destination
406 * folder (typically workspace/TraceProject/.traceImport or a subfolder of
409 private void extractArchiveContent(Iterator
<TraceFileSystemElement
> fileSystemElementsIter
, IFolder tempFolder
, IProgressMonitor progressMonitor
) throws InterruptedException
,
410 InvocationTargetException
{
411 List
<TraceFileSystemElement
> subList
= new ArrayList
<>();
412 // Collect all the elements
413 while (fileSystemElementsIter
.hasNext()) {
414 ModalContext
.checkCanceled(progressMonitor
);
415 TraceFileSystemElement element
= fileSystemElementsIter
.next();
416 if (element
.isDirectory()) {
417 Object
[] array
= element
.getFiles().getChildren();
418 for (int i
= 0; i
< array
.length
; i
++) {
419 subList
.add((TraceFileSystemElement
) array
[i
]);
422 subList
.add(element
);
425 if (subList
.isEmpty()) {
429 TraceFileSystemElement root
= getRootElement(subList
.get(0));
431 ImportProvider fileSystemStructureProvider
= new ImportProvider();
433 IOverwriteQuery myQueryImpl
= new IOverwriteQuery() {
435 public String
queryOverwrite(String file
) {
436 return IOverwriteQuery
.NO_ALL
;
440 progressMonitor
.setTaskName(Messages
.ImportTraceWizard_ExtractImportOperationTaskName
);
441 IPath containerPath
= tempFolder
.getFullPath();
442 ImportOperation operation
= new ImportOperation(containerPath
, root
, fileSystemStructureProvider
, myQueryImpl
, subList
);
443 operation
.setContext(fShell
);
445 operation
.setCreateContainerStructure(true);
446 operation
.setOverwriteResources(false);
447 operation
.setVirtualFolders(false);
449 operation
.run(new SubProgressMonitor(progressMonitor
, subList
.size(), SubProgressMonitor
.PREPEND_MAIN_LABEL_TO_SUBTASK
));
452 private static TraceFileSystemElement
getRootElement(TraceFileSystemElement element
) {
453 TraceFileSystemElement root
= element
;
454 while (root
.getParent() != null) {
455 root
= (TraceFileSystemElement
) root
.getParent();
460 private IPath
computeDestinationContainerPath(Path resourcePath
) {
461 IPath destinationContainerPath
= fDestinationContainerPath
;
463 // We need to figure out the new destination path relative to the
464 // selected "base" source directory.
465 // Here for example, the selected source directory is /home/user
466 if ((fImportOptionFlags
& ImportTraceWizardPage
.OPTION_PRESERVE_FOLDER_STRUCTURE
) != 0) {
467 // /home/user/bar/foo/trace -> /home/user/bar/foo
468 IPath sourceContainerPath
= resourcePath
.removeLastSegments(1);
469 if (fBaseSourceContainerPath
.equals(resourcePath
)) {
470 // Use resourcePath directory if fBaseSourceContainerPath
471 // points to a directory trace
472 sourceContainerPath
= resourcePath
;
474 // /home/user/bar/foo, /home/user -> bar/foo
475 IPath relativeContainerPath
= sourceContainerPath
.makeRelativeTo(fBaseSourceContainerPath
);
476 // project/Traces + bar/foo -> project/Traces/bar/foo
477 destinationContainerPath
= fDestinationContainerPath
.append(relativeContainerPath
);
479 return destinationContainerPath
;
483 * Import a single file system element into the workspace.
485 private void validateAndImportTrace(TraceFileSystemElement fileSystemElement
, IProgressMonitor monitor
)
486 throws TmfTraceImportException
, CoreException
, InvocationTargetException
, InterruptedException
{
487 String path
= fileSystemElement
.getFileSystemObject().getAbsolutePath();
488 TraceTypeHelper traceTypeHelper
= null;
490 File file
= (File
) fileSystemElement
.getFileSystemObject().getRawFileSystemObject();
491 boolean isArchiveFileElement
= fileSystemElement
.getFileSystemObject() instanceof FileFileSystemObject
&& ArchiveUtil
.isArchiveFile(file
);
492 if (isArchiveFileElement
) {
493 // We'll be extracting this later, do not import as a trace
497 if (fTraceType
== null) {
500 traceTypeHelper
= TmfTraceTypeUIUtils
.selectTraceType(path
, null, null);
501 } catch (TmfTraceImportException e
) {
502 // the trace did not match any trace type
504 if (traceTypeHelper
== null) {
505 if ((fImportOptionFlags
& ImportTraceWizardPage
.OPTION_IMPORT_UNRECOGNIZED_TRACES
) != 0) {
506 importResource(fileSystemElement
, monitor
);
511 boolean isDirectoryTraceType
= TmfTraceType
.isDirectoryTraceType(fTraceType
);
512 if (fileSystemElement
.isDirectory() != isDirectoryTraceType
) {
515 traceTypeHelper
= TmfTraceType
.getTraceType(fTraceType
);
517 if (traceTypeHelper
== null) {
518 // Trace type not found
519 throw new TmfTraceImportException(Messages
.ImportTraceWizard_TraceTypeNotFound
);
522 if (!traceTypeHelper
.validate(path
).isOK()) {
523 // Trace type exist but doesn't validate for given trace.
528 // Finally import trace
529 IResource importedResource
= importResource(fileSystemElement
, monitor
);
530 if (importedResource
!= null) {
531 TmfTraceTypeUIUtils
.setTraceType(importedResource
, traceTypeHelper
);
532 fImportedResources
.add(importedResource
);
538 * Imports a trace resource to project. In case of name collision the user
539 * will be asked to confirm overwriting the existing trace, overwriting or
540 * skipping the trace to be imported.
542 * @param fileSystemElement
543 * trace file system object to import
546 * @return the imported resource or null if no resource was imported
548 * @throws InvocationTargetException
549 * if problems during import operation
550 * @throws InterruptedException
552 * @throws CoreException
553 * if problems with workspace
555 private IResource
importResource(TraceFileSystemElement fileSystemElement
, IProgressMonitor monitor
)
556 throws InvocationTargetException
, InterruptedException
, CoreException
{
558 IPath tracePath
= getInitialDestinationPath(fileSystemElement
);
559 String newName
= fConflictHandler
.checkAndHandleNameClash(tracePath
, monitor
);
561 if (newName
== null) {
564 fileSystemElement
.setLabel(newName
);
566 List
<TraceFileSystemElement
> subList
= new ArrayList
<>();
568 FileSystemElement parentFolder
= fileSystemElement
.getParent();
570 IPath containerPath
= fileSystemElement
.getDestinationContainerPath();
571 tracePath
= containerPath
.addTrailingSeparator().append(fileSystemElement
.getLabel());
572 boolean createLinksInWorkspace
= (fImportOptionFlags
& ImportTraceWizardPage
.OPTION_CREATE_LINKS_IN_WORKSPACE
) != 0;
573 if (fileSystemElement
.isDirectory() && !createLinksInWorkspace
) {
574 containerPath
= tracePath
;
576 Object
[] array
= fileSystemElement
.getFiles().getChildren();
577 for (int i
= 0; i
< array
.length
; i
++) {
578 subList
.add((TraceFileSystemElement
) array
[i
]);
580 parentFolder
= fileSystemElement
;
583 if (!fileSystemElement
.isDirectory()) {
585 IFileInfo info
= EFS
.getStore(new File(fileSystemElement
.getFileSystemObject().getAbsolutePath()).toURI()).fetchInfo();
586 if (info
.getLength() == 0) {
587 // Don't import empty traces
591 subList
.add(fileSystemElement
);
594 ImportProvider fileSystemStructureProvider
= new ImportProvider();
596 IOverwriteQuery myQueryImpl
= new IOverwriteQuery() {
598 public String
queryOverwrite(String file
) {
599 return IOverwriteQuery
.NO_ALL
;
603 monitor
.setTaskName(Messages
.ImportTraceWizard_ImportOperationTaskName
+ " " + fileSystemElement
.getFileSystemObject().getAbsolutePath()); //$NON-NLS-1$
604 ImportOperation operation
= new ImportOperation(containerPath
, parentFolder
, fileSystemStructureProvider
, myQueryImpl
, subList
);
605 operation
.setContext(fShell
);
607 operation
.setCreateContainerStructure(false);
608 operation
.setOverwriteResources(false);
609 operation
.setCreateLinks(createLinksInWorkspace
);
610 operation
.setVirtualFolders(false);
612 operation
.run(new SubProgressMonitor(monitor
, 1, SubProgressMonitor
.PREPEND_MAIN_LABEL_TO_SUBTASK
));
613 String sourceLocation
= fileSystemElement
.getSourceLocation();
614 IResource resource
= ResourcesPlugin
.getWorkspace().getRoot().findMember(tracePath
);
615 if (sourceLocation
!= null) {
616 resource
.setPersistentProperty(TmfCommonConstants
.SOURCE_LOCATION
, sourceLocation
);
622 private static boolean isDirectoryTrace(TraceFileSystemElement fileSystemElement
) {
623 String path
= fileSystemElement
.getFileSystemObject().getAbsolutePath();
624 if (TmfTraceType
.isDirectoryTrace(path
)) {
631 * @return the initial destination path, before rename, if any
633 private static IPath
getInitialDestinationPath(TraceFileSystemElement fileSystemElement
) {
634 IPath traceFolderPath
= fileSystemElement
.getDestinationContainerPath();
635 return traceFolderPath
.append(fileSystemElement
.getFileSystemObject().getName());
639 * Set the status for this operation
644 private void setStatus(IStatus status
) {
649 * Get the resulting status of this operation. Clients can use this for
650 * error reporting, etc.
652 * @return the resulting status
654 public IStatus
getStatus() {
658 private class ImportProvider
implements IImportStructureProvider
{
664 public String
getLabel(Object element
) {
665 TraceFileSystemElement resource
= (TraceFileSystemElement
) element
;
666 return resource
.getLabel();
670 public List
getChildren(Object element
) {
671 TraceFileSystemElement resource
= (TraceFileSystemElement
) element
;
672 return Arrays
.asList(resource
.getFiles().getChildren());
676 public InputStream
getContents(Object element
) {
677 TraceFileSystemElement resource
= (TraceFileSystemElement
) element
;
678 return resource
.getProvider().getContents(resource
.getFileSystemObject());
682 public String
getFullPath(Object element
) {
683 TraceFileSystemElement resource
= (TraceFileSystemElement
) element
;
684 return resource
.getProvider().getFullPath(resource
.getFileSystemObject());
688 public boolean isFolder(Object element
) {
689 TraceFileSystemElement resource
= (TraceFileSystemElement
) element
;
690 return resource
.isDirectory();
695 * Sets the conflict handler
697 * @param conflictHandler
698 * the conflict handler
700 public void setConflictHandler(ImportConflictHandler conflictHandler
) {
701 fConflictHandler
= conflictHandler
;