KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.internal.samples;
12
13 import java.io.*;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Properties JavaDoc;
17 import java.util.zip.ZipFile JavaDoc;
18
19 import org.eclipse.core.resources.*;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.pde.internal.ui.PDEPlugin;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.ui.dialogs.IOverwriteQuery;
25 import org.eclipse.ui.wizards.datatransfer.*;
26 import org.osgi.framework.*;
27
28 public class SampleOperation implements IRunnableWithProgress {
29     private static final String JavaDoc SAMPLE_PROPERTIES = "sample.properties"; //$NON-NLS-1$
30

31     private IConfigurationElement sample;
32
33     private String JavaDoc[] projectNames;
34
35     private IFile sampleManifest;
36
37     private IOverwriteQuery query;
38     
39     private boolean yesToAll;
40     
41     private boolean cancel;
42
43     private IProject[] createdProjects;
44
45     /**
46      *
47      */

48     public SampleOperation(IConfigurationElement sample, String JavaDoc[] projectNames,
49             IOverwriteQuery query) {
50         this.sample = sample;
51         this.query = query;
52         this.projectNames = projectNames;
53     }
54
55     public IFile getSampleManifest() {
56         return sampleManifest;
57     }
58
59     public IProject[] getCreatedProjects() {
60         return createdProjects;
61     }
62
63     /*
64      * (non-Javadoc)
65      *
66      * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
67      */

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