Patch 1692034 - set projects default charset to UTF-8
[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
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
93                                         // patch #1692034 - set the coding of this PHPeclipse
94                                         // project to UTF-8 by default for all operating systems:
95                                         newProject.setDefaultCharset("UTF-8", monitor);
96
97                                 } catch (CoreException e) {
98                                         throw new InvocationTargetException(e);
99                                 } finally {
100                                         monitor.done();
101                                 }
102                         }
103                 };
104         }
105
106         public void addPages() {
107                 super.addPages();
108
109                 projectPage = new WizardNewProjectCreationPage(PHPWizardMessages
110                                 .getString("WizardNewProjectCreationPage.pageName"));
111                 projectPage.setTitle(PHPWizardMessages
112                                 .getString("WizardNewProjectCreationPage.pageTitle"));
113                 projectPage.setDescription(PHPWizardMessages
114                                 .getString("WizardNewProjectCreationPage.pageDescription"));
115
116                 addPage(projectPage);
117         }
118
119         public void setInitializationData(IConfigurationElement config,
120                         String propertyName, Object data) throws CoreException {
121                 configurationElement = config;
122         }
123
124 }