KickJava   Java API By Example, From Geeks To Geeks.

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


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
15 import org.eclipse.core.filesystem.URIUtil;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IWorkspaceRoot;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.core.runtime.OperationCanceledException;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34
35 import org.eclipse.jface.dialogs.Dialog;
36 import org.eclipse.jface.operation.IRunnableWithProgress;
37
38 import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
39 import org.eclipse.ui.PlatformUI;
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 <code>JavaCapabilityConfigurationPage
59  * </code> in favor of this class.
60  * </p>
61  * <p>
62  * Clients may instantiate or subclass.
63  * </p>
64  */

65 public class NewJavaProjectWizardPage extends NewElementWizardPage {
66     
67     private static final String JavaDoc PAGE_NAME= "NewJavaProjectWizardPage"; //$NON-NLS-1$
68

69     private WizardNewProjectCreationPage fMainPage;
70     
71     private IPath fOutputLocation;
72     private IClasspathEntry[] fClasspathEntries;
73     private BuildPathsBlock fBuildPathsBlock;
74
75     private boolean fProjectModified;
76     
77     /**
78      * Creates a Java project wizard creation page.
79      * <p>
80      * The Java project wizard reads project name and location from the main page.
81      * </p>
82      *
83      * @param root the workspace root
84      * @param mainpage the main page of the wizard
85      */

86     public NewJavaProjectWizardPage(IWorkspaceRoot root, WizardNewProjectCreationPage mainpage) {
87         super(PAGE_NAME);
88         
89         setTitle(NewWizardMessages.NewJavaProjectWizardPage_title);
90         setDescription(NewWizardMessages.NewJavaProjectWizardPage_description);
91         
92         fMainPage= mainpage;
93         IStatusChangeListener listener= new IStatusChangeListener() {
94             public void statusChanged(IStatus status) {
95                 updateStatus(status);
96             }
97         };
98
99         fBuildPathsBlock= new BuildPathsBlock(new BusyIndicatorRunnableContext(), listener, 0, false, null);
100         
101         fProjectModified= true;
102         fOutputLocation= null;
103         fClasspathEntries= null;
104     }
105     
106     /*
107      * @see org.eclipse.jface.dialogs.DialogPage#dispose()
108      * @since 3.3
109      */

110     public void dispose() {
111         try {
112             super.dispose();
113         } finally {
114             if (fBuildPathsBlock != null) {
115                 fBuildPathsBlock.dispose();
116                 fBuildPathsBlock= null;
117             }
118         }
119     }
120     
121     /**
122      * Sets the default output location to be used for the new Java project.
123      * This is the path of the folder (with the project) into which the Java builder
124      * will generate binary class files corresponding to the project's Java source
125      * files.
126      * <p>
127      * The wizard will create this folder if required.
128      * </p>
129      * <p>
130      * The default classpath will be applied when <code>initBuildPaths</code> is
131      * called. This is done automatically when the page becomes visible and
132      * the project or the default paths have changed.
133      * </p>
134      *
135      * @param path the folder to be taken as the default output path
136      */

137     public void setDefaultOutputFolder(IPath path) {
138         fOutputLocation= path;
139         setProjectModified();
140     }
141
142     /**
143      * Sets the default classpath to be used for the new Java project.
144      * <p>
145      * The caller of this method is responsible for creating the classpath entries
146      * for the <code>IJavaProject</code> that corresponds to the created project.
147      * The caller is responsible for creating any new folders that might be mentioned
148      * on the classpath.
149      * </p>
150      * <p>
151      * The default output location will be applied when <code>initBuildPaths</code> is
152      * called. This is done automatically when the page becomes visible and
153      * the project or the default paths have changed.
154      * </p>
155      *
156      * @param entries the default classpath entries
157      * @param appendDefaultJRE <code>true</code> a variable entry for the
158      * default JRE (specified in the preferences) will be added to the classpath.
159      */

160     public void setDefaultClassPath(IClasspathEntry[] entries, boolean appendDefaultJRE) {
161         if (entries != null && appendDefaultJRE) {
162             IClasspathEntry[] jreEntry= NewJavaProjectPreferencePage.getDefaultJRELibrary();
163             IClasspathEntry[] newEntries= new IClasspathEntry[entries.length + jreEntry.length];
164             System.arraycopy(entries, 0, newEntries, 0, entries.length);
165             System.arraycopy(jreEntry, 0, newEntries, entries.length, jreEntry.length);
166             entries= newEntries;
167         }
168         fClasspathEntries= entries;
169         setProjectModified();
170     }
171     
172     /**
173      * Sets the project state to modified. Doing so will initialize the page
174      * the next time it becomes visible.
175      *
176      * @since 2.0
177      */

178     public void setProjectModified() {
179         fProjectModified= true;
180     }
181
182     /**
183      * Returns the project handle. Subclasses should override this
184      * method if they don't provide a main page or if they provide
185      * their own main page implementation.
186      *
187      * @return the project handle
188      */

189     protected IProject getProjectHandle() {
190         Assert.isNotNull(fMainPage);
191         return fMainPage.getProjectHandle();
192     }
193     
194     /**
195      * Returns the project location path. Subclasses should override this
196      * method if they don't provide a main page or if they provide
197      * their own main page implementation.
198      *
199      * @return the project location path
200      */

201     protected IPath getLocationPath() {
202         Assert.isNotNull(fMainPage);
203         return fMainPage.getLocationPath();
204     }
205
206     /**
207      * Returns the Java project handle by converting the result of
208      * <code>getProjectHandle()</code> into a Java project.
209      *
210      * @return the Java project handle
211      * @see #getProjectHandle()
212      */

213     public IJavaProject getNewJavaProject() {
214         return JavaCore.create(getProjectHandle());
215     }
216
217     /* (non-Javadoc)
218      * @see WizardPage#createControl
219      */

220     public void createControl(Composite parent) {
221         Composite composite= new Composite(parent, SWT.NONE);
222         composite.setFont(parent.getFont());
223         composite.setLayout(new GridLayout(1, false));
224         Control control= fBuildPathsBlock.createControl(composite);
225         control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
226         Dialog.applyDialogFont(composite);
227         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_JAVAPROJECT_WIZARD_PAGE);
228         setControl(composite);
229     }
230     
231     /**
232      * Forces the initialization of the Java project page. Default classpath or buildpath
233      * will be used if set. The initialization should only be performed when the project
234      * or default paths have changed. Toggling back and forward the pages without
235      * changes should not re-initialize the page, as changes from the user will be
236      * overwritten.
237      *
238      * @since 2.0
239      */

240     protected void initBuildPaths() {
241         fBuildPathsBlock.init(getNewJavaProject(), fOutputLocation, fClasspathEntries);
242     }
243
244     /**
245      * Extend this method to set a user defined default classpath or output location.
246      * The method <code>initBuildPaths</code> is called when the page becomes
247      * visible the first time or the project or the default paths have changed.
248      *
249      * @param visible if <code>true</code> the page becomes visible; otherwise
250      * it becomes invisible
251      */

252     public void setVisible(boolean visible) {
253         super.setVisible(visible);
254         if (visible) {
255             // evaluate if a initialization is required
256
if (fProjectModified || isNewProjectHandle()) {
257                 // only initialize the project when needed
258
initBuildPaths();
259                 fProjectModified= false;
260             }
261         }
262     }
263     
264     private boolean isNewProjectHandle() {
265         IProject oldProject= fBuildPathsBlock.getJavaProject().getProject();
266         return !oldProject.equals(getProjectHandle());
267     }
268     
269     
270     /**
271      * Returns the currently configured output location. Note that the returned path
272      * might not be valid.
273      *
274      * @return the configured output location
275      *
276      * @since 2.0
277      */

278     public IPath getOutputLocation() {
279         return fBuildPathsBlock.getOutputLocation();
280     }
281
282     /**
283      * Returns the currently configured classpath. Note that the classpath might
284      * not be valid.
285      *
286      * @return the configured classpath
287      *
288      * @since 2.0
289      */

290     public IClasspathEntry[] getRawClassPath() {
291         return fBuildPathsBlock.getRawClassPath();
292     }
293     
294
295     /**
296      * Returns the runnable that will create the Java project. The runnable will create
297      * and open the project if needed. The runnable will add the Java nature to the
298      * project, and set the project's classpath and output location.
299      * <p>
300      * To create the new java project, execute this runnable
301      * </p>
302      *
303      * @return the runnable
304      */

305     public IRunnableWithProgress getRunnable() {
306         return new IRunnableWithProgress() {
307             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
308                 if (monitor == null) {
309                     monitor= new NullProgressMonitor();
310                 }
311                 monitor.beginTask(NewWizardMessages.NewJavaProjectWizardPage_op_desc, 10);
312                 // initialize if needed
313
if (fProjectModified || isNewProjectHandle()) {
314                     initBuildPaths();
315                 }
316                 
317                 // create the project
318
try {
319                     IPath locationPath= getLocationPath();
320                     BuildPathsBlock.createProject(getProjectHandle(),
321                         locationPath != null ? URIUtil.toURI(locationPath) : null,
322                         new SubProgressMonitor(monitor, 2));
323                     BuildPathsBlock.addJavaNature(getProjectHandle(), new SubProgressMonitor(monitor, 2));
324                     fBuildPathsBlock.configureJavaProject(new SubProgressMonitor(monitor, 6));
325                 } catch (CoreException e) {
326                     throw new InvocationTargetException JavaDoc(e);
327                 } catch (OperationCanceledException e) {
328                     throw new InterruptedException JavaDoc();
329                 } finally {
330                     monitor.done();
331                 }
332             }
333         };
334     }
335 }
336
Popular Tags