KickJava   Java API By Example, From Geeks To Geeks.

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


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  * John Kaplan, johnkaplantech@gmail.com - 108071 [code templates] template for body of newly created class
11  *******************************************************************************/

12 package org.eclipse.jdt.ui.wizards;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.net.URI JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
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 /**
157  * The class <code>NewTypeWizardPage</code> contains controls and validation routines
158  * for a 'New Type WizardPage'. Implementors decide which components to add and to enable.
159  * Implementors can also customize the validation code. <code>NewTypeWizardPage</code>
160  * is intended to serve as base class of all wizards that create types like applets, servlets, classes,
161  * interfaces, etc.
162  * <p>
163  * See {@link NewClassWizardPage} or {@link NewInterfaceWizardPage} for an
164  * example usage of the <code>NewTypeWizardPage</code>.
165  * </p>
166  *
167  * @see org.eclipse.jdt.ui.wizards.NewClassWizardPage
168  * @see org.eclipse.jdt.ui.wizards.NewInterfaceWizardPage
169  * @see org.eclipse.jdt.ui.wizards.NewEnumWizardPage
170  * @see org.eclipse.jdt.ui.wizards.NewAnnotationWizardPage
171  *
172  * @since 2.0
173  */

174 public abstract class NewTypeWizardPage extends NewContainerWizardPage {
175
176     /**
177      * Class used in stub creation routines to add needed imports to a
178      * compilation unit.
179      */

180     public static class ImportsManager {
181
182         private ImportRewrite fImportsRewrite;
183                 
184         /* package */ ImportsManager(CompilationUnit astRoot) throws CoreException {
185             fImportsRewrite= CodeStyleConfiguration.createImportRewrite(astRoot, true);
186         }
187
188         /* package */ ICompilationUnit getCompilationUnit() {
189             return fImportsRewrite.getCompilationUnit();
190         }
191                         
192         /**
193          * Adds a new import declaration that is sorted in the existing imports.
194          * If an import already exists or the import would conflict with an import
195          * of an other type with the same simple name, the import is not added.
196          *
197          * @param qualifiedTypeName The fully qualified name of the type to import
198          * (dot separated).
199          * @return Returns the simple type name that can be used in the code or the
200          * fully qualified type name if an import conflict prevented the import.
201          */

202         public String JavaDoc addImport(String JavaDoc qualifiedTypeName) {
203             return fImportsRewrite.addImport(qualifiedTypeName);
204         }
205                 
206         /**
207          * Adds a new import declaration that is sorted in the existing imports.
208          * If an import already exists or the import would conflict with an import
209          * of an other type with the same simple name, the import is not added.
210          *
211          * @param typeBinding the binding of the type to import
212          *
213          * @return Returns the simple type name that can be used in the code or the
214          * fully qualified type name if an import conflict prevented the import.
215          */

216         public String JavaDoc addImport(ITypeBinding typeBinding) {
217             return fImportsRewrite.addImport(typeBinding);
218         }
219         
220         /**
221          * Adds a new import declaration for a static type that is sorted in the existing imports.
222          * If an import already exists or the import would conflict with an import
223          * of an other static import with the same simple name, the import is not added.
224          *
225          * @param declaringTypeName The qualified name of the static's member declaring type
226          * @param simpleName the simple name of the member; either a field or a method name.
227          * @param isField <code>true</code> specifies that the member is a field, <code>false</code> if it is a
228          * method.
229          * @return returns either the simple member name if the import was successful or else the qualified name if
230          * an import conflict prevented the import.
231          *
232          * @since 3.2
233          */

234         public String JavaDoc addStaticImport(String JavaDoc declaringTypeName, String JavaDoc simpleName, boolean isField) {
235             return fImportsRewrite.addStaticImport(declaringTypeName, simpleName, isField);
236         }
237                 
238         /* package */ 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         /* package */ void removeImport(String JavaDoc qualifiedName) {
244             fImportsRewrite.removeImport(qualifiedName);
245         }
246         
247         /* package */ void removeStaticImport(String JavaDoc qualifiedName) {
248             fImportsRewrite.removeStaticImport(qualifiedName);
249         }
250     }
251         
252     
253     /** Public access flag. See The Java Virtual Machine Specification for more details. */
254     public int F_PUBLIC = Flags.AccPublic;
255     /** Private access flag. See The Java Virtual Machine Specification for more details. */
256     public int F_PRIVATE = Flags.AccPrivate;
257     /** Protected access flag. See The Java Virtual Machine Specification for more details. */
258     public int F_PROTECTED = Flags.AccProtected;
259     /** Static access flag. See The Java Virtual Machine Specification for more details. */
260     public int F_STATIC = Flags.AccStatic;
261     /** Final access flag. See The Java Virtual Machine Specification for more details. */
262     public int F_FINAL = Flags.AccFinal;
263     /** Abstract property flag. See The Java Virtual Machine Specification for more details. */
264     public int F_ABSTRACT = Flags.AccAbstract;
265
266     private final static String JavaDoc PAGE_NAME= "NewTypeWizardPage"; //$NON-NLS-1$
267

268     /** Field ID of the package input field. */
269     protected final static String JavaDoc PACKAGE= PAGE_NAME + ".package"; //$NON-NLS-1$
270
/** Field ID of the enclosing type input field. */
271     protected final static String JavaDoc ENCLOSING= PAGE_NAME + ".enclosing"; //$NON-NLS-1$
272
/** Field ID of the enclosing type checkbox. */
273     protected final static String JavaDoc ENCLOSINGSELECTION= ENCLOSING + ".selection"; //$NON-NLS-1$
274
/** Field ID of the type name input field. */
275     protected final static String JavaDoc TYPENAME= PAGE_NAME + ".typename"; //$NON-NLS-1$
276
/** Field ID of the super type input field. */
277     protected final static String JavaDoc SUPER= PAGE_NAME + ".superclass"; //$NON-NLS-1$
278
/** Field ID of the super interfaces input field. */
279     protected final static String JavaDoc INTERFACES= PAGE_NAME + ".interfaces"; //$NON-NLS-1$
280
/** Field ID of the modifier check boxes. */
281     protected final static String JavaDoc MODIFIERS= PAGE_NAME + ".modifiers"; //$NON-NLS-1$
282
/** Field ID of the method stubs check boxes. */
283     protected final static String JavaDoc METHODS= PAGE_NAME + ".methods"; //$NON-NLS-1$
284

285     private static class InterfaceWrapper {
286         public String JavaDoc interfaceName;
287
288         public InterfaceWrapper(String JavaDoc interfaceName) {
289             this.interfaceName= interfaceName;
290         }
291
292         public int hashCode() {
293             return interfaceName.hashCode();
294         }
295
296         public boolean equals(Object JavaDoc 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 JavaDoc getText(Object JavaDoc element) {
309             return ((InterfaceWrapper) element).interfaceName;
310         }
311         
312         public Image getImage(Object JavaDoc 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     /**
329      * a handle to the type to be created (does usually not exist, can be null)
330      */

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; // used for compatibility: Wizards that don't show the comment button control
342
// will use the preferences settings
343

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     /**
364      * Constant to signal that the created type is a class.
365      * @since 3.1
366      */

367     public static final int CLASS_TYPE = 1;
368     
369     /**
370      * Constant to signal that the created type is a interface.
371      * @since 3.1
372      */

373     public static final int INTERFACE_TYPE = 2;
374     
375     /**
376      * Constant to signal that the created type is an enum.
377      * @since 3.1
378      */

379     public static final int ENUM_TYPE = 3;
380     
381     /**
382      * Constant to signal that the created type is an annotation.
383      * @since 3.1
384      */

385     public static final int ANNOTATION_TYPE = 4;
386
387     /**
388      * Creates a new <code>NewTypeWizardPage</code>.
389      *
390      * @param isClass <code>true</code> if a new class is to be created; otherwise
391      * an interface is to be created
392      * @param pageName the wizard page's name
393      */

394     public NewTypeWizardPage(boolean isClass, String JavaDoc pageName) {
395         this(isClass ? CLASS_TYPE : INTERFACE_TYPE, pageName);
396     }
397     
398     /**
399      * Creates a new <code>NewTypeWizardPage</code>.
400      *
401      * @param typeKind Signals the kind of the type to be created. Valid kinds are
402      * {@link #CLASS_TYPE}, {@link #INTERFACE_TYPE}, {@link #ENUM_TYPE} and {@link #ANNOTATION_TYPE}
403      * @param pageName the wizard page's name
404      * @since 3.1
405      */

406     public NewTypeWizardPage(int typeKind, String JavaDoc 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 JavaDoc[] addButtons= new String JavaDoc[] {
438             NewWizardMessages.NewTypeWizardPage_interfaces_add,
439             /* 1 */ 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 JavaDoc[] buttonNames1= new String JavaDoc[] {
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 JavaDoc[] buttonNames2;
460         if (fTypeKind == CLASS_TYPE) {
461             buttonNames2= new String JavaDoc[] {
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 JavaDoc[] {
469                     NewWizardMessages.NewTypeWizardPage_modifiers_abstract,
470                     NewWizardMessages.NewTypeWizardPage_modifiers_static
471                 };
472             }
473             else
474                 buttonNames2= new String JavaDoc[] {};
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; // only used when enabled
493

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     /**
511      * Initializes all fields provided by the page with a given selection.
512      *
513      * @param elem the selection used to initialize this page or <code>
514      * null</code> if no selection was available
515      */

516     protected void initTypePage(IJavaElement elem) {
517         String JavaDoc initSuperclass= "java.lang.Object"; //$NON-NLS-1$
518
ArrayList JavaDoc initSuperinterfaces= new ArrayList JavaDoc(5);
519
520         IJavaProject project= null;
521         IPackageFragment pack= null;
522         IType enclosingType= null;
523                 
524         if (elem != null) {
525             // evaluate the enclosing type
526
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 JavaDoc 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                 // ignore this exception now
556
}
557         }
558         
559         String JavaDoc typeName= ""; //$NON-NLS-1$
560

561         ITextSelection selection= getCurrentTextSelection();
562         if (selection != null) {
563             String JavaDoc 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); // from project or workspace
578
}
579     
580     private static IStatus validateJavaTypeName(String JavaDoc 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 JavaDoc sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true);
585         String JavaDoc compliance= project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
586         return JavaConventions.validateJavaTypeName(text, sourceLevel, compliance);
587     }
588     
589     private static IStatus validatePackageName(String JavaDoc 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 JavaDoc sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true);
594         String JavaDoc compliance= project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
595         return JavaConventions.validatePackageName(text, sourceLevel, compliance);
596     }
597     
598     // -------- UI Creation ---------
599

600     /**
601      * Returns the label that is used for the package input field.
602      *
603      * @return the label that is used for the package input field.
604      * @since 3.2
605      */

606     protected String JavaDoc getPackageLabel() {
607         return NewWizardMessages.NewTypeWizardPage_package_label;
608     }
609
610     /**
611      * Returns the label that is used for the enclosing type input field.
612      *
613      * @return the label that is used for the enclosing type input field.
614      * @since 3.2
615      */

616     protected String JavaDoc getEnclosingTypeLabel() {
617         return NewWizardMessages.NewTypeWizardPage_enclosing_selection_label;
618     }
619     
620     /**
621      * Returns the label that is used for the type name input field.
622      *
623      * @return the label that is used for the type name input field.
624      * @since 3.2
625      */

626     protected String JavaDoc getTypeNameLabel() {
627         return NewWizardMessages.NewTypeWizardPage_typename_label;
628     }
629
630     /**
631      * Returns the label that is used for the modifiers input field.
632      *
633      * @return the label that is used for the modifiers input field
634      * @since 3.2
635      */

636     protected String JavaDoc getModifiersLabel() {
637         return NewWizardMessages.NewTypeWizardPage_modifiers_acc_label;
638     }
639
640     /**
641      * Returns the label that is used for the super class input field.
642      *
643      * @return the label that is used for the super class input field.
644      * @since 3.2
645      */

646     protected String JavaDoc getSuperClassLabel() {
647         return NewWizardMessages.NewTypeWizardPage_superclass_label;
648     }
649
650     /**
651      * Returns the label that is used for the super interfaces input field.
652      *
653      * @return the label that is used for the super interfaces input field.
654      * @since 3.2
655      */

656     protected String JavaDoc getSuperInterfacesLabel() {
657         if (fTypeKind != INTERFACE_TYPE)
658             return NewWizardMessages.NewTypeWizardPage_interfaces_class_label;
659         return NewWizardMessages.NewTypeWizardPage_interfaces_ifc_label;
660     }
661     
662     /**
663      * Creates a separator line. Expects a <code>GridLayout</code> with at least 1 column.
664      *
665      * @param composite the parent composite
666      * @param nColumns number of columns to span
667      */

668     protected void createSeparator(Composite composite, int nColumns) {
669         (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(composite, nColumns, convertHeightInCharsToPixels(1));
670     }
671
672     /**
673      * Creates the controls for the package name field. Expects a <code>GridLayout</code> with at
674      * least 4 columns.
675      *
676      * @param composite the parent composite
677      * @param nColumns number of columns to span
678      */

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     /**
689      * Creates the controls for the enclosing type name field. Expects a <code>GridLayout</code> with at
690      * least 4 columns.
691      *
692      * @param composite the parent composite
693      * @param nColumns number of columns to span
694      */

695     protected void createEnclosingTypeControls(Composite composite, int nColumns) {
696         // #6891
697
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     /**
725      * Creates the controls for the type name field. Expects a <code>GridLayout</code> with at
726      * least 2 columns.
727      *
728      * @param composite the parent composite
729      * @param nColumns number of columns to span
730      */

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     /**
741      * Creates the controls for the modifiers radio/checkbox buttons. Expects a
742      * <code>GridLayout</code> with at least 3 columns.
743      *
744      * @param composite the parent composite
745      * @param nColumns number of columns to span
746      */

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     /**
770      * Creates the controls for the superclass name field. Expects a <code>GridLayout</code>
771      * with at least 3 columns.
772      *
773      * @param composite the parent composite
774      * @param nColumns number of columns to span
775      */

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     /**
793      * Creates the controls for the superclass name field. Expects a <code>GridLayout</code> with
794      * at least 3 columns.
795      *
796      * @param composite the parent composite
797      * @param nColumns number of columns to span
798      */

799     protected void createSuperInterfacesControls(Composite composite, int nColumns) {
800         final String JavaDoc INTERFACE= "interface"; //$NON-NLS-1$
801
fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns);
802         final TableViewer tableViewer= fSuperInterfacesDialogField.getTableViewer();
803         tableViewer.setColumnProperties(new String JavaDoc[] {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 JavaDoc element, String JavaDoc property, Object JavaDoc value) {
831                 if (element instanceof Item)
832                     element = ((Item) element).getData();
833                 
834                 ((InterfaceWrapper) element).interfaceName= (String JavaDoc) value;
835                 fSuperInterfacesDialogField.elementChanged(element);
836             }
837             public Object JavaDoc getValue(Object JavaDoc element, String JavaDoc property) {
838                 return ((InterfaceWrapper) element).interfaceName;
839             }
840             public boolean canModify(Object JavaDoc element, String JavaDoc 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     /**
866      * Creates the controls for the preference page links. Expects a <code>GridLayout</code> with
867      * at least 3 columns.
868      *
869      * @param composite the parent composite
870      * @param nColumns number of columns to span
871      *
872      * @since 3.1
873      */

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     /**
886      * Sets the focus on the type name input field.
887      */

888     protected void setFocus() {
889         fTypeNameDialogField.setFocus();
890     }
891                 
892     // -------- TypeFieldsAdapter --------
893

894     private class TypeFieldsAdapter implements IStringButtonAdapter, IDialogFieldListener, IListAdapter, SelectionListener {
895         
896         // -------- IStringButtonAdapter
897
public void changeControlPressed(DialogField field) {
898             typePageChangeControlPressed(field);
899         }
900         
901         // -------- IListAdapter
902
public void customButtonPressed(ListDialogField field, int index) {
903             typePageCustomButtonPressed(field, index);
904         }
905         
906         public void selectionChanged(ListDialogField field) {}
907         
908         // -------- IDialogFieldListener
909
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 JavaDoc title= NewWizardMessages.NewTypeWizardPage_configure_templates_title;
933             String JavaDoc 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 JavaDoc interfaces= fSuperInterfacesDialogField.getElements();
961             if (!interfaces.isEmpty()) {
962                 Object JavaDoc element= interfaces.get(interfaces.size() - 1);
963                 fSuperInterfacesDialogField.editElement(element);
964             }
965         }
966     }
967     
968     /*
969      * A field on the type has changed. The fields' status and all dependent
970      * status are updated.
971      */

972     private void typePageDialogFieldChanged(DialogField field) {
973         String JavaDoc 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        // tell all others
1020
handleFieldChanged(fieldName);
1021    }
1022    
1023    // -------- update message ----------------
1024

1025    /*
1026     * @see org.eclipse.jdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
1027     */

1028    protected void handleFieldChanged(String JavaDoc 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    // ---- set / get ----------------
1040

1041    /**
1042     * Returns the text of the package input field.
1043     *
1044     * @return the text of the package input field
1045     */

1046    public String JavaDoc getPackageText() {
1047        return fPackageDialogField.getText();
1048    }
1049
1050    /**
1051     * Returns the text of the enclosing type input field.
1052     *
1053     * @return the text of the enclosing type input field
1054     */

1055    public String JavaDoc getEnclosingTypeText() {
1056        return fEnclosingTypeDialogField.getText();
1057    }
1058    
1059    
1060    /**
1061     * Returns the package fragment corresponding to the current input.
1062     *
1063     * @return a package fragment or <code>null</code> if the input
1064     * could not be resolved.
1065     */

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    /**
1078     * Sets the package fragment to the given value. The method updates the model
1079     * and the text of the control.
1080     *
1081     * @param pack the package fragment to be set
1082     * @param canBeModified if <code>true</code> the package fragment is
1083     * editable; otherwise it is read-only.
1084     */

1085    public void setPackageFragment(IPackageFragment pack, boolean canBeModified) {
1086        fCurrPackage= pack;
1087        fCanModifyPackage= canBeModified;
1088        String JavaDoc str= (pack == null) ? "" : pack.getElementName(); //$NON-NLS-1$
1089
fPackageDialogField.setText(str);
1090        updateEnableState();
1091    }
1092
1093    /**
1094     * Returns the enclosing type corresponding to the current input.
1095     *
1096     * @return the enclosing type or <code>null</code> if the enclosing type is
1097     * not selected or the input could not be resolved
1098     */

1099    public IType getEnclosingType() {
1100        if (isEnclosingTypeSelected()) {
1101            return fCurrEnclosingType;
1102        }
1103        return null;
1104    }
1105
1106    /**
1107     * Sets the enclosing type. The method updates the underlying model
1108     * and the text of the control.
1109     *
1110     * @param type the enclosing type
1111     * @param canBeModified if <code>true</code> the enclosing type field is
1112     * editable; otherwise it is read-only.
1113     */

1114    public void setEnclosingType(IType type, boolean canBeModified) {
1115        fCurrEnclosingType= type;
1116        fCanModifyEnclosingType= canBeModified;
1117        String JavaDoc str= (type == null) ? "" : JavaModelUtil.getFullyQualifiedName(type); //$NON-NLS-1$
1118
fEnclosingTypeDialogField.setText(str);
1119        updateEnableState();
1120    }
1121    
1122    /**
1123     * Returns the selection state of the enclosing type checkbox.
1124     *
1125     * @return the selection state of the enclosing type checkbox
1126     */

1127    public boolean isEnclosingTypeSelected() {
1128        return fEnclosingTypeSelection.isSelected();
1129    }
1130
1131    /**
1132     * Sets the enclosing type checkbox's selection state.
1133     *
1134     * @param isSelected the checkbox's selection state
1135     * @param canBeModified if <code>true</code> the enclosing type checkbox is
1136     * modifiable; otherwise it is read-only.
1137     */

1138    public void setEnclosingTypeSelection(boolean isSelected, boolean canBeModified) {
1139        fEnclosingTypeSelection.setSelection(isSelected);
1140        fEnclosingTypeSelection.setEnabled(canBeModified);
1141        updateEnableState();
1142    }
1143    
1144    /**
1145     * Returns the type name entered into the type input field.
1146     *
1147     * @return the type name
1148     */

1149    public String JavaDoc getTypeName() {
1150        return fTypeNameDialogField.getText();
1151    }
1152
1153    /**
1154     * Sets the type name input field's text to the given value. Method doesn't update
1155     * the model.
1156     *
1157     * @param name the new type name
1158     * @param canBeModified if <code>true</code> the type name field is
1159     * editable; otherwise it is read-only.
1160     */

1161    public void setTypeName(String JavaDoc name, boolean canBeModified) {
1162        fTypeNameDialogField.setText(name);
1163        fTypeNameDialogField.setEnabled(canBeModified);
1164    }
1165    
1166    /**
1167     * Returns the selected modifiers.
1168     *
1169     * @return the selected modifiers
1170     * @see Flags
1171     */

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    /**
1194     * Sets the modifiers.
1195     *
1196     * @param modifiers <code>F_PUBLIC</code>, <code>F_PRIVATE</code>,
1197     * <code>F_PROTECTED</code>, <code>F_ABSTRACT</code>, <code>F_FINAL</code>
1198     * or <code>F_STATIC</code> or a valid combination.
1199     * @param canBeModified if <code>true</code> the modifier fields are
1200     * editable; otherwise they are read-only
1201     * @see Flags
1202     */

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    /**
1228     * Returns the content of the superclass input field.
1229     *
1230     * @return the superclass name
1231     */

1232    public String JavaDoc getSuperClass() {
1233        return fSuperClassDialogField.getText();
1234    }
1235
1236    /**
1237     * Sets the super class name.
1238     *
1239     * @param name the new superclass name
1240     * @param canBeModified if <code>true</code> the superclass name field is
1241     * editable; otherwise it is read-only.
1242     */

1243    public void setSuperClass(String JavaDoc name, boolean canBeModified) {
1244        fSuperClassDialogField.setText(name);
1245        fSuperClassDialogField.setEnabled(canBeModified);
1246    }
1247    
1248    /**
1249     * Returns the chosen super interfaces.
1250     *
1251     * @return a list of chosen super interfaces. The list's elements
1252     * are of type <code>String</code>
1253     */

1254    public List JavaDoc getSuperInterfaces() {
1255        List JavaDoc interfaces= fSuperInterfacesDialogField.getElements();
1256        ArrayList JavaDoc result= new ArrayList JavaDoc(interfaces.size());
1257        for (Iterator JavaDoc iter= interfaces.iterator(); iter.hasNext();) {
1258            InterfaceWrapper wrapper= (InterfaceWrapper) iter.next();
1259            result.add(wrapper.interfaceName);
1260        }
1261        return result;
1262    }
1263
1264    /**
1265     * Sets the super interfaces.
1266     *
1267     * @param interfacesNames a list of super interface. The method requires that
1268     * the list's elements are of type <code>String</code>
1269     * @param canBeModified if <code>true</code> the super interface field is
1270     * editable; otherwise it is read-only.
1271     */

1272    public void setSuperInterfaces(List JavaDoc interfacesNames, boolean canBeModified) {
1273        ArrayList JavaDoc interfaces= new ArrayList JavaDoc(interfacesNames.size());
1274        for (Iterator JavaDoc iter= interfacesNames.iterator(); iter.hasNext();) {
1275            interfaces.add(new InterfaceWrapper((String JavaDoc) iter.next()));
1276        }
1277        fSuperInterfacesDialogField.setElements(interfaces);
1278        fSuperInterfacesDialogField.setEnabled(canBeModified);
1279    }
1280    
1281    /**
1282     * Adds a super interface to the end of the list and selects it if it is not in the list yet.
1283     *
1284     * @param superInterface the fully qualified type name of the interface.
1285     * @return returns <code>true</code>if the interfaces has been added, <code>false</code>
1286     * if the interface already is in the list.
1287     * @since 3.2
1288     */

1289    public boolean addSuperInterface(String JavaDoc superInterface) {
1290        return fSuperInterfacesDialogField.addElement(new InterfaceWrapper(superInterface));
1291    }
1292    
1293    
1294    /**
1295     * Sets 'Add comment' checkbox. The value set will only be used when creating source when
1296     * the comment control is enabled (see {@link #enableCommentControl(boolean)}
1297     *
1298     * @param doAddComments if <code>true</code>, comments are added.
1299     * @param canBeModified if <code>true</code> check box is
1300     * editable; otherwise it is read-only.
1301     * @since 3.1
1302     */

1303    public void setAddComments(boolean doAddComments, boolean canBeModified) {
1304        fAddCommentButton.setSelection(doAddComments);
1305        fAddCommentButton.setEnabled(canBeModified);
1306    }
1307    
1308    /**
1309     * Sets to use the 'Add comment' checkbox value. Clients that use the 'Add comment' checkbox
1310     * additionally have to enable the control. This has been added for backwards compatibility.
1311     *
1312     * @param useAddCommentValue if <code>true</code>,
1313     * @since 3.1
1314     */

1315    public void enableCommentControl(boolean useAddCommentValue) {
1316        fUseAddCommentButtonValue= useAddCommentValue;
1317    }
1318    
1319    
1320    /**
1321     * Returns if comments are added. This method can be overridden by clients.
1322     * The selection of the comment control is taken if enabled (see {@link #enableCommentControl(boolean)}, otherwise
1323     * the settings as specified in the preferences is used.
1324     *
1325     * @return Returns <code>true</code> if comments can be added
1326     * @since 3.1
1327     */

1328    public boolean isAddComments() {
1329        if (fUseAddCommentButtonValue) {
1330            return fAddCommentButton.isSelected();
1331        }
1332        return StubUtility.doAddComments(getJavaProject());
1333    }
1334            
1335    /**
1336     * Returns the resource handle that corresponds to the compilation unit to was or
1337     * will be created or modified.
1338     * @return A resource or null if the page contains illegal values.
1339     * @since 3.0
1340     */

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 JavaDoc cuName= getCompilationUnitName(getTypeNameWithoutParameters());
1349            return pack.getCompilationUnit(cuName).getResource();
1350        }
1351        return null;
1352    }
1353            
1354    // ----------- validation ----------
1355

1356    /*
1357     * @see org.eclipse.jdt.ui.wizards.NewContainerWizardPage#containerChanged()
1358     */

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                // error as createType will fail otherwise (bug 96928)
1365
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(...) == null then Enum is unavailable
1370
if (findType(root.getJavaProject(), "java.lang.Enum") == null) //$NON-NLS-1$
1371
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("")); //$NON-NLS-1$
1381
}
1382        return status;
1383    }
1384    
1385    /**
1386     * A hook method that gets called when the package field has changed. The method
1387     * validates the package name and returns the status of the validation. The validation
1388     * also updates the package fragment model.
1389     * <p>
1390     * Subclasses may extend this method to perform their own validation.
1391     * </p>
1392     *
1393     * @return the status of the validation
1394     */

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 JavaDoc 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                // continue
1411
}
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                        // if the bin folder is inside of our root, don't allow to name a package
1423
// like the bin folder
1424
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                    // let pass
1433
}
1434            }
1435            
1436            fCurrPackage= root.getPackageFragment(packName);
1437        } else {
1438            status.setError(""); //$NON-NLS-1$
1439
}
1440        return status;
1441    }
1442
1443    /*
1444     * Updates the 'default' label next to the package field.
1445     */

1446    private void updatePackageStatusLabel() {
1447        String JavaDoc packName= getPackageText();
1448        
1449        if (packName.length() == 0) {
1450            fPackageDialogField.setStatus(NewWizardMessages.NewTypeWizardPage_default);
1451        } else {
1452            fPackageDialogField.setStatus(""); //$NON-NLS-1$
1453
}
1454    }
1455    
1456    /*
1457     * Updates the enable state of buttons related to the enclosing type selection checkbox.
1458     */

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    /**
1470     * Hook method that gets called when the enclosing type name has changed. The method
1471     * validates the enclosing type and returns the status of the validation. It also updates the
1472     * enclosing type model.
1473     * <p>
1474     * Subclasses may extend this method to perform their own validation.
1475     * </p>
1476     *
1477     * @return the status of the validation
1478     */

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(""); //$NON-NLS-1$
1488
return status;
1489        }
1490        
1491        String JavaDoc 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 JavaDoc typeName) throws JavaModelException {
1526        if (project.exists()) {
1527            return project.findType(typeName);
1528        }
1529        return null;
1530    }
1531    
1532    private String JavaDoc getTypeNameWithoutParameters() {
1533        String JavaDoc 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    /**
1543     * Hook method that is called when evaluating the name of the compilation unit to create. By default, a file extension
1544     * <code>java</code> is added to the given type name, but implementors can override this behavior.
1545     *
1546     * @param typeName the name of the type to create the compilation unit for.
1547     * @return the name of the compilation unit to be created for the given name
1548     *
1549     * @since 3.2
1550     */

1551    protected String JavaDoc getCompilationUnitName(String JavaDoc typeName) {
1552        return typeName + JavaModelUtil.DEFAULT_CU_SUFFIX;
1553    }
1554    
1555    
1556    /**
1557     * Hook method that gets called when the type name has changed. The method validates the
1558     * type name and returns the status of the validation.
1559     * <p>
1560     * Subclasses may extend this method to perform their own validation.
1561     * </p>
1562     *
1563     * @return the status of the validation
1564     */

1565    protected IStatus typeNameChanged() {
1566        StatusInfo status= new StatusInfo();
1567        fCurrType= null;
1568        String JavaDoc typeNameWithParameters= getTypeName();
1569        // must not be empty
1570
if (typeNameWithParameters.length() == 0) {
1571            status.setError(NewWizardMessages.NewTypeWizardPage_error_EnterTypeName);
1572            return status;
1573        }
1574        
1575        String JavaDoc 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            // continue checking
1589
}
1590
1591        // must not exist
1592
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 JavaDoc 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 JavaDoc typeDeclaration= "class " + typeNameWithParameters + " {}"; //$NON-NLS-1$//$NON-NLS-2$
1635
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    /**
1649     * Hook method that gets called when the superclass name has changed. The method
1650     * validates the superclass name and returns the status of the validation.
1651     * <p>
1652     * Subclasses may extend this method to perform their own validation.
1653     * </p>
1654     *
1655     * @return the status of the validation
1656     */

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 JavaDoc sclassName= getSuperClass();
1665        if (sclassName.length() == 0) {
1666            // accept the empty field (stands for java.lang.Object)
1667
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(""); //$NON-NLS-1$
1682
}
1683        return status;
1684    }
1685
1686    private StubTypeContext getSuperClassStubTypeContext() {
1687        if (fSuperClassStubTypeContext == null) {
1688            String JavaDoc 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    /**
1700     * Hook method that gets called when the list of super interface has changed. The method
1701     * validates the super interfaces and returns the status of the validation.
1702     * <p>
1703     * Subclasses may extend this method to perform their own validation.
1704     * </p>
1705     *
1706     * @return the status of the validation
1707     */

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 JavaDoc elements= fSuperInterfacesDialogField.getElements();
1716            int nElements= elements.size();
1717            for (int i= 0; i < nElements; i++) {
1718                String JavaDoc 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 JavaDoc 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    /**
1747     * Hook method that gets called when the modifiers have changed. The method validates
1748     * the modifiers and returns the status of the validation.
1749     * <p>
1750     * Subclasses may extend this method to perform their own validation.
1751     * </p>
1752     *
1753     * @return the status of the validation
1754     */

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    // selection dialogs
1765

1766    /**
1767     * Opens a selection dialog that allows to select a package.
1768     *
1769     * @return returns the selected package or <code>null</code> if the dialog has been canceled.
1770     * The caller typically sets the result to the package input field.
1771     * <p>
1772     * Clients can override this method if they want to offer a different dialog.
1773     * </p>
1774     *
1775     * @since 3.2
1776     */

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 JavaDoc[] { pack });
1802        }
1803
1804        if (dialog.open() == Window.OK) {
1805            return (IPackageFragment) dialog.getFirstResult();
1806        }
1807        return null;
1808    }
1809    
1810    /**
1811     * Opens a selection dialog that allows to select an enclosing type.
1812     *
1813     * @return returns the selected type or <code>null</code> if the dialog has been canceled.
1814     * The caller typically sets the result to the enclosing type input field.
1815     * <p>
1816     * Clients can override this method if they want to offer a different dialog.
1817     * </p>
1818     *
1819     * @since 3.2
1820     */

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    /**
1842     * Opens a selection dialog that allows to select a super class.
1843     *
1844     * @return returns the selected type or <code>null</code> if the dialog has been canceled.
1845     * The caller typically sets the result to the super class input field.
1846     * <p>
1847     * Clients can override this method if they want to offer a different dialog.
1848     * </p>
1849     *
1850     * @since 3.2
1851     */

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    /**
1874     * Opens a selection dialog that allows to select the super interfaces. The selected interfaces are
1875     * directly added to the wizard page using {@link #addSuperInterface(String)}.
1876     *
1877     * <p>
1878     * Clients can override this method if they want to offer a different dialog.
1879     * </p>
1880     *
1881     * @since 3.2
1882     */

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 JavaDoc getInterfaceDialogTitle() {
1896        if (fTypeKind == INTERFACE_TYPE)
1897            return NewWizardMessages.NewTypeWizardPage_InterfacesDialog_interface_title;
1898        return NewWizardMessages.NewTypeWizardPage_InterfacesDialog_class_title;
1899    }
1900    
1901        
1902    // ---- creation ----------------
1903

1904    /**
1905     * Creates the new type using the entered field values.
1906     *
1907     * @param monitor a progress monitor to report progress.
1908     * @throws CoreException Thrown when the creation failed.
1909     * @throws InterruptedException Thrown when the operation was canceled.
1910     */

1911    public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException JavaDoc {
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(""); //$NON-NLS-1$
1922
}
1923        
1924        if (!pack.exists()) {
1925            String JavaDoc 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 JavaDoc typeName= getTypeNameWithoutParameters();
1936            
1937            boolean isInnerClass= isEnclosingTypeSelected();
1938        
1939            IType createdType;
1940            ImportsManager imports;
1941            int indent= 0;
1942
1943            Set JavaDoc /* String (import names) */ existingImports;
1944            
1945            String JavaDoc lineDelimiter= null;
1946            if (!isInnerClass) {
1947                lineDelimiter= StubUtility.getLineDelimiterUsed(pack.getJavaProject());
1948                
1949                String JavaDoc cuName= getCompilationUnitName(typeName);
1950                ICompilationUnit parentCU= pack.createCompilationUnit(cuName, "", false, new SubProgressMonitor(monitor, 2)); //$NON-NLS-1$
1951
// create a working copy with a new owner
1952

1953                needsSave= true;
1954                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); // cu is now a (primary) working copy
1955
connectedCU= parentCU;
1956                
1957                IBuffer buffer= parentCU.getBuffer();
1958                
1959                String JavaDoc simpleTypeStub= constructSimpleTypeStub();
1960                String JavaDoc 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                // add an import that will be removed again. Having this import solves 14661
1968
imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), typeName));
1969                
1970                String JavaDoc 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)); // cu is now for sure (primary) a working copy
1990
connectedCU= parentCU;
1991                
1992                CompilationUnit astRoot= createASTForImports(parentCU);
1993                imports= new ImportsManager(astRoot);
1994                existingImports= getExistingImports(astRoot);
1995
1996    
1997                // add imports that will be removed again. Having the imports solves 14661
1998
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 JavaDoc content= new StringBuffer JavaDoc();
2005                
2006                String JavaDoc 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 JavaDoc();
2035            }
2036            
2037            // add imports for superclass/interfaces, so types can be resolved correctly
2038

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 JavaDoc();
2047            }
2048            
2049            // set up again
2050
CompilationUnit astRoot= createASTForImports(imports.getCompilationUnit());
2051            imports= new ImportsManager(astRoot);
2052            
2053            createTypeMembers(createdType, imports, new SubProgressMonitor(monitor, 1));
2054    
2055            // add imports
2056
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 JavaDoc originalContent= buf.getText(range.getOffset(), range.getLength());
2066            
2067            String JavaDoc 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 JavaDoc 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 JavaDoc /* String */ getExistingImports(CompilationUnit root) {
2102        List JavaDoc imports= root.imports();
2103        Set JavaDoc res= new HashSet JavaDoc(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 JavaDoc 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 JavaDoc 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) { // not visible problems hide unused -> remove both
2133
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 JavaDoc name= decl.getName().getFullyQualifiedName();
2139                                if (decl.isOnDemand()) {
2140                                    name += ".*"; //$NON-NLS-1$
2141
}
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    /**
2158     * Uses the New Java file template from the code template page to generate a
2159     * compilation unit with the given type content.
2160     * @param cu The new created compilation unit
2161     * @param typeContent The content of the type, including signature and type
2162     * body.
2163     * @param lineDelimiter The line delimiter to be used.
2164     * @return String Returns the result of evaluating the new file template
2165     * with the given type content.
2166     * @throws CoreException
2167     * @since 2.1
2168     */

2169    protected String JavaDoc constructCUContent(ICompilationUnit cu, String JavaDoc typeContent, String JavaDoc lineDelimiter) throws CoreException {
2170        String JavaDoc fileComment= getFileComment(cu, lineDelimiter);
2171        String JavaDoc typeComment= getTypeComment(cu, lineDelimiter);
2172        IPackageFragment pack= (IPackageFragment) cu.getParent();
2173        String JavaDoc 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 JavaDoc buf= new StringBuffer JavaDoc();
2184        if (!pack.isDefaultPackage()) {
2185            buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
2186
}
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    /**
2197     * Returns the created type or <code>null</code> is the type has not been created yet. The method
2198     * only returns a valid type after <code>createType</code> has been called.
2199     *
2200     * @return the created type
2201     * @see #createType(IProgressMonitor)
2202     */

2203    public IType getCreatedType() {
2204        return fCreatedType;
2205    }
2206    
2207    // ---- construct CU body----------------
2208

2209    private void writeSuperClass(StringBuffer JavaDoc buf, ImportsManager imports) {
2210        String JavaDoc superclass= getSuperClass();
2211        if (fTypeKind == CLASS_TYPE && superclass.length() > 0 && !"java.lang.Object".equals(superclass)) { //$NON-NLS-1$
2212
buf.append(" extends "); //$NON-NLS-1$
2213

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 JavaDoc buf, ImportsManager imports) {
2227        List JavaDoc interfaces= getSuperInterfaces();
2228        int last= interfaces.size() - 1;
2229        if (last >= 0) {
2230            if (fTypeKind != INTERFACE_TYPE) {
2231                buf.append(" implements "); //$NON-NLS-1$
2232
} else {
2233                buf.append(" extends "); //$NON-NLS-1$
2234
}
2235            String JavaDoc[] intfs= (String JavaDoc[]) interfaces.toArray(new String JavaDoc[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 JavaDoc constructSimpleTypeStub() {
2258        StringBuffer JavaDoc buf= new StringBuffer JavaDoc("public class "); //$NON-NLS-1$
2259
buf.append(getTypeName());
2260        buf.append("{ }"); //$NON-NLS-1$
2261
return buf.toString();
2262    }
2263    
2264    /*
2265     * Called from createType to construct the source for this type
2266     */

2267    private String JavaDoc constructTypeStub(ICompilationUnit parentCU, ImportsManager imports, String JavaDoc lineDelimiter) throws CoreException {
2268        StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
2269        
2270        int modifiers= getModifiers();
2271        buf.append(Flags.toString(modifiers));
2272        if (modifiers != 0) {
2273            buf.append(' ');
2274        }
2275        String JavaDoc type= ""; //$NON-NLS-1$
2276
String JavaDoc templateID= ""; //$NON-NLS-1$
2277
switch (fTypeKind) {
2278            case CLASS_TYPE:
2279                type= "class "; //$NON-NLS-1$
2280
templateID= CodeGeneration.CLASS_BODY_TEMPLATE_ID;
2281                break;
2282            case INTERFACE_TYPE:
2283                type= "interface "; //$NON-NLS-1$
2284
templateID= CodeGeneration.INTERFACE_BODY_TEMPLATE_ID;
2285                break;
2286            case ENUM_TYPE:
2287                type= "enum "; //$NON-NLS-1$
2288
templateID= CodeGeneration.ENUM_BODY_TEMPLATE_ID;
2289                break;
2290            case ANNOTATION_TYPE:
2291                type= "@interface "; //$NON-NLS-1$
2292
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); //$NON-NLS-1$
2301
String JavaDoc 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    /**
2312     * Hook method that gets called from <code>createType</code> to support adding of
2313     * unanticipated methods, fields, and inner types to the created type.
2314     * <p>
2315     * Implementers can use any methods defined on <code>IType</code> to manipulate the
2316     * new type.
2317     * </p>
2318     * <p>
2319     * The source code of the new type will be formatted using the platform's formatter. Needed
2320     * imports are added by the wizard at the end of the type creation process using the given
2321     * import manager.
2322     * </p>
2323     *
2324     * @param newType the new type created via <code>createType</code>
2325     * @param imports an import manager which can be used to add new imports
2326     * @param monitor a progress monitor to report progress. Must not be <code>null</code>
2327     * @throws CoreException thrown when creation of the type members failed
2328     *
2329     * @see #createType(IProgressMonitor)
2330     */

2331    protected void createTypeMembers(IType newType, final ImportsManager imports, IProgressMonitor monitor) throws CoreException {
2332        // default implementation does nothing
2333
// example would be
2334
// String mainMathod= "public void foo(Vector vec) {}"
2335
// createdType.createMethod(main, null, false, null);
2336
// imports.addImport("java.lang.Vector");
2337
}
2338    
2339        
2340    /**
2341     * @param parentCU the current compilation unit
2342     * @return returns the file template or <code>null</code>
2343     * @deprecated Instead of file templates, the new type code template
2344     * specifies the stub for a compilation unit.
2345     */

2346    protected String JavaDoc getFileComment(ICompilationUnit parentCU) {
2347        return null;
2348    }
2349    
2350    /**
2351     * Hook method that gets called from <code>createType</code> to retrieve
2352     * a file comment. This default implementation returns the content of the
2353     * 'file comment' template or <code>null</code> if no comment should be created.
2354     *
2355     * @param parentCU the parent compilation unit
2356     * @param lineDelimiter the line delimiter to use
2357     * @return the file comment or <code>null</code> if a file comment
2358     * is not desired
2359     * @throws CoreException
2360     *
2361     * @since 3.1
2362     */

2363    protected String JavaDoc getFileComment(ICompilationUnit parentCU, String JavaDoc lineDelimiter) throws CoreException {
2364        if (isAddComments()) {
2365            return CodeGeneration.getFileComment(parentCU, lineDelimiter);
2366        }
2367        return null;
2368        
2369    }
2370    
2371    private boolean isValidComment(String JavaDoc 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    /**
2386     * Hook method that gets called from <code>createType</code> to retrieve
2387     * a type comment. This default implementation returns the content of the
2388     * 'type comment' template.
2389     *
2390     * @param parentCU the parent compilation unit
2391     * @param lineDelimiter the line delimiter to use
2392     * @return the type comment or <code>null</code> if a type comment
2393     * is not desired
2394     *
2395     * @since 3.0
2396     */

2397    protected String JavaDoc getTypeComment(ICompilationUnit parentCU, String JavaDoc lineDelimiter) {
2398        if (isAddComments()) {
2399            try {
2400                StringBuffer JavaDoc typeName= new StringBuffer JavaDoc();
2401                if (isEnclosingTypeSelected()) {
2402                    typeName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
2403                }
2404                typeName.append(getTypeNameWithoutParameters());
2405                String JavaDoc[] typeParamNames= new String JavaDoc[0];
2406                String JavaDoc 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    /**
2418     * @param parentCU the current compilation unit
2419     * @return returns the template or <code>null</code>
2420     * @deprecated Use getTypeComment(ICompilationUnit, String)
2421     */

2422    protected String JavaDoc getTypeComment(ICompilationUnit parentCU) {
2423        if (StubUtility.doAddComments(parentCU.getJavaProject()))
2424            return getTypeComment(parentCU, StubUtility.getLineDelimiterUsed(parentCU));
2425        return null;
2426    }
2427
2428    /**
2429     * @param name the name of the template
2430     * @param parentCU the current compilation unit
2431     * @return returns the template or <code>null</code>
2432     * @deprecated Use getTemplate(String,ICompilationUnit,int)
2433     */

2434    protected String JavaDoc getTemplate(String JavaDoc name, ICompilationUnit parentCU) {
2435        return getTemplate(name, parentCU, 0);
2436    }
2437        
2438    
2439    /**
2440     * Returns the string resulting from evaluation the given template in
2441     * the context of the given compilation unit. This accesses the normal
2442     * template page, not the code templates. To use code templates use
2443     * <code>constructCUContent</code> to construct a compilation unit stub or
2444     * getTypeComment for the comment of the type.
2445     *
2446     * @param name the template to be evaluated
2447     * @param parentCU the templates evaluation context
2448     * @param pos a source offset into the parent compilation unit. The
2449     * template is evaluated at the given source offset
2450     * @return return the template with the given name or <code>null</code> if the template could not be found.
2451     */

2452    protected String JavaDoc getTemplate(String JavaDoc 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    /**
2470     * Creates the bodies of all unimplemented methods and constructors and adds them to the type.
2471     * Method is typically called by implementers of <code>NewTypeWizardPage</code> to add
2472     * needed method and constructors.
2473     *
2474     * @param type the type for which the new methods and constructor are to be created
2475     * @param doConstructors if <code>true</code> unimplemented constructors are created
2476     * @param doUnimplementedMethods if <code>true</code> unimplemented methods are created
2477     * @param imports an import manager to add all needed import statements
2478     * @param monitor a progress monitor to report progress
2479     * @return the created methods.
2480     * @throws CoreException thrown when the creation fails.
2481     */

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 JavaDoc handleIds= new HashSet JavaDoc(typeMethods.length);
2487        for (int index= 0; index < typeMethods.length; index++)
2488            handleIds.add(typeMethods[index].getHandleIdentifier());
2489        ArrayList JavaDoc newMethods= new ArrayList JavaDoc();
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 JavaDoc[] createdImports) {
2523        for (int index= 0; index < createdImports.length; index++)
2524            imports.addImport(createdImports[index]);
2525    }
2526
2527    
2528    // ---- creation ----------------
2529

2530    /**
2531     * Returns the runnable that creates the type using the current settings.
2532     * The returned runnable must be executed in the UI thread.
2533     *
2534     * @return the runnable to create the new type
2535     */

2536    public IRunnableWithProgress getRunnable() {
2537        return new IRunnableWithProgress() {
2538            public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
2539                try {
2540                    if (monitor == null) {
2541                        monitor= new NullProgressMonitor();
2542                    }
2543                    createType(monitor);
2544                } catch (CoreException e) {
2545                    throw new InvocationTargetException JavaDoc(e);
2546                }
2547            }
2548        };
2549    }
2550}
2551
Popular Tags