blob: 69c4b5197f3eabf7858515080099ec8a1af4e889 [file] [log] [blame]
jeffliuec1c4782006-05-24 14:16:24 +00001/*******************************************************************************
2 * Copyright (c) 2002 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 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
david_williams4c93c712005-04-16 01:16:58 +000013package org.eclipse.wst.xml.ui.internal.wizards;
14
15import java.io.IOException;
16import java.lang.reflect.InvocationTargetException;
17import java.net.URL;
18import java.util.zip.ZipFile;
19
20import org.eclipse.core.resources.IFolder;
21import org.eclipse.core.resources.IProject;
22import org.eclipse.core.resources.IProjectDescription;
23import org.eclipse.core.resources.IResource;
24import org.eclipse.core.resources.IWorkspaceRoot;
25import org.eclipse.core.resources.ResourcesPlugin;
26import org.eclipse.core.runtime.CoreException;
27import org.eclipse.core.runtime.IConfigurationElement;
28import org.eclipse.core.runtime.IExtension;
29import org.eclipse.core.runtime.IPath;
30import org.eclipse.core.runtime.IProgressMonitor;
31import org.eclipse.core.runtime.IStatus;
32import org.eclipse.core.runtime.NullProgressMonitor;
33import org.eclipse.core.runtime.Path;
34import org.eclipse.core.runtime.Platform;
35import org.eclipse.core.runtime.Status;
36import org.eclipse.core.runtime.SubProgressMonitor;
37import org.eclipse.jface.operation.IRunnableWithProgress;
38import org.eclipse.ui.dialogs.IOverwriteQuery;
39import org.eclipse.ui.wizards.datatransfer.ImportOperation;
40import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
nitind3329cf82005-05-17 18:17:55 +000041import org.eclipse.wst.xml.ui.internal.Logger;
42import org.eclipse.wst.xml.ui.internal.XMLUIPlugin;
david_williams4c93c712005-04-16 01:16:58 +000043import org.osgi.framework.Bundle;
44
45public class ExampleProjectCreationOperation implements IRunnableWithProgress {
46
47 private IResource elementToOpen;
48
49 private IOverwriteQuery overwriteQuery;
50
51 private ExampleProjectCreationWizardPage[] pages;
52
53 /**
54 * Constructor for ExampleProjectCreationOperation
55 */
56 public ExampleProjectCreationOperation(ExampleProjectCreationWizardPage[] myPages, IOverwriteQuery myOverwriteQuery) {
57 elementToOpen = null;
58 pages = myPages;
59 overwriteQuery = myOverwriteQuery;
60 }
61
62 private IProject configNewProject(IWorkspaceRoot root, String name, String[] natureIds, IProject[] referencedProjects, IProgressMonitor monitor) throws InvocationTargetException {
63 try {
64 IProject project = root.getProject(name);
65 if (!project.exists()) {
66 project.create(null);
67 }
68 if (!project.isOpen()) {
69 project.open(null);
70 }
71 IProjectDescription desc = project.getDescription();
72 desc.setLocation(null);
73 desc.setNatureIds(natureIds);
74 desc.setReferencedProjects(referencedProjects);
75
76 project.setDescription(desc, new SubProgressMonitor(monitor, 1));
77
78 return project;
79 }
80 catch (CoreException e) {
81 throw new InvocationTargetException(e);
82 }
83 }
84
85 private void createProject(IWorkspaceRoot root, ExampleProjectCreationWizardPage page, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
86 IConfigurationElement desc = page.getConfigurationElement();
87
88 IConfigurationElement[] imports = desc.getChildren("import"); //$NON-NLS-1$
89 IConfigurationElement[] natures = desc.getChildren("nature"); //$NON-NLS-1$
90 IConfigurationElement[] references = desc.getChildren("references"); //$NON-NLS-1$
91 int nImports = (imports == null) ? 0 : imports.length;
92 int nNatures = (natures == null) ? 0 : natures.length;
93 int nReferences = (references == null) ? 0 : references.length;
94
nitind3329cf82005-05-17 18:17:55 +000095 monitor.beginTask(XMLWizardsMessages.ExampleProjectCreationOperation_op_desc_proj, nImports + 1);
david_williams4c93c712005-04-16 01:16:58 +000096
97 String name = page.getProjectName();
98
99 String[] natureIds = new String[nNatures];
100 for (int i = 0; i < nNatures; i++) {
101 natureIds[i] = natures[i].getAttribute("id"); //$NON-NLS-1$
102 }
103 IProject[] referencedProjects = new IProject[nReferences];
104 for (int i = 0; i < nReferences; i++) {
105 referencedProjects[i] = root.getProject(references[i].getAttribute("id")); //$NON-NLS-1$
106 }
107
108 IProject proj = configNewProject(root, name, natureIds, referencedProjects, monitor);
109
110 for (int i = 0; i < nImports; i++) {
111 doImports(proj, imports[i], new SubProgressMonitor(monitor, 1));
112 }
113
114 String open = desc.getAttribute("open"); //$NON-NLS-1$
david_williamsef7420b2006-11-23 04:30:09 +0000115 if ((open != null) && (open.length() > 0)) {
david_williams4c93c712005-04-16 01:16:58 +0000116 IResource fileToOpen = proj.findMember(new Path(open));
117 if (fileToOpen != null) {
118 elementToOpen = fileToOpen;
119 }
120 }
121
122 }
123
124 private void doImports(IProject project, IConfigurationElement curr, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
125 try {
126 IPath destPath;
127 String name = curr.getAttribute("dest"); //$NON-NLS-1$
david_williamsef7420b2006-11-23 04:30:09 +0000128 if ((name == null) || (name.length() == 0)) {
david_williams4c93c712005-04-16 01:16:58 +0000129 destPath = project.getFullPath();
130 }
131 else {
132 IFolder folder = project.getFolder(name);
133 if (!folder.exists()) {
134 folder.create(true, true, null);
135 }
136 destPath = folder.getFullPath();
137 }
138 String importPath = curr.getAttribute("src"); //$NON-NLS-1$
139 if (importPath == null) {
140 importPath = ""; //$NON-NLS-1$
nitind3329cf82005-05-17 18:17:55 +0000141 Logger.log(Logger.ERROR, "projectsetup descriptor: import missing"); //$NON-NLS-1$
david_williams4c93c712005-04-16 01:16:58 +0000142 return;
143 }
144
145 ZipFile zipFile = getZipFileFromPluginDir(importPath, getContributingPlugin(curr));
146 importFilesFromZip(zipFile, destPath, new SubProgressMonitor(monitor, 1));
147 }
148 catch (CoreException e) {
149 throw new InvocationTargetException(e);
150 }
151 }
152
153 private String getContributingPlugin(IConfigurationElement configurationElement) {
154 Object parent = configurationElement;
155 while (parent != null) {
david_williamsef7420b2006-11-23 04:30:09 +0000156 if (parent instanceof IExtension) {
david_williams4c93c712005-04-16 01:16:58 +0000157 return ((IExtension) parent).getNamespace();
david_williamsef7420b2006-11-23 04:30:09 +0000158 }
david_williams4c93c712005-04-16 01:16:58 +0000159 parent = ((IConfigurationElement) parent).getParent();
160 }
161 return null;
162 }
163
164 public IResource getElementToOpen() {
165 return elementToOpen;
166 }
167
168 private ZipFile getZipFileFromPluginDir(String pluginRelativePath, String symbolicName) throws CoreException {
169 try {
170 Bundle bundle = Platform.getBundle(symbolicName);
171 URL starterURL = new URL(bundle.getEntry("/"), pluginRelativePath); //$NON-NLS-1$
172 return new ZipFile(Platform.asLocalURL(starterURL).getFile());
173 }
174 catch (IOException e) {
175 String message = pluginRelativePath + ": " + e.getMessage(); //$NON-NLS-1$
nitind3329cf82005-05-17 18:17:55 +0000176 Status status = new Status(IStatus.ERROR, XMLUIPlugin.ID, IStatus.ERROR, message, e);
david_williams4c93c712005-04-16 01:16:58 +0000177 throw new CoreException(status);
178 }
179 }
180
181 private void importFilesFromZip(ZipFile srcZipFile, IPath destPath, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
182 ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(srcZipFile);
183 ImportOperation op = new ImportOperation(destPath, structureProvider.getRoot(), structureProvider, overwriteQuery);
184 op.run(monitor);
185 }
186
187 /*
188 * @see IRunnableWithProgress#run(IProgressMonitor)
189 */
190 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
191 if (monitor == null) {
192 monitor = new NullProgressMonitor();
193 }
194 try {
nitind3329cf82005-05-17 18:17:55 +0000195 monitor.beginTask(XMLWizardsMessages.ExampleProjectCreationOperation_op_desc, pages.length);
david_williams4c93c712005-04-16 01:16:58 +0000196 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
197
198 for (int i = 0; i < pages.length; i++) {
199 createProject(root, pages[i], new SubProgressMonitor(monitor, 1));
200 }
201 }
202 finally {
203 monitor.done();
204 }
205 }
206}