1 11 package org.eclipse.ui.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.ui.internal.ide.IDEWorkbenchPlugin; 31 32 35 36 class ZipFileExportOperation implements IRunnableWithProgress { 37 private ZipFileExporter exporter; 38 private String destinationFilename; 39 private IProgressMonitor monitor; 40 41 private List resourcesToExport; 42 private IResource resource; 43 private List errorTable = new ArrayList (1); 45 private boolean useCompression = true; 46 private boolean createLeadupStructure = true; 47 private boolean generateManifestFile = false; 48 55 public ZipFileExportOperation(List resources, String filename) { 56 super(); 57 58 Iterator elementsEnum = resources.iterator(); 60 while (elementsEnum.hasNext()) { 61 IResource currentResource = (IResource) elementsEnum.next(); 62 if (isDescendent(resources, currentResource)) 63 elementsEnum.remove(); } 65 66 resourcesToExport = resources; 67 destinationFilename = filename; 68 } 69 76 public ZipFileExportOperation(IResource res, String filename) { 77 super(); 78 resource = res; 79 destinationFilename = filename; 80 } 81 90 public ZipFileExportOperation( 91 IResource res, 92 List resources, 93 String filename) { 94 this(res, filename); 95 resourcesToExport = resources; 96 } 97 100 protected void addError(String message, Throwable e) { 101 errorTable.add( 102 new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 0, message, e)); 103 } 104 111 protected int countChildrenOf(IResource checkResource) throws CoreException { 112 if (checkResource.getType() == IResource.FILE) 113 return 1; 114 115 int count = 0; 116 if (checkResource.isAccessible()) { 117 IResource[] children = ((IContainer) checkResource).members(); 118 for (int i = 0; i < children.length; i++) 119 count += countChildrenOf(children[i]); 120 } 121 122 return count; 123 } 124 130 protected int countSelectedResources() throws CoreException { 131 int result = 0; 132 Iterator resources = resourcesToExport.iterator(); 133 while (resources.hasNext()) 134 result += countChildrenOf((IResource) resources.next()); 135 136 return result; 137 } 138 139 145 protected void exportResource(IResource exportResource) 146 throws InterruptedException { 147 exportResource(exportResource, 1); 148 } 149 150 157 protected void exportResource(IResource exportResource, int leadupDepth) 158 throws InterruptedException { 159 if (!exportResource.isAccessible()) 160 return; 161 162 if (exportResource.getType() == IResource.FILE) { 163 String destinationName; 164 IPath fullPath = exportResource.getFullPath(); 165 if (createLeadupStructure) 166 destinationName = fullPath.makeRelative().toString(); 167 else 168 destinationName = 169 fullPath 170 .removeFirstSegments( 171 fullPath.segmentCount() - leadupDepth) 172 .toString(); 173 monitor.subTask(destinationName); 174 175 try { 176 exporter.write((IFile) exportResource, destinationName); 177 } catch (IOException e) { 178 addError(DataTransferMessages.format("DataTransfer.errorExporting", new Object [] { 180 exportResource.getFullPath().makeRelative(), 181 e.getMessage()}), 182 e); 183 } catch (CoreException e) { 184 addError(DataTransferMessages.format("DataTransfer.errorExporting", new Object [] { 186 exportResource.getFullPath().makeRelative(), 187 e.getMessage()}), 188 e); 189 } 190 191 monitor.worked(1); 192 ModalContext.checkCanceled(monitor); 193 } else { 194 IResource[] children = null; 195 196 try { 197 children = ((IContainer) exportResource).members(); 198 } catch (CoreException e) { 199 addError(DataTransferMessages.format("DataTransfer.errorExporting", new Object [] { exportResource.getFullPath()}), e); } 202 203 for (int i = 0; i < children.length; i++) 204 exportResource(children[i], leadupDepth + 1); 205 206 } 207 } 208 212 protected void exportSpecifiedResources() throws InterruptedException { 213 Iterator resources = resourcesToExport.iterator(); 214 215 while (resources.hasNext()) { 216 IResource currentResource = (IResource) resources.next(); 217 exportResource(currentResource); 218 } 219 } 220 225 public List getResult() { 226 return errorTable; 227 } 228 236 public IStatus getStatus() { 237 IStatus[] errors = new IStatus[errorTable.size()]; 238 errorTable.toArray(errors); 239 return new MultiStatus(IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.OK, errors, DataTransferMessages.getString("FileSystemExportOperation.problemsExporting"), null); 241 } 242 247 protected void initialize() throws IOException { 248 exporter = 249 new ZipFileExporter( 250 destinationFilename, 251 useCompression, 252 generateManifestFile); 253 254 } 255 263 protected boolean isDescendent(List resources, IResource child) { 264 if (child.getType() == IResource.PROJECT) 265 return false; 266 267 IResource parent = child.getParent(); 268 if (resources.contains(parent)) 269 return true; 270 271 return isDescendent(resources, parent); 272 } 273 277 public void run(IProgressMonitor progressMonitor) 278 throws InvocationTargetException , InterruptedException { 279 this.monitor = progressMonitor; 280 281 try { 282 initialize(); 283 } catch (IOException e) { 284 throw new InvocationTargetException (e, DataTransferMessages.format("ZipExport.cannotOpen", new Object [] { e.getMessage()})); } 286 287 try { 288 int totalWork = IProgressMonitor.UNKNOWN; 290 try { 291 if (resourcesToExport == null) 292 totalWork = countChildrenOf(resource); 293 else 294 totalWork = countSelectedResources(); 295 } catch (CoreException e) { 296 } 298 monitor.beginTask(DataTransferMessages.getString("DataTransfer.exportingTitle"), totalWork); if (resourcesToExport == null) { 300 exportResource(resource); 301 } else { 302 exportSpecifiedResources(); 304 } 305 306 try { 307 exporter.finished(); 308 } catch (IOException e) { 309 throw new InvocationTargetException (e, DataTransferMessages.format("ZipExport.cannotClose", new Object [] { e.getMessage()})); } 311 } finally { 312 monitor.done(); 313 } 314 } 315 321 public void setCreateLeadupStructure(boolean value) { 322 createLeadupStructure = value; 323 } 324 331 public void setGenerateManifestFile(boolean value) { 332 generateManifestFile = value; 333 } 334 340 public void setUseCompression(boolean value) { 341 useCompression = value; 342 } 343 } 344 | Popular Tags |