KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > ZipFileResourceExportOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.util.*;
21
22 /**
23  * Operation for exporting a resource and its children to a new .zip file
24  */

25 /*package*/ class ZipFileResourceExportOperation implements IRunnableWithProgress {
26     private ZipFileResourceExporter exporter;
27     private String JavaDoc 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); //IStatus
33

34     private boolean useCompression = true;
35     private boolean createLeadupStructure = true;
36     private boolean generateManifestFile = false;
37 /**
38  * Create an instance of this class. Use this constructor if you wish to
39  * export specific resources without a common parent resource
40  *
41  * @param resources java.util.Vector
42  * @param filename java.lang.String
43  */

44 public ZipFileResourceExportOperation(List resources,String JavaDoc filename) {
45     super();
46
47     // Eliminate redundancies in list of resources being exported
48
Iterator elementsEnum = resources.iterator();
49     while (elementsEnum.hasNext()) {
50         IResource currentResource = (IResource) elementsEnum.next();
51         if (isDescendent(resources, currentResource))
52             elementsEnum.remove(); //Removes currentResource;
53
}
54
55     resourcesToExport = resources;
56     destinationFilename = filename;
57 }
58 /**
59  * Create an instance of this class. Use this constructor if you wish
60  * to recursively export a single resource.
61  *
62  * @param res org.eclipse.core.resources.IResource;
63  * @param filename java.lang.String
64  */

65 public ZipFileResourceExportOperation(IResource res,String JavaDoc filename) {
66     super();
67     resource = res;
68     destinationFilename = filename;
69 }
70 /**
71  * Create an instance of this class. Use this constructor if you wish to
72  * export specific resources with a common parent resource (affects container
73  * directory creation)
74  *
75  * @param res org.eclipse.core.resources.IResource
76  * @param resources java.util.Vector
77  * @param filename java.lang.String
78  */

79 public ZipFileResourceExportOperation(IResource res, List resources, String JavaDoc filename) {
80     this(res,filename);
81     resourcesToExport = resources;
82 }
83 /**
84  * Add a new entry to the error table with the passed information
85  */

86 protected void addError(String JavaDoc message,Throwable JavaDoc e) {
87     errorTable.add(
88         new Status(
89             IStatus.ERROR,
90             PlatformUI.PLUGIN_ID,
91             0,
92             message,
93             e));
94 }
95 /**
96  * Answer the total number of file resources that exist at or below self
97  * in the resources hierarchy.
98  *
99  * @return int
100  * @param resource org.eclipse.core.resources.IResource
101  */

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 /**
116  * Answer a boolean indicating the number of file resources that were
117  * specified for export
118  *
119  * @return int
120  */

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 /**
130  * Export the passed resource to the destination .zip
131  *
132  * @param resource org.eclipse.core.resources.IResource
133  */

134 protected void exportResource(IResource resource) throws InterruptedException JavaDoc {
135     if (!resource.isAccessible())
136         return;
137         
138     if (resource.getType() == IResource.FILE) {
139         String JavaDoc destinationName = resource.getFullPath().removeFirstSegments(leadupStartDepth).toString();
140         monitor.subTask(destinationName);
141
142         try {
143             exporter.write((IFile)resource,destinationName);
144         } catch (IOException JavaDoc e) {
145             addError(IDEWorkbenchMessages.format("ZipExport.errorOnResource", new Object JavaDoc[] {resource.getFullPath()}) ,e); //$NON-NLS-1$
146
} catch (CoreException e) {
147             addError(IDEWorkbenchMessages.format("ZipExport.errorOnResource", new Object JavaDoc[] {resource.getFullPath()}) ,e); //$NON-NLS-1$
148
}
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             // this should never happen because an #isAccessible check is done before #members is invoked
159
addError(IDEWorkbenchMessages.format("ZipExport.errorOnResource", new Object JavaDoc[] {resource.getFullPath()}) ,e); //$NON-NLS-1$
160
}
161
162         for (int i = 0; i<children.length; i++)
163             exportResource(children[i]);
164     }
165 }
166 /**
167  * Export the resources contained in the previously-defined
168  * resourcesToExport collection
169  */

170 protected void exportSpecifiedResources() throws InterruptedException JavaDoc {
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 /**
182  * Answer the error table
183  *
184  * @return Vector of IStatus
185  */

186 public List getResult() {
187     return errorTable;
188 }
189 /**
190  * Returns the status of the operation.
191  * If there were any errors, the result is a status object containing
192  * individual status objects for each error.
193  * If there were no errors, the result is a status object with error code <code>OK</code>.
194  *
195  * @return the status
196  */

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"), //$NON-NLS-1$
205
null);
206 }
207 /**
208  * Initialize this operation
209  *
210  * @exception java.io.IOException
211  */

212 protected void initialize() throws IOException JavaDoc {
213     exporter = new ZipFileResourceExporter(destinationFilename,useCompression,generateManifestFile);
214
215     if (resource == null) // ie.- no parent resource was specified so just strip out projects
216
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 /**
228  * Answer a boolean indicating whether the passed child is a descendent
229  * of one or more members of the passed resources collection
230  *
231  * @return boolean
232  * @param resources java.util.Vector
233  * @param child org.eclipse.core.resources.IResource
234  */

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 /**
246  * Export the resources that were previously specified for export
247  * (or if a single resource was specified then export it recursively)
248  */

249 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
250     this.monitor = monitor;
251
252     try {
253         initialize();
254     } catch (IOException JavaDoc e) {
255         throw new InvocationTargetException JavaDoc(e, IDEWorkbenchMessages.getString("ZipExport.unableToOpen") + e.getMessage()); //$NON-NLS-1$
256
}
257
258     try {
259         // ie.- a single resource for recursive export was specified
260
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             // Should not happen
269
}
270         monitor.beginTask(IDEWorkbenchMessages.getString("ZipExport.progress"), totalWork); //$NON-NLS-1$
271
if (resourcesToExport == null) {
272             exportResource(resource);
273         }
274         else {
275             // ie.- a list of specific resources to export was specified
276
exportSpecifiedResources();
277         }
278
279         try {
280             exporter.finished();
281         } catch (IOException JavaDoc e) {
282             throw new InvocationTargetException JavaDoc(e, IDEWorkbenchMessages.getString("ZipExport.unableToClose") + e.getMessage()); //$NON-NLS-1$
283
}
284     } finally {
285         monitor.done();
286     }
287 }
288 /**
289  * Set this boolean indicating whether each exported resource's path should
290  * include containment hierarchies as dictated by its parents
291  *
292  * @param value boolean
293  */

294 public void setCreateLeadupStructure(boolean value) {
295     createLeadupStructure = value;
296 }
297 /**
298  * Set this boolean indicating whether a manifest.mf file based upon
299  * the exported contents should be created and included in the final
300  * archive
301  *
302  * @param value boolean
303  */

304 public void setGenerateManifestFile(boolean value) {
305     generateManifestFile = value;
306 }
307 /**
308  * Set this boolean indicating whether exported resources should
309  * be compressed (as opposed to simply being stored)
310  *
311  * @param value boolean
312  */

313 public void setUseCompression(boolean value) {
314     useCompression = value;
315 }
316 }
317
Popular Tags