]>
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 | import java.net.URI; | |
15 | ||
16 | import org.eclipse.swt.SWT; | |
17 | import org.eclipse.swt.layout.GridData; | |
18 | import org.eclipse.swt.layout.GridLayout; | |
19 | import org.eclipse.swt.widgets.Composite; | |
20 | import org.eclipse.swt.widgets.Control; | |
21 | ||
22 | import org.eclipse.core.filesystem.URIUtil; | |
23 | ||
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 | ||
34 | import org.eclipse.jface.dialogs.Dialog; | |
35 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
36 | ||
37 | import org.eclipse.ui.PlatformUI; | |
38 | ||
39 | import org.eclipse.jdt.core.IClasspathEntry; | |
40 | import org.eclipse.jdt.core.IJavaProject; | |
41 | ||
42 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
43 | import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext; | |
44 | import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener; | |
45 | import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; | |
46 | import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock; | |
47 | ||
48 | /** | |
49 | * Basic wizard page for creating new Java projects. This page can be used in | |
50 | * project creation wizards. The page shows UI to configure the project with a Java | |
51 | * build path and output location. On finish the page will also configure the Java nature. | |
52 | * <p> | |
53 | * This is a replacement for {@link NewJavaProjectWizardPage} with a cleaner API. | |
54 | * </p> | |
55 | * <p> | |
56 | * The pages used by the New Java Project wizard are {@link NewJavaProjectWizardPageOne} and | |
57 | * {@link NewJavaProjectWizardPageTwo}. | |
58 | * </p> | |
59 | * <p> | |
60 | * Clients may instantiate or subclass. | |
61 | * </p> | |
62 | * | |
63 | * @since 2.0 | |
64 | */ | |
65 | public class JavaCapabilityConfigurationPage extends NewElementWizardPage { | |
66 | ||
67 | private static final String PAGE_NAME= "JavaCapabilityConfigurationPage"; //$NON-NLS-1$ | |
68 | ||
69 | private IJavaProject fJavaProject; | |
70 | private BuildPathsBlock fBuildPathsBlock; | |
71 | ||
72 | /** | |
73 | * Creates a wizard page that can be used in a Java project creation wizard. | |
74 | * It contains UI to configure a the classpath and the output folder. | |
75 | * | |
76 | * <p> | |
77 | * After constructing, a call to {@link #init(IJavaProject, IPath, IClasspathEntry[], boolean)} is required. | |
78 | * </p> | |
79 | */ | |
80 | public JavaCapabilityConfigurationPage() { | |
81 | super(PAGE_NAME); | |
82 | fJavaProject= null; | |
83 | ||
84 | setTitle(NewWizardMessages.JavaCapabilityConfigurationPage_title); | |
85 | setDescription(NewWizardMessages.JavaCapabilityConfigurationPage_description); | |
86 | } | |
87 | ||
88 | private BuildPathsBlock getBuildPathsBlock() { | |
89 | if (fBuildPathsBlock == null) { | |
90 | IStatusChangeListener listener= new IStatusChangeListener() { | |
91 | public void statusChanged(IStatus status) { | |
92 | updateStatus(status); | |
93 | } | |
94 | }; | |
95 | fBuildPathsBlock= new BuildPathsBlock(new BusyIndicatorRunnableContext(), listener, 0, useNewSourcePage(), null); | |
96 | } | |
97 | return fBuildPathsBlock; | |
98 | } | |
99 | ||
100 | /* | |
101 | * @see org.eclipse.jface.dialogs.DialogPage#dispose() | |
102 | * @since 3.3 | |
103 | */ | |
104 | @Override | |
105 | public void dispose() { | |
106 | try { | |
107 | super.dispose(); | |
108 | } finally { | |
109 | if (fBuildPathsBlock != null) { | |
110 | fBuildPathsBlock.dispose(); | |
111 | fBuildPathsBlock= null; | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | /** | |
117 | * Clients can override this method to choose if the new source page is used. The new source page | |
118 | * requires that the project is already created as Java project. The page will directly manipulate the classpath. | |
119 | * By default <code>false</code> is returned. | |
120 | * @return Returns <code>true</code> if the new source page should be used. | |
121 | * @since 3.1 | |
122 | */ | |
123 | protected boolean useNewSourcePage() { | |
124 | return false; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Initializes the page with the project and default classpath. | |
129 | * <p> | |
130 | * The default classpath entries must correspond the given project. | |
131 | * </p> | |
132 | * <p> | |
133 | * The caller of this method is responsible for creating the underlying project. The page will create the output, | |
134 | * source and library folders if required. | |
135 | * </p> | |
136 | * <p> | |
137 | * The project does not have to exist at the time of initialization, but must exist when executing the runnable | |
138 | * obtained by <code>getRunnable()</code>. | |
139 | * </p> | |
140 | * @param jproject The Java project. | |
141 | * @param defaultOutputLocation The default classpath entries or <code>null</code> to let the page choose the default | |
142 | * @param defaultEntries The folder to be taken as the default output path or <code>null</code> to let the page choose the default | |
143 | * @param defaultsOverrideExistingClasspath If set to <code>true</code>, an existing '.classpath' file is ignored. If set to <code>false</code> | |
144 | * the given default classpath and output location is only used if no '.classpath' exists. | |
145 | */ | |
146 | public void init(IJavaProject jproject, IPath defaultOutputLocation, IClasspathEntry[] defaultEntries, boolean defaultsOverrideExistingClasspath) { | |
147 | if (!defaultsOverrideExistingClasspath && jproject.exists() && jproject.getProject().getFile(".classpath").exists()) { //$NON-NLS-1$ | |
148 | defaultOutputLocation= null; | |
149 | defaultEntries= null; | |
150 | } | |
151 | getBuildPathsBlock().init(jproject, defaultOutputLocation, defaultEntries); | |
152 | fJavaProject= jproject; | |
153 | } | |
154 | ||
155 | /* (non-Javadoc) | |
156 | * @see WizardPage#createControl | |
157 | */ | |
158 | public void createControl(Composite parent) { | |
159 | Composite composite= new Composite(parent, SWT.NONE); | |
160 | composite.setFont(parent.getFont()); | |
161 | composite.setLayout(new GridLayout(1, false)); | |
162 | Control control= getBuildPathsBlock().createControl(composite); | |
163 | control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
164 | Dialog.applyDialogFont(composite); | |
165 | PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_JAVAPROJECT_WIZARD_PAGE); | |
166 | setControl(composite); | |
167 | } | |
168 | ||
169 | /** | |
170 | * Returns the currently configured output location. Note that the returned path | |
171 | * might not be a valid path. | |
172 | * | |
173 | * @return the currently configured output location | |
174 | */ | |
175 | public IPath getOutputLocation() { | |
176 | return getBuildPathsBlock().getOutputLocation(); | |
177 | } | |
178 | ||
179 | /** | |
180 | * Returns the currently configured classpath. Note that the classpath might | |
181 | * not be valid. | |
182 | * | |
183 | * @return the currently configured classpath | |
184 | */ | |
185 | public IClasspathEntry[] getRawClassPath() { | |
186 | return getBuildPathsBlock().getRawClassPath(); | |
187 | } | |
188 | ||
189 | /** | |
190 | * Returns the Java project that was passed in {@link #init(IJavaProject, IPath, IClasspathEntry[], boolean)} or <code>null</code> if the | |
191 | * page has not been initialized yet. | |
192 | * | |
193 | * @return the managed Java project or <code>null</code> | |
194 | */ | |
195 | public IJavaProject getJavaProject() { | |
196 | return fJavaProject; | |
197 | } | |
198 | ||
199 | ||
200 | /** | |
201 | * Returns the runnable that will create the Java project or <code>null</code> if the page has | |
202 | * not been initialized. The runnable sets the project's classpath and output location to the values | |
203 | * configured in the page and adds the Java nature if not set yet. The method requires that the | |
204 | * project is created and opened. | |
205 | * | |
206 | * @return the runnable that creates the new Java project | |
207 | */ | |
208 | public IRunnableWithProgress getRunnable() { | |
209 | if (getJavaProject() != null) { | |
210 | return new IRunnableWithProgress() { | |
211 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
212 | try { | |
213 | configureJavaProject(monitor); | |
214 | } catch (CoreException e) { | |
215 | throw new InvocationTargetException(e); | |
216 | } | |
217 | } | |
218 | }; | |
219 | } | |
220 | return null; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Helper method to create and open a IProject. The project location | |
225 | * is configured. No natures are added. | |
226 | * | |
227 | * @param project The handle of the project to create. | |
228 | * @param locationPath The location of the project <code>null</code> to create the project in the workspace | |
229 | * @param monitor a progress monitor to report progress or <code>null</code> if | |
230 | * progress reporting is not desired | |
231 | * @throws CoreException if the project couldn't be created | |
232 | * @since 2.1 | |
233 | * @deprecated use {@link #createProject(IProject, URI, IProgressMonitor)} instead. | |
234 | */ | |
235 | public static void createProject(IProject project, IPath locationPath, IProgressMonitor monitor) throws CoreException { | |
236 | createProject(project, locationPath != null ? URIUtil.toURI(locationPath) : null, monitor); | |
237 | } | |
238 | ||
239 | /** | |
240 | * Helper method to create and open a IProject. The project location | |
241 | * is configured. No natures are added. | |
242 | * | |
243 | * @param project The handle of the project to create. | |
244 | * @param locationURI The location of the project or <code>null</code> to create the project in the workspace | |
245 | * @param monitor a progress monitor to report progress or <code>null</code> if | |
246 | * progress reporting is not desired | |
247 | * @throws CoreException if the project couldn't be created | |
248 | * @see org.eclipse.core.resources.IProjectDescription#setLocationURI(java.net.URI) | |
249 | * @since 3.2 | |
250 | */ | |
251 | public static void createProject(IProject project, URI locationURI, IProgressMonitor monitor) throws CoreException { | |
252 | BuildPathsBlock.createProject(project, locationURI, monitor); | |
253 | } | |
254 | ||
255 | /** | |
256 | * Adds the Java nature to the project (if not set yet) and configures the build classpath. | |
257 | * | |
258 | * @param monitor a progress monitor to report progress or <code>null</code> if | |
259 | * progress reporting is not desired | |
260 | * @throws CoreException Thrown when the configuring the Java project failed. | |
261 | * @throws InterruptedException Thrown when the operation has been canceled. | |
262 | */ | |
263 | public void configureJavaProject(IProgressMonitor monitor) throws CoreException, InterruptedException { | |
264 | configureJavaProject(null, monitor); | |
265 | } | |
266 | ||
267 | /** | |
268 | * Adds the Java nature to the project (if not set yet) and configures the build classpath. | |
269 | * | |
270 | * @param newProjectCompliance compliance to set for a new project, can be <code>null</code> | |
271 | * @param monitor a progress monitor to report progress or <code>null</code> if | |
272 | * progress reporting is not desired | |
273 | * @throws CoreException Thrown when the configuring the Java project failed. | |
274 | * @throws InterruptedException Thrown when the operation has been canceled. | |
275 | * @since 3.5 | |
276 | */ | |
277 | public void configureJavaProject(String newProjectCompliance, IProgressMonitor monitor) throws CoreException, InterruptedException { | |
278 | if (monitor == null) { | |
279 | monitor= new NullProgressMonitor(); | |
280 | } | |
281 | ||
282 | int nSteps= 6; | |
283 | monitor.beginTask(NewWizardMessages.JavaCapabilityConfigurationPage_op_desc_java, nSteps); | |
284 | ||
285 | try { | |
286 | IProject project= getJavaProject().getProject(); | |
287 | BuildPathsBlock.addJavaNature(project, new SubProgressMonitor(monitor, 1)); | |
288 | getBuildPathsBlock().configureJavaProject(newProjectCompliance, new SubProgressMonitor(monitor, 5)); | |
289 | } catch (OperationCanceledException e) { | |
290 | throw new InterruptedException(); | |
291 | } finally { | |
292 | monitor.done(); | |
293 | } | |
294 | } | |
295 | ||
296 | /** | |
297 | * Transfers the focus into this page. | |
298 | * | |
299 | * @since 3.3 | |
300 | */ | |
301 | protected void setFocus() { | |
302 | getBuildPathsBlock().setFocus(); | |
303 | } | |
304 | } |