KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > wizards > JavaCapabilityConfigurationPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 JavaDoc;
14 import java.net.URI JavaDoc;
15
16 import org.eclipse.core.filesystem.URIUtil;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.core.runtime.OperationCanceledException;
24 import org.eclipse.core.runtime.SubProgressMonitor;
25
26 import org.eclipse.core.resources.IProject;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
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  * Standard 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 <code>NewJavaProjectWizardPage</code> with a cleaner API.
54  * </p>
55  * <p>
56  * Clients may instantiate or subclass.
57  * </p>
58  *
59  * @since 2.0
60  */

61 public class JavaCapabilityConfigurationPage extends NewElementWizardPage {
62
63     private static final String JavaDoc PAGE_NAME= "JavaCapabilityConfigurationPage"; //$NON-NLS-1$
64

65     private IJavaProject fJavaProject;
66     private BuildPathsBlock fBuildPathsBlock;
67     
68     /**
69      * Creates a wizard page that can be used in a Java project creation wizard.
70      * It contains UI to configure a the classpath and the output folder.
71      *
72      * <p>
73      * After constructing, a call to {@link #init(IJavaProject, IPath, IClasspathEntry[], boolean)} is required.
74      * </p>
75      */

76     public JavaCapabilityConfigurationPage() {
77         super(PAGE_NAME);
78         fJavaProject= null;
79         
80         setTitle(NewWizardMessages.JavaCapabilityConfigurationPage_title);
81         setDescription(NewWizardMessages.JavaCapabilityConfigurationPage_description);
82     }
83     
84     private BuildPathsBlock getBuildPathsBlock() {
85         if (fBuildPathsBlock == null) {
86             IStatusChangeListener listener= new IStatusChangeListener() {
87                 public void statusChanged(IStatus status) {
88                     updateStatus(status);
89                 }
90             };
91             fBuildPathsBlock= new BuildPathsBlock(new BusyIndicatorRunnableContext(), listener, 0, useNewSourcePage(), null);
92         }
93         return fBuildPathsBlock;
94     }
95     
96     /*
97      * @see org.eclipse.jface.dialogs.DialogPage#dispose()
98      * @since 3.3
99      */

100     public void dispose() {
101         try {
102             super.dispose();
103         } finally {
104             if (fBuildPathsBlock != null) {
105                 fBuildPathsBlock.dispose();
106                 fBuildPathsBlock= null;
107             }
108         }
109     }
110     
111     /**
112      * Clients can override this method to choose if the new source page is used. The new source page
113      * requires that the project is already created as Java project. The page will directly manipulate the classpath.
114      * By default <code>false</code> is returned.
115      * @return Returns <code>true</code> if the new source page should be used.
116      * @since 3.1
117      */

118     protected boolean useNewSourcePage() {
119         return false;
120     }
121
122     /**
123      * Initializes the page with the project and default classpath.
124      * <p>
125      * The default classpath entries must correspond the given project.
126      * </p>
127      * <p>
128      * The caller of this method is responsible for creating the underlying project. The page will create the output,
129      * source and library folders if required.
130      * </p>
131      * <p>
132      * The project does not have to exist at the time of initialization, but must exist when executing the runnable
133      * obtained by <code>getRunnable()</code>.
134      * </p>
135      * @param jproject The Java project.
136      * @param defaultOutputLocation The default classpath entries or <code>null</code> to let the page choose the default
137      * @param defaultEntries The folder to be taken as the default output path or <code>null</code> to let the page choose the default
138      * @param defaultsOverrideExistingClasspath If set to <code>true</code>, an existing '.classpath' file is ignored. If set to <code>false</code>
139      * the given default classpath and output location is only used if no '.classpath' exists.
140      */

141     public void init(IJavaProject jproject, IPath defaultOutputLocation, IClasspathEntry[] defaultEntries, boolean defaultsOverrideExistingClasspath) {
142         if (!defaultsOverrideExistingClasspath && jproject.exists() && jproject.getProject().getFile(".classpath").exists()) { //$NON-NLS-1$
143
defaultOutputLocation= null;
144             defaultEntries= null;
145         }
146         getBuildPathsBlock().init(jproject, defaultOutputLocation, defaultEntries);
147         fJavaProject= jproject;
148     }
149
150     /* (non-Javadoc)
151      * @see WizardPage#createControl
152      */

153     public void createControl(Composite parent) {
154         Composite composite= new Composite(parent, SWT.NONE);
155         composite.setFont(parent.getFont());
156         composite.setLayout(new GridLayout(1, false));
157         Control control= getBuildPathsBlock().createControl(composite);
158         control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
159         Dialog.applyDialogFont(composite);
160         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_JAVAPROJECT_WIZARD_PAGE);
161         setControl(composite);
162     }
163         
164     /**
165      * Returns the currently configured output location. Note that the returned path
166      * might not be a valid path.
167      *
168      * @return the currently configured output location
169      */

170     public IPath getOutputLocation() {
171         return getBuildPathsBlock().getOutputLocation();
172     }
173
174     /**
175      * Returns the currently configured classpath. Note that the classpath might
176      * not be valid.
177      *
178      * @return the currently configured classpath
179      */

180     public IClasspathEntry[] getRawClassPath() {
181         return getBuildPathsBlock().getRawClassPath();
182     }
183     
184     /**
185      * Returns the Java project that was passed in {@link #init(IJavaProject, IPath, IClasspathEntry[], boolean)} or <code>null</code> if the
186      * page has not been initialized yet.
187      *
188      * @return the managed Java project or <code>null</code>
189      */

190     public IJavaProject getJavaProject() {
191         return fJavaProject;
192     }
193     
194
195     /**
196      * Returns the runnable that will create the Java project or <code>null</code> if the page has
197      * not been initialized. The runnable sets the project's classpath and output location to the values
198      * configured in the page and adds the Java nature if not set yet. The method requires that the
199      * project is created and opened.
200      *
201      * @return the runnable that creates the new Java project
202      */

203     public IRunnableWithProgress getRunnable() {
204         if (getJavaProject() != null) {
205             return new IRunnableWithProgress() {
206                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
207                     try {
208                         configureJavaProject(monitor);
209                     } catch (CoreException e) {
210                         throw new InvocationTargetException JavaDoc(e);
211                     }
212                 }
213             };
214         }
215         return null;
216     }
217
218     /**
219      * Helper method to create and open a IProject. The project location
220      * is configured. No natures are added.
221      *
222      * @param project The handle of the project to create.
223      * @param locationPath The location of the project <code>null</code> to create the project in the workspace
224      * @param monitor a progress monitor to report progress or <code>null</code> if
225      * progress reporting is not desired
226      * @throws CoreException if the project couldn't be created
227      * @since 2.1
228      * @deprecated use {@link #createProject(IProject, URI, IProgressMonitor)} instead.
229      */

230     public static void createProject(IProject project, IPath locationPath, IProgressMonitor monitor) throws CoreException {
231         createProject(project, locationPath != null ? URIUtil.toURI(locationPath) : null, monitor);
232     }
233     
234     /**
235      * Helper method to create and open a IProject. The project location
236      * is configured. No natures are added.
237      *
238      * @param project The handle of the project to create.
239      * @param locationURI The location of the project or <code>null</code> to create the project in the workspace
240      * @param monitor a progress monitor to report progress or <code>null</code> if
241      * progress reporting is not desired
242      * @throws CoreException if the project couldn't be created
243      * @see org.eclipse.core.resources.IProjectDescription#setLocationURI(java.net.URI)
244      * @since 3.2
245      */

246     public static void createProject(IProject project, URI JavaDoc locationURI, IProgressMonitor monitor) throws CoreException {
247         BuildPathsBlock.createProject(project, locationURI, monitor);
248     }
249     
250     /**
251      * Adds the Java nature to the project (if not set yet) and configures the build classpath.
252      *
253      * @param monitor a progress monitor to report progress or <code>null</code> if
254      * progress reporting is not desired
255      * @throws CoreException Thrown when the configuring the Java project failed.
256      * @throws InterruptedException Thrown when the operation has been canceled.
257      */

258     public void configureJavaProject(IProgressMonitor monitor) throws CoreException, InterruptedException JavaDoc {
259         if (monitor == null) {
260             monitor= new NullProgressMonitor();
261         }
262         
263         int nSteps= 6;
264         monitor.beginTask(NewWizardMessages.JavaCapabilityConfigurationPage_op_desc_java, nSteps);
265         
266         try {
267             IProject project= getJavaProject().getProject();
268             BuildPathsBlock.addJavaNature(project, new SubProgressMonitor(monitor, 1));
269             getBuildPathsBlock().configureJavaProject(new SubProgressMonitor(monitor, 5));
270         } catch (OperationCanceledException e) {
271             throw new InterruptedException JavaDoc();
272         } finally {
273             monitor.done();
274         }
275     }
276     
277     /**
278      * Transfers the focus into this page.
279      *
280      * @since 3.3
281      */

282     protected void setFocus() {
283         getBuildPathsBlock().setFocus();
284     }
285 }
286
Popular Tags