tmf: Make order of files processing more natural during import
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / importtrace / FileSystemObjectImportStructureProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2015 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
14
15 import java.io.File;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.Comparator;
19 import java.util.List;
20 import java.util.zip.ZipEntry;
21
22 import org.eclipse.ui.internal.wizards.datatransfer.TarEntry;
23 import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
24
25 /**
26 * An import provider that makes use of the IFileSystemObject abstraction
27 * instead of using plain file system objects (File, TarEntry, ZipEntry, etc)
28 */
29 @SuppressWarnings("restriction")
30 public class FileSystemObjectImportStructureProvider implements IImportStructureProvider {
31
32 private IImportStructureProvider fImportProvider;
33 private String fArchivePath;
34
35 /**
36 * Constructor
37 *
38 * @param importStructureProvider
39 * the {@link IImportStructureProvider}
40 * @param archivePath
41 * the path of the archive file
42 */
43 public FileSystemObjectImportStructureProvider(IImportStructureProvider importStructureProvider, String archivePath) {
44 fImportProvider = importStructureProvider;
45 fArchivePath = archivePath;
46 }
47
48 /**
49 * This orders by files first then the folders. Then by lexical order.
50 */
51 private final class FileObjectPathComparator implements Comparator<IFileSystemObject> {
52 @Override
53 public int compare(IFileSystemObject o1, IFileSystemObject o2) {
54 if (o1.isDirectory() != o2.isDirectory()) {
55 if (o1.isDirectory()) {
56 return 1;
57 }
58 return -1;
59 }
60
61 return o1.getName().compareToIgnoreCase(o2.getName());
62 }
63 }
64
65 @Override
66 public List<IFileSystemObject> getChildren(Object element) {
67 @SuppressWarnings("rawtypes")
68 List children = fImportProvider.getChildren(((IFileSystemObject) element).getRawFileSystemObject());
69 List<IFileSystemObject> adapted = new ArrayList<>(children.size());
70 for (Object o : children) {
71 adapted.add(getIFileSystemObject(o));
72 }
73
74 adapted.sort(new FileObjectPathComparator());
75 return adapted;
76 }
77
78 /**
79 * Get the IFileSystemObject corresponding to the specified raw object
80 *
81 * @param o
82 * the raw object
83 * @return the corresponding IFileSystemObject
84 */
85 public IFileSystemObject getIFileSystemObject(Object o) {
86 if (o == null) {
87 return null;
88 }
89
90 if (o instanceof File) {
91 return new FileFileSystemObject((File) o);
92 } else if (o instanceof TarEntry) {
93 return new TarFileSystemObject((TarEntry) o, fArchivePath);
94 } else if (o instanceof ZipEntry) {
95 return new ZipFileSystemObject((ZipEntry) o, fArchivePath);
96 } else if (o instanceof GzipEntry) {
97 return new GzipFileSystemObject((GzipEntry) o, fArchivePath);
98 }
99
100 throw new IllegalArgumentException("Object type not handled"); //$NON-NLS-1$
101 }
102
103 @Override
104 public InputStream getContents(Object fileSystemObject) {
105 return fImportProvider.getContents(((IFileSystemObject) fileSystemObject).getRawFileSystemObject());
106 }
107
108 @Override
109 public String getFullPath(Object element) {
110 return fImportProvider.getFullPath(((IFileSystemObject) element).getRawFileSystemObject());
111 }
112
113 @Override
114 public String getLabel(Object element) {
115 return fImportProvider.getLabel(((IFileSystemObject) element).getRawFileSystemObject());
116 }
117
118 @Override
119 public boolean isFolder(Object element) {
120 return fImportProvider.isFolder(((IFileSystemObject) element).getRawFileSystemObject());
121 }
122
123 /**
124 * Disposes of the resources associated with the provider.
125 */
126 public void dispose() {
127 }
128 }
This page took 0.033977 seconds and 6 git commands to generate.