1 11 package org.eclipse.jdt.ui.wizards; 12 13 import java.lang.reflect.InvocationTargetException ; 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 65 public class NewJavaProjectWizardPage extends NewElementWizardPage { 66 67 private static final String PAGE_NAME= "NewJavaProjectWizardPage"; 69 private WizardNewProjectCreationPage fMainPage; 70 71 private IPath fOutputLocation; 72 private IClasspathEntry[] fClasspathEntries; 73 private BuildPathsBlock fBuildPathsBlock; 74 75 private boolean fProjectModified; 76 77 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 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 137 public void setDefaultOutputFolder(IPath path) { 138 fOutputLocation= path; 139 setProjectModified(); 140 } 141 142 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 178 public void setProjectModified() { 179 fProjectModified= true; 180 } 181 182 189 protected IProject getProjectHandle() { 190 Assert.isNotNull(fMainPage); 191 return fMainPage.getProjectHandle(); 192 } 193 194 201 protected IPath getLocationPath() { 202 Assert.isNotNull(fMainPage); 203 return fMainPage.getLocationPath(); 204 } 205 206 213 public IJavaProject getNewJavaProject() { 214 return JavaCore.create(getProjectHandle()); 215 } 216 217 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 240 protected void initBuildPaths() { 241 fBuildPathsBlock.init(getNewJavaProject(), fOutputLocation, fClasspathEntries); 242 } 243 244 252 public void setVisible(boolean visible) { 253 super.setVisible(visible); 254 if (visible) { 255 if (fProjectModified || isNewProjectHandle()) { 257 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 278 public IPath getOutputLocation() { 279 return fBuildPathsBlock.getOutputLocation(); 280 } 281 282 290 public IClasspathEntry[] getRawClassPath() { 291 return fBuildPathsBlock.getRawClassPath(); 292 } 293 294 295 305 public IRunnableWithProgress getRunnable() { 306 return new IRunnableWithProgress() { 307 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 308 if (monitor == null) { 309 monitor= new NullProgressMonitor(); 310 } 311 monitor.beginTask(NewWizardMessages.NewJavaProjectWizardPage_op_desc, 10); 312 if (fProjectModified || isNewProjectHandle()) { 314 initBuildPaths(); 315 } 316 317 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 (e); 327 } catch (OperationCanceledException e) { 328 throw new InterruptedException (); 329 } finally { 330 monitor.done(); 331 } 332 } 333 }; 334 } 335 } 336 | Popular Tags |