| 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.util.Iterator; |
| 17 | import java.util.List; |
| 18 | |
| 19 | import org.eclipse.core.runtime.IPath; |
| 20 | import org.eclipse.ui.dialogs.FileSystemElement; |
| 21 | import org.eclipse.ui.model.AdaptableList; |
| 22 | |
| 23 | /** |
| 24 | * The <code>TraceFileSystemElement</code> is a <code>FileSystemElement</code> |
| 25 | * that knows if it has been populated or not. |
| 26 | */ |
| 27 | public class TraceFileSystemElement extends FileSystemElement { |
| 28 | |
| 29 | private boolean fIsPopulated = false; |
| 30 | private String fLabel = null; |
| 31 | private IPath fDestinationContainerPath; |
| 32 | private FileSystemObjectImportStructureProvider fProvider; |
| 33 | private String fSourceLocation; |
| 34 | |
| 35 | /** |
| 36 | * Constructs a new TraceFileSystemElement |
| 37 | * |
| 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 |
| 42 | */ |
| 43 | public TraceFileSystemElement(String name, FileSystemElement parent, boolean isDirectory, FileSystemObjectImportStructureProvider provider) { |
| 44 | super(name, parent, isDirectory); |
| 45 | fProvider = provider; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Set the path for the container that will contain the trace once imported. |
| 50 | * |
| 51 | * @param destinationContainerPath the destination container path |
| 52 | */ |
| 53 | public void setDestinationContainerPath(IPath destinationContainerPath) { |
| 54 | fDestinationContainerPath = destinationContainerPath; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Mark this element as populated. |
| 59 | */ |
| 60 | public void setPopulated() { |
| 61 | fIsPopulated = true; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns whether or not the children of the element have been populated. |
| 66 | * |
| 67 | * @return whether or not the children of the element have been populated. |
| 68 | */ |
| 69 | public boolean isPopulated() { |
| 70 | return fIsPopulated; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public AdaptableList getFiles() { |
| 75 | if (!fIsPopulated) { |
| 76 | populateElementChildren(); |
| 77 | } |
| 78 | return super.getFiles(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public AdaptableList getFolders() { |
| 83 | if (!fIsPopulated) { |
| 84 | populateElementChildren(); |
| 85 | } |
| 86 | return super.getFolders(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets the label for the trace to be used when importing at trace. |
| 91 | * |
| 92 | * @param name |
| 93 | * the label for the trace |
| 94 | */ |
| 95 | public void setLabel(String name) { |
| 96 | fLabel = name; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the label for the trace to be used when importing at trace. |
| 101 | * |
| 102 | * @return the label of trace resource |
| 103 | */ |
| 104 | public String getLabel() { |
| 105 | if (fLabel == null) { |
| 106 | return getProvider().getLabel(this.getFileSystemObject()); |
| 107 | } |
| 108 | return fLabel; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * The full path to the container that will contain the trace once imported. |
| 113 | * |
| 114 | * @return the destination container path |
| 115 | */ |
| 116 | public IPath getDestinationContainerPath() { |
| 117 | return fDestinationContainerPath; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Populates the children of the specified parent |
| 122 | * <code>FileSystemElement</code> |
| 123 | */ |
| 124 | private void populateElementChildren() { |
| 125 | List<IFileSystemObject> allchildren = fProvider.getChildren(this.getFileSystemObject()); |
| 126 | Object child = null; |
| 127 | TraceFileSystemElement newelement = null; |
| 128 | Iterator<IFileSystemObject> iter = allchildren.iterator(); |
| 129 | while (iter.hasNext()) { |
| 130 | child = iter.next(); |
| 131 | newelement = new TraceFileSystemElement(fProvider.getLabel(child), this, fProvider.isFolder(child), fProvider); |
| 132 | newelement.setFileSystemObject(child); |
| 133 | } |
| 134 | setPopulated(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the import provider associated with this element. |
| 139 | * |
| 140 | * @return the import provider. |
| 141 | */ |
| 142 | public FileSystemObjectImportStructureProvider getProvider() { |
| 143 | return fProvider; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public IFileSystemObject getFileSystemObject() { |
| 148 | return (IFileSystemObject) super.getFileSystemObject(); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get the source location for this element. |
| 153 | * |
| 154 | * @return the source location |
| 155 | */ |
| 156 | public String getSourceLocation() { |
| 157 | if (fSourceLocation == null) { |
| 158 | fSourceLocation = getFileSystemObject().getSourceLocation(); |
| 159 | } |
| 160 | return fSourceLocation; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Set the source location for this element. |
| 165 | * |
| 166 | * @param sourceLocation |
| 167 | * the source location |
| 168 | */ |
| 169 | public void setSourceLocation(String sourceLocation) { |
| 170 | fSourceLocation = sourceLocation; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get all the TraceFileSystemElements recursively. |
| 175 | * |
| 176 | * @param result |
| 177 | * the list accumulating the result |
| 178 | */ |
| 179 | public void getAllChildren(List<TraceFileSystemElement> result) { |
| 180 | AdaptableList files = getFiles(); |
| 181 | for (Object file : files.getChildren()) { |
| 182 | result.add((TraceFileSystemElement) file); |
| 183 | } |
| 184 | |
| 185 | AdaptableList folders = getFolders(); |
| 186 | for (Object folder : folders.getChildren()) { |
| 187 | TraceFileSystemElement traceElementFolder = (TraceFileSystemElement) folder; |
| 188 | traceElementFolder.getAllChildren(result); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Create a root TraceFileSystemElement suitable to be passed to a |
| 194 | * {@link TraceValidateAndImportOperation} |
| 195 | * |
| 196 | * @param object |
| 197 | * the IFileSystemObject (abstracting File, Tar, Zip, etc) to |
| 198 | * create the root TraceFileSystemElement from |
| 199 | * @param provider |
| 200 | * the import provider to be used, compatible with the |
| 201 | * IFileSystemObject |
| 202 | * @return the resulting root TraceFileSystemElement |
| 203 | * |
| 204 | * @See {@link TraceValidateAndImportOperation} |
| 205 | * @See {@link ArchiveUtil#getRootObjectAndProvider(File, org.eclipse.swt.widgets.Shell)} |
| 206 | */ |
| 207 | public static TraceFileSystemElement createRootTraceFileElement(IFileSystemObject object, |
| 208 | FileSystemObjectImportStructureProvider provider) { |
| 209 | boolean isContainer = provider.isFolder(object); |
| 210 | String elementLabel = provider.getLabel(object); |
| 211 | |
| 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()); |
| 219 | } |
| 220 | dummyParent.setFileSystemObject(dummyParentFileSystemObject); |
| 221 | dummyParent.setPopulated(); |
| 222 | TraceFileSystemElement result = new TraceFileSystemElement( |
| 223 | elementLabel, dummyParent, isContainer, provider); |
| 224 | result.setFileSystemObject(object); |
| 225 | |
| 226 | // Get the files for the element so as to build the first level |
| 227 | result.getFiles(); |
| 228 | |
| 229 | return dummyParent; |
| 230 | } |
| 231 | } |