jeffliu | ec1c478 | 2006-05-24 14:16:24 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 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_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.xml.ui.internal.wizards; |
| 14 | |
| 15 | import java.io.IOException; |
| 16 | import java.lang.reflect.InvocationTargetException; |
| 17 | import java.net.URL; |
| 18 | import java.util.zip.ZipFile; |
| 19 | |
| 20 | import org.eclipse.core.resources.IFolder; |
| 21 | import org.eclipse.core.resources.IProject; |
| 22 | import org.eclipse.core.resources.IProjectDescription; |
| 23 | import org.eclipse.core.resources.IResource; |
| 24 | import org.eclipse.core.resources.IWorkspaceRoot; |
| 25 | import org.eclipse.core.resources.ResourcesPlugin; |
| 26 | import org.eclipse.core.runtime.CoreException; |
| 27 | import org.eclipse.core.runtime.IConfigurationElement; |
| 28 | import org.eclipse.core.runtime.IExtension; |
| 29 | import org.eclipse.core.runtime.IPath; |
| 30 | import org.eclipse.core.runtime.IProgressMonitor; |
| 31 | import org.eclipse.core.runtime.IStatus; |
| 32 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 33 | import org.eclipse.core.runtime.Path; |
| 34 | import org.eclipse.core.runtime.Platform; |
| 35 | import org.eclipse.core.runtime.Status; |
| 36 | import org.eclipse.core.runtime.SubProgressMonitor; |
| 37 | import org.eclipse.jface.operation.IRunnableWithProgress; |
| 38 | import org.eclipse.ui.dialogs.IOverwriteQuery; |
| 39 | import org.eclipse.ui.wizards.datatransfer.ImportOperation; |
| 40 | import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider; |
nitind | 3329cf8 | 2005-05-17 18:17:55 +0000 | [diff] [blame] | 41 | import org.eclipse.wst.xml.ui.internal.Logger; |
| 42 | import org.eclipse.wst.xml.ui.internal.XMLUIPlugin; |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 43 | import org.osgi.framework.Bundle; |
| 44 | |
| 45 | public 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 | |
nitind | 3329cf8 | 2005-05-17 18:17:55 +0000 | [diff] [blame] | 95 | monitor.beginTask(XMLWizardsMessages.ExampleProjectCreationOperation_op_desc_proj, nImports + 1); |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 96 | |
| 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_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 115 | if ((open != null) && (open.length() > 0)) { |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 116 | 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_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 128 | if ((name == null) || (name.length() == 0)) { |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 129 | 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$ |
nitind | 3329cf8 | 2005-05-17 18:17:55 +0000 | [diff] [blame] | 141 | Logger.log(Logger.ERROR, "projectsetup descriptor: import missing"); //$NON-NLS-1$ |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 142 | 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_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 156 | if (parent instanceof IExtension) { |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 157 | return ((IExtension) parent).getNamespace(); |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 158 | } |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 159 | 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$ |
nitind | 3329cf8 | 2005-05-17 18:17:55 +0000 | [diff] [blame] | 176 | Status status = new Status(IStatus.ERROR, XMLUIPlugin.ID, IStatus.ERROR, message, e); |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 177 | 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 { |
nitind | 3329cf8 | 2005-05-17 18:17:55 +0000 | [diff] [blame] | 195 | monitor.beginTask(XMLWizardsMessages.ExampleProjectCreationOperation_op_desc, pages.length); |
david_williams | 4c93c71 | 2005-04-16 01:16:58 +0000 | [diff] [blame] | 196 | 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 | } |