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
.util
.Iterator
;
17 import java
.util
.List
;
19 import org
.eclipse
.core
.runtime
.IPath
;
20 import org
.eclipse
.ui
.dialogs
.FileSystemElement
;
21 import org
.eclipse
.ui
.model
.AdaptableList
;
24 * The <code>TraceFileSystemElement</code> is a <code>FileSystemElement</code>
25 * that knows if it has been populated or not.
27 public class TraceFileSystemElement
extends FileSystemElement
{
29 private boolean fIsPopulated
= false;
30 private String fLabel
= null;
31 private IPath fDestinationContainerPath
;
32 private FileSystemObjectImportStructureProvider fProvider
;
33 private String fSourceLocation
;
36 * Constructs a new TraceFileSystemElement
38 * @param name the name of the element
39 * @param parent the parent element
40 * @param isDirectory whether or not this element is a directory
41 * @param provider the provider associated with this element
43 public TraceFileSystemElement(String name
, FileSystemElement parent
, boolean isDirectory
, FileSystemObjectImportStructureProvider provider
) {
44 super(name
, parent
, isDirectory
);
49 * Set the path for the container that will contain the trace once imported.
51 * @param destinationContainerPath the destination container path
53 public void setDestinationContainerPath(IPath destinationContainerPath
) {
54 fDestinationContainerPath
= destinationContainerPath
;
58 * Mark this element as populated.
60 public void setPopulated() {
65 * Returns whether or not the children of the element have been populated.
67 * @return whether or not the children of the element have been populated.
69 public boolean isPopulated() {
74 public AdaptableList
getFiles() {
76 populateElementChildren();
78 return super.getFiles();
82 public AdaptableList
getFolders() {
84 populateElementChildren();
86 return super.getFolders();
90 * Sets the label for the trace to be used when importing at trace.
93 * the label for the trace
95 public void setLabel(String name
) {
100 * Returns the label for the trace to be used when importing at trace.
102 * @return the label of trace resource
104 public String
getLabel() {
105 if (fLabel
== null) {
106 return getFileSystemObject().getName();
112 * The full path to the container that will contain the trace once imported.
114 * @return the destination container path
116 public IPath
getDestinationContainerPath() {
117 return fDestinationContainerPath
;
121 * Populates the children of the specified parent
122 * <code>FileSystemElement</code>
124 private void populateElementChildren() {
125 List
<IFileSystemObject
> allchildren
= fProvider
.getChildren(this.getFileSystemObject());
127 TraceFileSystemElement newelement
= null;
128 Iterator
<IFileSystemObject
> iter
= allchildren
.iterator();
129 while (iter
.hasNext()) {
131 newelement
= new TraceFileSystemElement(fProvider
.getLabel(child
), this, fProvider
.isFolder(child
), fProvider
);
132 newelement
.setFileSystemObject(child
);
138 * Get the import provider associated with this element.
140 * @return the import provider.
142 public FileSystemObjectImportStructureProvider
getProvider() {
147 public IFileSystemObject
getFileSystemObject() {
148 return (IFileSystemObject
) super.getFileSystemObject();
152 * Get the source location for this element.
154 * @return the source location
156 public String
getSourceLocation() {
157 if (fSourceLocation
== null) {
158 fSourceLocation
= getFileSystemObject().getSourceLocation();
160 return fSourceLocation
;
164 * Set the source location for this element.
166 * @param sourceLocation
167 * the source location
169 public void setSourceLocation(String sourceLocation
) {
170 fSourceLocation
= sourceLocation
;
174 * Get all the TraceFileSystemElements recursively.
177 * the list accumulating the result
179 public void getAllChildren(List
<TraceFileSystemElement
> result
) {
180 AdaptableList files
= getFiles();
181 for (Object file
: files
.getChildren()) {
182 result
.add((TraceFileSystemElement
) file
);
185 AdaptableList folders
= getFolders();
186 for (Object folder
: folders
.getChildren()) {
187 TraceFileSystemElement traceElementFolder
= (TraceFileSystemElement
) folder
;
188 traceElementFolder
.getAllChildren(result
);
193 * Create a root TraceFileSystemElement suitable to be passed to a
194 * {@link TraceValidateAndImportOperation}
197 * the IFileSystemObject (abstracting File, Tar, Zip, etc) to
198 * create the root TraceFileSystemElement from
200 * the import provider to be used, compatible with the
202 * @return the resulting root TraceFileSystemElement
204 * @See {@link TraceValidateAndImportOperation}
205 * @See {@link ArchiveUtil#getRootObjectAndProvider(File, org.eclipse.swt.widgets.Shell)}
207 public static TraceFileSystemElement
createRootTraceFileElement(IFileSystemObject object
,
208 FileSystemObjectImportStructureProvider provider
) {
209 boolean isContainer
= provider
.isFolder(object
);
210 String elementLabel
= provider
.getLabel(object
);
212 // Use an empty label so that display of the element's full name
213 // doesn't include a confusing label
214 TraceFileSystemElement dummyParent
= new TraceFileSystemElement("", null, true, provider
);//$NON-NLS-1$
215 Object dummyParentFileSystemObject
= object
;
216 Object rawFileSystemObject
= object
.getRawFileSystemObject();
217 if (rawFileSystemObject
instanceof File
) {
218 dummyParentFileSystemObject
= provider
.getIFileSystemObject(((File
) rawFileSystemObject
).getParentFile());
220 dummyParent
.setFileSystemObject(dummyParentFileSystemObject
);
221 dummyParent
.setPopulated();
222 TraceFileSystemElement result
= new TraceFileSystemElement(
223 elementLabel
, dummyParent
, isContainer
, provider
);
224 result
.setFileSystemObject(object
);
226 // Get the files for the element so as to build the first level