KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > samples > SampleOperation


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

11 package org.eclipse.pde.internal.ui.samples;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.Properties JavaDoc;
20 import java.util.zip.ZipFile JavaDoc;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IWorkspace;
25 import org.eclipse.core.resources.IWorkspaceRoot;
26 import org.eclipse.core.resources.IWorkspaceRunnable;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.FileLocator;
29 import org.eclipse.core.runtime.IConfigurationElement;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.core.runtime.NullProgressMonitor;
34 import org.eclipse.core.runtime.OperationCanceledException;
35 import org.eclipse.core.runtime.Platform;
36 import org.eclipse.core.runtime.Status;
37 import org.eclipse.core.runtime.SubProgressMonitor;
38 import org.eclipse.jface.operation.IRunnableWithProgress;
39 import org.eclipse.pde.internal.ui.IPDEUIConstants;
40 import org.eclipse.pde.internal.ui.PDEPlugin;
41 import org.eclipse.pde.internal.ui.PDEUIMessages;
42 import org.eclipse.ui.dialogs.IOverwriteQuery;
43 import org.eclipse.ui.wizards.datatransfer.ImportOperation;
44 import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
45 import org.osgi.framework.Bundle;
46
47 public class SampleOperation implements IRunnableWithProgress {
48     private static final String JavaDoc SAMPLE_PROPERTIES = "sample.properties"; //$NON-NLS-1$
49

50     private IConfigurationElement sample;
51
52     private String JavaDoc[] projectNames;
53
54     private IFile sampleManifest;
55
56     private IOverwriteQuery query;
57     
58     private boolean yesToAll;
59     
60     private boolean cancel;
61
62     private IProject[] createdProjects;
63
64     /**
65      *
66      */

67     public SampleOperation(IConfigurationElement sample, String JavaDoc[] projectNames,
68             IOverwriteQuery query) {
69         this.sample = sample;
70         this.query = query;
71         this.projectNames = projectNames;
72     }
73
74     public IFile getSampleManifest() {
75         return sampleManifest;
76     }
77
78     public IProject[] getCreatedProjects() {
79         return createdProjects;
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
86      */

87     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
88             InterruptedException JavaDoc {
89         try {
90             IWorkspaceRunnable op = new IWorkspaceRunnable() {
91                 public void run(IProgressMonitor monitor) throws CoreException {
92                     IConfigurationElement[] projects = sample
93                             .getChildren("project"); //$NON-NLS-1$
94
monitor
95                             .beginTask(
96                                     PDEUIMessages.SampleOperation_creating, 4 * projects.length);
97                     createdProjects = new IProject[projects.length];
98                     try {
99                     for (int i = 0; i < projects.length; i++) {
100                         IFile file = importProject(projectNames[i],
101                                 projects[i], new SubProgressMonitor(monitor, 4));
102                         if (file != null && sampleManifest == null)
103                             sampleManifest = file;
104                         if (file != null) {
105                             createdProjects[i] = file.getProject();
106                         }
107                         if(cancel)
108                             // if user has cancelled operation, exit.
109
break;
110                     }
111                     }
112                     catch (InterruptedException JavaDoc e) {
113                         throw new OperationCanceledException();
114                     }
115                     catch (InvocationTargetException JavaDoc e) {
116                         throwCoreException(e);
117                     }
118                 }
119             };
120             PDEPlugin.getWorkspace().run(op, monitor);
121         } catch (CoreException e) {
122             throw new InvocationTargetException JavaDoc(e);
123         } catch (OperationCanceledException e) {
124             throw e;
125         } finally {
126             monitor.done();
127         }
128     }
129     
130     private void throwCoreException(InvocationTargetException JavaDoc e) throws CoreException {
131         Throwable JavaDoc t = e.getCause();
132         Status status= new Status(IStatus.ERROR,
133                 IPDEUIConstants.PLUGIN_ID,
134                 IStatus.OK,
135                 e.getMessage(),
136                 t);
137         throw new CoreException(status);
138     }
139
140     private IFile importProject(String JavaDoc name, IConfigurationElement config,
141             IProgressMonitor monitor) throws CoreException,
142             InvocationTargetException JavaDoc, InterruptedException JavaDoc {
143         String JavaDoc path = config.getAttribute("archive"); //$NON-NLS-1$
144
if (name == null || path == null)
145             return null;
146         IWorkspace workspace = PDEPlugin.getWorkspace();
147         IWorkspaceRoot root = workspace.getRoot();
148         IProject project = root.getProject(name);
149         boolean skip = false;
150         if (project.exists()) {
151             if (!yesToAll) {
152                 String JavaDoc returnId = query.queryOverwrite(project.getFullPath()
153                     .toString());
154                 if (returnId.equals(IOverwriteQuery.ALL)) {
155                     yesToAll = true;
156                     skip = false;
157                 } else if (returnId.equals(IOverwriteQuery.YES)) {
158                     skip = false;
159                 } else if (returnId.equals(IOverwriteQuery.NO)) {
160                     skip = true;
161                 } else if (returnId.equals(IOverwriteQuery.CANCEL)) {
162                     skip = true;
163                     cancel = true;
164                 }
165             }
166             if (!skip) {
167                 project.delete(true, true, new SubProgressMonitor(monitor, 1));
168                 project = root.getProject(name);
169             } else
170                 monitor.worked(1);
171         }
172         if (skip) {
173             monitor.worked(3);
174             IFile manifest = project.getFile(SAMPLE_PROPERTIES);
175             return manifest;
176         }
177         
178         project.create(new SubProgressMonitor(monitor, 1));
179         project.open(new NullProgressMonitor());
180         Bundle bundle = Platform.getBundle(sample.getNamespaceIdentifier());
181         ZipFile JavaDoc zipFile = getZipFileFromPluginDir(path, bundle);
182         importFilesFromZip(zipFile, project.getFullPath(),
183                 new SubProgressMonitor(monitor, 1));
184         return createSampleManifest(project, config, new SubProgressMonitor(
185                 monitor, 1));
186     }
187
188     private IFile createSampleManifest(IProject project,
189             IConfigurationElement config, IProgressMonitor monitor)
190             throws CoreException {
191         IFile file = project.getFile(SAMPLE_PROPERTIES);
192         if (!file.exists()) {
193             try {
194                 ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
195                 Properties JavaDoc properties = new Properties JavaDoc();
196                 createSampleManifestContent(
197                         config.getAttribute("name"), properties); //$NON-NLS-1$
198
properties.store(out, ""); //$NON-NLS-1$
199
out.flush();
200                 String JavaDoc contents = out.toString();
201                 out.close();
202                 ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(contents
203                         .getBytes("UTF8")); //$NON-NLS-1$
204
file.create(stream, true, monitor);
205                 stream.close();
206             } catch (UnsupportedEncodingException JavaDoc e) {
207             } catch (IOException JavaDoc e) {
208             }
209         }
210         return file;
211     }
212
213     private void createSampleManifestContent(String JavaDoc projectName,
214             Properties JavaDoc properties) {
215         writeProperty(properties, "id", sample.getAttribute("id")); //$NON-NLS-1$ //$NON-NLS-2$
216
writeProperty(properties, "name", sample.getAttribute("name")); //$NON-NLS-1$ //$NON-NLS-2$
217
writeProperty(properties, "projectName", projectName); //$NON-NLS-1$
218
writeProperty(properties, "launcher", sample.getAttribute("launcher")); //$NON-NLS-1$ //$NON-NLS-2$
219
IConfigurationElement desc[] = sample.getChildren("description"); //$NON-NLS-1$
220
if (desc.length == 1) {
221             writeProperty(properties, "helpHref", desc[0] //$NON-NLS-1$
222
.getAttribute("helpHref")); //$NON-NLS-1$
223
writeProperty(properties, "description", desc[0].getValue()); //$NON-NLS-1$
224
}
225     }
226
227     private void writeProperty(Properties JavaDoc properties, String JavaDoc name, String JavaDoc value) {
228         if (value == null)
229             return;
230         properties.setProperty(name, value);
231     }
232
233     private ZipFile JavaDoc getZipFileFromPluginDir(String JavaDoc pluginRelativePath,
234             Bundle bundle) throws CoreException {
235         try {
236             URL JavaDoc starterURL = FileLocator.resolve(bundle.getEntry(pluginRelativePath));
237             return new ZipFile JavaDoc(FileLocator.toFileURL(starterURL).getFile());
238         } catch (IOException JavaDoc e) {
239             String JavaDoc message = pluginRelativePath + ": " + e.getMessage(); //$NON-NLS-1$
240
Status status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(),
241                     IStatus.ERROR, message, e);
242             throw new CoreException(status);
243         }
244     }
245
246     private void importFilesFromZip(ZipFile JavaDoc srcZipFile, IPath destPath,
247             IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
248             InterruptedException JavaDoc {
249         ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(
250                 srcZipFile);
251         ImportOperation op = new ImportOperation(destPath, structureProvider
252                 .getRoot(), structureProvider, query);
253         op.run(monitor);
254     }
255 }
256
Popular Tags