KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > CreateProjectWizard


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.ui.internal.ide.dialogs;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IProjectDescription;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceStatus;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.OperationCanceledException;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.SubProgressMonitor;
29 import org.eclipse.jface.dialogs.ErrorDialog;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.wizard.Wizard;
32 import org.eclipse.osgi.util.NLS;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.actions.WorkspaceModifyOperation;
35 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
36 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
37
38 /**
39  * Internal workbench wizard to create a project
40  * resource in the workspace. This project will have
41  * no capabilities when created. This wizard is intended
42  * to be used by the CreateProjectStep class only.
43  */

44 public class CreateProjectWizard extends Wizard {
45     private NewProjectWizard wizard;
46
47     private WizardNewProjectNameAndLocationPage page;
48
49     /**
50      * Creates an empty wizard for creating a new project
51      * in the workspace.
52      */

53     /* package */CreateProjectWizard(WizardNewProjectNameAndLocationPage page,
54             NewProjectWizard wizard) {
55         super();
56         this.page = page;
57         this.wizard = wizard;
58     }
59
60     /**
61      * Creates a new project resource with the entered name.
62      *
63      * @return the created project resource, or <code>null</code> if the project
64      * was not created
65      */

66     private IProject createNewProject() {
67         // get a project handle
68
final IProject newProjectHandle = page.getProjectHandle();
69
70         // get a project descriptor
71
IPath defaultPath = Platform.getLocation();
72         IPath newPath = page.getLocationPath();
73         if (defaultPath.equals(newPath)) {
74             newPath = null;
75         }
76         IWorkspace workspace = ResourcesPlugin.getWorkspace();
77         final IProjectDescription description = workspace
78                 .newProjectDescription(newProjectHandle.getName());
79         description.setLocation(newPath);
80
81         // define the operation to create a new project
82
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
83             protected void execute(IProgressMonitor monitor)
84                     throws CoreException {
85                 createProject(description, newProjectHandle, monitor);
86             }
87         };
88
89         // run the operation to create a new project
90
try {
91             getContainer().run(true, true, op);
92         } catch (InterruptedException JavaDoc e) {
93             return null;
94         } catch (InvocationTargetException JavaDoc e) {
95             Throwable JavaDoc t = e.getTargetException();
96             if (t instanceof CoreException) {
97                 if (((CoreException) t).getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
98                     MessageDialog
99                             .openError(
100                                     getShell(),
101                                     IDEWorkbenchMessages.CreateProjectWizard_errorTitle,
102                                     IDEWorkbenchMessages.CreateProjectWizard_caseVariantExistsError
103                             );
104                 } else {
105                     ErrorDialog.openError(getShell(), IDEWorkbenchMessages.CreateProjectWizard_errorTitle,
106                             null, // no special message
107
((CoreException) t).getStatus());
108                 }
109             } else {
110                 // Unexpected runtime exceptions and errors may still occur.
111
IDEWorkbenchPlugin.getDefault().getLog().log(
112                         new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, t
113                                 .toString(), t));
114                 MessageDialog
115                         .openError(
116                                 getShell(),
117                                 IDEWorkbenchMessages.CreateProjectWizard_errorTitle,
118                                 NLS.bind(IDEWorkbenchMessages.CreateProjectWizard_internalError, t.getMessage()));
119             }
120             return null;
121         }
122
123         return newProjectHandle;
124     }
125
126     /**
127      * Creates a project resource given the project handle and description.
128      *
129      * @param description the project description to create a project resource for
130      * @param projectHandle the project handle to create a project resource for
131      * @param monitor the progress monitor to show visual progress with
132      *
133      * @exception CoreException if the operation fails
134      * @exception OperationCanceledException if the operation is canceled
135      */

136     private void createProject(IProjectDescription description,
137             IProject projectHandle, IProgressMonitor monitor)
138             throws CoreException, OperationCanceledException {
139         try {
140             monitor.beginTask("", 2000); //$NON-NLS-1$
141

142             projectHandle.create(description, new SubProgressMonitor(monitor,
143                     1000));
144
145             if (monitor.isCanceled()) {
146                 throw new OperationCanceledException();
147             }
148
149             projectHandle.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 1000));
150
151         } finally {
152             monitor.done();
153         }
154     }
155
156     /**
157      * Returns the current project name.
158      *
159      * @return the project name or <code>null</code>
160      * if no project name is known
161      */

162     /* package */String JavaDoc getProjectName() {
163         return page.getProjectName();
164     }
165
166     /* (non-Javadoc)
167      * Method declared on IWizard.
168      */

169     public boolean performFinish() {
170         if (wizard.getNewProject() != null) {
171             return true;
172         }
173
174         IProject project = createNewProject();
175         if (project != null) {
176             wizard.setNewProject(project);
177             return true;
178         } else {
179             return false;
180         }
181     }
182 }
183
Popular Tags