001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.samples;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.io.UnsupportedEncodingException;
016: import java.lang.reflect.InvocationTargetException;
017: import java.net.URL;
018: import java.util.Properties;
019: import java.util.zip.ZipFile;
020:
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.resources.IProject;
023: import org.eclipse.core.resources.IWorkspace;
024: import org.eclipse.core.resources.IWorkspaceRoot;
025: import org.eclipse.core.resources.IWorkspaceRunnable;
026: import org.eclipse.core.runtime.CoreException;
027: import org.eclipse.core.runtime.FileLocator;
028: import org.eclipse.core.runtime.IConfigurationElement;
029: import org.eclipse.core.runtime.IPath;
030: import org.eclipse.core.runtime.IProgressMonitor;
031: import org.eclipse.core.runtime.IStatus;
032: import org.eclipse.core.runtime.NullProgressMonitor;
033: import org.eclipse.core.runtime.OperationCanceledException;
034: import org.eclipse.core.runtime.Platform;
035: import org.eclipse.core.runtime.Status;
036: import org.eclipse.core.runtime.SubProgressMonitor;
037: import org.eclipse.jface.operation.IRunnableWithProgress;
038: import org.eclipse.pde.internal.ui.IPDEUIConstants;
039: import org.eclipse.pde.internal.ui.PDEPlugin;
040: import org.eclipse.pde.internal.ui.PDEUIMessages;
041: import org.eclipse.ui.dialogs.IOverwriteQuery;
042: import org.eclipse.ui.wizards.datatransfer.ImportOperation;
043: import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
044: import org.osgi.framework.Bundle;
045:
046: public class SampleOperation implements IRunnableWithProgress {
047: private static final String SAMPLE_PROPERTIES = "sample.properties"; //$NON-NLS-1$
048:
049: private IConfigurationElement sample;
050:
051: private String[] projectNames;
052:
053: private IFile sampleManifest;
054:
055: private IOverwriteQuery query;
056:
057: private boolean yesToAll;
058:
059: private boolean cancel;
060:
061: private IProject[] createdProjects;
062:
063: /**
064: *
065: */
066: public SampleOperation(IConfigurationElement sample,
067: String[] projectNames, IOverwriteQuery query) {
068: this .sample = sample;
069: this .query = query;
070: this .projectNames = projectNames;
071: }
072:
073: public IFile getSampleManifest() {
074: return sampleManifest;
075: }
076:
077: public IProject[] getCreatedProjects() {
078: return createdProjects;
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
085: */
086: public void run(IProgressMonitor monitor)
087: throws InvocationTargetException, InterruptedException {
088: try {
089: IWorkspaceRunnable op = new IWorkspaceRunnable() {
090: public void run(IProgressMonitor monitor)
091: throws CoreException {
092: IConfigurationElement[] projects = sample
093: .getChildren("project"); //$NON-NLS-1$
094: monitor.beginTask(
095: PDEUIMessages.SampleOperation_creating,
096: 4 * projects.length);
097: createdProjects = new IProject[projects.length];
098: try {
099: for (int i = 0; i < projects.length; i++) {
100: IFile file = importProject(projectNames[i],
101: projects[i],
102: new SubProgressMonitor(monitor, 4));
103: if (file != null && sampleManifest == null)
104: sampleManifest = file;
105: if (file != null) {
106: createdProjects[i] = file.getProject();
107: }
108: if (cancel)
109: // if user has cancelled operation, exit.
110: break;
111: }
112: } catch (InterruptedException e) {
113: throw new OperationCanceledException();
114: } catch (InvocationTargetException e) {
115: throwCoreException(e);
116: }
117: }
118: };
119: PDEPlugin.getWorkspace().run(op, monitor);
120: } catch (CoreException e) {
121: throw new InvocationTargetException(e);
122: } catch (OperationCanceledException e) {
123: throw e;
124: } finally {
125: monitor.done();
126: }
127: }
128:
129: private void throwCoreException(InvocationTargetException e)
130: throws CoreException {
131: Throwable t = e.getCause();
132: Status status = new Status(IStatus.ERROR,
133: IPDEUIConstants.PLUGIN_ID, IStatus.OK, e.getMessage(),
134: t);
135: throw new CoreException(status);
136: }
137:
138: private IFile importProject(String name,
139: IConfigurationElement config, IProgressMonitor monitor)
140: throws CoreException, InvocationTargetException,
141: InterruptedException {
142: String path = config.getAttribute("archive"); //$NON-NLS-1$
143: if (name == null || path == null)
144: return null;
145: IWorkspace workspace = PDEPlugin.getWorkspace();
146: IWorkspaceRoot root = workspace.getRoot();
147: IProject project = root.getProject(name);
148: boolean skip = false;
149: if (project.exists()) {
150: if (!yesToAll) {
151: String returnId = query.queryOverwrite(project
152: .getFullPath().toString());
153: if (returnId.equals(IOverwriteQuery.ALL)) {
154: yesToAll = true;
155: skip = false;
156: } else if (returnId.equals(IOverwriteQuery.YES)) {
157: skip = false;
158: } else if (returnId.equals(IOverwriteQuery.NO)) {
159: skip = true;
160: } else if (returnId.equals(IOverwriteQuery.CANCEL)) {
161: skip = true;
162: cancel = true;
163: }
164: }
165: if (!skip) {
166: project.delete(true, true, new SubProgressMonitor(
167: 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
181: .getNamespaceIdentifier());
182: ZipFile zipFile = getZipFileFromPluginDir(path, bundle);
183: importFilesFromZip(zipFile, project.getFullPath(),
184: new SubProgressMonitor(monitor, 1));
185: return createSampleManifest(project, config,
186: new SubProgressMonitor(monitor, 1));
187: }
188:
189: private IFile createSampleManifest(IProject project,
190: IConfigurationElement config, IProgressMonitor monitor)
191: throws CoreException {
192: IFile file = project.getFile(SAMPLE_PROPERTIES);
193: if (!file.exists()) {
194: try {
195: ByteArrayOutputStream out = new ByteArrayOutputStream();
196: Properties properties = new Properties();
197: createSampleManifestContent(
198: config.getAttribute("name"), properties); //$NON-NLS-1$
199: properties.store(out, ""); //$NON-NLS-1$
200: out.flush();
201: String contents = out.toString();
202: out.close();
203: ByteArrayInputStream stream = new ByteArrayInputStream(
204: contents.getBytes("UTF8")); //$NON-NLS-1$
205: file.create(stream, true, monitor);
206: stream.close();
207: } catch (UnsupportedEncodingException e) {
208: } catch (IOException e) {
209: }
210: }
211: return file;
212: }
213:
214: private void createSampleManifestContent(String projectName,
215: Properties properties) {
216: writeProperty(properties, "id", sample.getAttribute("id")); //$NON-NLS-1$ //$NON-NLS-2$
217: writeProperty(properties, "name", sample.getAttribute("name")); //$NON-NLS-1$ //$NON-NLS-2$
218: writeProperty(properties, "projectName", projectName); //$NON-NLS-1$
219: writeProperty(properties,
220: "launcher", sample.getAttribute("launcher")); //$NON-NLS-1$ //$NON-NLS-2$
221: IConfigurationElement desc[] = sample
222: .getChildren("description"); //$NON-NLS-1$
223: if (desc.length == 1) {
224: writeProperty(properties, "helpHref", desc[0] //$NON-NLS-1$
225: .getAttribute("helpHref")); //$NON-NLS-1$
226: writeProperty(properties, "description", desc[0].getValue()); //$NON-NLS-1$
227: }
228: }
229:
230: private void writeProperty(Properties properties, String name,
231: String value) {
232: if (value == null)
233: return;
234: properties.setProperty(name, value);
235: }
236:
237: private ZipFile getZipFileFromPluginDir(String pluginRelativePath,
238: Bundle bundle) throws CoreException {
239: try {
240: URL starterURL = FileLocator.resolve(bundle
241: .getEntry(pluginRelativePath));
242: return new ZipFile(FileLocator.toFileURL(starterURL)
243: .getFile());
244: } catch (IOException e) {
245: String message = pluginRelativePath + ": " + e.getMessage(); //$NON-NLS-1$
246: Status status = new Status(IStatus.ERROR, PDEPlugin
247: .getPluginId(), IStatus.ERROR, message, e);
248: throw new CoreException(status);
249: }
250: }
251:
252: private void importFilesFromZip(ZipFile srcZipFile, IPath destPath,
253: IProgressMonitor monitor) throws InvocationTargetException,
254: InterruptedException {
255: ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(
256: srcZipFile);
257: ImportOperation op = new ImportOperation(destPath,
258: structureProvider.getRoot(), structureProvider, query);
259: op.run(monitor);
260: }
261: }
|