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