]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.jdt.ui.wizards; | |
12 | ||
13 | import java.lang.reflect.InvocationTargetException; | |
14 | ||
15 | import org.eclipse.swt.SWT; | |
16 | import org.eclipse.swt.layout.GridData; | |
17 | import org.eclipse.swt.layout.GridLayout; | |
18 | import org.eclipse.swt.widgets.Composite; | |
19 | import org.eclipse.swt.widgets.Control; | |
20 | ||
21 | import org.eclipse.core.filesystem.URIUtil; | |
22 | ||
23 | import org.eclipse.core.runtime.Assert; | |
24 | import org.eclipse.core.runtime.CoreException; | |
25 | import org.eclipse.core.runtime.IPath; | |
26 | import org.eclipse.core.runtime.IProgressMonitor; | |
27 | import org.eclipse.core.runtime.IStatus; | |
28 | import org.eclipse.core.runtime.NullProgressMonitor; | |
29 | import org.eclipse.core.runtime.OperationCanceledException; | |
30 | import org.eclipse.core.runtime.SubProgressMonitor; | |
31 | ||
32 | import org.eclipse.core.resources.IProject; | |
33 | import org.eclipse.core.resources.IWorkspaceRoot; | |
34 | ||
35 | import org.eclipse.jface.dialogs.Dialog; | |
36 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
37 | ||
38 | import org.eclipse.ui.PlatformUI; | |
39 | import org.eclipse.ui.dialogs.WizardNewProjectCreationPage; | |
40 | ||
41 | import org.eclipse.jdt.core.IClasspathEntry; | |
42 | import org.eclipse.jdt.core.IJavaProject; | |
43 | import org.eclipse.jdt.core.JavaCore; | |
44 | ||
45 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
46 | import org.eclipse.jdt.internal.ui.preferences.NewJavaProjectPreferencePage; | |
47 | import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext; | |
48 | import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener; | |
49 | import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; | |
50 | import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock; | |
51 | ||
52 | /** | |
53 | * Standard wizard page for creating new Java projects. This page can be used in | |
54 | * project creation wizards for projects and will configure the project with the | |
55 | * Java nature. This page also allows the user to configure the Java project's | |
56 | * output location for class files generated by the Java builder. | |
57 | * <p> | |
58 | * Whenever possible clients should use the class {@link JavaCapabilityConfigurationPage} | |
59 | * in favor of this class. | |
60 | * </p> | |
61 | * <p> | |
62 | * Clients may instantiate or subclass. | |
63 | * </p> | |
64 | * @deprecated Use {@link NewJavaProjectWizardPageTwo} or {@link JavaCapabilityConfigurationPage}. | |
65 | */ | |
66 | public class NewJavaProjectWizardPage extends NewElementWizardPage { | |
67 | ||
68 | private static final String PAGE_NAME= "NewJavaProjectWizardPage"; //$NON-NLS-1$ | |
69 | ||
70 | private WizardNewProjectCreationPage fMainPage; | |
71 | ||
72 | private IPath fOutputLocation; | |
73 | private IClasspathEntry[] fClasspathEntries; | |
74 | private BuildPathsBlock fBuildPathsBlock; | |
75 | ||
76 | private boolean fProjectModified; | |
77 | ||
78 | /** | |
79 | * Creates a Java project wizard creation page. | |
80 | * <p> | |
81 | * The Java project wizard reads project name and location from the main page. | |
82 | * </p> | |
83 | * | |
84 | * @param root the workspace root | |
85 | * @param mainpage the main page of the wizard | |
86 | */ | |
87 | public NewJavaProjectWizardPage(IWorkspaceRoot root, WizardNewProjectCreationPage mainpage) { | |
88 | super(PAGE_NAME); | |
89 | ||
90 | setTitle(NewWizardMessages.NewJavaProjectWizardPage_title); | |
91 | setDescription(NewWizardMessages.NewJavaProjectWizardPage_description); | |
92 | ||
93 | fMainPage= mainpage; | |
94 | IStatusChangeListener listener= new IStatusChangeListener() { | |
95 | public void statusChanged(IStatus status) { | |
96 | updateStatus(status); | |
97 | } | |
98 | }; | |
99 | ||
100 | fBuildPathsBlock= new BuildPathsBlock(new BusyIndicatorRunnableContext(), listener, 0, false, null); | |
101 | ||
102 | fProjectModified= true; | |
103 | fOutputLocation= null; | |
104 | fClasspathEntries= null; | |
105 | } | |
106 | ||
107 | /* | |
108 | * @see org.eclipse.jface.dialogs.DialogPage#dispose() | |
109 | * @since 3.3 | |
110 | */ | |
111 | @Override | |
112 | public void dispose() { | |
113 | try { | |
114 | super.dispose(); | |
115 | } finally { | |
116 | if (fBuildPathsBlock != null) { | |
117 | fBuildPathsBlock.dispose(); | |
118 | fBuildPathsBlock= null; | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | /** | |
124 | * Sets the default output location to be used for the new Java project. | |
125 | * This is the path of the folder (with the project) into which the Java builder | |
126 | * will generate binary class files corresponding to the project's Java source | |
127 | * files. | |
128 | * <p> | |
129 | * The wizard will create this folder if required. | |
130 | * </p> | |
131 | * <p> | |
132 | * The default classpath will be applied when <code>initBuildPaths</code> is | |
133 | * called. This is done automatically when the page becomes visible and | |
134 | * the project or the default paths have changed. | |
135 | * </p> | |
136 | * | |
137 | * @param path the folder to be taken as the default output path | |
138 | */ | |
139 | public void setDefaultOutputFolder(IPath path) { | |
140 | fOutputLocation= path; | |
141 | setProjectModified(); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Sets the default classpath to be used for the new Java project. | |
146 | * <p> | |
147 | * The caller of this method is responsible for creating the classpath entries | |
148 | * for the <code>IJavaProject</code> that corresponds to the created project. | |
149 | * The caller is responsible for creating any new folders that might be mentioned | |
150 | * on the classpath. | |
151 | * </p> | |
152 | * <p> | |
153 | * The default output location will be applied when <code>initBuildPaths</code> is | |
154 | * called. This is done automatically when the page becomes visible and | |
155 | * the project or the default paths have changed. | |
156 | * </p> | |
157 | * | |
158 | * @param entries the default classpath entries | |
159 | * @param appendDefaultJRE <code>true</code> a variable entry for the | |
160 | * default JRE (specified in the preferences) will be added to the classpath. | |
161 | */ | |
162 | public void setDefaultClassPath(IClasspathEntry[] entries, boolean appendDefaultJRE) { | |
163 | if (entries != null && appendDefaultJRE) { | |
164 | IClasspathEntry[] jreEntry= NewJavaProjectPreferencePage.getDefaultJRELibrary(); | |
165 | IClasspathEntry[] newEntries= new IClasspathEntry[entries.length + jreEntry.length]; | |
166 | System.arraycopy(entries, 0, newEntries, 0, entries.length); | |
167 | System.arraycopy(jreEntry, 0, newEntries, entries.length, jreEntry.length); | |
168 | entries= newEntries; | |
169 | } | |
170 | fClasspathEntries= entries; | |
171 | setProjectModified(); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Sets the project state to modified. Doing so will initialize the page | |
176 | * the next time it becomes visible. | |
177 | * | |
178 | * @since 2.0 | |
179 | */ | |
180 | public void setProjectModified() { | |
181 | fProjectModified= true; | |
182 | } | |
183 | ||
184 | /** | |
185 | * Returns the project handle. Subclasses should override this | |
186 | * method if they don't provide a main page or if they provide | |
187 | * their own main page implementation. | |
188 | * | |
189 | * @return the project handle | |
190 | */ | |
191 | protected IProject getProjectHandle() { | |
192 | Assert.isNotNull(fMainPage); | |
193 | return fMainPage.getProjectHandle(); | |
194 | } | |
195 | ||
196 | /** | |
197 | * Returns the project location path. Subclasses should override this | |
198 | * method if they don't provide a main page or if they provide | |
199 | * their own main page implementation. | |
200 | * | |
201 | * @return the project location path | |
202 | */ | |
203 | protected IPath getLocationPath() { | |
204 | Assert.isNotNull(fMainPage); | |
205 | return fMainPage.getLocationPath(); | |
206 | } | |
207 | ||
208 | /** | |
209 | * Returns the Java project handle by converting the result of | |
210 | * <code>getProjectHandle()</code> into a Java project. | |
211 | * | |
212 | * @return the Java project handle | |
213 | * @see #getProjectHandle() | |
214 | */ | |
215 | public IJavaProject getNewJavaProject() { | |
216 | return JavaCore.create(getProjectHandle()); | |
217 | } | |
218 | ||
219 | /* (non-Javadoc) | |
220 | * @see WizardPage#createControl | |
221 | */ | |
222 | public void createControl(Composite parent) { | |
223 | Composite composite= new Composite(parent, SWT.NONE); | |
224 | composite.setFont(parent.getFont()); | |
225 | composite.setLayout(new GridLayout(1, false)); | |
226 | Control control= fBuildPathsBlock.createControl(composite); | |
227 | control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
228 | Dialog.applyDialogFont(composite); | |
229 | PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_JAVAPROJECT_WIZARD_PAGE); | |
230 | setControl(composite); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Forces the initialization of the Java project page. Default classpath or buildpath | |
235 | * will be used if set. The initialization should only be performed when the project | |
236 | * or default paths have changed. Toggling back and forward the pages without | |
237 | * changes should not re-initialize the page, as changes from the user will be | |
238 | * overwritten. | |
239 | * | |
240 | * @since 2.0 | |
241 | */ | |
242 | protected void initBuildPaths() { | |
243 | fBuildPathsBlock.init(getNewJavaProject(), fOutputLocation, fClasspathEntries); | |
244 | } | |
245 | ||
246 | /** | |
247 | * Extend this method to set a user defined default classpath or output location. | |
248 | * The method <code>initBuildPaths</code> is called when the page becomes | |
249 | * visible the first time or the project or the default paths have changed. | |
250 | * | |
251 | * @param visible if <code>true</code> the page becomes visible; otherwise | |
252 | * it becomes invisible | |
253 | */ | |
254 | @Override | |
255 | public void setVisible(boolean visible) { | |
256 | super.setVisible(visible); | |
257 | if (visible) { | |
258 | // evaluate if a initialization is required | |
259 | if (fProjectModified || isNewProjectHandle()) { | |
260 | // only initialize the project when needed | |
261 | initBuildPaths(); | |
262 | fProjectModified= false; | |
263 | } | |
264 | } | |
265 | } | |
266 | ||
267 | private boolean isNewProjectHandle() { | |
268 | IProject oldProject= fBuildPathsBlock.getJavaProject().getProject(); | |
269 | return !oldProject.equals(getProjectHandle()); | |
270 | } | |
271 | ||
272 | ||
273 | /** | |
274 | * Returns the currently configured output location. Note that the returned path | |
275 | * might not be valid. | |
276 | * | |
277 | * @return the configured output location | |
278 | * | |
279 | * @since 2.0 | |
280 | */ | |
281 | public IPath getOutputLocation() { | |
282 | return fBuildPathsBlock.getOutputLocation(); | |
283 | } | |
284 | ||
285 | /** | |
286 | * Returns the currently configured classpath. Note that the classpath might | |
287 | * not be valid. | |
288 | * | |
289 | * @return the configured classpath | |
290 | * | |
291 | * @since 2.0 | |
292 | */ | |
293 | public IClasspathEntry[] getRawClassPath() { | |
294 | return fBuildPathsBlock.getRawClassPath(); | |
295 | } | |
296 | ||
297 | ||
298 | /** | |
299 | * Returns the runnable that will create the Java project. The runnable will create | |
300 | * and open the project if needed. The runnable will add the Java nature to the | |
301 | * project, and set the project's classpath and output location. | |
302 | * <p> | |
303 | * To create the new java project, execute this runnable | |
304 | * </p> | |
305 | * | |
306 | * @return the runnable | |
307 | */ | |
308 | public IRunnableWithProgress getRunnable() { | |
309 | return new IRunnableWithProgress() { | |
310 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
311 | if (monitor == null) { | |
312 | monitor= new NullProgressMonitor(); | |
313 | } | |
314 | monitor.beginTask(NewWizardMessages.NewJavaProjectWizardPage_op_desc, 10); | |
315 | // initialize if needed | |
316 | if (fProjectModified || isNewProjectHandle()) { | |
317 | initBuildPaths(); | |
318 | } | |
319 | ||
320 | // create the project | |
321 | try { | |
322 | IPath locationPath= getLocationPath(); | |
323 | BuildPathsBlock.createProject(getProjectHandle(), | |
324 | locationPath != null ? URIUtil.toURI(locationPath) : null, | |
325 | new SubProgressMonitor(monitor, 2)); | |
326 | BuildPathsBlock.addJavaNature(getProjectHandle(), new SubProgressMonitor(monitor, 2)); | |
327 | fBuildPathsBlock.configureJavaProject(new SubProgressMonitor(monitor, 6)); | |
328 | } catch (CoreException e) { | |
329 | throw new InvocationTargetException(e); | |
330 | } catch (OperationCanceledException e) { | |
331 | throw new InterruptedException(); | |
332 | } finally { | |
333 | monitor.done(); | |
334 | } | |
335 | } | |
336 | }; | |
337 | } | |
338 | } |