001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.wizards;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.net.URI;
014:
015: import org.eclipse.core.filesystem.URIUtil;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.IStatus;
021: import org.eclipse.core.runtime.NullProgressMonitor;
022: import org.eclipse.core.runtime.OperationCanceledException;
023: import org.eclipse.core.runtime.SubProgressMonitor;
024:
025: import org.eclipse.core.resources.IProject;
026:
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.layout.GridLayout;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032:
033: import org.eclipse.jface.dialogs.Dialog;
034: import org.eclipse.jface.operation.IRunnableWithProgress;
035:
036: import org.eclipse.ui.PlatformUI;
037:
038: import org.eclipse.jdt.core.IClasspathEntry;
039: import org.eclipse.jdt.core.IJavaProject;
040:
041: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
042: import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
043: import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
044: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
045: import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock;
046:
047: /**
048: * Basic wizard page for creating new Java projects. This page can be used in
049: * project creation wizards. The page shows UI to configure the project with a Java
050: * build path and output location. On finish the page will also configure the Java nature.
051: * <p>
052: * This is a replacement for {@link NewJavaProjectWizardPage} with a cleaner API.
053: * </p>
054: * <p>
055: * The pages used by the New Java Project wizard are {@link NewJavaProjectWizardPageOne} and
056: * {@link NewJavaProjectWizardPageTwo}.
057: * </p>
058: * <p>
059: * Clients may instantiate or subclass.
060: * </p>
061: *
062: * @since 2.0
063: */
064: public class JavaCapabilityConfigurationPage extends
065: NewElementWizardPage {
066:
067: private static final String PAGE_NAME = "JavaCapabilityConfigurationPage"; //$NON-NLS-1$
068:
069: private IJavaProject fJavaProject;
070: private BuildPathsBlock fBuildPathsBlock;
071:
072: /**
073: * Creates a wizard page that can be used in a Java project creation wizard.
074: * It contains UI to configure a the classpath and the output folder.
075: *
076: * <p>
077: * After constructing, a call to {@link #init(IJavaProject, IPath, IClasspathEntry[], boolean)} is required.
078: * </p>
079: */
080: public JavaCapabilityConfigurationPage() {
081: super (PAGE_NAME);
082: fJavaProject = null;
083:
084: setTitle(NewWizardMessages.JavaCapabilityConfigurationPage_title);
085: setDescription(NewWizardMessages.JavaCapabilityConfigurationPage_description);
086: }
087:
088: private BuildPathsBlock getBuildPathsBlock() {
089: if (fBuildPathsBlock == null) {
090: IStatusChangeListener listener = new IStatusChangeListener() {
091: public void statusChanged(IStatus status) {
092: updateStatus(status);
093: }
094: };
095: fBuildPathsBlock = new BuildPathsBlock(
096: new BusyIndicatorRunnableContext(), listener, 0,
097: useNewSourcePage(), null);
098: }
099: return fBuildPathsBlock;
100: }
101:
102: /*
103: * @see org.eclipse.jface.dialogs.DialogPage#dispose()
104: * @since 3.3
105: */
106: public void dispose() {
107: try {
108: super .dispose();
109: } finally {
110: if (fBuildPathsBlock != null) {
111: fBuildPathsBlock.dispose();
112: fBuildPathsBlock = null;
113: }
114: }
115: }
116:
117: /**
118: * Clients can override this method to choose if the new source page is used. The new source page
119: * requires that the project is already created as Java project. The page will directly manipulate the classpath.
120: * By default <code>false</code> is returned.
121: * @return Returns <code>true</code> if the new source page should be used.
122: * @since 3.1
123: */
124: protected boolean useNewSourcePage() {
125: return false;
126: }
127:
128: /**
129: * Initializes the page with the project and default classpath.
130: * <p>
131: * The default classpath entries must correspond the given project.
132: * </p>
133: * <p>
134: * The caller of this method is responsible for creating the underlying project. The page will create the output,
135: * source and library folders if required.
136: * </p>
137: * <p>
138: * The project does not have to exist at the time of initialization, but must exist when executing the runnable
139: * obtained by <code>getRunnable()</code>.
140: * </p>
141: * @param jproject The Java project.
142: * @param defaultOutputLocation The default classpath entries or <code>null</code> to let the page choose the default
143: * @param defaultEntries The folder to be taken as the default output path or <code>null</code> to let the page choose the default
144: * @param defaultsOverrideExistingClasspath If set to <code>true</code>, an existing '.classpath' file is ignored. If set to <code>false</code>
145: * the given default classpath and output location is only used if no '.classpath' exists.
146: */
147: public void init(IJavaProject jproject,
148: IPath defaultOutputLocation,
149: IClasspathEntry[] defaultEntries,
150: boolean defaultsOverrideExistingClasspath) {
151: if (!defaultsOverrideExistingClasspath && jproject.exists()
152: && jproject.getProject().getFile(".classpath").exists()) { //$NON-NLS-1$
153: defaultOutputLocation = null;
154: defaultEntries = null;
155: }
156: getBuildPathsBlock().init(jproject, defaultOutputLocation,
157: defaultEntries);
158: fJavaProject = jproject;
159: }
160:
161: /* (non-Javadoc)
162: * @see WizardPage#createControl
163: */
164: public void createControl(Composite parent) {
165: Composite composite = new Composite(parent, SWT.NONE);
166: composite.setFont(parent.getFont());
167: composite.setLayout(new GridLayout(1, false));
168: Control control = getBuildPathsBlock().createControl(composite);
169: control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
170: true));
171: Dialog.applyDialogFont(composite);
172: PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
173: IJavaHelpContextIds.NEW_JAVAPROJECT_WIZARD_PAGE);
174: setControl(composite);
175: }
176:
177: /**
178: * Returns the currently configured output location. Note that the returned path
179: * might not be a valid path.
180: *
181: * @return the currently configured output location
182: */
183: public IPath getOutputLocation() {
184: return getBuildPathsBlock().getOutputLocation();
185: }
186:
187: /**
188: * Returns the currently configured classpath. Note that the classpath might
189: * not be valid.
190: *
191: * @return the currently configured classpath
192: */
193: public IClasspathEntry[] getRawClassPath() {
194: return getBuildPathsBlock().getRawClassPath();
195: }
196:
197: /**
198: * Returns the Java project that was passed in {@link #init(IJavaProject, IPath, IClasspathEntry[], boolean)} or <code>null</code> if the
199: * page has not been initialized yet.
200: *
201: * @return the managed Java project or <code>null</code>
202: */
203: public IJavaProject getJavaProject() {
204: return fJavaProject;
205: }
206:
207: /**
208: * Returns the runnable that will create the Java project or <code>null</code> if the page has
209: * not been initialized. The runnable sets the project's classpath and output location to the values
210: * configured in the page and adds the Java nature if not set yet. The method requires that the
211: * project is created and opened.
212: *
213: * @return the runnable that creates the new Java project
214: */
215: public IRunnableWithProgress getRunnable() {
216: if (getJavaProject() != null) {
217: return new IRunnableWithProgress() {
218: public void run(IProgressMonitor monitor)
219: throws InvocationTargetException,
220: InterruptedException {
221: try {
222: configureJavaProject(monitor);
223: } catch (CoreException e) {
224: throw new InvocationTargetException(e);
225: }
226: }
227: };
228: }
229: return null;
230: }
231:
232: /**
233: * Helper method to create and open a IProject. The project location
234: * is configured. No natures are added.
235: *
236: * @param project The handle of the project to create.
237: * @param locationPath The location of the project <code>null</code> to create the project in the workspace
238: * @param monitor a progress monitor to report progress or <code>null</code> if
239: * progress reporting is not desired
240: * @throws CoreException if the project couldn't be created
241: * @since 2.1
242: * @deprecated use {@link #createProject(IProject, URI, IProgressMonitor)} instead.
243: */
244: public static void createProject(IProject project,
245: IPath locationPath, IProgressMonitor monitor)
246: throws CoreException {
247: createProject(project, locationPath != null ? URIUtil
248: .toURI(locationPath) : null, monitor);
249: }
250:
251: /**
252: * Helper method to create and open a IProject. The project location
253: * is configured. No natures are added.
254: *
255: * @param project The handle of the project to create.
256: * @param locationURI The location of the project or <code>null</code> to create the project in the workspace
257: * @param monitor a progress monitor to report progress or <code>null</code> if
258: * progress reporting is not desired
259: * @throws CoreException if the project couldn't be created
260: * @see org.eclipse.core.resources.IProjectDescription#setLocationURI(java.net.URI)
261: * @since 3.2
262: */
263: public static void createProject(IProject project, URI locationURI,
264: IProgressMonitor monitor) throws CoreException {
265: BuildPathsBlock.createProject(project, locationURI, monitor);
266: }
267:
268: /**
269: * Adds the Java nature to the project (if not set yet) and configures the build classpath.
270: *
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: */
276: public void configureJavaProject(IProgressMonitor monitor)
277: throws CoreException, InterruptedException {
278: if (monitor == null) {
279: monitor = new NullProgressMonitor();
280: }
281:
282: int nSteps = 6;
283: monitor
284: .beginTask(
285: NewWizardMessages.JavaCapabilityConfigurationPage_op_desc_java,
286: nSteps);
287:
288: try {
289: IProject project = getJavaProject().getProject();
290: BuildPathsBlock.addJavaNature(project,
291: new SubProgressMonitor(monitor, 1));
292: getBuildPathsBlock().configureJavaProject(
293: new SubProgressMonitor(monitor, 5));
294: } catch (OperationCanceledException e) {
295: throw new InterruptedException();
296: } finally {
297: monitor.done();
298: }
299: }
300:
301: /**
302: * Transfers the focus into this page.
303: *
304: * @since 3.3
305: */
306: protected void setFocus() {
307: getBuildPathsBlock().setFocus();
308: }
309: }
|