28ce60315397f727c14d8d4ed46bbdcb7a8b9c0f
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / NewProjectCreationWizard.java
1 package net.sourceforge.phpeclipse.wizards;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.net.URI;
5
6 import net.sourceforge.phpdt.core.JavaCore;
7 import net.sourceforge.phpdt.ui.actions.OpenPHPPerspectiveAction;
8 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.resources.IProjectDescription;
12 import org.eclipse.core.resources.IResource;
13 import org.eclipse.core.resources.IWorkspace;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExecutableExtension;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.ui.INewWizard;
22 import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
23 import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
24 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
25 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
26
27 public class NewProjectCreationWizard extends BasicNewResourceWizard implements
28                 INewWizard, IExecutableExtension {
29         protected WizardNewProjectCreationPage projectPage;
30
31         protected IConfigurationElement configurationElement;
32
33         protected IProject newProject;
34
35         public NewProjectCreationWizard() {
36                 setWindowTitle(PHPWizardMessages
37                                 .getString("NewProjectCreationWizard.windowTitle"));
38         }
39
40         public boolean performFinish() {
41                 IRunnableWithProgress projectCreationOperation = new WorkspaceModifyDelegatingOperation(
42                                 getProjectCreationRunnable());
43
44                 try {
45                         getContainer().run(false, true, projectCreationOperation);
46                 } catch (Exception e) {
47                         PHPeclipsePlugin.log(e);
48                         return false;
49                 }
50
51                 BasicNewProjectResourceWizard.updatePerspective(configurationElement);
52                 selectAndReveal(newProject);
53                 // open the PHP perspective
54                 new OpenPHPPerspectiveAction().run();
55                 return true;
56         }
57
58         protected IRunnableWithProgress getProjectCreationRunnable() {
59                 return new IRunnableWithProgress() {
60                         public void run(IProgressMonitor monitor)
61                                         throws InvocationTargetException, InterruptedException {
62                                 int remainingWorkUnits = 10;
63                                 monitor
64                                                 .beginTask(
65                                                                 PHPWizardMessages
66                                                                                 .getString("NewProjectCreationWizard.projectCreationMessage"),
67                                                                 remainingWorkUnits);
68
69                                 IWorkspace workspace = PHPeclipsePlugin.getWorkspace();
70                                 String projectName = projectPage.getProjectHandle().getName();
71                                 newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
72                                 IProjectDescription description = workspace
73                                                 .newProjectDescription(projectName);
74                                 
75                                 URI uriPath = (!projectPage.useDefaults()) ? projectPage
76                                 .getLocationURI() : null;
77                                 if (uriPath != null) {                              
78                                         description.setLocationURI(uriPath);
79                                 }
80
81                                 try {
82                                         if (!newProject.exists()) {
83                                                 newProject.create(description, new SubProgressMonitor(
84                                                                 monitor, 1));
85                                                 remainingWorkUnits--;
86                                         }
87                                         if (!newProject.isOpen()) {
88                                             newProject.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 1));
89                                                 remainingWorkUnits--;
90                                         }
91                                         JavaCore.addPHPNature(newProject, new SubProgressMonitor(
92                                                         monitor, remainingWorkUnits));
93
94                                 } catch (CoreException e) {
95                                     System.out.println(e);
96                                         throw new InvocationTargetException(e);
97                                 } finally {
98                                         monitor.done();
99                                 }
100                         }
101                 };
102         }
103
104         public void addPages() {
105                 super.addPages();
106
107                 projectPage = new WizardNewProjectCreationPage(PHPWizardMessages
108                                 .getString("WizardNewProjectCreationPage.pageName"));
109                 projectPage.setTitle(PHPWizardMessages
110                                 .getString("WizardNewProjectCreationPage.pageTitle"));
111                 projectPage.setDescription(PHPWizardMessages
112                                 .getString("WizardNewProjectCreationPage.pageDescription"));
113
114                 addPage(projectPage);
115         }
116
117         public void setInitializationData(IConfigurationElement config,
118                         String propertyName, Object data) throws CoreException {
119                 configurationElement = config;
120         }
121
122 }