Version 1.1.7
[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
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 INewWizard, IExecutableExtension {
27         protected WizardNewProjectCreationPage projectPage;
28         protected IConfigurationElement configurationElement;
29         protected IProject newProject;
30         
31         public NewProjectCreationWizard() {
32                 setWindowTitle(PHPWizardMessages.getString("NewProjectCreationWizard.windowTitle"));
33         }
34
35         public boolean performFinish() {
36                 IRunnableWithProgress projectCreationOperation = new WorkspaceModifyDelegatingOperation(getProjectCreationRunnable());
37
38                 try {
39                         getContainer().run(false, true, projectCreationOperation);
40                 } catch (Exception e) { 
41                         PHPeclipsePlugin.log(e);
42                         return false;
43                 }
44
45                 BasicNewProjectResourceWizard.updatePerspective(configurationElement);
46                 selectAndReveal(newProject);
47         // open the PHP perspective
48                 new OpenPHPPerspectiveAction().run();
49                 return true;
50         }
51
52         protected IRunnableWithProgress getProjectCreationRunnable() {
53                 return new IRunnableWithProgress() {
54                         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
55                                 int remainingWorkUnits = 10;
56                                 monitor.beginTask(PHPWizardMessages.getString("NewProjectCreationWizard.projectCreationMessage"), remainingWorkUnits);
57
58                                 IWorkspace workspace = PHPeclipsePlugin.getWorkspace();
59                                 newProject = projectPage.getProjectHandle();
60                                 
61                                 IProjectDescription description = workspace.newProjectDescription(newProject.getName());
62                                 IPath path = Platform.getLocation();
63                                 IPath customPath = projectPage.getLocationPath();
64                                 if (!path.equals(customPath)) {
65                                         path = customPath;
66                                         description.setLocation(path);
67                                 }
68
69                                 try {
70                                         if (!newProject.exists()) {
71                                                 newProject.create(description, new SubProgressMonitor(monitor, 1));
72                                                 remainingWorkUnits--;
73                                         }
74                                         if (!newProject.isOpen()) {
75                                                 newProject.open(new SubProgressMonitor(monitor, 1));
76                                                 remainingWorkUnits--;
77                                         }
78                                         JavaCore.addPHPNature(newProject, new SubProgressMonitor(monitor, remainingWorkUnits));
79                                 } catch (CoreException e) {
80                                         throw new InvocationTargetException(e);
81                                 } finally {
82                                         monitor.done();
83                                 }
84                         }
85                 };
86         }
87
88         public void addPages() {
89                 super.addPages();
90
91                 projectPage = new WizardNewProjectCreationPage(PHPWizardMessages.getString("WizardNewProjectCreationPage.pageName"));
92                 projectPage.setTitle(PHPWizardMessages.getString("WizardNewProjectCreationPage.pageTitle"));
93                 projectPage.setDescription(PHPWizardMessages.getString("WizardNewProjectCreationPage.pageDescription"));
94
95                 addPage(projectPage);
96         }
97
98         public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
99                 configurationElement = config;
100         }
101
102 }