1 11 package org.eclipse.ui.internal.wizards.datatransfer; 12 13 import java.io.IOException ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.resources.IContainer; 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.MultiStatus; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.jface.operation.IRunnableWithProgress; 29 import org.eclipse.jface.operation.ModalContext; 30 import org.eclipse.osgi.util.NLS; 31 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 32 33 39 public class ArchiveFileExportOperation implements IRunnableWithProgress { 40 private IFileExporter exporter; 41 42 private String destinationFilename; 43 44 private IProgressMonitor monitor; 45 46 private List resourcesToExport; 47 48 private IResource resource; 49 50 private List errorTable = new ArrayList (1); 52 private boolean useCompression = true; 53 54 private boolean useTarFormat = false; 55 56 private boolean createLeadupStructure = true; 57 58 65 public ArchiveFileExportOperation(List resources, String filename) { 66 super(); 67 68 Iterator elementsEnum = resources.iterator(); 70 while (elementsEnum.hasNext()) { 71 IResource currentResource = (IResource) elementsEnum.next(); 72 if (isDescendent(resources, currentResource)) { 73 elementsEnum.remove(); } 75 } 76 77 resourcesToExport = resources; 78 destinationFilename = filename; 79 } 80 81 88 public ArchiveFileExportOperation(IResource res, String filename) { 89 super(); 90 resource = res; 91 destinationFilename = filename; 92 } 93 94 103 public ArchiveFileExportOperation(IResource res, List resources, String filename) { 104 this(res, filename); 105 resourcesToExport = resources; 106 } 107 108 111 protected void addError(String message, Throwable e) { 112 errorTable.add(new Status(IStatus.ERROR, 113 IDEWorkbenchPlugin.IDE_WORKBENCH, 0, message, e)); 114 } 115 116 123 protected int countChildrenOf(IResource checkResource) throws CoreException { 124 if (checkResource.getType() == IResource.FILE) { 125 return 1; 126 } 127 128 int count = 0; 129 if (checkResource.isAccessible()) { 130 IResource[] children = ((IContainer) checkResource).members(); 131 for (int i = 0; i < children.length; i++) { 132 count += countChildrenOf(children[i]); 133 } 134 } 135 136 return count; 137 } 138 139 145 protected int countSelectedResources() throws CoreException { 146 int result = 0; 147 Iterator resources = resourcesToExport.iterator(); 148 while (resources.hasNext()) { 149 result += countChildrenOf((IResource) resources.next()); 150 } 151 152 return result; 153 } 154 155 161 protected void exportResource(IResource exportResource) 162 throws InterruptedException { 163 exportResource(exportResource, 1); 164 } 165 166 173 protected void exportResource(IResource exportResource, int leadupDepth) 174 throws InterruptedException { 175 if (!exportResource.isAccessible()) { 176 return; 177 } 178 179 if (exportResource.getType() == IResource.FILE) { 180 String destinationName; 181 IPath fullPath = exportResource.getFullPath(); 182 if (createLeadupStructure) { 183 destinationName = fullPath.makeRelative().toString(); 184 } else { 185 destinationName = fullPath.removeFirstSegments( 186 fullPath.segmentCount() - leadupDepth).toString(); 187 } 188 monitor.subTask(destinationName); 189 190 try { 191 exporter.write((IFile) exportResource, destinationName); 192 } catch (IOException e) { 193 addError(NLS.bind(DataTransferMessages.DataTransfer_errorExporting, exportResource.getFullPath().makeRelative(), e.getMessage()), e); 194 } catch (CoreException e) { 195 addError(NLS.bind(DataTransferMessages.DataTransfer_errorExporting, exportResource.getFullPath().makeRelative(), e.getMessage()), e); 196 } 197 198 monitor.worked(1); 199 ModalContext.checkCanceled(monitor); 200 } else { 201 IResource[] children = null; 202 203 try { 204 children = ((IContainer) exportResource).members(); 205 } catch (CoreException e) { 206 addError(NLS.bind(DataTransferMessages.DataTransfer_errorExporting, exportResource.getFullPath()), e); 208 } 209 210 for (int i = 0; i < children.length; i++) { 211 exportResource(children[i], leadupDepth + 1); 212 } 213 214 } 215 } 216 217 221 protected void exportSpecifiedResources() throws InterruptedException { 222 Iterator resources = resourcesToExport.iterator(); 223 224 while (resources.hasNext()) { 225 IResource currentResource = (IResource) resources.next(); 226 exportResource(currentResource); 227 } 228 } 229 230 238 public IStatus getStatus() { 239 IStatus[] errors = new IStatus[errorTable.size()]; 240 errorTable.toArray(errors); 241 return new MultiStatus( 242 IDEWorkbenchPlugin.IDE_WORKBENCH, 243 IStatus.OK, 244 errors, 245 DataTransferMessages.FileSystemExportOperation_problemsExporting, 246 null); 247 } 248 249 254 protected void initialize() throws IOException { 255 if(useTarFormat) { 256 exporter = new TarFileExporter(destinationFilename, useCompression); 257 } else { 258 exporter = new ZipFileExporter(destinationFilename, useCompression); 259 } 260 } 261 262 270 protected boolean isDescendent(List resources, IResource child) { 271 if (child.getType() == IResource.PROJECT) { 272 return false; 273 } 274 275 IResource parent = child.getParent(); 276 if (resources.contains(parent)) { 277 return true; 278 } 279 280 return isDescendent(resources, parent); 281 } 282 283 287 public void run(IProgressMonitor progressMonitor) 288 throws InvocationTargetException , InterruptedException { 289 this.monitor = progressMonitor; 290 291 try { 292 initialize(); 293 } catch (IOException e) { 294 throw new InvocationTargetException (e, NLS.bind(DataTransferMessages.ZipExport_cannotOpen, e.getMessage())); 295 } 296 297 try { 298 int totalWork = IProgressMonitor.UNKNOWN; 300 try { 301 if (resourcesToExport == null) { 302 totalWork = countChildrenOf(resource); 303 } else { 304 totalWork = countSelectedResources(); 305 } 306 } catch (CoreException e) { 307 } 309 monitor.beginTask(DataTransferMessages.DataTransfer_exportingTitle, totalWork); 310 if (resourcesToExport == null) { 311 exportResource(resource); 312 } else { 313 exportSpecifiedResources(); 315 } 316 317 try { 318 exporter.finished(); 319 } catch (IOException e) { 320 throw new InvocationTargetException ( 321 e, 322 NLS.bind(DataTransferMessages.ZipExport_cannotClose, e.getMessage())); 323 } 324 } finally { 325 monitor.done(); 326 } 327 } 328 329 335 public void setCreateLeadupStructure(boolean value) { 336 createLeadupStructure = value; 337 } 338 339 345 public void setUseCompression(boolean value) { 346 useCompression = value; 347 } 348 349 355 public void setUseTarFormat(boolean value) { 356 useTarFormat = value; 357 } 358 } 359 | Popular Tags |