1 11 package org.eclipse.jdt.ui.wizards; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IAdaptable; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Path; 18 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IWorkspaceRoot; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 24 import org.eclipse.swt.widgets.Composite; 25 26 import org.eclipse.jface.viewers.ILabelProvider; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.ISelectionProvider; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.Viewer; 31 import org.eclipse.jface.viewers.ViewerFilter; 32 import org.eclipse.jface.window.Window; 33 34 import org.eclipse.jface.text.ITextSelection; 35 36 import org.eclipse.ui.IEditorPart; 37 import org.eclipse.ui.IWorkbenchPart; 38 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 39 40 import org.eclipse.ui.views.contentoutline.ContentOutline; 41 42 import org.eclipse.jdt.core.IJavaElement; 43 import org.eclipse.jdt.core.IJavaModel; 44 import org.eclipse.jdt.core.IJavaProject; 45 import org.eclipse.jdt.core.IPackageFragmentRoot; 46 import org.eclipse.jdt.core.JavaCore; 47 import org.eclipse.jdt.core.JavaModelException; 48 49 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 50 import org.eclipse.jdt.internal.corext.util.Messages; 51 52 import org.eclipse.jdt.ui.JavaElementComparator; 53 import org.eclipse.jdt.ui.JavaElementLabelProvider; 54 import org.eclipse.jdt.ui.StandardJavaElementContentProvider; 55 56 import org.eclipse.jdt.internal.ui.JavaPlugin; 57 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 58 import org.eclipse.jdt.internal.ui.viewsupport.IViewPartInputProvider; 59 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 60 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; 61 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter; 62 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 63 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 64 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter; 65 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 66 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField; 67 68 79 public abstract class NewContainerWizardPage extends NewElementWizardPage { 80 81 82 protected static final String CONTAINER= "NewContainerWizardPage.container"; 84 85 protected IStatus fContainerStatus; 86 87 private StringButtonDialogField fContainerDialogField; 88 89 92 private IPackageFragmentRoot fCurrRoot; 93 94 private IWorkspaceRoot fWorkspaceRoot; 95 96 101 public NewContainerWizardPage(String name) { 102 super(name); 103 fWorkspaceRoot= ResourcesPlugin.getWorkspace().getRoot(); 104 ContainerFieldAdapter adapter= new ContainerFieldAdapter(); 105 106 fContainerDialogField= new StringButtonDialogField(adapter); 107 fContainerDialogField.setDialogFieldListener(adapter); 108 fContainerDialogField.setLabelText(getContainerLabel()); 109 fContainerDialogField.setButtonLabel(NewWizardMessages.NewContainerWizardPage_container_button); 110 111 fContainerStatus= new StatusInfo(); 112 fCurrRoot= null; 113 } 114 115 121 protected String getContainerLabel() { 122 return NewWizardMessages.NewContainerWizardPage_container_label; 123 } 124 125 132 protected void initContainerPage(IJavaElement elem) { 133 IPackageFragmentRoot initRoot= null; 134 if (elem != null) { 135 initRoot= JavaModelUtil.getPackageFragmentRoot(elem); 136 try { 137 if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) { 138 IJavaProject jproject= elem.getJavaProject(); 139 if (jproject != null) { 140 initRoot= null; 141 if (jproject.exists()) { 142 IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots(); 143 for (int i= 0; i < roots.length; i++) { 144 if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) { 145 initRoot= roots[i]; 146 break; 147 } 148 } 149 } 150 if (initRoot == null) { 151 initRoot= jproject.getPackageFragmentRoot(jproject.getResource()); 152 } 153 } 154 } 155 } catch (JavaModelException e) { 156 JavaPlugin.log(e); 157 } 158 } 159 setPackageFragmentRoot(initRoot, true); 160 } 161 162 169 protected IJavaElement getInitialJavaElement(IStructuredSelection selection) { 170 IJavaElement jelem= null; 171 if (selection != null && !selection.isEmpty()) { 172 Object selectedElement= selection.getFirstElement(); 173 if (selectedElement instanceof IAdaptable) { 174 IAdaptable adaptable= (IAdaptable) selectedElement; 175 176 jelem= (IJavaElement) adaptable.getAdapter(IJavaElement.class); 177 if (jelem == null) { 178 IResource resource= (IResource) adaptable.getAdapter(IResource.class); 179 if (resource != null && resource.getType() != IResource.ROOT) { 180 while (jelem == null && resource.getType() != IResource.PROJECT) { 181 resource= resource.getParent(); 182 jelem= (IJavaElement) resource.getAdapter(IJavaElement.class); 183 } 184 if (jelem == null) { 185 jelem= JavaCore.create(resource); } 187 } 188 } 189 } 190 } 191 if (jelem == null) { 192 IWorkbenchPart part= JavaPlugin.getActivePage().getActivePart(); 193 if (part instanceof ContentOutline) { 194 part= JavaPlugin.getActivePage().getActiveEditor(); 195 } 196 197 if (part instanceof IViewPartInputProvider) { 198 Object elem= ((IViewPartInputProvider)part).getViewPartInput(); 199 if (elem instanceof IJavaElement) { 200 jelem= (IJavaElement) elem; 201 } 202 } 203 } 204 205 if (jelem == null || jelem.getElementType() == IJavaElement.JAVA_MODEL) { 206 try { 207 IJavaProject[] projects= JavaCore.create(getWorkspaceRoot()).getJavaProjects(); 208 if (projects.length == 1) { 209 jelem= projects[0]; 210 } 211 } catch (JavaModelException e) { 212 JavaPlugin.log(e); 213 } 214 } 215 return jelem; 216 } 217 218 225 protected ITextSelection getCurrentTextSelection() { 226 IWorkbenchPart part= JavaPlugin.getActivePage().getActivePart(); 227 if (part instanceof IEditorPart) { 228 ISelectionProvider selectionProvider= part.getSite().getSelectionProvider(); 229 if (selectionProvider != null) { 230 ISelection selection= selectionProvider.getSelection(); 231 if (selection instanceof ITextSelection) { 232 return (ITextSelection) selection; 233 } 234 } 235 } 236 return null; 237 } 238 239 240 248 protected int getMaxFieldWidth() { 249 return convertWidthInCharsToPixels(40); 250 } 251 252 253 263 protected void createContainerControls(Composite parent, int nColumns) { 264 fContainerDialogField.doFillIntoGrid(parent, nColumns); 265 LayoutUtil.setWidthHint(fContainerDialogField.getTextControl(null), getMaxFieldWidth()); 266 } 267 268 271 protected void setFocusOnContainer() { 272 fContainerDialogField.setFocus(); 273 } 274 275 277 private class ContainerFieldAdapter implements IStringButtonAdapter, IDialogFieldListener { 278 279 public void changeControlPressed(DialogField field) { 281 containerChangeControlPressed(field); 282 } 283 284 public void dialogFieldChanged(DialogField field) { 286 containerDialogFieldChanged(field); 287 } 288 } 289 290 private void containerChangeControlPressed(DialogField field) { 291 IPackageFragmentRoot root= chooseContainer(); 293 if (root != null) { 294 setPackageFragmentRoot(root, true); 295 } 296 } 297 298 private void containerDialogFieldChanged(DialogField field) { 299 if (field == fContainerDialogField) { 300 fContainerStatus= containerChanged(); 301 } 302 handleFieldChanged(CONTAINER); 304 } 305 306 308 316 protected IStatus containerChanged() { 317 StatusInfo status= new StatusInfo(); 318 319 fCurrRoot= null; 320 String str= getPackageFragmentRootText(); 321 if (str.length() == 0) { 322 status.setError(NewWizardMessages.NewContainerWizardPage_error_EnterContainerName); 323 return status; 324 } 325 IPath path= new Path(str); 326 IResource res= fWorkspaceRoot.findMember(path); 327 if (res != null) { 328 int resType= res.getType(); 329 if (resType == IResource.PROJECT || resType == IResource.FOLDER) { 330 IProject proj= res.getProject(); 331 if (!proj.isOpen()) { 332 status.setError(Messages.format(NewWizardMessages.NewContainerWizardPage_error_ProjectClosed, proj.getFullPath().toString())); 333 return status; 334 } 335 IJavaProject jproject= JavaCore.create(proj); 336 fCurrRoot= jproject.getPackageFragmentRoot(res); 337 if (res.exists()) { 338 try { 339 if (!proj.hasNature(JavaCore.NATURE_ID)) { 340 if (resType == IResource.PROJECT) { 341 status.setError(NewWizardMessages.NewContainerWizardPage_warning_NotAJavaProject); 342 } else { 343 status.setWarning(NewWizardMessages.NewContainerWizardPage_warning_NotInAJavaProject); 344 } 345 return status; 346 } 347 if (fCurrRoot.isArchive()) { 348 status.setError(Messages.format(NewWizardMessages.NewContainerWizardPage_error_ContainerIsBinary, str)); 349 return status; 350 } 351 if (fCurrRoot.getKind() == IPackageFragmentRoot.K_BINARY) { 352 status.setWarning(Messages.format(NewWizardMessages.NewContainerWizardPage_warning_inside_classfolder, str)); 353 } else if (!jproject.isOnClasspath(fCurrRoot)) { 354 status.setWarning(Messages.format(NewWizardMessages.NewContainerWizardPage_warning_NotOnClassPath, str)); 355 } 356 } catch (CoreException e) { 357 status.setWarning(NewWizardMessages.NewContainerWizardPage_warning_NotAJavaProject); 358 } 359 } 360 return status; 361 } else { 362 status.setError(Messages.format(NewWizardMessages.NewContainerWizardPage_error_NotAFolder, str)); 363 return status; 364 } 365 } else { 366 status.setError(Messages.format(NewWizardMessages.NewContainerWizardPage_error_ContainerDoesNotExist, str)); 367 return status; 368 } 369 } 370 371 373 385 protected void handleFieldChanged(String fieldName) { 386 } 387 388 389 391 396 protected IWorkspaceRoot getWorkspaceRoot() { 397 return fWorkspaceRoot; 398 } 399 400 407 public IJavaProject getJavaProject() { 408 IPackageFragmentRoot root= getPackageFragmentRoot(); 409 if (root != null) { 410 return root.getJavaProject(); 411 } 412 return null; 413 } 414 415 423 public IPackageFragmentRoot getPackageFragmentRoot() { 424 return fCurrRoot; 425 } 426 427 432 public String getPackageFragmentRootText() { 433 return fContainerDialogField.getText(); 434 } 435 436 437 445 public void setPackageFragmentRoot(IPackageFragmentRoot root, boolean canBeModified) { 446 fCurrRoot= root; 447 String str= (root == null) ? "" : root.getPath().makeRelative().toString(); fContainerDialogField.setText(str); 449 fContainerDialogField.setEnabled(canBeModified); 450 } 451 452 454 465 protected IPackageFragmentRoot chooseContainer() { 466 IJavaElement initElement= getPackageFragmentRoot(); 467 Class [] acceptedClasses= new Class [] { IPackageFragmentRoot.class, IJavaProject.class }; 468 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, false) { 469 public boolean isSelectedValid(Object element) { 470 try { 471 if (element instanceof IJavaProject) { 472 IJavaProject jproject= (IJavaProject)element; 473 IPath path= jproject.getProject().getFullPath(); 474 return (jproject.findPackageFragmentRoot(path) != null); 475 } else if (element instanceof IPackageFragmentRoot) { 476 return (((IPackageFragmentRoot)element).getKind() == IPackageFragmentRoot.K_SOURCE); 477 } 478 return true; 479 } catch (JavaModelException e) { 480 JavaPlugin.log(e.getStatus()); } 482 return false; 483 } 484 }; 485 486 acceptedClasses= new Class [] { IJavaModel.class, IPackageFragmentRoot.class, IJavaProject.class }; 487 ViewerFilter filter= new TypedViewerFilter(acceptedClasses) { 488 public boolean select(Viewer viewer, Object parent, Object element) { 489 if (element instanceof IPackageFragmentRoot) { 490 try { 491 return (((IPackageFragmentRoot)element).getKind() == IPackageFragmentRoot.K_SOURCE); 492 } catch (JavaModelException e) { 493 JavaPlugin.log(e.getStatus()); return false; 495 } 496 } 497 return super.select(viewer, parent, element); 498 } 499 }; 500 501 StandardJavaElementContentProvider provider= new StandardJavaElementContentProvider(); 502 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 503 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), labelProvider, provider); 504 dialog.setValidator(validator); 505 dialog.setComparator(new JavaElementComparator()); 506 dialog.setTitle(NewWizardMessages.NewContainerWizardPage_ChooseSourceContainerDialog_title); 507 dialog.setMessage(NewWizardMessages.NewContainerWizardPage_ChooseSourceContainerDialog_description); 508 dialog.addFilter(filter); 509 dialog.setInput(JavaCore.create(fWorkspaceRoot)); 510 dialog.setInitialSelection(initElement); 511 dialog.setHelpAvailable(false); 512 513 if (dialog.open() == Window.OK) { 514 Object element= dialog.getFirstResult(); 515 if (element instanceof IJavaProject) { 516 IJavaProject jproject= (IJavaProject)element; 517 return jproject.getPackageFragmentRoot(jproject.getProject()); 518 } else if (element instanceof IPackageFragmentRoot) { 519 return (IPackageFragmentRoot)element; 520 } 521 return null; 522 } 523 return null; 524 } 525 526 } 527 | Popular Tags |