1 12 package org.eclipse.jdt.ui.wizards; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.net.URI ; 16 import java.util.ArrayList ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 import org.eclipse.text.edits.TextEdit; 23 24 import org.eclipse.core.filesystem.EFS; 25 import org.eclipse.core.filesystem.IFileStore; 26 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.IPath; 29 import org.eclipse.core.runtime.IProgressMonitor; 30 import org.eclipse.core.runtime.IStatus; 31 import org.eclipse.core.runtime.NullProgressMonitor; 32 import org.eclipse.core.runtime.SubProgressMonitor; 33 34 import org.eclipse.core.resources.IResource; 35 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.accessibility.AccessibleAdapter; 38 import org.eclipse.swt.accessibility.AccessibleEvent; 39 import org.eclipse.swt.events.KeyAdapter; 40 import org.eclipse.swt.events.KeyEvent; 41 import org.eclipse.swt.events.SelectionEvent; 42 import org.eclipse.swt.events.SelectionListener; 43 import org.eclipse.swt.graphics.Image; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.layout.GridLayout; 46 import org.eclipse.swt.widgets.Button; 47 import org.eclipse.swt.widgets.Composite; 48 import org.eclipse.swt.widgets.Control; 49 import org.eclipse.swt.widgets.Item; 50 import org.eclipse.swt.widgets.Link; 51 import org.eclipse.swt.widgets.Text; 52 53 import org.eclipse.jface.contentassist.SubjectControlContentAssistant; 54 import org.eclipse.jface.dialogs.MessageDialog; 55 import org.eclipse.jface.operation.IRunnableWithProgress; 56 import org.eclipse.jface.preference.PreferenceDialog; 57 import org.eclipse.jface.viewers.CellEditor; 58 import org.eclipse.jface.viewers.ICellModifier; 59 import org.eclipse.jface.viewers.ISelection; 60 import org.eclipse.jface.viewers.IStructuredSelection; 61 import org.eclipse.jface.viewers.LabelProvider; 62 import org.eclipse.jface.viewers.TableViewer; 63 import org.eclipse.jface.window.Window; 64 65 import org.eclipse.jface.text.BadLocationException; 66 import org.eclipse.jface.text.ITextSelection; 67 import org.eclipse.jface.text.templates.Template; 68 import org.eclipse.jface.text.templates.TemplateException; 69 70 import org.eclipse.ui.contentassist.ContentAssistHandler; 71 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 72 import org.eclipse.ui.dialogs.PreferencesUtil; 73 74 import org.eclipse.jdt.core.Flags; 75 import org.eclipse.jdt.core.IBuffer; 76 import org.eclipse.jdt.core.ICompilationUnit; 77 import org.eclipse.jdt.core.IField; 78 import org.eclipse.jdt.core.IJavaElement; 79 import org.eclipse.jdt.core.IJavaProject; 80 import org.eclipse.jdt.core.IMethod; 81 import org.eclipse.jdt.core.IPackageFragment; 82 import org.eclipse.jdt.core.IPackageFragmentRoot; 83 import org.eclipse.jdt.core.ISourceRange; 84 import org.eclipse.jdt.core.IType; 85 import org.eclipse.jdt.core.JavaConventions; 86 import org.eclipse.jdt.core.JavaCore; 87 import org.eclipse.jdt.core.JavaModelException; 88 import org.eclipse.jdt.core.Signature; 89 import org.eclipse.jdt.core.ToolFactory; 90 import org.eclipse.jdt.core.compiler.IProblem; 91 import org.eclipse.jdt.core.compiler.IScanner; 92 import org.eclipse.jdt.core.compiler.ITerminalSymbols; 93 import org.eclipse.jdt.core.compiler.InvalidInputException; 94 import org.eclipse.jdt.core.dom.AST; 95 import org.eclipse.jdt.core.dom.ASTNode; 96 import org.eclipse.jdt.core.dom.ASTParser; 97 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 98 import org.eclipse.jdt.core.dom.CompilationUnit; 99 import org.eclipse.jdt.core.dom.ITypeBinding; 100 import org.eclipse.jdt.core.dom.ImportDeclaration; 101 import org.eclipse.jdt.core.dom.ParameterizedType; 102 import org.eclipse.jdt.core.dom.Type; 103 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 104 import org.eclipse.jdt.core.formatter.CodeFormatter; 105 import org.eclipse.jdt.core.search.IJavaSearchConstants; 106 import org.eclipse.jdt.core.search.IJavaSearchScope; 107 import org.eclipse.jdt.core.search.SearchEngine; 108 109 import org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedConstructorsOperation; 110 import org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedMethodsOperation; 111 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; 112 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 113 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 114 import org.eclipse.jdt.internal.corext.dom.TokenScanner; 115 import org.eclipse.jdt.internal.corext.refactoring.StubTypeContext; 116 import org.eclipse.jdt.internal.corext.refactoring.TypeContextChecker; 117 import org.eclipse.jdt.internal.corext.template.java.JavaContext; 118 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; 119 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 120 import org.eclipse.jdt.internal.corext.util.Messages; 121 import org.eclipse.jdt.internal.corext.util.Resources; 122 import org.eclipse.jdt.internal.corext.util.Strings; 123 124 import org.eclipse.jdt.ui.CodeGeneration; 125 import org.eclipse.jdt.ui.CodeStyleConfiguration; 126 import org.eclipse.jdt.ui.JavaElementLabelProvider; 127 128 import org.eclipse.jdt.internal.ui.JavaPlugin; 129 import org.eclipse.jdt.internal.ui.JavaPluginImages; 130 import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog; 131 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 132 import org.eclipse.jdt.internal.ui.dialogs.TableTextCellEditor; 133 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler; 134 import org.eclipse.jdt.internal.ui.preferences.CodeTemplatePreferencePage; 135 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; 136 import org.eclipse.jdt.internal.ui.refactoring.contentassist.CompletionContextRequestor; 137 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper; 138 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaPackageCompletionProcessor; 139 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor; 140 import org.eclipse.jdt.internal.ui.util.SWTUtil; 141 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 142 import org.eclipse.jdt.internal.ui.wizards.SuperInterfaceSelectionDialog; 143 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 144 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 145 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter; 146 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter; 147 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 148 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; 149 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 150 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup; 151 import org.eclipse.jdt.internal.ui.wizards.dialogfields.Separator; 152 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField; 153 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField; 154 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 155 156 174 public abstract class NewTypeWizardPage extends NewContainerWizardPage { 175 176 180 public static class ImportsManager { 181 182 private ImportRewrite fImportsRewrite; 183 184 ImportsManager(CompilationUnit astRoot) throws CoreException { 185 fImportsRewrite= CodeStyleConfiguration.createImportRewrite(astRoot, true); 186 } 187 188 ICompilationUnit getCompilationUnit() { 189 return fImportsRewrite.getCompilationUnit(); 190 } 191 192 202 public String addImport(String qualifiedTypeName) { 203 return fImportsRewrite.addImport(qualifiedTypeName); 204 } 205 206 216 public String addImport(ITypeBinding typeBinding) { 217 return fImportsRewrite.addImport(typeBinding); 218 } 219 220 234 public String addStaticImport(String declaringTypeName, String simpleName, boolean isField) { 235 return fImportsRewrite.addStaticImport(declaringTypeName, simpleName, isField); 236 } 237 238 void create(boolean needsSave, IProgressMonitor monitor) throws CoreException { 239 TextEdit edit= fImportsRewrite.rewriteImports(monitor); 240 JavaModelUtil.applyEdit(fImportsRewrite.getCompilationUnit(), edit, needsSave, null); 241 } 242 243 void removeImport(String qualifiedName) { 244 fImportsRewrite.removeImport(qualifiedName); 245 } 246 247 void removeStaticImport(String qualifiedName) { 248 fImportsRewrite.removeStaticImport(qualifiedName); 249 } 250 } 251 252 253 254 public int F_PUBLIC = Flags.AccPublic; 255 256 public int F_PRIVATE = Flags.AccPrivate; 257 258 public int F_PROTECTED = Flags.AccProtected; 259 260 public int F_STATIC = Flags.AccStatic; 261 262 public int F_FINAL = Flags.AccFinal; 263 264 public int F_ABSTRACT = Flags.AccAbstract; 265 266 private final static String PAGE_NAME= "NewTypeWizardPage"; 268 269 protected final static String PACKAGE= PAGE_NAME + ".package"; 271 protected final static String ENCLOSING= PAGE_NAME + ".enclosing"; 273 protected final static String ENCLOSINGSELECTION= ENCLOSING + ".selection"; 275 protected final static String TYPENAME= PAGE_NAME + ".typename"; 277 protected final static String SUPER= PAGE_NAME + ".superclass"; 279 protected final static String INTERFACES= PAGE_NAME + ".interfaces"; 281 protected final static String MODIFIERS= PAGE_NAME + ".modifiers"; 283 protected final static String METHODS= PAGE_NAME + ".methods"; 285 private static class InterfaceWrapper { 286 public String interfaceName; 287 288 public InterfaceWrapper(String interfaceName) { 289 this.interfaceName= interfaceName; 290 } 291 292 public int hashCode() { 293 return interfaceName.hashCode(); 294 } 295 296 public boolean equals(Object obj) { 297 return obj != null && getClass().equals(obj.getClass()) && ((InterfaceWrapper) obj).interfaceName.equals(interfaceName); 298 } 299 } 300 301 private static class InterfacesListLabelProvider extends LabelProvider { 302 private Image fInterfaceImage; 303 304 public InterfacesListLabelProvider() { 305 fInterfaceImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_INTERFACE); 306 } 307 308 public String getText(Object element) { 309 return ((InterfaceWrapper) element).interfaceName; 310 } 311 312 public Image getImage(Object element) { 313 return fInterfaceImage; 314 } 315 } 316 317 private StringButtonStatusDialogField fPackageDialogField; 318 319 private SelectionButtonDialogField fEnclosingTypeSelection; 320 private StringButtonDialogField fEnclosingTypeDialogField; 321 322 private boolean fCanModifyPackage; 323 private boolean fCanModifyEnclosingType; 324 325 private IPackageFragment fCurrPackage; 326 327 private IType fCurrEnclosingType; 328 331 private IType fCurrType; 332 private StringDialogField fTypeNameDialogField; 333 334 private StringButtonDialogField fSuperClassDialogField; 335 private ListDialogField fSuperInterfacesDialogField; 336 337 private SelectionButtonDialogFieldGroup fAccMdfButtons; 338 private SelectionButtonDialogFieldGroup fOtherMdfButtons; 339 340 private SelectionButtonDialogField fAddCommentButton; 341 private boolean fUseAddCommentButtonValue; 344 private IType fCreatedType; 345 346 private JavaPackageCompletionProcessor fCurrPackageCompletionProcessor; 347 private JavaTypeCompletionProcessor fEnclosingTypeCompletionProcessor; 348 private StubTypeContext fSuperClassStubTypeContext; 349 private StubTypeContext fSuperInterfaceStubTypeContext; 350 351 protected IStatus fEnclosingTypeStatus; 352 protected IStatus fPackageStatus; 353 protected IStatus fTypeNameStatus; 354 protected IStatus fSuperClassStatus; 355 protected IStatus fModifierStatus; 356 protected IStatus fSuperInterfacesStatus; 357 358 private final int PUBLIC_INDEX= 0, DEFAULT_INDEX= 1, PRIVATE_INDEX= 2, PROTECTED_INDEX= 3; 359 private final int ABSTRACT_INDEX= 0, FINAL_INDEX= 1, STATIC_INDEX= 2, ENUM_ANNOT_STATIC_INDEX= 1; 360 361 private int fTypeKind; 362 363 367 public static final int CLASS_TYPE = 1; 368 369 373 public static final int INTERFACE_TYPE = 2; 374 375 379 public static final int ENUM_TYPE = 3; 380 381 385 public static final int ANNOTATION_TYPE = 4; 386 387 394 public NewTypeWizardPage(boolean isClass, String pageName) { 395 this(isClass ? CLASS_TYPE : INTERFACE_TYPE, pageName); 396 } 397 398 406 public NewTypeWizardPage(int typeKind, String pageName) { 407 super(pageName); 408 fTypeKind= typeKind; 409 410 fCreatedType= null; 411 412 TypeFieldsAdapter adapter= new TypeFieldsAdapter(); 413 414 fPackageDialogField= new StringButtonStatusDialogField(adapter); 415 fPackageDialogField.setDialogFieldListener(adapter); 416 fPackageDialogField.setLabelText(getPackageLabel()); 417 fPackageDialogField.setButtonLabel(NewWizardMessages.NewTypeWizardPage_package_button); 418 fPackageDialogField.setStatusWidthHint(NewWizardMessages.NewTypeWizardPage_default); 419 420 fEnclosingTypeSelection= new SelectionButtonDialogField(SWT.CHECK); 421 fEnclosingTypeSelection.setDialogFieldListener(adapter); 422 fEnclosingTypeSelection.setLabelText(getEnclosingTypeLabel()); 423 424 fEnclosingTypeDialogField= new StringButtonDialogField(adapter); 425 fEnclosingTypeDialogField.setDialogFieldListener(adapter); 426 fEnclosingTypeDialogField.setButtonLabel(NewWizardMessages.NewTypeWizardPage_enclosing_button); 427 428 fTypeNameDialogField= new StringDialogField(); 429 fTypeNameDialogField.setDialogFieldListener(adapter); 430 fTypeNameDialogField.setLabelText(getTypeNameLabel()); 431 432 fSuperClassDialogField= new StringButtonDialogField(adapter); 433 fSuperClassDialogField.setDialogFieldListener(adapter); 434 fSuperClassDialogField.setLabelText(getSuperClassLabel()); 435 fSuperClassDialogField.setButtonLabel(NewWizardMessages.NewTypeWizardPage_superclass_button); 436 437 String [] addButtons= new String [] { 438 NewWizardMessages.NewTypeWizardPage_interfaces_add, 439 null, 440 NewWizardMessages.NewTypeWizardPage_interfaces_remove 441 }; 442 fSuperInterfacesDialogField= new ListDialogField(adapter, addButtons, new InterfacesListLabelProvider()); 443 fSuperInterfacesDialogField.setDialogFieldListener(adapter); 444 fSuperInterfacesDialogField.setTableColumns(new ListDialogField.ColumnsDescription(1, false)); 445 fSuperInterfacesDialogField.setLabelText(getSuperInterfacesLabel()); 446 fSuperInterfacesDialogField.setRemoveButtonIndex(2); 447 448 String [] buttonNames1= new String [] { 449 NewWizardMessages.NewTypeWizardPage_modifiers_public, 450 NewWizardMessages.NewTypeWizardPage_modifiers_default, 451 NewWizardMessages.NewTypeWizardPage_modifiers_private, 452 NewWizardMessages.NewTypeWizardPage_modifiers_protected 453 }; 454 fAccMdfButtons= new SelectionButtonDialogFieldGroup(SWT.RADIO, buttonNames1, 4); 455 fAccMdfButtons.setDialogFieldListener(adapter); 456 fAccMdfButtons.setLabelText(getModifiersLabel()); 457 fAccMdfButtons.setSelection(0, true); 458 459 String [] buttonNames2; 460 if (fTypeKind == CLASS_TYPE) { 461 buttonNames2= new String [] { 462 NewWizardMessages.NewTypeWizardPage_modifiers_abstract, 463 NewWizardMessages.NewTypeWizardPage_modifiers_final, 464 NewWizardMessages.NewTypeWizardPage_modifiers_static 465 }; 466 } else { 467 if (fTypeKind == ENUM_TYPE || fTypeKind == ANNOTATION_TYPE) { 468 buttonNames2= new String [] { 469 NewWizardMessages.NewTypeWizardPage_modifiers_abstract, 470 NewWizardMessages.NewTypeWizardPage_modifiers_static 471 }; 472 } 473 else 474 buttonNames2= new String [] {}; 475 } 476 477 fOtherMdfButtons= new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames2, 4); 478 fOtherMdfButtons.setDialogFieldListener(adapter); 479 480 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, false); 481 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, false); 482 fOtherMdfButtons.enableSelectionButton(STATIC_INDEX, false); 483 484 if (fTypeKind == ENUM_TYPE || fTypeKind == ANNOTATION_TYPE) { 485 fOtherMdfButtons.enableSelectionButton(ABSTRACT_INDEX, false); 486 fOtherMdfButtons.enableSelectionButton(ENUM_ANNOT_STATIC_INDEX, false); 487 } 488 489 fAddCommentButton= new SelectionButtonDialogField(SWT.CHECK); 490 fAddCommentButton.setLabelText(NewWizardMessages.NewTypeWizardPage_addcomment_label); 491 492 fUseAddCommentButtonValue= false; 494 fCurrPackageCompletionProcessor= new JavaPackageCompletionProcessor(); 495 fEnclosingTypeCompletionProcessor= new JavaTypeCompletionProcessor(false, false, true); 496 497 fPackageStatus= new StatusInfo(); 498 fEnclosingTypeStatus= new StatusInfo(); 499 500 fCanModifyPackage= true; 501 fCanModifyEnclosingType= true; 502 updateEnableState(); 503 504 fTypeNameStatus= new StatusInfo(); 505 fSuperClassStatus= new StatusInfo(); 506 fSuperInterfacesStatus= new StatusInfo(); 507 fModifierStatus= new StatusInfo(); 508 } 509 510 516 protected void initTypePage(IJavaElement elem) { 517 String initSuperclass= "java.lang.Object"; ArrayList initSuperinterfaces= new ArrayList (5); 519 520 IJavaProject project= null; 521 IPackageFragment pack= null; 522 IType enclosingType= null; 523 524 if (elem != null) { 525 project= elem.getJavaProject(); 527 pack= (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); 528 IType typeInCU= (IType) elem.getAncestor(IJavaElement.TYPE); 529 if (typeInCU != null) { 530 if (typeInCU.getCompilationUnit() != null) { 531 enclosingType= typeInCU; 532 } 533 } else { 534 ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT); 535 if (cu != null) { 536 enclosingType= cu.findPrimaryType(); 537 } 538 } 539 540 try { 541 IType type= null; 542 if (elem.getElementType() == IJavaElement.TYPE) { 543 type= (IType)elem; 544 if (type.exists()) { 545 String superName= JavaModelUtil.getFullyQualifiedName(type); 546 if (type.isInterface()) { 547 initSuperinterfaces.add(superName); 548 } else { 549 initSuperclass= superName; 550 } 551 } 552 } 553 } catch (JavaModelException e) { 554 JavaPlugin.log(e); 555 } 557 } 558 559 String typeName= ""; 561 ITextSelection selection= getCurrentTextSelection(); 562 if (selection != null) { 563 String text= selection.getText(); 564 if (text != null && validateJavaTypeName(text, project).isOK()) { 565 typeName= text; 566 } 567 } 568 569 setPackageFragment(pack, true); 570 setEnclosingType(enclosingType, true); 571 setEnclosingTypeSelection(false, true); 572 573 setTypeName(typeName, true); 574 setSuperClass(initSuperclass, true); 575 setSuperInterfaces(initSuperinterfaces, true); 576 577 setAddComments(StubUtility.doAddComments(project), true); } 579 580 private static IStatus validateJavaTypeName(String text, IJavaProject project) { 581 if (project == null || !project.exists()) { 582 return JavaConventions.validateJavaTypeName(text, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3); 583 } 584 String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); 585 String compliance= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); 586 return JavaConventions.validateJavaTypeName(text, sourceLevel, compliance); 587 } 588 589 private static IStatus validatePackageName(String text, IJavaProject project) { 590 if (project == null || !project.exists()) { 591 return JavaConventions.validatePackageName(text, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3); 592 } 593 String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); 594 String compliance= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); 595 return JavaConventions.validatePackageName(text, sourceLevel, compliance); 596 } 597 598 600 606 protected String getPackageLabel() { 607 return NewWizardMessages.NewTypeWizardPage_package_label; 608 } 609 610 616 protected String getEnclosingTypeLabel() { 617 return NewWizardMessages.NewTypeWizardPage_enclosing_selection_label; 618 } 619 620 626 protected String getTypeNameLabel() { 627 return NewWizardMessages.NewTypeWizardPage_typename_label; 628 } 629 630 636 protected String getModifiersLabel() { 637 return NewWizardMessages.NewTypeWizardPage_modifiers_acc_label; 638 } 639 640 646 protected String getSuperClassLabel() { 647 return NewWizardMessages.NewTypeWizardPage_superclass_label; 648 } 649 650 656 protected String getSuperInterfacesLabel() { 657 if (fTypeKind != INTERFACE_TYPE) 658 return NewWizardMessages.NewTypeWizardPage_interfaces_class_label; 659 return NewWizardMessages.NewTypeWizardPage_interfaces_ifc_label; 660 } 661 662 668 protected void createSeparator(Composite composite, int nColumns) { 669 (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(composite, nColumns, convertHeightInCharsToPixels(1)); 670 } 671 672 679 protected void createPackageControls(Composite composite, int nColumns) { 680 fPackageDialogField.doFillIntoGrid(composite, nColumns); 681 Text text= fPackageDialogField.getTextControl(null); 682 LayoutUtil.setWidthHint(text, getMaxFieldWidth()); 683 LayoutUtil.setHorizontalGrabbing(text); 684 ControlContentAssistHelper.createTextContentAssistant(text, fCurrPackageCompletionProcessor); 685 TextFieldNavigationHandler.install(text); 686 } 687 688 695 protected void createEnclosingTypeControls(Composite composite, int nColumns) { 696 Composite tabGroup= new Composite(composite, SWT.NONE); 698 GridLayout layout= new GridLayout(); 699 layout.marginWidth= 0; 700 layout.marginHeight= 0; 701 tabGroup.setLayout(layout); 702 703 fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1); 704 705 Text text= fEnclosingTypeDialogField.getTextControl(composite); 706 text.getAccessible().addAccessibleListener(new AccessibleAdapter() { 707 public void getName(AccessibleEvent e) { 708 e.result= NewWizardMessages.NewTypeWizardPage_enclosing_field_description; 709 } 710 }); 711 GridData gd= new GridData(GridData.FILL_HORIZONTAL); 712 gd.widthHint= getMaxFieldWidth(); 713 gd.horizontalSpan= 2; 714 text.setLayoutData(gd); 715 716 Button button= fEnclosingTypeDialogField.getChangeControl(composite); 717 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 718 gd.widthHint = SWTUtil.getButtonWidthHint(button); 719 button.setLayoutData(gd); 720 ControlContentAssistHelper.createTextContentAssistant(text, fEnclosingTypeCompletionProcessor); 721 TextFieldNavigationHandler.install(text); 722 } 723 724 731 protected void createTypeNameControls(Composite composite, int nColumns) { 732 fTypeNameDialogField.doFillIntoGrid(composite, nColumns - 1); 733 DialogField.createEmptySpace(composite); 734 735 Text text= fTypeNameDialogField.getTextControl(null); 736 LayoutUtil.setWidthHint(text, getMaxFieldWidth()); 737 TextFieldNavigationHandler.install(text); 738 } 739 740 747 protected void createModifierControls(Composite composite, int nColumns) { 748 LayoutUtil.setHorizontalSpan(fAccMdfButtons.getLabelControl(composite), 1); 749 750 Control control= fAccMdfButtons.getSelectionButtonsGroup(composite); 751 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 752 gd.horizontalSpan= nColumns - 2; 753 control.setLayoutData(gd); 754 755 DialogField.createEmptySpace(composite); 756 757 if (fTypeKind == CLASS_TYPE) { 758 DialogField.createEmptySpace(composite); 759 760 control= fOtherMdfButtons.getSelectionButtonsGroup(composite); 761 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 762 gd.horizontalSpan= nColumns - 2; 763 control.setLayoutData(gd); 764 765 DialogField.createEmptySpace(composite); 766 } 767 } 768 769 776 protected void createSuperClassControls(Composite composite, int nColumns) { 777 fSuperClassDialogField.doFillIntoGrid(composite, nColumns); 778 Text text= fSuperClassDialogField.getTextControl(null); 779 LayoutUtil.setWidthHint(text, getMaxFieldWidth()); 780 781 JavaTypeCompletionProcessor superClassCompletionProcessor= new JavaTypeCompletionProcessor(false, false, true); 782 superClassCompletionProcessor.setCompletionContextRequestor(new CompletionContextRequestor() { 783 public StubTypeContext getStubTypeContext() { 784 return getSuperClassStubTypeContext(); 785 } 786 }); 787 788 ControlContentAssistHelper.createTextContentAssistant(text, superClassCompletionProcessor); 789 TextFieldNavigationHandler.install(text); 790 } 791 792 799 protected void createSuperInterfacesControls(Composite composite, int nColumns) { 800 final String INTERFACE= "interface"; fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns); 802 final TableViewer tableViewer= fSuperInterfacesDialogField.getTableViewer(); 803 tableViewer.setColumnProperties(new String [] {INTERFACE}); 804 805 TableTextCellEditor cellEditor= new TableTextCellEditor(tableViewer, 0) { 806 protected void doSetFocus() { 807 if (text != null) { 808 text.setFocus(); 809 text.setSelection(text.getText().length()); 810 checkSelection(); 811 checkDeleteable(); 812 checkSelectable(); 813 } 814 } 815 }; 816 JavaTypeCompletionProcessor superInterfaceCompletionProcessor= new JavaTypeCompletionProcessor(false, false, true); 817 superInterfaceCompletionProcessor.setCompletionContextRequestor(new CompletionContextRequestor() { 818 public StubTypeContext getStubTypeContext() { 819 return getSuperInterfacesStubTypeContext(); 820 } 821 }); 822 SubjectControlContentAssistant contentAssistant= ControlContentAssistHelper.createJavaContentAssistant(superInterfaceCompletionProcessor); 823 Text cellEditorText= cellEditor.getText(); 824 ContentAssistHandler.createHandlerForText(cellEditorText, contentAssistant); 825 TextFieldNavigationHandler.install(cellEditorText); 826 cellEditor.setContentAssistant(contentAssistant); 827 828 tableViewer.setCellEditors(new CellEditor[] { cellEditor }); 829 tableViewer.setCellModifier(new ICellModifier() { 830 public void modify(Object element, String property, Object value) { 831 if (element instanceof Item) 832 element = ((Item) element).getData(); 833 834 ((InterfaceWrapper) element).interfaceName= (String ) value; 835 fSuperInterfacesDialogField.elementChanged(element); 836 } 837 public Object getValue(Object element, String property) { 838 return ((InterfaceWrapper) element).interfaceName; 839 } 840 public boolean canModify(Object element, String property) { 841 return true; 842 } 843 }); 844 tableViewer.getTable().addKeyListener(new KeyAdapter() { 845 public void keyPressed(KeyEvent event) { 846 if (event.keyCode == SWT.F2 && event.stateMask == 0) { 847 ISelection selection= tableViewer.getSelection(); 848 if (! (selection instanceof IStructuredSelection)) 849 return; 850 IStructuredSelection structuredSelection= (IStructuredSelection) selection; 851 tableViewer.editElement(structuredSelection.getFirstElement(), 0); 852 } 853 } 854 }); 855 GridData gd= (GridData) fSuperInterfacesDialogField.getListControl(null).getLayoutData(); 856 if (fTypeKind == CLASS_TYPE) { 857 gd.heightHint= convertHeightInCharsToPixels(3); 858 } else { 859 gd.heightHint= convertHeightInCharsToPixels(6); 860 } 861 gd.grabExcessVerticalSpace= false; 862 gd.widthHint= getMaxFieldWidth(); 863 } 864 865 874 protected void createCommentControls(Composite composite, int nColumns) { 875 Link link= new Link(composite, SWT.NONE); 876 link.setText(NewWizardMessages.NewTypeWizardPage_addcomment_description); 877 link.addSelectionListener(new TypeFieldsAdapter()); 878 link.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false, nColumns, 1)); 879 DialogField.createEmptySpace(composite); 880 fAddCommentButton.doFillIntoGrid(composite, nColumns - 1); 881 } 882 883 884 885 888 protected void setFocus() { 889 fTypeNameDialogField.setFocus(); 890 } 891 892 894 private class TypeFieldsAdapter implements IStringButtonAdapter, IDialogFieldListener, IListAdapter, SelectionListener { 895 896 public void changeControlPressed(DialogField field) { 898 typePageChangeControlPressed(field); 899 } 900 901 public void customButtonPressed(ListDialogField field, int index) { 903 typePageCustomButtonPressed(field, index); 904 } 905 906 public void selectionChanged(ListDialogField field) {} 907 908 public void dialogFieldChanged(DialogField field) { 910 typePageDialogFieldChanged(field); 911 } 912 913 public void doubleClicked(ListDialogField field) { 914 } 915 916 917 public void widgetSelected(SelectionEvent e) { 918 typePageLinkActivated(e); 919 } 920 921 public void widgetDefaultSelected(SelectionEvent e) { 922 typePageLinkActivated(e); 923 } 924 } 925 926 private void typePageLinkActivated(SelectionEvent e) { 927 IJavaProject project= getJavaProject(); 928 if (project != null) { 929 PreferenceDialog dialog= PreferencesUtil.createPropertyDialogOn(getShell(), project.getProject(), CodeTemplatePreferencePage.PROP_ID, null, null); 930 dialog.open(); 931 } else { 932 String title= NewWizardMessages.NewTypeWizardPage_configure_templates_title; 933 String message= NewWizardMessages.NewTypeWizardPage_configure_templates_message; 934 MessageDialog.openInformation(getShell(), title, message); 935 } 936 } 937 938 private void typePageChangeControlPressed(DialogField field) { 939 if (field == fPackageDialogField) { 940 IPackageFragment pack= choosePackage(); 941 if (pack != null) { 942 fPackageDialogField.setText(pack.getElementName()); 943 } 944 } else if (field == fEnclosingTypeDialogField) { 945 IType type= chooseEnclosingType(); 946 if (type != null) { 947 fEnclosingTypeDialogField.setText(JavaModelUtil.getFullyQualifiedName(type)); 948 } 949 } else if (field == fSuperClassDialogField) { 950 IType type= chooseSuperClass(); 951 if (type != null) { 952 fSuperClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type)); 953 } 954 } 955 } 956 957 private void typePageCustomButtonPressed(DialogField field, int index) { 958 if (field == fSuperInterfacesDialogField) { 959 chooseSuperInterfaces(); 960 List interfaces= fSuperInterfacesDialogField.getElements(); 961 if (!interfaces.isEmpty()) { 962 Object element= interfaces.get(interfaces.size() - 1); 963 fSuperInterfacesDialogField.editElement(element); 964 } 965 } 966 } 967 968 972 private void typePageDialogFieldChanged(DialogField field) { 973 String fieldName= null; 974 if (field == fPackageDialogField) { 975 fPackageStatus= packageChanged(); 976 updatePackageStatusLabel(); 977 fTypeNameStatus= typeNameChanged(); 978 fSuperClassStatus= superClassChanged(); 979 fieldName= PACKAGE; 980 } else if (field == fEnclosingTypeDialogField) { 981 fEnclosingTypeStatus= enclosingTypeChanged(); 982 fTypeNameStatus= typeNameChanged(); 983 fSuperClassStatus= superClassChanged(); 984 fieldName= ENCLOSING; 985 } else if (field == fEnclosingTypeSelection) { 986 updateEnableState(); 987 boolean isEnclosedType= isEnclosingTypeSelected(); 988 if (!isEnclosedType) { 989 if (fAccMdfButtons.isSelected(PRIVATE_INDEX) || fAccMdfButtons.isSelected(PROTECTED_INDEX)) { 990 fAccMdfButtons.setSelection(PRIVATE_INDEX, false); 991 fAccMdfButtons.setSelection(PROTECTED_INDEX, false); 992 fAccMdfButtons.setSelection(PUBLIC_INDEX, true); 993 } 994 if (fOtherMdfButtons.isSelected(STATIC_INDEX)) { 995 fOtherMdfButtons.setSelection(STATIC_INDEX, false); 996 } 997 } 998 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, isEnclosedType); 999 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, isEnclosedType); 1000 fOtherMdfButtons.enableSelectionButton(STATIC_INDEX, isEnclosedType); 1001 fTypeNameStatus= typeNameChanged(); 1002 fSuperClassStatus= superClassChanged(); 1003 fieldName= ENCLOSINGSELECTION; 1004 } else if (field == fTypeNameDialogField) { 1005 fTypeNameStatus= typeNameChanged(); 1006 fieldName= TYPENAME; 1007 } else if (field == fSuperClassDialogField) { 1008 fSuperClassStatus= superClassChanged(); 1009 fieldName= SUPER; 1010 } else if (field == fSuperInterfacesDialogField) { 1011 fSuperInterfacesStatus= superInterfacesChanged(); 1012 fieldName= INTERFACES; 1013 } else if (field == fOtherMdfButtons || field == fAccMdfButtons) { 1014 fModifierStatus= modifiersChanged(); 1015 fieldName= MODIFIERS; 1016 } else { 1017 fieldName= METHODS; 1018 } 1019 handleFieldChanged(fieldName); 1021 } 1022 1023 1025 1028 protected void handleFieldChanged(String fieldName) { 1029 super.handleFieldChanged(fieldName); 1030 if (fieldName == CONTAINER) { 1031 fPackageStatus= packageChanged(); 1032 fEnclosingTypeStatus= enclosingTypeChanged(); 1033 fTypeNameStatus= typeNameChanged(); 1034 fSuperClassStatus= superClassChanged(); 1035 fSuperInterfacesStatus= superInterfacesChanged(); 1036 } 1037 } 1038 1039 1041 1046 public String getPackageText() { 1047 return fPackageDialogField.getText(); 1048 } 1049 1050 1055 public String getEnclosingTypeText() { 1056 return fEnclosingTypeDialogField.getText(); 1057 } 1058 1059 1060 1066 public IPackageFragment getPackageFragment() { 1067 if (!isEnclosingTypeSelected()) { 1068 return fCurrPackage; 1069 } else { 1070 if (fCurrEnclosingType != null) { 1071 return fCurrEnclosingType.getPackageFragment(); 1072 } 1073 } 1074 return null; 1075 } 1076 1077 1085 public void setPackageFragment(IPackageFragment pack, boolean canBeModified) { 1086 fCurrPackage= pack; 1087 fCanModifyPackage= canBeModified; 1088 String str= (pack == null) ? "" : pack.getElementName(); fPackageDialogField.setText(str); 1090 updateEnableState(); 1091 } 1092 1093 1099 public IType getEnclosingType() { 1100 if (isEnclosingTypeSelected()) { 1101 return fCurrEnclosingType; 1102 } 1103 return null; 1104 } 1105 1106 1114 public void setEnclosingType(IType type, boolean canBeModified) { 1115 fCurrEnclosingType= type; 1116 fCanModifyEnclosingType= canBeModified; 1117 String str= (type == null) ? "" : JavaModelUtil.getFullyQualifiedName(type); fEnclosingTypeDialogField.setText(str); 1119 updateEnableState(); 1120 } 1121 1122 1127 public boolean isEnclosingTypeSelected() { 1128 return fEnclosingTypeSelection.isSelected(); 1129 } 1130 1131 1138 public void setEnclosingTypeSelection(boolean isSelected, boolean canBeModified) { 1139 fEnclosingTypeSelection.setSelection(isSelected); 1140 fEnclosingTypeSelection.setEnabled(canBeModified); 1141 updateEnableState(); 1142 } 1143 1144 1149 public String getTypeName() { 1150 return fTypeNameDialogField.getText(); 1151 } 1152 1153 1161 public void setTypeName(String name, boolean canBeModified) { 1162 fTypeNameDialogField.setText(name); 1163 fTypeNameDialogField.setEnabled(canBeModified); 1164 } 1165 1166 1172 public int getModifiers() { 1173 int mdf= 0; 1174 if (fAccMdfButtons.isSelected(PUBLIC_INDEX)) { 1175 mdf+= F_PUBLIC; 1176 } else if (fAccMdfButtons.isSelected(PRIVATE_INDEX)) { 1177 mdf+= F_PRIVATE; 1178 } else if (fAccMdfButtons.isSelected(PROTECTED_INDEX)) { 1179 mdf+= F_PROTECTED; 1180 } 1181 if (fOtherMdfButtons.isSelected(ABSTRACT_INDEX)) { 1182 mdf+= F_ABSTRACT; 1183 } 1184 if (fOtherMdfButtons.isSelected(FINAL_INDEX)) { 1185 mdf+= F_FINAL; 1186 } 1187 if (fOtherMdfButtons.isSelected(STATIC_INDEX)) { 1188 mdf+= F_STATIC; 1189 } 1190 return mdf; 1191 } 1192 1193 1203 public void setModifiers(int modifiers, boolean canBeModified) { 1204 if (Flags.isPublic(modifiers)) { 1205 fAccMdfButtons.setSelection(PUBLIC_INDEX, true); 1206 } else if (Flags.isPrivate(modifiers)) { 1207 fAccMdfButtons.setSelection(PRIVATE_INDEX, true); 1208 } else if (Flags.isProtected(modifiers)) { 1209 fAccMdfButtons.setSelection(PROTECTED_INDEX, true); 1210 } else { 1211 fAccMdfButtons.setSelection(DEFAULT_INDEX, true); 1212 } 1213 if (Flags.isAbstract(modifiers)) { 1214 fOtherMdfButtons.setSelection(ABSTRACT_INDEX, true); 1215 } 1216 if (Flags.isFinal(modifiers)) { 1217 fOtherMdfButtons.setSelection(FINAL_INDEX, true); 1218 } 1219 if (Flags.isStatic(modifiers)) { 1220 fOtherMdfButtons.setSelection(STATIC_INDEX, true); 1221 } 1222 1223 fAccMdfButtons.setEnabled(canBeModified); 1224 fOtherMdfButtons.setEnabled(canBeModified); 1225 } 1226 1227 1232 public String getSuperClass() { 1233 return fSuperClassDialogField.getText(); 1234 } 1235 1236 1243 public void setSuperClass(String name, boolean canBeModified) { 1244 fSuperClassDialogField.setText(name); 1245 fSuperClassDialogField.setEnabled(canBeModified); 1246 } 1247 1248 1254 public List getSuperInterfaces() { 1255 List interfaces= fSuperInterfacesDialogField.getElements(); 1256 ArrayList result= new ArrayList (interfaces.size()); 1257 for (Iterator iter= interfaces.iterator(); iter.hasNext();) { 1258 InterfaceWrapper wrapper= (InterfaceWrapper) iter.next(); 1259 result.add(wrapper.interfaceName); 1260 } 1261 return result; 1262 } 1263 1264 1272 public void setSuperInterfaces(List interfacesNames, boolean canBeModified) { 1273 ArrayList interfaces= new ArrayList (interfacesNames.size()); 1274 for (Iterator iter= interfacesNames.iterator(); iter.hasNext();) { 1275 interfaces.add(new InterfaceWrapper((String ) iter.next())); 1276 } 1277 fSuperInterfacesDialogField.setElements(interfaces); 1278 fSuperInterfacesDialogField.setEnabled(canBeModified); 1279 } 1280 1281 1289 public boolean addSuperInterface(String superInterface) { 1290 return fSuperInterfacesDialogField.addElement(new InterfaceWrapper(superInterface)); 1291 } 1292 1293 1294 1303 public void setAddComments(boolean doAddComments, boolean canBeModified) { 1304 fAddCommentButton.setSelection(doAddComments); 1305 fAddCommentButton.setEnabled(canBeModified); 1306 } 1307 1308 1315 public void enableCommentControl(boolean useAddCommentValue) { 1316 fUseAddCommentButtonValue= useAddCommentValue; 1317 } 1318 1319 1320 1328 public boolean isAddComments() { 1329 if (fUseAddCommentButtonValue) { 1330 return fAddCommentButton.isSelected(); 1331 } 1332 return StubUtility.doAddComments(getJavaProject()); 1333 } 1334 1335 1341 public IResource getModifiedResource() { 1342 IType enclosing= getEnclosingType(); 1343 if (enclosing != null) { 1344 return enclosing.getResource(); 1345 } 1346 IPackageFragment pack= getPackageFragment(); 1347 if (pack != null) { 1348 String cuName= getCompilationUnitName(getTypeNameWithoutParameters()); 1349 return pack.getCompilationUnit(cuName).getResource(); 1350 } 1351 return null; 1352 } 1353 1354 1356 1359 protected IStatus containerChanged() { 1360 IStatus status= super.containerChanged(); 1361 IPackageFragmentRoot root= getPackageFragmentRoot(); 1362 if ((fTypeKind == ANNOTATION_TYPE || fTypeKind == ENUM_TYPE) && !status.matches(IStatus.ERROR)) { 1363 if (root != null && !JavaModelUtil.is50OrHigher(root.getJavaProject())) { 1364 return new StatusInfo(IStatus.ERROR, Messages.format(NewWizardMessages.NewTypeWizardPage_warning_NotJDKCompliant, root.getJavaProject().getElementName())); 1366 } 1367 if (fTypeKind == ENUM_TYPE) { 1368 try { 1369 if (findType(root.getJavaProject(), "java.lang.Enum") == null) return new StatusInfo(IStatus.WARNING, NewWizardMessages.NewTypeWizardPage_warning_EnumClassNotFound); 1372 } catch (JavaModelException e) { 1373 JavaPlugin.log(e); 1374 } 1375 } 1376 } 1377 1378 fCurrPackageCompletionProcessor.setPackageFragmentRoot(root); 1379 if (root != null) { 1380 fEnclosingTypeCompletionProcessor.setPackageFragment(root.getPackageFragment("")); } 1382 return status; 1383 } 1384 1385 1395 protected IStatus packageChanged() { 1396 StatusInfo status= new StatusInfo(); 1397 IPackageFragmentRoot root= getPackageFragmentRoot(); 1398 fPackageDialogField.enableButton(root != null); 1399 1400 IJavaProject project= root != null ? root.getJavaProject() : null; 1401 1402 String packName= getPackageText(); 1403 if (packName.length() > 0) { 1404 IStatus val= validatePackageName(packName, project); 1405 if (val.getSeverity() == IStatus.ERROR) { 1406 status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidPackageName, val.getMessage())); 1407 return status; 1408 } else if (val.getSeverity() == IStatus.WARNING) { 1409 status.setWarning(Messages.format(NewWizardMessages.NewTypeWizardPage_warning_DiscouragedPackageName, val.getMessage())); 1410 } 1412 } else { 1413 status.setWarning(NewWizardMessages.NewTypeWizardPage_warning_DefaultPackageDiscouraged); 1414 } 1415 1416 if (project != null) { 1417 if (project.exists() && packName.length() > 0) { 1418 try { 1419 IPath rootPath= root.getPath(); 1420 IPath outputPath= project.getOutputLocation(); 1421 if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) { 1422 IPath packagePath= rootPath.append(packName.replace('.', '/')); 1425 if (outputPath.isPrefixOf(packagePath)) { 1426 status.setError(NewWizardMessages.NewTypeWizardPage_error_ClashOutputLocation); 1427 return status; 1428 } 1429 } 1430 } catch (JavaModelException e) { 1431 JavaPlugin.log(e); 1432 } 1434 } 1435 1436 fCurrPackage= root.getPackageFragment(packName); 1437 } else { 1438 status.setError(""); } 1440 return status; 1441 } 1442 1443 1446 private void updatePackageStatusLabel() { 1447 String packName= getPackageText(); 1448 1449 if (packName.length() == 0) { 1450 fPackageDialogField.setStatus(NewWizardMessages.NewTypeWizardPage_default); 1451 } else { 1452 fPackageDialogField.setStatus(""); } 1454 } 1455 1456 1459 private void updateEnableState() { 1460 boolean enclosing= isEnclosingTypeSelected(); 1461 fPackageDialogField.setEnabled(fCanModifyPackage && !enclosing); 1462 fEnclosingTypeDialogField.setEnabled(fCanModifyEnclosingType && enclosing); 1463 if (fTypeKind == ENUM_TYPE || fTypeKind == ANNOTATION_TYPE) { 1464 fOtherMdfButtons.enableSelectionButton(ABSTRACT_INDEX, enclosing); 1465 fOtherMdfButtons.enableSelectionButton(ENUM_ANNOT_STATIC_INDEX, enclosing); 1466 } 1467 } 1468 1469 1479 protected IStatus enclosingTypeChanged() { 1480 StatusInfo status= new StatusInfo(); 1481 fCurrEnclosingType= null; 1482 1483 IPackageFragmentRoot root= getPackageFragmentRoot(); 1484 1485 fEnclosingTypeDialogField.enableButton(root != null); 1486 if (root == null) { 1487 status.setError(""); return status; 1489 } 1490 1491 String enclName= getEnclosingTypeText(); 1492 if (enclName.length() == 0) { 1493 status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingTypeEnterName); 1494 return status; 1495 } 1496 try { 1497 IType type= findType(root.getJavaProject(), enclName); 1498 if (type == null) { 1499 status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingTypeNotExists); 1500 return status; 1501 } 1502 1503 if (type.getCompilationUnit() == null) { 1504 status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingNotInCU); 1505 return status; 1506 } 1507 if (!JavaModelUtil.isEditable(type.getCompilationUnit())) { 1508 status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingNotEditable); 1509 return status; 1510 } 1511 1512 fCurrEnclosingType= type; 1513 IPackageFragmentRoot enclosingRoot= JavaModelUtil.getPackageFragmentRoot(type); 1514 if (!enclosingRoot.equals(root)) { 1515 status.setWarning(NewWizardMessages.NewTypeWizardPage_warning_EnclosingNotInSourceFolder); 1516 } 1517 return status; 1518 } catch (JavaModelException e) { 1519 status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingTypeNotExists); 1520 JavaPlugin.log(e); 1521 return status; 1522 } 1523 } 1524 1525 private IType findType(IJavaProject project, String typeName) throws JavaModelException { 1526 if (project.exists()) { 1527 return project.findType(typeName); 1528 } 1529 return null; 1530 } 1531 1532 private String getTypeNameWithoutParameters() { 1533 String typeNameWithParameters= getTypeName(); 1534 int angleBracketOffset= typeNameWithParameters.indexOf('<'); 1535 if (angleBracketOffset == -1) { 1536 return typeNameWithParameters; 1537 } else { 1538 return typeNameWithParameters.substring(0, angleBracketOffset); 1539 } 1540 } 1541 1542 1551 protected String getCompilationUnitName(String typeName) { 1552 return typeName + JavaModelUtil.DEFAULT_CU_SUFFIX; 1553 } 1554 1555 1556 1565 protected IStatus typeNameChanged() { 1566 StatusInfo status= new StatusInfo(); 1567 fCurrType= null; 1568 String typeNameWithParameters= getTypeName(); 1569 if (typeNameWithParameters.length() == 0) { 1571 status.setError(NewWizardMessages.NewTypeWizardPage_error_EnterTypeName); 1572 return status; 1573 } 1574 1575 String typeName= getTypeNameWithoutParameters(); 1576 if (typeName.indexOf('.') != -1) { 1577 status.setError(NewWizardMessages.NewTypeWizardPage_error_QualifiedName); 1578 return status; 1579 } 1580 1581 IJavaProject project= getJavaProject(); 1582 IStatus val= validateJavaTypeName(typeName, project); 1583 if (val.getSeverity() == IStatus.ERROR) { 1584 status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidTypeName, val.getMessage())); 1585 return status; 1586 } else if (val.getSeverity() == IStatus.WARNING) { 1587 status.setWarning(Messages.format(NewWizardMessages.NewTypeWizardPage_warning_TypeNameDiscouraged, val.getMessage())); 1588 } 1590 1591 if (!isEnclosingTypeSelected()) { 1593 IPackageFragment pack= getPackageFragment(); 1594 if (pack != null) { 1595 ICompilationUnit cu= pack.getCompilationUnit(getCompilationUnitName(typeName)); 1596 fCurrType= cu.getType(typeName); 1597 IResource resource= cu.getResource(); 1598 1599 if (resource.exists()) { 1600 status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExists); 1601 return status; 1602 } 1603 URI location= resource.getLocationURI(); 1604 if (location != null) { 1605 try { 1606 IFileStore store= EFS.getStore(location); 1607 if (store.fetchInfo().exists()) { 1608 status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExistsDifferentCase); 1609 return status; 1610 } 1611 } catch (CoreException e) { 1612 status.setError(Messages.format( 1613 NewWizardMessages.NewTypeWizardPage_error_uri_location_unkown, 1614 Resources.getLocationString(resource))); 1615 } 1616 } 1617 } 1618 } else { 1619 IType type= getEnclosingType(); 1620 if (type != null) { 1621 fCurrType= type.getType(typeName); 1622 if (fCurrType.exists()) { 1623 status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExists); 1624 return status; 1625 } 1626 } 1627 } 1628 1629 if (!typeNameWithParameters.equals(typeName) && project != null) { 1630 if (!JavaModelUtil.is50OrHigher(project)) { 1631 status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeParameters); 1632 return status; 1633 } 1634 String typeDeclaration= "class " + typeNameWithParameters + " {}"; ASTParser parser= ASTParser.newParser(AST.JLS3); 1636 parser.setSource(typeDeclaration.toCharArray()); 1637 parser.setProject(project); 1638 CompilationUnit compilationUnit= (CompilationUnit) parser.createAST(null); 1639 IProblem[] problems= compilationUnit.getProblems(); 1640 if (problems.length > 0) { 1641 status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidTypeName, problems[0].getMessage())); 1642 return status; 1643 } 1644 } 1645 return status; 1646 } 1647 1648 1657 protected IStatus superClassChanged() { 1658 StatusInfo status= new StatusInfo(); 1659 IPackageFragmentRoot root= getPackageFragmentRoot(); 1660 fSuperClassDialogField.enableButton(root != null); 1661 1662 fSuperClassStubTypeContext= null; 1663 1664 String sclassName= getSuperClass(); 1665 if (sclassName.length() == 0) { 1666 return status; 1668 } 1669 1670 if (root != null) { 1671 Type type= TypeContextChecker.parseSuperClass(sclassName); 1672 if (type == null) { 1673 status.setError(NewWizardMessages.NewTypeWizardPage_error_InvalidSuperClassName); 1674 return status; 1675 } 1676 if (type instanceof ParameterizedType && ! JavaModelUtil.is50OrHigher(root.getJavaProject())) { 1677 status.setError(NewWizardMessages.NewTypeWizardPage_error_SuperClassNotParameterized); 1678 return status; 1679 } 1680 } else { 1681 status.setError(""); } 1683 return status; 1684 } 1685 1686 private StubTypeContext getSuperClassStubTypeContext() { 1687 if (fSuperClassStubTypeContext == null) { 1688 String typeName; 1689 if (fCurrType != null) { 1690 typeName= getTypeName(); 1691 } else { 1692 typeName= JavaTypeCompletionProcessor.DUMMY_CLASS_NAME; 1693 } 1694 fSuperClassStubTypeContext= TypeContextChecker.createSuperClassStubTypeContext(typeName, getEnclosingType(), getPackageFragment()); 1695 } 1696 return fSuperClassStubTypeContext; 1697 } 1698 1699 1708 protected IStatus superInterfacesChanged() { 1709 StatusInfo status= new StatusInfo(); 1710 1711 IPackageFragmentRoot root= getPackageFragmentRoot(); 1712 fSuperInterfacesDialogField.enableButton(0, root != null); 1713 1714 if (root != null) { 1715 List elements= fSuperInterfacesDialogField.getElements(); 1716 int nElements= elements.size(); 1717 for (int i= 0; i < nElements; i++) { 1718 String intfname= ((InterfaceWrapper) elements.get(i)).interfaceName; 1719 Type type= TypeContextChecker.parseSuperInterface(intfname); 1720 if (type == null) { 1721 status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidSuperInterfaceName, intfname)); 1722 return status; 1723 } 1724 if (type instanceof ParameterizedType && ! JavaModelUtil.is50OrHigher(root.getJavaProject())) { 1725 status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_SuperInterfaceNotParameterized, intfname)); 1726 return status; 1727 } 1728 } 1729 } 1730 return status; 1731 } 1732 1733 private StubTypeContext getSuperInterfacesStubTypeContext() { 1734 if (fSuperInterfaceStubTypeContext == null) { 1735 String typeName; 1736 if (fCurrType != null) { 1737 typeName= getTypeName(); 1738 } else { 1739 typeName= JavaTypeCompletionProcessor.DUMMY_CLASS_NAME; 1740 } 1741 fSuperInterfaceStubTypeContext= TypeContextChecker.createSuperInterfaceStubTypeContext(typeName, getEnclosingType(), getPackageFragment()); 1742 } 1743 return fSuperInterfaceStubTypeContext; 1744 } 1745 1746 1755 protected IStatus modifiersChanged() { 1756 StatusInfo status= new StatusInfo(); 1757 int modifiers= getModifiers(); 1758 if (Flags.isFinal(modifiers) && Flags.isAbstract(modifiers)) { 1759 status.setError(NewWizardMessages.NewTypeWizardPage_error_ModifiersFinalAndAbstract); 1760 } 1761 return status; 1762 } 1763 1764 1766 1777 protected IPackageFragment choosePackage() { 1778 IPackageFragmentRoot froot= getPackageFragmentRoot(); 1779 IJavaElement[] packages= null; 1780 try { 1781 if (froot != null && froot.exists()) { 1782 packages= froot.getChildren(); 1783 } 1784 } catch (JavaModelException e) { 1785 JavaPlugin.log(e); 1786 } 1787 if (packages == null) { 1788 packages= new IJavaElement[0]; 1789 } 1790 1791 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT)); 1792 dialog.setIgnoreCase(false); 1793 dialog.setTitle(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_title); 1794 dialog.setMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_description); 1795 dialog.setEmptyListMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_empty); 1796 dialog.setElements(packages); 1797 dialog.setHelpAvailable(false); 1798 1799 IPackageFragment pack= getPackageFragment(); 1800 if (pack != null) { 1801 dialog.setInitialSelections(new Object [] { pack }); 1802 } 1803 1804 if (dialog.open() == Window.OK) { 1805 return (IPackageFragment) dialog.getFirstResult(); 1806 } 1807 return null; 1808 } 1809 1810 1821 protected IType chooseEnclosingType() { 1822 IPackageFragmentRoot root= getPackageFragmentRoot(); 1823 if (root == null) { 1824 return null; 1825 } 1826 1827 IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { root }); 1828 1829 FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(getShell(), 1830 false, getWizard().getContainer(), scope, IJavaSearchConstants.TYPE); 1831 dialog.setTitle(NewWizardMessages.NewTypeWizardPage_ChooseEnclosingTypeDialog_title); 1832 dialog.setMessage(NewWizardMessages.NewTypeWizardPage_ChooseEnclosingTypeDialog_description); 1833 dialog.setInitialPattern(Signature.getSimpleName(getEnclosingTypeText())); 1834 1835 if (dialog.open() == Window.OK) { 1836 return (IType) dialog.getFirstResult(); 1837 } 1838 return null; 1839 } 1840 1841 1852 protected IType chooseSuperClass() { 1853 IJavaProject project= getJavaProject(); 1854 if (project == null) { 1855 return null; 1856 } 1857 1858 IJavaElement[] elements= new IJavaElement[] { project }; 1859 IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements); 1860 1861 FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(getShell(), false, 1862 getWizard().getContainer(), scope, IJavaSearchConstants.CLASS); 1863 dialog.setTitle(NewWizardMessages.NewTypeWizardPage_SuperClassDialog_title); 1864 dialog.setMessage(NewWizardMessages.NewTypeWizardPage_SuperClassDialog_message); 1865 dialog.setInitialPattern(getSuperClass()); 1866 1867 if (dialog.open() == Window.OK) { 1868 return (IType) dialog.getFirstResult(); 1869 } 1870 return null; 1871 } 1872 1873 1883 protected void chooseSuperInterfaces() { 1884 IJavaProject project= getJavaProject(); 1885 if (project == null) { 1886 return; 1887 } 1888 1889 SuperInterfaceSelectionDialog dialog= new SuperInterfaceSelectionDialog(getShell(), getWizard().getContainer(), this, project); 1890 dialog.setTitle(getInterfaceDialogTitle()); 1891 dialog.setMessage(NewWizardMessages.NewTypeWizardPage_InterfacesDialog_message); 1892 dialog.open(); 1893 } 1894 1895 private String getInterfaceDialogTitle() { 1896 if (fTypeKind == INTERFACE_TYPE) 1897 return NewWizardMessages.NewTypeWizardPage_InterfacesDialog_interface_title; 1898 return NewWizardMessages.NewTypeWizardPage_InterfacesDialog_class_title; 1899 } 1900 1901 1902 1904 1911 public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException { 1912 if (monitor == null) { 1913 monitor= new NullProgressMonitor(); 1914 } 1915 1916 monitor.beginTask(NewWizardMessages.NewTypeWizardPage_operationdesc, 8); 1917 1918 IPackageFragmentRoot root= getPackageFragmentRoot(); 1919 IPackageFragment pack= getPackageFragment(); 1920 if (pack == null) { 1921 pack= root.getPackageFragment(""); } 1923 1924 if (!pack.exists()) { 1925 String packName= pack.getElementName(); 1926 pack= root.createPackageFragment(packName, true, new SubProgressMonitor(monitor, 1)); 1927 } else { 1928 monitor.worked(1); 1929 } 1930 1931 boolean needsSave; 1932 ICompilationUnit connectedCU= null; 1933 1934 try { 1935 String typeName= getTypeNameWithoutParameters(); 1936 1937 boolean isInnerClass= isEnclosingTypeSelected(); 1938 1939 IType createdType; 1940 ImportsManager imports; 1941 int indent= 0; 1942 1943 Set existingImports; 1944 1945 String lineDelimiter= null; 1946 if (!isInnerClass) { 1947 lineDelimiter= StubUtility.getLineDelimiterUsed(pack.getJavaProject()); 1948 1949 String cuName= getCompilationUnitName(typeName); 1950 ICompilationUnit parentCU= pack.createCompilationUnit(cuName, "", false, new SubProgressMonitor(monitor, 2)); 1953 needsSave= true; 1954 parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); connectedCU= parentCU; 1956 1957 IBuffer buffer= parentCU.getBuffer(); 1958 1959 String simpleTypeStub= constructSimpleTypeStub(); 1960 String cuContent= constructCUContent(parentCU, simpleTypeStub, lineDelimiter); 1961 buffer.setContents(cuContent); 1962 1963 CompilationUnit astRoot= createASTForImports(parentCU); 1964 existingImports= getExistingImports(astRoot); 1965 1966 imports= new ImportsManager(astRoot); 1967 imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), typeName)); 1969 1970 String typeContent= constructTypeStub(parentCU, imports, lineDelimiter); 1971 1972 int index= cuContent.lastIndexOf(simpleTypeStub); 1973 if (index == -1) { 1974 AbstractTypeDeclaration typeNode= (AbstractTypeDeclaration) astRoot.types().get(0); 1975 int start= ((ASTNode) typeNode.modifiers().get(0)).getStartPosition(); 1976 int end= typeNode.getStartPosition() + typeNode.getLength(); 1977 buffer.replace(start, end - start, typeContent); 1978 } else { 1979 buffer.replace(index, simpleTypeStub.length(), typeContent); 1980 } 1981 1982 createdType= parentCU.getType(typeName); 1983 } else { 1984 IType enclosingType= getEnclosingType(); 1985 1986 ICompilationUnit parentCU= enclosingType.getCompilationUnit(); 1987 1988 needsSave= !parentCU.isWorkingCopy(); 1989 parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); connectedCU= parentCU; 1991 1992 CompilationUnit astRoot= createASTForImports(parentCU); 1993 imports= new ImportsManager(astRoot); 1994 existingImports= getExistingImports(astRoot); 1995 1996 1997 IType[] topLevelTypes= parentCU.getTypes(); 1999 for (int i= 0; i < topLevelTypes.length; i++) { 2000 imports.addImport(topLevelTypes[i].getFullyQualifiedName('.')); 2001 } 2002 2003 lineDelimiter= StubUtility.getLineDelimiterUsed(enclosingType); 2004 StringBuffer content= new StringBuffer (); 2005 2006 String comment= getTypeComment(parentCU, lineDelimiter); 2007 if (comment != null) { 2008 content.append(comment); 2009 content.append(lineDelimiter); 2010 } 2011 2012 content.append(constructTypeStub(parentCU, imports, lineDelimiter)); 2013 IJavaElement sibling= null; 2014 if (enclosingType.isEnum()) { 2015 IField[] fields = enclosingType.getFields(); 2016 if (fields.length > 0) { 2017 for (int i = 0, max = fields.length; i < max; i++) { 2018 if (!fields[i].isEnumConstant()) { 2019 sibling = fields[i]; 2020 break; 2021 } 2022 } 2023 } 2024 } else { 2025 IJavaElement[] elems= enclosingType.getChildren(); 2026 sibling = elems.length > 0 ? elems[0] : null; 2027 } 2028 2029 createdType= enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitor, 2)); 2030 2031 indent= StubUtility.getIndentUsed(enclosingType) + 1; 2032 } 2033 if (monitor.isCanceled()) { 2034 throw new InterruptedException (); 2035 } 2036 2037 2039 ICompilationUnit cu= createdType.getCompilationUnit(); 2040 2041 imports.create(false, new SubProgressMonitor(monitor, 1)); 2042 2043 JavaModelUtil.reconcile(cu); 2044 2045 if (monitor.isCanceled()) { 2046 throw new InterruptedException (); 2047 } 2048 2049 CompilationUnit astRoot= createASTForImports(imports.getCompilationUnit()); 2051 imports= new ImportsManager(astRoot); 2052 2053 createTypeMembers(createdType, imports, new SubProgressMonitor(monitor, 1)); 2054 2055 imports.create(false, new SubProgressMonitor(monitor, 1)); 2057 2058 removeUnusedImports(cu, existingImports, false); 2059 2060 JavaModelUtil.reconcile(cu); 2061 2062 ISourceRange range= createdType.getSourceRange(); 2063 2064 IBuffer buf= cu.getBuffer(); 2065 String originalContent= buf.getText(range.getOffset(), range.getLength()); 2066 2067 String formattedContent= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, indent, null, lineDelimiter, pack.getJavaProject()); 2068 formattedContent= Strings.trimLeadingTabsAndSpaces(formattedContent); 2069 buf.replace(range.getOffset(), range.getLength(), formattedContent); 2070 if (!isInnerClass) { 2071 String fileComment= getFileComment(cu); 2072 if (fileComment != null && fileComment.length() > 0) { 2073 buf.replace(0, 0, fileComment + lineDelimiter); 2074 } 2075 } 2076 fCreatedType= createdType; 2077 2078 if (needsSave) { 2079 cu.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1)); 2080 } else { 2081 monitor.worked(1); 2082 } 2083 2084 } finally { 2085 if (connectedCU != null) { 2086 connectedCU.discardWorkingCopy(); 2087 } 2088 monitor.done(); 2089 } 2090 } 2091 2092 private CompilationUnit createASTForImports(ICompilationUnit cu) { 2093 ASTParser parser= ASTParser.newParser(AST.JLS3); 2094 parser.setSource(cu); 2095 parser.setResolveBindings(false); 2096 parser.setFocalPosition(0); 2097 return (CompilationUnit) parser.createAST(null); 2098 } 2099 2100 2101 private Set getExistingImports(CompilationUnit root) { 2102 List imports= root.imports(); 2103 Set res= new HashSet (imports.size()); 2104 for (int i= 0; i < imports.size(); i++) { 2105 res.add(ASTNodes.asString((ImportDeclaration) imports.get(i))); 2106 } 2107 return res; 2108 } 2109 2110 private void removeUnusedImports(ICompilationUnit cu, Set existingImports, boolean needsSave) throws CoreException { 2111 ASTParser parser= ASTParser.newParser(AST.JLS3); 2112 parser.setSource(cu); 2113 parser.setResolveBindings(true); 2114 2115 CompilationUnit root= (CompilationUnit) parser.createAST(null); 2116 if (root.getProblems().length == 0) { 2117 return; 2118 } 2119 2120 List importsDecls= root.imports(); 2121 if (importsDecls.isEmpty()) { 2122 return; 2123 } 2124 ImportsManager imports= new ImportsManager(root); 2125 2126 int importsEnd= ASTNodes.getExclusiveEnd((ASTNode) importsDecls.get(importsDecls.size() - 1)); 2127 IProblem[] problems= root.getProblems(); 2128 for (int i= 0; i < problems.length; i++) { 2129 IProblem curr= problems[i]; 2130 if (curr.getSourceEnd() < importsEnd) { 2131 int id= curr.getID(); 2132 if (id == IProblem.UnusedImport || id == IProblem.NotVisibleType) { int pos= curr.getSourceStart(); 2134 for (int k= 0; k < importsDecls.size(); k++) { 2135 ImportDeclaration decl= (ImportDeclaration) importsDecls.get(k); 2136 if (decl.getStartPosition() <= pos && pos < decl.getStartPosition() + decl.getLength()) { 2137 if (existingImports.isEmpty() || !existingImports.contains(ASTNodes.asString(decl))) { 2138 String name= decl.getName().getFullyQualifiedName(); 2139 if (decl.isOnDemand()) { 2140 name += ".*"; } 2142 if (decl.isStatic()) { 2143 imports.removeStaticImport(name); 2144 } else { 2145 imports.removeImport(name); 2146 } 2147 } 2148 break; 2149 } 2150 } 2151 } 2152 } 2153 } 2154 imports.create(needsSave, null); 2155 } 2156 2157 2169 protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException { 2170 String fileComment= getFileComment(cu, lineDelimiter); 2171 String typeComment= getTypeComment(cu, lineDelimiter); 2172 IPackageFragment pack= (IPackageFragment) cu.getParent(); 2173 String content= CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter); 2174 if (content != null) { 2175 ASTParser parser= ASTParser.newParser(AST.JLS3); 2176 parser.setProject(cu.getJavaProject()); 2177 parser.setSource(content.toCharArray()); 2178 CompilationUnit unit= (CompilationUnit) parser.createAST(null); 2179 if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) { 2180 return content; 2181 } 2182 } 2183 StringBuffer buf= new StringBuffer (); 2184 if (!pack.isDefaultPackage()) { 2185 buf.append("package ").append(pack.getElementName()).append(';'); } 2187 buf.append(lineDelimiter).append(lineDelimiter); 2188 if (typeComment != null) { 2189 buf.append(typeComment).append(lineDelimiter); 2190 } 2191 buf.append(typeContent); 2192 return buf.toString(); 2193 } 2194 2195 2196 2203 public IType getCreatedType() { 2204 return fCreatedType; 2205 } 2206 2207 2209 private void writeSuperClass(StringBuffer buf, ImportsManager imports) { 2210 String superclass= getSuperClass(); 2211 if (fTypeKind == CLASS_TYPE && superclass.length() > 0 && !"java.lang.Object".equals(superclass)) { buf.append(" extends "); 2214 ITypeBinding binding= null; 2215 if (fCurrType != null) { 2216 binding= TypeContextChecker.resolveSuperClass(superclass, fCurrType, getSuperClassStubTypeContext()); 2217 } 2218 if (binding != null) { 2219 buf.append(imports.addImport(binding)); 2220 } else { 2221 buf.append(imports.addImport(superclass)); 2222 } 2223 } 2224 } 2225 2226 private void writeSuperInterfaces(StringBuffer buf, ImportsManager imports) { 2227 List interfaces= getSuperInterfaces(); 2228 int last= interfaces.size() - 1; 2229 if (last >= 0) { 2230 if (fTypeKind != INTERFACE_TYPE) { 2231 buf.append(" implements "); } else { 2233 buf.append(" extends "); } 2235 String [] intfs= (String []) interfaces.toArray(new String [interfaces.size()]); 2236 ITypeBinding[] bindings; 2237 if (fCurrType != null) { 2238 bindings= TypeContextChecker.resolveSuperInterfaces(intfs, fCurrType, getSuperInterfacesStubTypeContext()); 2239 } else { 2240 bindings= new ITypeBinding[intfs.length]; 2241 } 2242 for (int i= 0; i <= last; i++) { 2243 ITypeBinding binding= bindings[i]; 2244 if (binding != null) { 2245 buf.append(imports.addImport(binding)); 2246 } else { 2247 buf.append(imports.addImport(intfs[i])); 2248 } 2249 if (i < last) { 2250 buf.append(','); 2251 } 2252 } 2253 } 2254 } 2255 2256 2257 private String constructSimpleTypeStub() { 2258 StringBuffer buf= new StringBuffer ("public class "); buf.append(getTypeName()); 2260 buf.append("{ }"); return buf.toString(); 2262 } 2263 2264 2267 private String constructTypeStub(ICompilationUnit parentCU, ImportsManager imports, String lineDelimiter) throws CoreException { 2268 StringBuffer buf= new StringBuffer (); 2269 2270 int modifiers= getModifiers(); 2271 buf.append(Flags.toString(modifiers)); 2272 if (modifiers != 0) { 2273 buf.append(' '); 2274 } 2275 String type= ""; String templateID= ""; switch (fTypeKind) { 2278 case CLASS_TYPE: 2279 type= "class "; templateID= CodeGeneration.CLASS_BODY_TEMPLATE_ID; 2281 break; 2282 case INTERFACE_TYPE: 2283 type= "interface "; templateID= CodeGeneration.INTERFACE_BODY_TEMPLATE_ID; 2285 break; 2286 case ENUM_TYPE: 2287 type= "enum "; templateID= CodeGeneration.ENUM_BODY_TEMPLATE_ID; 2289 break; 2290 case ANNOTATION_TYPE: 2291 type= "@interface "; templateID= CodeGeneration.ANNOTATION_BODY_TEMPLATE_ID; 2293 break; 2294 } 2295 buf.append(type); 2296 buf.append(getTypeName()); 2297 writeSuperClass(buf, imports); 2298 writeSuperInterfaces(buf, imports); 2299 2300 buf.append(" {").append(lineDelimiter); String typeBody= CodeGeneration.getTypeBody(templateID, parentCU, getTypeName(), lineDelimiter); 2302 if (typeBody != null) { 2303 buf.append(typeBody); 2304 } else { 2305 buf.append(lineDelimiter); 2306 } 2307 buf.append('}').append(lineDelimiter); 2308 return buf.toString(); 2309 } 2310 2311 2331 protected void createTypeMembers(IType newType, final ImportsManager imports, IProgressMonitor monitor) throws CoreException { 2332 } 2338 2339 2340 2346 protected String getFileComment(ICompilationUnit parentCU) { 2347 return null; 2348 } 2349 2350 2363 protected String getFileComment(ICompilationUnit parentCU, String lineDelimiter) throws CoreException { 2364 if (isAddComments()) { 2365 return CodeGeneration.getFileComment(parentCU, lineDelimiter); 2366 } 2367 return null; 2368 2369 } 2370 2371 private boolean isValidComment(String template) { 2372 IScanner scanner= ToolFactory.createScanner(true, false, false, false); 2373 scanner.setSource(template.toCharArray()); 2374 try { 2375 int next= scanner.getNextToken(); 2376 while (TokenScanner.isComment(next)) { 2377 next= scanner.getNextToken(); 2378 } 2379 return next == ITerminalSymbols.TokenNameEOF; 2380 } catch (InvalidInputException e) { 2381 } 2382 return false; 2383 } 2384 2385 2397 protected String getTypeComment(ICompilationUnit parentCU, String lineDelimiter) { 2398 if (isAddComments()) { 2399 try { 2400 StringBuffer typeName= new StringBuffer (); 2401 if (isEnclosingTypeSelected()) { 2402 typeName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.'); 2403 } 2404 typeName.append(getTypeNameWithoutParameters()); 2405 String [] typeParamNames= new String [0]; 2406 String comment= CodeGeneration.getTypeComment(parentCU, typeName.toString(), typeParamNames, lineDelimiter); 2407 if (comment != null && isValidComment(comment)) { 2408 return comment; 2409 } 2410 } catch (CoreException e) { 2411 JavaPlugin.log(e); 2412 } 2413 } 2414 return null; 2415 } 2416 2417 2422 protected String getTypeComment(ICompilationUnit parentCU) { 2423 if (StubUtility.doAddComments(parentCU.getJavaProject())) 2424 return getTypeComment(parentCU, StubUtility.getLineDelimiterUsed(parentCU)); 2425 return null; 2426 } 2427 2428 2434 protected String getTemplate(String name, ICompilationUnit parentCU) { 2435 return getTemplate(name, parentCU, 0); 2436 } 2437 2438 2439 2452 protected String getTemplate(String name, ICompilationUnit parentCU, int pos) { 2453 try { 2454 Template template= JavaPlugin.getDefault().getTemplateStore().findTemplate(name); 2455 if (template != null) { 2456 return JavaContext.evaluateTemplate(template, parentCU, pos); 2457 } 2458 } catch (CoreException e) { 2459 JavaPlugin.log(e); 2460 } catch (BadLocationException e) { 2461 JavaPlugin.log(e); 2462 } catch (TemplateException e) { 2463 JavaPlugin.log(e); 2464 } 2465 return null; 2466 } 2467 2468 2469 2482 protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException { 2483 final ICompilationUnit cu= type.getCompilationUnit(); 2484 JavaModelUtil.reconcile(cu); 2485 IMethod[] typeMethods= type.getMethods(); 2486 Set handleIds= new HashSet (typeMethods.length); 2487 for (int index= 0; index < typeMethods.length; index++) 2488 handleIds.add(typeMethods[index].getHandleIdentifier()); 2489 ArrayList newMethods= new ArrayList (); 2490 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject()); 2491 settings.createComments= isAddComments(); 2492 ASTParser parser= ASTParser.newParser(AST.JLS3); 2493 parser.setResolveBindings(true); 2494 parser.setSource(cu); 2495 CompilationUnit unit= (CompilationUnit) parser.createAST(new SubProgressMonitor(monitor, 1)); 2496 final ITypeBinding binding= ASTNodes.getTypeBinding(unit, type); 2497 if (binding != null) { 2498 if (doUnimplementedMethods) { 2499 AddUnimplementedMethodsOperation operation= new AddUnimplementedMethodsOperation(unit, binding, null, -1, false, true, false); 2500 operation.setCreateComments(isAddComments()); 2501 operation.run(monitor); 2502 createImports(imports, operation.getCreatedImports()); 2503 } 2504 if (doConstructors) { 2505 AddUnimplementedConstructorsOperation operation= new AddUnimplementedConstructorsOperation(unit, binding, null, -1, false, true, false); 2506 operation.setOmitSuper(true); 2507 operation.setCreateComments(isAddComments()); 2508 operation.run(monitor); 2509 createImports(imports, operation.getCreatedImports()); 2510 } 2511 } 2512 JavaModelUtil.reconcile(cu); 2513 typeMethods= type.getMethods(); 2514 for (int index= 0; index < typeMethods.length; index++) 2515 if (!handleIds.contains(typeMethods[index].getHandleIdentifier())) 2516 newMethods.add(typeMethods[index]); 2517 IMethod[] methods= new IMethod[newMethods.size()]; 2518 newMethods.toArray(methods); 2519 return methods; 2520 } 2521 2522 private void createImports(ImportsManager imports, String [] createdImports) { 2523 for (int index= 0; index < createdImports.length; index++) 2524 imports.addImport(createdImports[index]); 2525 } 2526 2527 2528 2530 2536 public IRunnableWithProgress getRunnable() { 2537 return new IRunnableWithProgress() { 2538 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 2539 try { 2540 if (monitor == null) { 2541 monitor= new NullProgressMonitor(); 2542 } 2543 createType(monitor); 2544 } catch (CoreException e) { 2545 throw new InvocationTargetException (e); 2546 } 2547 } 2548 }; 2549 } 2550} 2551 | Popular Tags |