1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.ui.wizards;
13 import java.util.ArrayList;
14 import java.util.List;
16 import net.sourceforge.phpdt.core.Flags;
17 import net.sourceforge.phpdt.core.IBuffer;
18 import net.sourceforge.phpdt.core.ICompilationUnit;
19 import net.sourceforge.phpdt.core.IJavaElement;
20 import net.sourceforge.phpdt.core.IPackageFragment;
21 import net.sourceforge.phpdt.core.ISourceRange;
22 import net.sourceforge.phpdt.core.IType;
23 import net.sourceforge.phpdt.core.ToolFactory;
24 import net.sourceforge.phpdt.core.compiler.IScanner;
25 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
26 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
27 import net.sourceforge.phpdt.externaltools.internal.ui.StatusInfo;
28 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
29 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
30 import net.sourceforge.phpdt.internal.corext.template.php.Templates;
31 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
32 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
33 import net.sourceforge.phpdt.internal.ui.wizards.NewWizardMessages;
34 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
35 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
36 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IListAdapter;
37 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
38 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
39 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ListDialogField;
40 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
41 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
42 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.Separator;
43 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
44 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField;
45 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringDialogField;
46 import net.sourceforge.phpdt.ui.PreferenceConstants;
47 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
49 import org.eclipse.core.runtime.CoreException;
50 import org.eclipse.core.runtime.IProgressMonitor;
51 import org.eclipse.core.runtime.IStatus;
52 import org.eclipse.core.runtime.NullProgressMonitor;
53 import org.eclipse.core.runtime.SubProgressMonitor;
54 import org.eclipse.jface.preference.IPreferenceStore;
55 import org.eclipse.jface.text.BadLocationException;
56 import org.eclipse.jface.text.templates.Template;
57 import org.eclipse.jface.text.templates.TemplateException;
58 import org.eclipse.jface.viewers.LabelProvider;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.graphics.Image;
61 import org.eclipse.swt.layout.GridData;
62 import org.eclipse.swt.layout.GridLayout;
63 import org.eclipse.swt.widgets.Button;
64 import org.eclipse.swt.widgets.Composite;
65 import org.eclipse.swt.widgets.Control;
68 * The class <code>NewTypeWizardPage</code> contains controls and validation routines
69 * for a 'New Type WizardPage'. Implementors decide which components to add and to enable.
70 * Implementors can also customize the validation code. <code>NewTypeWizardPage</code>
71 * is intended to serve as base class of all wizards that create types like applets, servlets, classes,
74 * See <code>NewClassWizardPage</code> or <code>NewInterfaceWizardPage</code> for an
75 * example usage of <code>NewTypeWizardPage</code>.
78 * @see org.eclipse.jdt.ui.wizards.NewClassWizardPage
79 * @see org.eclipse.jdt.ui.wizards.NewInterfaceWizardPage
83 public abstract class NewTypeWizardPage extends NewContainerWizardPage {
86 * Class used in stub creation routines to add needed imports to a
89 // public static class ImportsManager {
91 // private IImportsStructure fImportsStructure;
93 // /* package */ ImportsManager(IImportsStructure structure) {
94 // fImportsStructure= structure;
97 // /* package */ IImportsStructure getImportsStructure() {
98 // return fImportsStructure;
102 // * Adds a new import declaration that is sorted in the existing imports.
103 // * If an import already exists or the import would conflict with another import
104 // * of an other type with the same simple name the import is not added.
106 // * @param qualifiedTypeName The fully qualified name of the type to import
108 // * @return Returns the simple type name that can be used in the code or the
109 // * fully qualified type name if an import conflict prevented the import
111 // public String addImport(String qualifiedTypeName) {
112 // return fImportsStructure.addImport(qualifiedTypeName);
116 /** Public access flag. See The Java Virtual Machine Specification for more details. */
117 public int F_PUBLIC = Flags.AccPublic;
118 /** Private access flag. See The Java Virtual Machine Specification for more details. */
119 public int F_PRIVATE = Flags.AccPrivate;
120 /** Protected access flag. See The Java Virtual Machine Specification for more details. */
121 public int F_PROTECTED = Flags.AccProtected;
122 /** Static access flag. See The Java Virtual Machine Specification for more details. */
123 public int F_STATIC = Flags.AccStatic;
124 /** Final access flag. See The Java Virtual Machine Specification for more details. */
125 public int F_FINAL = Flags.AccFinal;
126 /** Abstract property flag. See The Java Virtual Machine Specification for more details. */
127 // public int F_ABSTRACT = Flags.AccAbstract;
129 private final static String PAGE_NAME = "NewTypeWizardPage"; //$NON-NLS-1$
131 /** Field ID of the package input field */
132 protected final static String PACKAGE = PAGE_NAME + ".package"; //$NON-NLS-1$
133 /** Field ID of the eclosing type input field */
134 protected final static String ENCLOSING = PAGE_NAME + ".enclosing"; //$NON-NLS-1$
135 /** Field ID of the enclosing type checkbox */
136 protected final static String ENCLOSINGSELECTION = ENCLOSING + ".selection"; //$NON-NLS-1$
137 /** Field ID of the type name input field */
138 protected final static String TYPENAME = PAGE_NAME + ".typename"; //$NON-NLS-1$
139 /** Field ID of the super type input field */
140 protected final static String SUPER = PAGE_NAME + ".superclass"; //$NON-NLS-1$
141 /** Field ID of the super interfaces input field */
142 protected final static String INTERFACES = PAGE_NAME + ".interfaces"; //$NON-NLS-1$
143 /** Field ID of the modifier checkboxes */
144 protected final static String MODIFIERS = PAGE_NAME + ".modifiers"; //$NON-NLS-1$
145 /** Field ID of the method stubs checkboxes */
146 protected final static String METHODS = PAGE_NAME + ".methods"; //$NON-NLS-1$
148 private class InterfacesListLabelProvider extends LabelProvider {
150 private Image fInterfaceImage;
152 public InterfacesListLabelProvider() {
154 fInterfaceImage = PHPUiImages.get(PHPUiImages.IMG_OBJS_INTERFACE);
157 public Image getImage(Object element) {
158 return fInterfaceImage;
162 private StringButtonStatusDialogField fPackageDialogField;
164 private SelectionButtonDialogField fEnclosingTypeSelection;
165 private StringButtonDialogField fEnclosingTypeDialogField;
167 private boolean fCanModifyPackage;
168 private boolean fCanModifyEnclosingType;
170 private IPackageFragment fCurrPackage;
172 // private IType fCurrEnclosingType;
173 private StringDialogField fTypeNameDialogField;
175 private StringButtonDialogField fSuperClassDialogField;
176 private ListDialogField fSuperInterfacesDialogField;
178 // private IType fSuperClass;
180 private SelectionButtonDialogFieldGroup fAccMdfButtons;
181 private SelectionButtonDialogFieldGroup fOtherMdfButtons;
183 private IType fCreatedType;
185 protected IStatus fEnclosingTypeStatus;
186 protected IStatus fPackageStatus;
187 protected IStatus fTypeNameStatus;
188 // protected IStatus fSuperClassStatus;
189 protected IStatus fModifierStatus;
190 // protected IStatus fSuperInterfacesStatus;
192 private boolean fIsClass;
193 private int fStaticMdfIndex;
195 private final int PUBLIC_INDEX = 0, DEFAULT_INDEX = 1, PRIVATE_INDEX = 2, PROTECTED_INDEX = 3;
196 private final int ABSTRACT_INDEX = 0, FINAL_INDEX = 1;
199 * Creates a new <code>NewTypeWizardPage</code>
201 * @param isClass <code>true</code> if a new class is to be created; otherwise
202 * an interface is to be created
203 * @param pageName the wizard page's name
205 public NewTypeWizardPage(boolean isClass, String pageName) {
211 TypeFieldsAdapter adapter = new TypeFieldsAdapter();
213 fPackageDialogField = new StringButtonStatusDialogField(adapter);
214 fPackageDialogField.setDialogFieldListener(adapter);
215 fPackageDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.package.label")); //$NON-NLS-1$
216 fPackageDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.package.button")); //$NON-NLS-1$
217 fPackageDialogField.setStatusWidthHint(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
219 fEnclosingTypeSelection = new SelectionButtonDialogField(SWT.CHECK);
220 fEnclosingTypeSelection.setDialogFieldListener(adapter);
221 fEnclosingTypeSelection.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.enclosing.selection.label")); //$NON-NLS-1$
223 fEnclosingTypeDialogField = new StringButtonDialogField(adapter);
224 fEnclosingTypeDialogField.setDialogFieldListener(adapter);
225 fEnclosingTypeDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.enclosing.button")); //$NON-NLS-1$
227 fTypeNameDialogField = new StringDialogField();
228 fTypeNameDialogField.setDialogFieldListener(adapter);
229 fTypeNameDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.typename.label")); //$NON-NLS-1$
231 fSuperClassDialogField = new StringButtonDialogField(adapter);
232 fSuperClassDialogField.setDialogFieldListener(adapter);
233 fSuperClassDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.superclass.label")); //$NON-NLS-1$
234 fSuperClassDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.superclass.button")); //$NON-NLS-1$
236 String[] addButtons = new String[] {
238 NewWizardMessages.getString("NewTypeWizardPage.interfaces.add"), //$NON-NLS-1$
242 NewWizardMessages.getString("NewTypeWizardPage.interfaces.remove") //$NON-NLS-1$
244 fSuperInterfacesDialogField = new ListDialogField(adapter, addButtons, new InterfacesListLabelProvider());
245 fSuperInterfacesDialogField.setDialogFieldListener(adapter);
246 String interfaceLabel = fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.interfaces.class.label") : NewWizardMessages.getString("NewTypeWizardPage.interfaces.ifc.label"); //$NON-NLS-1$ //$NON-NLS-2$
247 fSuperInterfacesDialogField.setLabelText(interfaceLabel);
248 fSuperInterfacesDialogField.setRemoveButtonIndex(2);
250 String[] buttonNames1 = new String[] {
251 /* 0 == PUBLIC_INDEX */
252 NewWizardMessages.getString("NewTypeWizardPage.modifiers.public"), //$NON-NLS-1$
253 /* 1 == DEFAULT_INDEX */
254 NewWizardMessages.getString("NewTypeWizardPage.modifiers.default"), //$NON-NLS-1$
255 /* 2 == PRIVATE_INDEX */
256 NewWizardMessages.getString("NewTypeWizardPage.modifiers.private"), //$NON-NLS-1$
257 /* 3 == PROTECTED_INDEX*/
258 NewWizardMessages.getString("NewTypeWizardPage.modifiers.protected") //$NON-NLS-1$
260 fAccMdfButtons = new SelectionButtonDialogFieldGroup(SWT.RADIO, buttonNames1, 4);
261 fAccMdfButtons.setDialogFieldListener(adapter);
262 fAccMdfButtons.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.modifiers.acc.label")); //$NON-NLS-1$
263 fAccMdfButtons.setSelection(0, true);
265 String[] buttonNames2;
267 buttonNames2 = new String[] {
268 /* 0 == ABSTRACT_INDEX */
269 NewWizardMessages.getString("NewTypeWizardPage.modifiers.abstract"), //$NON-NLS-1$
270 /* 1 == FINAL_INDEX */
271 NewWizardMessages.getString("NewTypeWizardPage.modifiers.final"), //$NON-NLS-1$
273 NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
275 fStaticMdfIndex = 2; // index of the static checkbox is 2
277 buttonNames2 = new String[] { NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
279 fStaticMdfIndex = 0; // index of the static checkbox is 0
282 fOtherMdfButtons = new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames2, 4);
283 fOtherMdfButtons.setDialogFieldListener(adapter);
285 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, false);
286 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, false);
287 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, false);
289 fPackageStatus = new StatusInfo();
290 fEnclosingTypeStatus = new StatusInfo();
292 fCanModifyPackage = true;
293 fCanModifyEnclosingType = true;
296 fTypeNameStatus = new StatusInfo();
297 // fSuperClassStatus= new StatusInfo();
298 // fSuperInterfacesStatus= new StatusInfo();
299 fModifierStatus = new StatusInfo();
303 * Initializes all fields provided by the page with a given selection.
305 * @param elem the selection used to intialize this page or <code>
306 * null</code> if no selection was available
308 protected void initTypePage(IJavaElement elem) {
309 String initSuperclass = "java.lang.Object"; //$NON-NLS-1$
310 ArrayList initSuperinterfaces = new ArrayList(5);
312 IPackageFragment pack = null;
313 IType enclosingType = null;
316 // evaluate the enclosing type
317 pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
318 IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
319 if (typeInCU != null) {
320 if (typeInCU.getCompilationUnit() != null) {
321 enclosingType = typeInCU;
324 ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
326 // enclosingType= cu.findPrimaryType();
332 // if (elem.getElementType() == IJavaElement.TYPE) {
333 // type= (IType)elem;
334 // if (type.exists()) {
335 // String superName= JavaModelUtil.getFullyQualifiedName(type);
336 // if (type.isInterface()) {
337 // initSuperinterfaces.add(superName);
339 // initSuperclass= superName;
343 // } catch (JavaModelException e) {
344 // PHPeclipsePlugin.log(e);
345 // // ignore this exception now
349 setPackageFragment(pack, true);
350 // setEnclosingType(enclosingType, true);
351 setEnclosingTypeSelection(false, true);
353 setTypeName("", true); //$NON-NLS-1$
354 setSuperClass(initSuperclass, true);
355 setSuperInterfaces(initSuperinterfaces, true);
358 // -------- UI Creation ---------
361 * Creates a separator line. Expects a <code>GridLayout</code> with at least 1 column.
363 * @param composite the parent composite
364 * @param nColumns number of columns to span
366 protected void createSeparator(Composite composite, int nColumns) {
367 (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(composite, nColumns, convertHeightInCharsToPixels(1));
371 * Creates the controls for the package name field. Expects a <code>GridLayout</code> with at
374 * @param composite the parent composite
375 * @param nColumns number of columns to span
377 protected void createPackageControls(Composite composite, int nColumns) {
378 fPackageDialogField.doFillIntoGrid(composite, nColumns);
379 LayoutUtil.setWidthHint(fPackageDialogField.getTextControl(null), getMaxFieldWidth());
380 LayoutUtil.setHorizontalGrabbing(fPackageDialogField.getTextControl(null));
384 * Creates the controls for the enclosing type name field. Expects a <code>GridLayout</code> with at
387 * @param composite the parent composite
388 * @param nColumns number of columns to span
390 protected void createEnclosingTypeControls(Composite composite, int nColumns) {
392 Composite tabGroup = new Composite(composite, SWT.NONE);
393 GridLayout layout = new GridLayout();
394 layout.marginWidth = 0;
395 layout.marginHeight = 0;
396 tabGroup.setLayout(layout);
398 fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1);
400 Control c = fEnclosingTypeDialogField.getTextControl(composite);
401 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
402 gd.widthHint = getMaxFieldWidth();
403 gd.horizontalSpan = 2;
406 Button button = fEnclosingTypeDialogField.getChangeControl(composite);
407 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
408 gd.heightHint = SWTUtil.getButtonHeightHint(button);
409 gd.widthHint = SWTUtil.getButtonWidthHint(button);
410 button.setLayoutData(gd);
414 * Creates the controls for the type name field. Expects a <code>GridLayout</code> with at
417 * @param composite the parent composite
418 * @param nColumns number of columns to span
420 protected void createTypeNameControls(Composite composite, int nColumns) {
421 fTypeNameDialogField.doFillIntoGrid(composite, nColumns - 1);
422 DialogField.createEmptySpace(composite);
424 LayoutUtil.setWidthHint(fTypeNameDialogField.getTextControl(null), getMaxFieldWidth());
428 * Creates the controls for the modifiers radio/ceckbox buttons. Expects a
429 * <code>GridLayout</code> with at least 3 columns.
431 * @param composite the parent composite
432 * @param nColumns number of columns to span
434 protected void createModifierControls(Composite composite, int nColumns) {
435 LayoutUtil.setHorizontalSpan(fAccMdfButtons.getLabelControl(composite), 1);
437 Control control = fAccMdfButtons.getSelectionButtonsGroup(composite);
438 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
439 gd.horizontalSpan = nColumns - 2;
440 control.setLayoutData(gd);
442 DialogField.createEmptySpace(composite);
444 DialogField.createEmptySpace(composite);
446 control = fOtherMdfButtons.getSelectionButtonsGroup(composite);
447 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
448 gd.horizontalSpan = nColumns - 2;
449 control.setLayoutData(gd);
451 DialogField.createEmptySpace(composite);
455 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code>
456 * with at least 3 columns.
458 * @param composite the parent composite
459 * @param nColumns number of columns to span
461 protected void createSuperClassControls(Composite composite, int nColumns) {
462 fSuperClassDialogField.doFillIntoGrid(composite, nColumns);
463 LayoutUtil.setWidthHint(fSuperClassDialogField.getTextControl(null), getMaxFieldWidth());
467 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code> with
468 * at least 3 columns.
470 * @param composite the parent composite
471 * @param nColumns number of columns to span
473 protected void createSuperInterfacesControls(Composite composite, int nColumns) {
474 fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns);
475 GridData gd = (GridData) fSuperInterfacesDialogField.getListControl(null).getLayoutData();
477 gd.heightHint = convertHeightInCharsToPixels(3);
479 gd.heightHint = convertHeightInCharsToPixels(6);
481 gd.grabExcessVerticalSpace = false;
482 gd.widthHint = getMaxFieldWidth();
486 * Sets the focus on the type name input field.
488 protected void setFocus() {
489 fTypeNameDialogField.setFocus();
492 // -------- TypeFieldsAdapter --------
494 private class TypeFieldsAdapter implements IStringButtonAdapter, IDialogFieldListener, IListAdapter {
496 // -------- IStringButtonAdapter
497 public void changeControlPressed(DialogField field) {
498 // typePageChangeControlPressed(field);
501 // -------- IListAdapter
502 public void customButtonPressed(ListDialogField field, int index) {
503 // typePageCustomButtonPressed(field, index);
506 public void selectionChanged(ListDialogField field) {
509 // -------- IDialogFieldListener
510 public void dialogFieldChanged(DialogField field) {
511 typePageDialogFieldChanged(field);
514 public void doubleClicked(ListDialogField field) {
518 // private void typePageChangeControlPressed(DialogField field) {
519 // if (field == fPackageDialogField) {
520 // IPackageFragment pack= choosePackage();
521 // if (pack != null) {
522 // fPackageDialogField.setText(pack.getElementName());
524 // } else if (field == fEnclosingTypeDialogField) {
525 // IType type= chooseEnclosingType();
526 // if (type != null) {
527 // fEnclosingTypeDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
529 // } else if (field == fSuperClassDialogField) {
530 // IType type= chooseSuperType();
531 // if (type != null) {
532 // fSuperClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
537 // private void typePageCustomButtonPressed(DialogField field, int index) {
538 // if (field == fSuperInterfacesDialogField) {
539 // chooseSuperInterfaces();
544 * A field on the type has changed. The fields' status and all dependend
545 * status are updated.
547 private void typePageDialogFieldChanged(DialogField field) {
548 String fieldName = null;
549 if (field == fPackageDialogField) {
550 fPackageStatus = packageChanged();
551 updatePackageStatusLabel();
552 fTypeNameStatus = typeNameChanged();
553 // fSuperClassStatus= superClassChanged();
555 } else if (field == fEnclosingTypeDialogField) {
556 // fEnclosingTypeStatus= enclosingTypeChanged();
557 fTypeNameStatus = typeNameChanged();
558 // fSuperClassStatus= superClassChanged();
559 fieldName = ENCLOSING;
560 } else if (field == fEnclosingTypeSelection) {
562 boolean isEnclosedType = isEnclosingTypeSelected();
563 if (!isEnclosedType) {
564 if (fAccMdfButtons.isSelected(PRIVATE_INDEX) || fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
565 fAccMdfButtons.setSelection(PRIVATE_INDEX, false);
566 fAccMdfButtons.setSelection(PROTECTED_INDEX, false);
567 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
569 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
570 fOtherMdfButtons.setSelection(fStaticMdfIndex, false);
573 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, isEnclosedType && fIsClass);
574 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, isEnclosedType && fIsClass);
575 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, isEnclosedType);
576 fTypeNameStatus = typeNameChanged();
577 // fSuperClassStatus= superClassChanged();
578 fieldName = ENCLOSINGSELECTION;
579 } else if (field == fTypeNameDialogField) {
580 fTypeNameStatus = typeNameChanged();
581 fieldName = TYPENAME;
582 } else if (field == fSuperClassDialogField) {
583 // fSuperClassStatus= superClassChanged();
585 } else if (field == fSuperInterfacesDialogField) {
586 // fSuperInterfacesStatus= superInterfacesChanged();
587 fieldName = INTERFACES;
588 } else if (field == fOtherMdfButtons) {
589 fModifierStatus = modifiersChanged();
590 fieldName = MODIFIERS;
595 handleFieldChanged(fieldName);
598 // -------- update message ----------------
601 * @see org.eclipse.jdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
603 protected void handleFieldChanged(String fieldName) {
604 super.handleFieldChanged(fieldName);
605 if (fieldName == CONTAINER) {
606 fPackageStatus = packageChanged();
607 // fEnclosingTypeStatus= enclosingTypeChanged();
608 fTypeNameStatus = typeNameChanged();
609 // fSuperClassStatus= superClassChanged();
610 // fSuperInterfacesStatus= superInterfacesChanged();
614 // ---- set / get ----------------
617 * Returns the text of the package input field.
619 * @return the text of the package input field
621 public String getPackageText() {
622 return fPackageDialogField.getText();
626 * Returns the text of the enclosing type input field.
628 * @return the text of the enclosing type input field
630 public String getEnclosingTypeText() {
631 return fEnclosingTypeDialogField.getText();
635 * Returns the package fragment corresponding to the current input.
637 * @return a package fragement or <code>null</code> if the input
638 * could not be resolved.
640 public IPackageFragment getPackageFragment() {
641 if (!isEnclosingTypeSelected()) {
644 // if (fCurrEnclosingType != null) {
645 // return fCurrEnclosingType.getPackageFragment();
652 * Sets the package fragment to the given value. The method updates the model
653 * and the text of the control.
655 * @param pack the package fragement to be set
656 * @param canBeModified if <code>true</code> the package fragment is
657 * editable; otherwise it is read-only.
659 public void setPackageFragment(IPackageFragment pack, boolean canBeModified) {
661 fCanModifyPackage = canBeModified;
662 String str = (pack == null) ? "" : pack.getElementName(); //$NON-NLS-1$
663 fPackageDialogField.setText(str);
668 * Returns the enclosing type corresponding to the current input.
670 * @return the enclosing type or <code>null</code> if the enclosing type is
671 * not selected or the input could not be resolved
673 public IType getEnclosingType() {
674 // if (isEnclosingTypeSelected()) {
675 // return fCurrEnclosingType;
681 * Sets the enclosing type. The method updates the underlying model
682 * and the text of the control.
684 * @param type the enclosing type
685 * @param canBeModified if <code>true</code> the enclosing type field is
686 * editable; otherwise it is read-only.
688 // public void setEnclosingType(IType type, boolean canBeModified) {
689 // fCurrEnclosingType= type;
690 // fCanModifyEnclosingType= canBeModified;
691 // String str= (type == null) ? "" : JavaModelUtil.getFullyQualifiedName(type); //$NON-NLS-1$
692 // fEnclosingTypeDialogField.setText(str);
693 // updateEnableState();
697 * Returns the selection state of the enclosing type checkbox.
699 * @return the seleciton state of the enclosing type checkbox
701 public boolean isEnclosingTypeSelected() {
702 return fEnclosingTypeSelection.isSelected();
706 * Sets the enclosing type checkbox's selection state.
708 * @param isSelected the checkbox's selection state
709 * @param canBeModified if <code>true</code> the enclosing type checkbox is
710 * modifiable; otherwise it is read-only.
712 public void setEnclosingTypeSelection(boolean isSelected, boolean canBeModified) {
713 fEnclosingTypeSelection.setSelection(isSelected);
714 fEnclosingTypeSelection.setEnabled(canBeModified);
719 * Returns the type name entered into the type input field.
721 * @return the type name
723 public String getTypeName() {
724 return fTypeNameDialogField.getText();
728 * Sets the type name input field's text to the given value. Method doesn't update
731 * @param name the new type name
732 * @param canBeModified if <code>true</code> the enclosing type name field is
733 * editable; otherwise it is read-only.
735 public void setTypeName(String name, boolean canBeModified) {
736 fTypeNameDialogField.setText(name);
737 fTypeNameDialogField.setEnabled(canBeModified);
741 * Returns the selected modifiers.
743 * @return the selected modifiers
746 public int getModifiers() {
748 if (fAccMdfButtons.isSelected(PUBLIC_INDEX)) {
750 } else if (fAccMdfButtons.isSelected(PRIVATE_INDEX)) {
752 } else if (fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
755 // if (fOtherMdfButtons.isSelected(ABSTRACT_INDEX) && (fStaticMdfIndex != 0)) {
758 if (fOtherMdfButtons.isSelected(FINAL_INDEX)) {
761 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
768 * Sets the modifiers.
770 * @param modifiers <code>F_PUBLIC</code>, <code>F_PRIVATE</code>,
771 * <code>F_PROTECTED</code>, <code>F_ABSTRACT, F_FINAL</code>
772 * or <code>F_STATIC</code> or, a valid combination.
773 * @param canBeModified if <code>true</code> the modifier fields are
774 * editable; otherwise they are read-only
777 public void setModifiers(int modifiers, boolean canBeModified) {
778 if (Flags.isPublic(modifiers)) {
779 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
780 } else if (Flags.isPrivate(modifiers)) {
781 fAccMdfButtons.setSelection(PRIVATE_INDEX, true);
782 } else if (Flags.isProtected(modifiers)) {
783 fAccMdfButtons.setSelection(PROTECTED_INDEX, true);
785 fAccMdfButtons.setSelection(DEFAULT_INDEX, true);
787 // if (Flags.isAbstract(modifiers)) {
788 // fOtherMdfButtons.setSelection(ABSTRACT_INDEX, true);
790 if (Flags.isFinal(modifiers)) {
791 fOtherMdfButtons.setSelection(FINAL_INDEX, true);
793 if (Flags.isStatic(modifiers)) {
794 fOtherMdfButtons.setSelection(fStaticMdfIndex, true);
797 fAccMdfButtons.setEnabled(canBeModified);
798 fOtherMdfButtons.setEnabled(canBeModified);
802 * Returns the content of the superclass input field.
804 * @return the superclass name
806 public String getSuperClass() {
807 return fSuperClassDialogField.getText();
811 * Sets the super class name.
813 * @param name the new superclass name
814 * @param canBeModified if <code>true</code> the superclass name field is
815 * editable; otherwise it is read-only.
817 public void setSuperClass(String name, boolean canBeModified) {
818 fSuperClassDialogField.setText(name);
819 fSuperClassDialogField.setEnabled(canBeModified);
823 * Returns the chosen super interfaces.
825 * @return a list of chosen super interfaces. The list's elements
826 * are of type <code>String</code>
828 public List getSuperInterfaces() {
829 return fSuperInterfacesDialogField.getElements();
833 * Sets the super interfaces.
835 * @param interfacesNames a list of super interface. The method requires that
836 * the list's elements are of type <code>String</code>
837 * @param canBeModified if <code>true</code> the super interface field is
838 * editable; otherwise it is read-only.
840 public void setSuperInterfaces(List interfacesNames, boolean canBeModified) {
841 fSuperInterfacesDialogField.setElements(interfacesNames);
842 fSuperInterfacesDialogField.setEnabled(canBeModified);
845 // ----------- validation ----------
848 * A hook method that gets called when the package field has changed. The method
849 * validates the package name and returns the status of the validation. The validation
850 * also updates the package fragment model.
852 * Subclasses may extend this method to perform their own validation.
855 * @return the status of the validation
857 protected IStatus packageChanged() {
858 StatusInfo status = new StatusInfo();
859 fPackageDialogField.enableButton(getPackageFragmentRoot() != null);
861 // String packName= getPackageText();
862 // if (packName.length() > 0) {
863 // IStatus val= JavaConventions.validatePackageName(packName);
864 // if (val.getSeverity() == IStatus.ERROR) {
865 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidPackageName", val.getMessage())); //$NON-NLS-1$
867 // } else if (val.getSeverity() == IStatus.WARNING) {
868 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.DiscouragedPackageName", val.getMessage())); //$NON-NLS-1$
873 // IPackageFragmentRoot root= getPackageFragmentRoot();
874 // if (root != null) {
875 // if (root.getJavaProject().exists() && packName.length() > 0) {
877 // IPath rootPath= root.getPath();
878 // IPath outputPath= root.getJavaProject().getOutputLocation();
879 // if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
880 // // if the bin folder is inside of our root, dont allow to name a package
881 // // like the bin folder
882 // IPath packagePath= rootPath.append(packName.replace('.', '/'));
883 // if (outputPath.isPrefixOf(packagePath)) {
884 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ClashOutputLocation")); //$NON-NLS-1$
888 // } catch (JavaModelException e) {
889 // PHPeclipsePlugin.log(e);
894 // fCurrPackage= root.getPackageFragment(packName);
896 // status.setError(""); //$NON-NLS-1$
902 * Updates the 'default' label next to the package field.
904 private void updatePackageStatusLabel() {
905 String packName = getPackageText();
907 if (packName.length() == 0) {
908 fPackageDialogField.setStatus(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
910 fPackageDialogField.setStatus(""); //$NON-NLS-1$
915 * Updates the enable state of buttons related to the enclosing type selection checkbox.
917 private void updateEnableState() {
918 boolean enclosing = isEnclosingTypeSelected();
919 fPackageDialogField.setEnabled(fCanModifyPackage && !enclosing);
920 fEnclosingTypeDialogField.setEnabled(fCanModifyEnclosingType && enclosing);
924 * Hook method that gets called when the enclosing type name has changed. The method
925 * validates the enclosing type and returns the status of the validation. It also updates the
926 * enclosing type model.
928 * Subclasses may extend this method to perform their own validation.
931 * @return the status of the validation
933 // protected IStatus enclosingTypeChanged() {
934 // StatusInfo status= new StatusInfo();
935 // fCurrEnclosingType= null;
937 // IPackageFragmentRoot root= getPackageFragmentRoot();
939 // fEnclosingTypeDialogField.enableButton(root != null);
940 // if (root == null) {
941 // status.setError(""); //$NON-NLS-1$
945 // String enclName= getEnclosingTypeText();
946 // if (enclName.length() == 0) {
947 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeEnterName")); //$NON-NLS-1$
951 // IType type= findType(root.getJavaProject(), enclName);
952 // if (type == null) {
953 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
957 // if (type.getCompilationUnit() == null) {
958 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotInCU")); //$NON-NLS-1$
961 // if (!JavaModelUtil.isEditable(type.getCompilationUnit())) {
962 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotEditable")); //$NON-NLS-1$
966 // fCurrEnclosingType= type;
967 // IPackageFragmentRoot enclosingRoot= JavaModelUtil.getPackageFragmentRoot(type);
968 // if (!enclosingRoot.equals(root)) {
969 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.EnclosingNotInSourceFolder")); //$NON-NLS-1$
972 // } catch (JavaModelException e) {
973 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
974 // PHPeclipsePlugin.log(e);
980 * Hook method that gets called when the type name has changed. The method validates the
981 * type name and returns the status of the validation.
983 * Subclasses may extend this method to perform their own validation.
986 * @return the status of the validation
988 protected IStatus typeNameChanged() {
989 StatusInfo status = new StatusInfo();
990 String typeName = getTypeName();
992 if (typeName.length() == 0) {
993 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnterTypeName")); //$NON-NLS-1$
996 if (typeName.indexOf('.') != -1) {
997 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.QualifiedName")); //$NON-NLS-1$
1000 // IStatus val= JavaConventions.validateJavaTypeName(typeName);
1001 // if (val.getSeverity() == IStatus.ERROR) {
1002 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidTypeName", val.getMessage())); //$NON-NLS-1$
1004 // } else if (val.getSeverity() == IStatus.WARNING) {
1005 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.TypeNameDiscouraged", val.getMessage())); //$NON-NLS-1$
1006 // // continue checking
1010 if (!isEnclosingTypeSelected()) {
1011 IPackageFragment pack = getPackageFragment();
1013 ICompilationUnit cu = pack.getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
1014 if (cu.getResource().exists()) {
1015 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1020 IType type = getEnclosingType();
1022 IType member = type.getType(typeName);
1023 if (member.exists()) {
1024 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1033 * Hook method that gets called when the superclass name has changed. The method
1034 * validates the superclass name and returns the status of the validation.
1036 * Subclasses may extend this method to perform their own validation.
1039 * @return the status of the validation
1041 // protected IStatus superClassChanged() {
1042 // StatusInfo status= new StatusInfo();
1043 // IPackageFragmentRoot root= getPackageFragmentRoot();
1044 // fSuperClassDialogField.enableButton(root != null);
1046 // fSuperClass= null;
1048 // String sclassName= getSuperClass();
1049 // if (sclassName.length() == 0) {
1050 // // accept the empty field (stands for java.lang.Object)
1053 // IStatus val= JavaConventions.validateJavaTypeName(sclassName);
1054 // if (val.getSeverity() == IStatus.ERROR) {
1055 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1058 // if (root != null) {
1060 // IType type= resolveSuperTypeName(root.getJavaProject(), sclassName);
1061 // if (type == null) {
1062 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.SuperClassNotExists")); //$NON-NLS-1$
1065 // if (type.isInterface()) {
1066 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotClass", sclassName)); //$NON-NLS-1$
1069 // int flags= type.getFlags();
1070 // if (Flags.isFinal(flags)) {
1071 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsFinal", sclassName)); //$NON-NLS-1$
1073 // } else if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1074 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotVisible", sclassName)); //$NON-NLS-1$
1078 // fSuperClass= type;
1079 // } catch (JavaModelException e) {
1080 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1081 // PHPeclipsePlugin.log(e);
1084 // status.setError(""); //$NON-NLS-1$
1090 // private IType resolveSuperTypeName(IJavaProject jproject, String sclassName) throws JavaModelException {
1091 // if (!jproject.exists()) {
1094 // IType type= null;
1095 // if (isEnclosingTypeSelected()) {
1096 // // search in the context of the enclosing type
1097 // IType enclosingType= getEnclosingType();
1098 // if (enclosingType != null) {
1099 // String[][] res= enclosingType.resolveType(sclassName);
1100 // if (res != null && res.length > 0) {
1101 // type= jproject.findType(res[0][0], res[0][1]);
1105 // IPackageFragment currPack= getPackageFragment();
1106 // if (type == null && currPack != null) {
1107 // String packName= currPack.getElementName();
1108 // // search in own package
1109 // if (!currPack.isDefaultPackage()) {
1110 // type= jproject.findType(packName, sclassName);
1112 // // search in java.lang
1113 // if (type == null && !"java.lang".equals(packName)) { //$NON-NLS-1$
1114 // type= jproject.findType("java.lang", sclassName); //$NON-NLS-1$
1117 // // search fully qualified
1118 // if (type == null) {
1119 // type= jproject.findType(sclassName);
1125 // private IType findType(IJavaProject project, String typeName) throws JavaModelException {
1126 // if (project.exists()) {
1127 // return project.findType(typeName);
1133 * Hook method that gets called when the list of super interface has changed. The method
1134 * validates the superinterfaces and returns the status of the validation.
1136 * Subclasses may extend this method to perform their own validation.
1139 * @return the status of the validation
1141 // protected IStatus superInterfacesChanged() {
1142 // StatusInfo status= new StatusInfo();
1144 // IPackageFragmentRoot root= getPackageFragmentRoot();
1145 // fSuperInterfacesDialogField.enableButton(0, root != null);
1147 // if (root != null) {
1148 // List elements= fSuperInterfacesDialogField.getElements();
1149 // int nElements= elements.size();
1150 // for (int i= 0; i < nElements; i++) {
1151 // String intfname= (String)elements.get(i);
1153 // IType type= findType(root.getJavaProject(), intfname);
1154 // if (type == null) {
1155 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceNotExists", intfname)); //$NON-NLS-1$
1158 // if (type.isClass()) {
1159 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotInterface", intfname)); //$NON-NLS-1$
1162 // if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1163 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotVisible", intfname)); //$NON-NLS-1$
1167 // } catch (JavaModelException e) {
1168 // PHPeclipsePlugin.log(e);
1169 // // let pass, checking is an extra
1177 * Hook method that gets called when the modifiers have changed. The method validates
1178 * the modifiers and returns the status of the validation.
1180 * Subclasses may extend this method to perform their own validation.
1183 * @return the status of the validation
1185 protected IStatus modifiersChanged() {
1186 StatusInfo status = new StatusInfo();
1187 int modifiers = getModifiers();
1188 // if (Flags.isFinal(modifiers) && Flags.isAbstract(modifiers)) {
1189 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ModifiersFinalAndAbstract")); //$NON-NLS-1$
1194 // selection dialogs
1196 // private IPackageFragment choosePackage() {
1197 // IPackageFragmentRoot froot= getPackageFragmentRoot();
1198 // IJavaElement[] packages= null;
1200 // if (froot != null && froot.exists()) {
1201 // packages= froot.getChildren();
1203 // } catch (JavaModelException e) {
1204 // PHPeclipsePlugin.log(e);
1206 // if (packages == null) {
1207 // packages= new IJavaElement[0];
1210 // ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
1211 // dialog.setIgnoreCase(false);
1212 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.title")); //$NON-NLS-1$
1213 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.description")); //$NON-NLS-1$
1214 // dialog.setEmptyListMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.empty")); //$NON-NLS-1$
1215 // dialog.setElements(packages);
1216 // IPackageFragment pack= getPackageFragment();
1217 // if (pack != null) {
1218 // dialog.setInitialSelections(new Object[] { pack });
1221 // if (dialog.open() == ElementListSelectionDialog.OK) {
1222 // return (IPackageFragment) dialog.getFirstResult();
1227 // private IType chooseEnclosingType() {
1228 // IPackageFragmentRoot root= getPackageFragmentRoot();
1229 // if (root == null) {
1233 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { root });
1235 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.TYPE, scope);
1236 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.title")); //$NON-NLS-1$
1237 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.description")); //$NON-NLS-1$
1238 // dialog.setFilter(Signature.getSimpleName(getEnclosingTypeText()));
1240 // if (dialog.open() == TypeSelectionDialog.OK) {
1241 // return (IType) dialog.getFirstResult();
1246 // private IType chooseSuperType() {
1247 // IPackageFragmentRoot root= getPackageFragmentRoot();
1248 // if (root == null) {
1252 // IJavaElement[] elements= new IJavaElement[] { root.getJavaProject() };
1253 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
1255 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.CLASS, scope);
1256 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.title")); //$NON-NLS-1$
1257 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.message")); //$NON-NLS-1$
1258 // dialog.setFilter(getSuperClass());
1260 // if (dialog.open() == TypeSelectionDialog.OK) {
1261 // return (IType) dialog.getFirstResult();
1266 // private void chooseSuperInterfaces() {
1267 // IPackageFragmentRoot root= getPackageFragmentRoot();
1268 // if (root == null) {
1272 // IJavaProject project= root.getJavaProject();
1273 // SuperInterfaceSelectionDialog dialog= new SuperInterfaceSelectionDialog(getShell(), getWizard().getContainer(), fSuperInterfacesDialogField, project);
1274 // dialog.setTitle(fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.class.title") : NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.interface.title")); //$NON-NLS-1$ //$NON-NLS-2$
1275 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.message")); //$NON-NLS-1$
1280 // ---- creation ----------------
1283 * Creates the new type using the entered field values.
1285 * @param monitor a progress monitor to report progress.
1287 public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException {
1288 if (monitor == null) {
1289 monitor = new NullProgressMonitor();
1292 monitor.beginTask(NewWizardMessages.getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$
1293 ICompilationUnit createdWorkingCopy = null;
1295 // IPackageFragmentRoot root = getPackageFragmentRoot();
1296 // IPackageFragment pack = getPackageFragment();
1297 // if (pack == null) {
1298 // pack = root.getPackageFragment(""); //$NON-NLS-1$
1301 // if (!pack.exists()) {
1302 // String packName = pack.getElementName();
1303 // pack = root.createPackageFragment(packName, true, null);
1308 String clName = getTypeName();
1310 boolean isInnerClass = isEnclosingTypeSelected();
1313 // ImportsStructure imports;
1316 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
1317 // String[] prefOrder = JavaPreferencesSettings.getImportOrderPreference(store);
1318 // int threshold = JavaPreferencesSettings.getImportNumberThreshold(store);
1320 String lineDelimiter = null;
1321 // if (!isInnerClass) {
1322 lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1324 // ICompilationUnit parentCU = pack.createCompilationUnit(clName + ".php", "", false, new SubProgressMonitor(monitor, 2)); //$NON-NLS-1$ //$NON-NLS-2$
1325 // createdWorkingCopy = (ICompilationUnit) parentCU.getSharedWorkingCopy(null, JavaUI.getBufferFactory(), null);
1327 // imports = new ImportsStructure(createdWorkingCopy, prefOrder, threshold, false);
1328 // // add an import that will be removed again. Having this import solves 14661
1329 // imports.addImport(pack.getElementName(), getTypeName());
1331 // String typeContent = constructTypeStub(new ImportsManager(imports), lineDelimiter);
1333 // String cuContent = constructCUContent(parentCU, typeContent, lineDelimiter);
1335 // createdWorkingCopy.getBuffer().setContents(cuContent);
1337 createdType = createdWorkingCopy.getType(clName);
1339 // IType enclosingType = getEnclosingType();
1341 // // if we are working on a enclosed type that is open in an editor,
1342 // // then replace the enclosing type with its working copy
1343 // IType workingCopy = (IType) EditorUtility.getWorkingCopy(enclosingType);
1344 // if (workingCopy != null) {
1345 // enclosingType = workingCopy;
1348 // ICompilationUnit parentCU = enclosingType.getCompilationUnit();
1349 // imports = new ImportsStructure(parentCU, prefOrder, threshold, true);
1351 // // add imports that will be removed again. Having the imports solves 14661
1352 // IType[] topLevelTypes = parentCU.getTypes();
1353 // for (int i = 0; i < topLevelTypes.length; i++) {
1354 // imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
1357 // lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
1358 // StringBuffer content = new StringBuffer();
1359 // String comment = getTypeComment(parentCU);
1360 // if (comment != null) {
1361 // content.append(comment);
1362 // content.append(lineDelimiter);
1364 // content.append(constructTypeStub(new ImportsManager(imports), lineDelimiter));
1365 // IJavaElement[] elems = enclosingType.getChildren();
1366 // IJavaElement sibling = elems.length > 0 ? elems[0] : null;
1368 // createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitor, 1));
1370 // indent = StubUtility.getIndentUsed(enclosingType) + 1;
1373 // // add imports for superclass/interfaces, so types can be resolved correctly
1374 // imports.create(false, new SubProgressMonitor(monitor, 1));
1376 ICompilationUnit cu = createdType.getCompilationUnit();
1380 // createTypeMembers(createdType, new ImportsManager(imports), new SubProgressMonitor(monitor, 1));
1383 // imports.create(false, new SubProgressMonitor(monitor, 1));
1388 ISourceRange range = createdType.getSourceRange();
1390 IBuffer buf = cu.getBuffer();
1391 String originalContent = buf.getText(range.getOffset(), range.getLength());
1392 String formattedContent = StubUtility.codeFormat(originalContent, indent, lineDelimiter);
1393 buf.replace(range.getOffset(), range.getLength(), formattedContent);
1394 if (!isInnerClass) {
1395 String fileComment = getFileComment(cu);
1396 if (fileComment != null && fileComment.length() > 0) {
1397 buf.replace(0, 0, fileComment + lineDelimiter);
1399 cu.commit(false, new SubProgressMonitor(monitor, 1));
1403 fCreatedType = createdType;
1405 if (createdWorkingCopy != null) {
1406 createdWorkingCopy.destroy();
1413 * Uses the New Java file template from the code template page to generate a
1414 * compilation unit with the given type content.
1415 * @param cu The new created compilation unit
1416 * @param typeContent The content of the type, including signature and type
1418 * @param lineDelimiter The line delimiter to be used.
1419 * @return String Returns the result of evaluating the new file template
1420 * with the given type content.
1421 * @throws CoreException
1424 // protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
1425 // StringBuffer typeQualifiedName= new StringBuffer();
1426 // if (isEnclosingTypeSelected()) {
1427 // typeQualifiedName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1429 // typeQualifiedName.append(getTypeName());
1430 // String typeComment= CodeGeneration.getTypeComment(cu, typeQualifiedName.toString(), lineDelimiter);
1431 // IPackageFragment pack= (IPackageFragment) cu.getParent();
1432 // String content= CodeGeneration.getCompilationUnitContent(cu, typeComment, typeContent, lineDelimiter);
1433 // if (content != null) {
1434 // CompilationUnit unit= AST.parseCompilationUnit(content.toCharArray());
1435 // if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
1439 // StringBuffer buf= new StringBuffer();
1440 // if (!pack.isDefaultPackage()) {
1441 // buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
1443 // buf.append(lineDelimiter).append(lineDelimiter);
1444 // if (typeComment != null) {
1445 // buf.append(typeComment).append(lineDelimiter);
1447 // buf.append(typeContent);
1448 // return buf.toString();
1452 * Returns the created type. The method only returns a valid type
1453 * after <code>createType</code> has been called.
1455 * @return the created type
1456 * @see #createType(IProgressMonitor)
1458 public IType getCreatedType() {
1459 return fCreatedType;
1462 // ---- construct cu body----------------
1464 // private void writeSuperClass(StringBuffer buf, ImportsManager imports) {
1465 // String typename= getSuperClass();
1466 // if (fIsClass && typename.length() > 0 && !"java.lang.Object".equals(typename)) { //$NON-NLS-1$
1467 // buf.append(" extends "); //$NON-NLS-1$
1469 // String qualifiedName= fSuperClass != null ? JavaModelUtil.getFullyQualifiedName(fSuperClass) : typename;
1470 // buf.append(imports.addImport(qualifiedName));
1474 // private void writeSuperInterfaces(StringBuffer buf, ImportsManager imports) {
1475 // List interfaces= getSuperInterfaces();
1476 // int last= interfaces.size() - 1;
1479 // buf.append(" implements "); //$NON-NLS-1$
1481 // buf.append(" extends "); //$NON-NLS-1$
1483 // for (int i= 0; i <= last; i++) {
1484 // String typename= (String) interfaces.get(i);
1485 // buf.append(imports.addImport(typename));
1494 * Called from createType to construct the source for this type
1496 // private String constructTypeStub(ImportsManager imports, String lineDelimiter) {
1497 // StringBuffer buf= new StringBuffer();
1499 // int modifiers= getModifiers();
1500 // buf.append(Flags.toString(modifiers));
1501 // if (modifiers != 0) {
1504 // buf.append(fIsClass ? "class " : "interface "); //$NON-NLS-2$ //$NON-NLS-1$
1505 // buf.append(getTypeName());
1506 // writeSuperClass(buf, imports);
1507 // writeSuperInterfaces(buf, imports);
1509 // buf.append(lineDelimiter);
1510 // buf.append(lineDelimiter);
1512 // buf.append(lineDelimiter);
1513 // return buf.toString();
1517 * @deprecated Overwrite createTypeMembers(IType, IImportsManager, IProgressMonitor) instead
1519 // protected void createTypeMembers(IType newType, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1524 * Hook method that gets called from <code>createType</code> to support adding of
1525 * unanticipated methods, fields, and inner types to the created type.
1527 * Implementers can use any methods defined on <code>IType</code> to manipulate the
1531 * The source code of the new type will be formtted using the platform's formatter. Needed
1532 * imports are added by the wizard at the end of the type creation process using the given
1536 * @param newType the new type created via <code>createType</code>
1537 * @param imports an import manager which can be used to add new imports
1538 * @param monitor a progress monitor to report progress. Must not be <code>null</code>
1540 * @see #createType(IProgressMonitor)
1542 // protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1543 // // call for compatibility
1544 // createTypeMembers(newType, ((ImportsManager)imports).getImportsStructure(), monitor);
1546 // // default implementation does nothing
1547 // // example would be
1548 // // String mainMathod= "public void foo(Vector vec) {}"
1549 // // createdType.createMethod(main, null, false, null);
1550 // // imports.addImport("java.lang.Vector");
1554 * @deprecated Instead of file templates, the new type code template
1555 * specifies the stub for a compilation unit.
1557 protected String getFileComment(ICompilationUnit parentCU) {
1561 private boolean isValidComment(String template) {
1562 IScanner scanner = ToolFactory.createScanner(true, false, false); //, false);
1563 scanner.setSource(template.toCharArray());
1565 int next = scanner.getNextToken();
1566 while (next == ITerminalSymbols.TokenNameCOMMENT_LINE
1567 || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
1568 || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
1569 next = scanner.getNextToken();
1571 return next == ITerminalSymbols.TokenNameEOF;
1572 } catch (InvalidInputException e) {
1578 * Hook method that gets called from <code>createType</code> to retrieve
1579 * a type comment. This default implementation returns the content of the
1580 * 'typecomment' template.
1582 * @return the type comment or <code>null</code> if a type comment
1585 // protected String getTypeComment(ICompilationUnit parentCU) {
1586 // if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_ADD_COMMENTS)) {
1588 // StringBuffer typeName= new StringBuffer();
1589 // if (isEnclosingTypeSelected()) {
1590 // typeName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1592 // typeName.append(getTypeName());
1593 // String comment= CodeGeneration.getTypeComment(parentCU, typeName.toString(), String.valueOf('\n'));
1594 // if (comment != null && isValidComment(comment)) {
1597 // } catch (CoreException e) {
1598 // PHPeclipsePlugin.log(e);
1605 * @deprecated Use getTemplate(String,ICompilationUnit,int)
1607 protected String getTemplate(String name, ICompilationUnit parentCU) {
1608 return getTemplate(name, parentCU, 0);
1612 * Returns the string resulting from evaluation the given template in
1613 * the context of the given compilation unit. This accesses the normal
1614 * template page, not the code templates. To use code templates use
1615 * <code>constructCUContent</code> to construct a compilation unit stub or
1616 * getTypeComment for the comment of the type.
1618 * @param name the template to be evaluated
1619 * @param parentCU the templates evaluation context
1620 * @param pos a source offset into the parent compilation unit. The
1621 * template is evalutated at the given source offset
1623 protected String getTemplate(String name, ICompilationUnit parentCU, int pos) {
1625 Template[] templates = Templates.getInstance().getTemplates(name);
1626 if (templates.length > 0) {
1627 return JavaContext.evaluateTemplate(templates[0], parentCU, pos);
1629 } catch (CoreException e) {
1630 PHPeclipsePlugin.log(e);
1631 } catch (BadLocationException e1) {
1632 // TODO Auto-generated catch block
1633 e1.printStackTrace();
1634 } catch (TemplateException e1) {
1635 // TODO Auto-generated catch block
1636 e1.printStackTrace();
1642 * @deprecated Use createInheritedMethods(IType,boolean,boolean,IImportsManager,IProgressMonitor)
1644 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1645 // return createInheritedMethods(type, doConstructors, doUnimplementedMethods, new ImportsManager(imports), monitor);
1649 * Creates the bodies of all unimplemented methods and constructors and adds them to the type.
1650 * Method is typically called by implementers of <code>NewTypeWizardPage</code> to add
1651 * needed method and constructors.
1653 * @param type the type for which the new methods and constructor are to be created
1654 * @param doConstructors if <code>true</code> unimplemented constructors are created
1655 * @param doUnimplementedMethods if <code>true</code> unimplemented methods are created
1656 * @param imports an import manager to add all neded import statements
1657 * @param monitor a progress monitor to report progress
1659 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1660 // ArrayList newMethods= new ArrayList();
1661 // ITypeHierarchy hierarchy= null;
1662 // CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings();
1664 // if (doConstructors) {
1665 // hierarchy= type.newSupertypeHierarchy(monitor);
1666 // IType superclass= hierarchy.getSuperclass(type);
1667 // if (superclass != null) {
1668 // String[] constructors= StubUtility.evalConstructors(type, superclass, settings, imports.getImportsStructure());
1669 // if (constructors != null) {
1670 // for (int i= 0; i < constructors.length; i++) {
1671 // newMethods.add(constructors[i]);
1677 // if (doUnimplementedMethods) {
1678 // if (hierarchy == null) {
1679 // hierarchy= type.newSupertypeHierarchy(monitor);
1681 // String[] unimplemented= StubUtility.evalUnimplementedMethods(type, hierarchy, false, settings, null, imports.getImportsStructure());
1682 // if (unimplemented != null) {
1683 // for (int i= 0; i < unimplemented.length; i++) {
1684 // newMethods.add(unimplemented[i]);
1688 // IMethod[] createdMethods= new IMethod[newMethods.size()];
1689 // for (int i= 0; i < newMethods.size(); i++) {
1690 // String content= (String) newMethods.get(i) + '\n'; // content will be formatted, ok to use \n
1691 // createdMethods[i]= type.createMethod(content, null, false, null);
1693 // return createdMethods;
1696 // ---- creation ----------------
1699 * Returns the runnable that creates the type using the current settings.
1700 * The returned runnable must be executed in the UI thread.
1702 * @return the runnable to create the new type
1704 // public IRunnableWithProgress getRunnable() {
1705 // return new IRunnableWithProgress() {
1706 // public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
1708 // if (monitor == null) {
1709 // monitor= new NullProgressMonitor();
1711 // createType(monitor);
1712 // } catch (CoreException e) {
1713 // throw new InvocationTargetException(e);