1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import org.eclipse.core.resources.*; 14 import org.eclipse.core.runtime.*; 15 import org.eclipse.ui.PlatformUI; 16 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 17 import org.eclipse.jface.operation.*; 18 import java.io.IOException ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.util.*; 21 22 25 class ZipFileResourceExportOperation implements IRunnableWithProgress { 26 private ZipFileResourceExporter exporter; 27 private String destinationFilename; 28 private IProgressMonitor monitor; 29 private int leadupStartDepth = 0; 30 private List resourcesToExport; 31 private IResource resource; 32 private List errorTable = new ArrayList(1); 34 private boolean useCompression = true; 35 private boolean createLeadupStructure = true; 36 private boolean generateManifestFile = false; 37 44 public ZipFileResourceExportOperation(List resources,String filename) { 45 super(); 46 47 Iterator elementsEnum = resources.iterator(); 49 while (elementsEnum.hasNext()) { 50 IResource currentResource = (IResource) elementsEnum.next(); 51 if (isDescendent(resources, currentResource)) 52 elementsEnum.remove(); } 54 55 resourcesToExport = resources; 56 destinationFilename = filename; 57 } 58 65 public ZipFileResourceExportOperation(IResource res,String filename) { 66 super(); 67 resource = res; 68 destinationFilename = filename; 69 } 70 79 public ZipFileResourceExportOperation(IResource res, List resources, String filename) { 80 this(res,filename); 81 resourcesToExport = resources; 82 } 83 86 protected void addError(String message,Throwable e) { 87 errorTable.add( 88 new Status( 89 IStatus.ERROR, 90 PlatformUI.PLUGIN_ID, 91 0, 92 message, 93 e)); 94 } 95 102 protected int countChildrenOf(IResource resource) throws CoreException { 103 if (resource.getType() == IResource.FILE) 104 return 1; 105 106 int count = 0; 107 if (resource.isAccessible()) { 108 IResource[] children = ((IContainer) resource).members(); 109 for (int i = 0; i<children.length; i++) 110 count += countChildrenOf(children[i]); 111 } 112 113 return count; 114 } 115 121 protected int countSelectedResources() throws CoreException { 122 int result = 0; 123 Iterator resources = resourcesToExport.iterator(); 124 while (resources.hasNext()) 125 result += countChildrenOf((IResource)resources.next()); 126 127 return result; 128 } 129 134 protected void exportResource(IResource resource) throws InterruptedException { 135 if (!resource.isAccessible()) 136 return; 137 138 if (resource.getType() == IResource.FILE) { 139 String destinationName = resource.getFullPath().removeFirstSegments(leadupStartDepth).toString(); 140 monitor.subTask(destinationName); 141 142 try { 143 exporter.write((IFile)resource,destinationName); 144 } catch (IOException e) { 145 addError(IDEWorkbenchMessages.format("ZipExport.errorOnResource", new Object [] {resource.getFullPath()}) ,e); } catch (CoreException e) { 147 addError(IDEWorkbenchMessages.format("ZipExport.errorOnResource", new Object [] {resource.getFullPath()}) ,e); } 149 150 monitor.worked(1); 151 ModalContext.checkCanceled(monitor); 152 } else { 153 IResource[] children = null; 154 155 try { 156 children = ((IContainer)resource).members(); 157 } catch (CoreException e) { 158 addError(IDEWorkbenchMessages.format("ZipExport.errorOnResource", new Object [] {resource.getFullPath()}) ,e); } 161 162 for (int i = 0; i<children.length; i++) 163 exportResource(children[i]); 164 } 165 } 166 170 protected void exportSpecifiedResources() throws InterruptedException { 171 Iterator resources = resourcesToExport.iterator(); 172 173 while (resources.hasNext()) { 174 IResource currentResource = (IResource)resources.next(); 175 if (resource == null && !createLeadupStructure) 176 leadupStartDepth = currentResource.getFullPath().segmentCount() - 1; 177 178 exportResource(currentResource); 179 } 180 } 181 186 public List getResult() { 187 return errorTable; 188 } 189 197 public IStatus getStatus() { 198 IStatus[] errors = new IStatus[errorTable.size()]; 199 errorTable.toArray(errors); 200 return new MultiStatus( 201 PlatformUI.PLUGIN_ID, 202 IStatus.OK, 203 errors, 204 IDEWorkbenchMessages.getString("ZipExport.problemEncountered"), null); 206 } 207 212 protected void initialize() throws IOException { 213 exporter = new ZipFileResourceExporter(destinationFilename,useCompression,generateManifestFile); 214 215 if (resource == null) leadupStartDepth = 1; 217 else { 218 leadupStartDepth = resource.getFullPath().segmentCount(); 219 220 if (resource.getType() == IResource.FILE) 221 leadupStartDepth--; 222 223 if (createLeadupStructure) 224 leadupStartDepth = Math.min(1,leadupStartDepth); 225 } 226 } 227 235 protected boolean isDescendent(List resources, IResource child) { 236 if (child.getType() == IResource.PROJECT) 237 return false; 238 239 IResource parent = child.getParent(); 240 if (resources.contains(parent)) 241 return true; 242 243 return isDescendent(resources,parent); 244 } 245 249 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 250 this.monitor = monitor; 251 252 try { 253 initialize(); 254 } catch (IOException e) { 255 throw new InvocationTargetException (e, IDEWorkbenchMessages.getString("ZipExport.unableToOpen") + e.getMessage()); } 257 258 try { 259 int totalWork = IProgressMonitor.UNKNOWN; 261 try { 262 if (resourcesToExport == null) 263 totalWork = countChildrenOf(resource); 264 else 265 totalWork = countSelectedResources(); 266 } 267 catch (CoreException e) { 268 } 270 monitor.beginTask(IDEWorkbenchMessages.getString("ZipExport.progress"), totalWork); if (resourcesToExport == null) { 272 exportResource(resource); 273 } 274 else { 275 exportSpecifiedResources(); 277 } 278 279 try { 280 exporter.finished(); 281 } catch (IOException e) { 282 throw new InvocationTargetException (e, IDEWorkbenchMessages.getString("ZipExport.unableToClose") + e.getMessage()); } 284 } finally { 285 monitor.done(); 286 } 287 } 288 294 public void setCreateLeadupStructure(boolean value) { 295 createLeadupStructure = value; 296 } 297 304 public void setGenerateManifestFile(boolean value) { 305 generateManifestFile = value; 306 } 307 313 public void setUseCompression(boolean value) { 314 useCompression = value; 315 } 316 } 317 | Popular Tags |