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.corext.util.JavaModelUtil;
32 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
33 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
34 import net.sourceforge.phpdt.internal.ui.wizards.NewWizardMessages;
35 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
36 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
37 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IListAdapter;
38 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
39 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
40 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ListDialogField;
41 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
42 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
43 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.Separator;
44 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
45 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField;
46 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringDialogField;
47 import net.sourceforge.phpdt.ui.CodeGeneration;
48 import net.sourceforge.phpdt.ui.PreferenceConstants;
49 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
51 import org.eclipse.core.runtime.CoreException;
52 import org.eclipse.core.runtime.IProgressMonitor;
53 import org.eclipse.core.runtime.IStatus;
54 import org.eclipse.core.runtime.NullProgressMonitor;
55 import org.eclipse.core.runtime.SubProgressMonitor;
56 import org.eclipse.jface.preference.IPreferenceStore;
57 import org.eclipse.jface.text.BadLocationException;
58 import org.eclipse.jface.text.templates.Template;
59 import org.eclipse.jface.text.templates.TemplateException;
60 import org.eclipse.jface.viewers.LabelProvider;
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.graphics.Image;
63 import org.eclipse.swt.layout.GridData;
64 import org.eclipse.swt.layout.GridLayout;
65 import org.eclipse.swt.widgets.Button;
66 import org.eclipse.swt.widgets.Composite;
67 import org.eclipse.swt.widgets.Control;
70 * The class <code>NewTypeWizardPage</code> contains controls and validation
71 * routines for a 'New Type WizardPage'. Implementors decide which components to
72 * add and to enable. Implementors can also customize the validation code.
73 * <code>NewTypeWizardPage</code> is intended to serve as base class of all
74 * wizards that create types like applets, servlets, classes, interfaces, etc.
76 * See <code>NewClassWizardPage</code> or <code>NewInterfaceWizardPage</code>
77 * for an example usage of <code>NewTypeWizardPage</code>.
80 * @see net.sourceforge.phpdt.ui.wizards.NewClassWizardPage
81 * @see net.sourceforge.phpdt.ui.wizards.NewInterfaceWizardPage
85 public abstract class NewTypeWizardPage extends NewContainerWizardPage {
88 * Class used in stub creation routines to add needed imports to a
91 // public static class ImportsManager {
93 // private IImportsStructure fImportsStructure;
95 // /* package */ ImportsManager(IImportsStructure structure) {
96 // fImportsStructure= structure;
99 // /* package */ IImportsStructure getImportsStructure() {
100 // return fImportsStructure;
104 // * Adds a new import declaration that is sorted in the existing imports.
105 // * If an import already exists or the import would conflict with another
107 // * of an other type with the same simple name the import is not added.
109 // * @param qualifiedTypeName The fully qualified name of the type to import
111 // * @return Returns the simple type name that can be used in the code or
113 // * fully qualified type name if an import conflict prevented the import
115 // public String addImport(String qualifiedTypeName) {
116 // return fImportsStructure.addImport(qualifiedTypeName);
120 * Public access flag. See The Java Virtual Machine Specification for more
123 public int F_PUBLIC = Flags.AccPublic;
126 * Private access flag. See The Java Virtual Machine Specification for more
129 public int F_PRIVATE = Flags.AccPrivate;
132 * Protected access flag. See The Java Virtual Machine Specification for
135 public int F_PROTECTED = Flags.AccProtected;
138 * Static access flag. See The Java Virtual Machine Specification for more
141 public int F_STATIC = Flags.AccStatic;
144 * Final access flag. See The Java Virtual Machine Specification for more
147 public int F_FINAL = Flags.AccFinal;
150 * Abstract property flag. See The Java Virtual Machine Specification for
153 // public int F_ABSTRACT = Flags.AccAbstract;
154 private final static String PAGE_NAME = "NewTypeWizardPage"; //$NON-NLS-1$
156 /** Field ID of the package input field */
157 protected final static String PACKAGE = PAGE_NAME + ".package"; //$NON-NLS-1$
159 /** Field ID of the eclosing type input field */
160 protected final static String ENCLOSING = PAGE_NAME + ".enclosing"; //$NON-NLS-1$
162 /** Field ID of the enclosing type checkbox */
163 protected final static String ENCLOSINGSELECTION = ENCLOSING + ".selection"; //$NON-NLS-1$
165 /** Field ID of the type name input field */
166 protected final static String TYPENAME = PAGE_NAME + ".typename"; //$NON-NLS-1$
168 /** Field ID of the super type input field */
169 protected final static String SUPER = PAGE_NAME + ".superclass"; //$NON-NLS-1$
171 /** Field ID of the super interfaces input field */
172 protected final static String INTERFACES = PAGE_NAME + ".interfaces"; //$NON-NLS-1$
174 /** Field ID of the modifier checkboxes */
175 protected final static String MODIFIERS = PAGE_NAME + ".modifiers"; //$NON-NLS-1$
177 /** Field ID of the method stubs checkboxes */
178 protected final static String METHODS = PAGE_NAME + ".methods"; //$NON-NLS-1$
180 private class InterfacesListLabelProvider extends LabelProvider {
182 private Image fInterfaceImage;
184 public InterfacesListLabelProvider() {
186 fInterfaceImage = PHPUiImages.get(PHPUiImages.IMG_OBJS_INTERFACE);
189 public Image getImage(Object element) {
190 return fInterfaceImage;
194 private StringButtonStatusDialogField fPackageDialogField;
196 private SelectionButtonDialogField fEnclosingTypeSelection;
198 private StringButtonDialogField fEnclosingTypeDialogField;
200 private boolean fCanModifyPackage;
202 private boolean fCanModifyEnclosingType;
204 private IPackageFragment fCurrPackage;
206 // private IType fCurrEnclosingType;
207 private StringDialogField fTypeNameDialogField;
209 private StringButtonDialogField fSuperClassDialogField;
211 private ListDialogField fSuperInterfacesDialogField;
213 // private IType fSuperClass;
215 private SelectionButtonDialogFieldGroup fAccMdfButtons;
217 private SelectionButtonDialogFieldGroup fOtherMdfButtons;
219 private IType fCreatedType;
221 protected IStatus fEnclosingTypeStatus;
223 protected IStatus fPackageStatus;
225 protected IStatus fTypeNameStatus;
227 // protected IStatus fSuperClassStatus;
228 protected IStatus fModifierStatus;
230 // protected IStatus fSuperInterfacesStatus;
232 private boolean fIsClass;
234 private int fStaticMdfIndex;
236 private final int PUBLIC_INDEX = 0, DEFAULT_INDEX = 1, PRIVATE_INDEX = 2,
239 private final int ABSTRACT_INDEX = 0, FINAL_INDEX = 1;
242 * Creates a new <code>NewTypeWizardPage</code>
245 * <code>true</code> if a new class is to be created; otherwise
246 * an interface is to be created
248 * the wizard page's name
250 public NewTypeWizardPage(boolean isClass, String pageName) {
256 TypeFieldsAdapter adapter = new TypeFieldsAdapter();
258 fPackageDialogField = new StringButtonStatusDialogField(adapter);
259 fPackageDialogField.setDialogFieldListener(adapter);
260 fPackageDialogField.setLabelText(NewWizardMessages
261 .getString("NewTypeWizardPage.package.label")); //$NON-NLS-1$
262 fPackageDialogField.setButtonLabel(NewWizardMessages
263 .getString("NewTypeWizardPage.package.button")); //$NON-NLS-1$
264 fPackageDialogField.setStatusWidthHint(NewWizardMessages
265 .getString("NewTypeWizardPage.default")); //$NON-NLS-1$
267 fEnclosingTypeSelection = new SelectionButtonDialogField(SWT.CHECK);
268 fEnclosingTypeSelection.setDialogFieldListener(adapter);
269 fEnclosingTypeSelection.setLabelText(NewWizardMessages
270 .getString("NewTypeWizardPage.enclosing.selection.label")); //$NON-NLS-1$
272 fEnclosingTypeDialogField = new StringButtonDialogField(adapter);
273 fEnclosingTypeDialogField.setDialogFieldListener(adapter);
274 fEnclosingTypeDialogField.setButtonLabel(NewWizardMessages
275 .getString("NewTypeWizardPage.enclosing.button")); //$NON-NLS-1$
277 fTypeNameDialogField = new StringDialogField();
278 fTypeNameDialogField.setDialogFieldListener(adapter);
279 fTypeNameDialogField.setLabelText(NewWizardMessages
280 .getString("NewTypeWizardPage.typename.label")); //$NON-NLS-1$
282 fSuperClassDialogField = new StringButtonDialogField(adapter);
283 fSuperClassDialogField.setDialogFieldListener(adapter);
284 fSuperClassDialogField.setLabelText(NewWizardMessages
285 .getString("NewTypeWizardPage.superclass.label")); //$NON-NLS-1$
286 fSuperClassDialogField.setButtonLabel(NewWizardMessages
287 .getString("NewTypeWizardPage.superclass.button")); //$NON-NLS-1$
289 String[] addButtons = new String[] {
291 NewWizardMessages.getString("NewTypeWizardPage.interfaces.add"), //$NON-NLS-1$
296 .getString("NewTypeWizardPage.interfaces.remove") //$NON-NLS-1$
298 fSuperInterfacesDialogField = new ListDialogField(adapter, addButtons,
299 new InterfacesListLabelProvider());
300 fSuperInterfacesDialogField.setDialogFieldListener(adapter);
301 String interfaceLabel = fIsClass ? NewWizardMessages
302 .getString("NewTypeWizardPage.interfaces.class.label") : NewWizardMessages.getString("NewTypeWizardPage.interfaces.ifc.label"); //$NON-NLS-1$ //$NON-NLS-2$
303 fSuperInterfacesDialogField.setLabelText(interfaceLabel);
304 fSuperInterfacesDialogField.setRemoveButtonIndex(2);
306 String[] buttonNames1 = new String[] {
307 /* 0 == PUBLIC_INDEX */
309 .getString("NewTypeWizardPage.modifiers.public"), //$NON-NLS-1$
310 /* 1 == DEFAULT_INDEX */
312 .getString("NewTypeWizardPage.modifiers.default"), //$NON-NLS-1$
313 /* 2 == PRIVATE_INDEX */
315 .getString("NewTypeWizardPage.modifiers.private"), //$NON-NLS-1$
316 /* 3 == PROTECTED_INDEX */
318 .getString("NewTypeWizardPage.modifiers.protected") //$NON-NLS-1$
320 fAccMdfButtons = new SelectionButtonDialogFieldGroup(SWT.RADIO,
322 fAccMdfButtons.setDialogFieldListener(adapter);
323 fAccMdfButtons.setLabelText(NewWizardMessages
324 .getString("NewTypeWizardPage.modifiers.acc.label")); //$NON-NLS-1$
325 fAccMdfButtons.setSelection(0, true);
327 String[] buttonNames2;
329 buttonNames2 = new String[] {
330 /* 0 == ABSTRACT_INDEX */
332 .getString("NewTypeWizardPage.modifiers.abstract"), //$NON-NLS-1$
333 /* 1 == FINAL_INDEX */
335 .getString("NewTypeWizardPage.modifiers.final"), //$NON-NLS-1$
338 .getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
340 fStaticMdfIndex = 2; // index of the static checkbox is 2
342 buttonNames2 = new String[] { NewWizardMessages
343 .getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
345 fStaticMdfIndex = 0; // index of the static checkbox is 0
348 fOtherMdfButtons = new SelectionButtonDialogFieldGroup(SWT.CHECK,
350 fOtherMdfButtons.setDialogFieldListener(adapter);
352 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, false);
353 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, false);
354 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, false);
356 fPackageStatus = new StatusInfo();
357 fEnclosingTypeStatus = new StatusInfo();
359 fCanModifyPackage = true;
360 fCanModifyEnclosingType = true;
363 fTypeNameStatus = new StatusInfo();
364 // fSuperClassStatus= new StatusInfo();
365 // fSuperInterfacesStatus= new StatusInfo();
366 fModifierStatus = new StatusInfo();
370 * Initializes all fields provided by the page with a given selection.
373 * the selection used to intialize this page or <code>
375 * if no selection was available
377 protected void initTypePage(IJavaElement elem) {
378 String initSuperclass = "java.lang.Object"; //$NON-NLS-1$
379 ArrayList initSuperinterfaces = new ArrayList(5);
381 IPackageFragment pack = null;
382 IType enclosingType = null;
385 // evaluate the enclosing type
386 pack = (IPackageFragment) elem
387 .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
388 IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
389 if (typeInCU != null) {
390 if (typeInCU.getCompilationUnit() != null) {
391 enclosingType = typeInCU;
394 ICompilationUnit cu = (ICompilationUnit) elem
395 .getAncestor(IJavaElement.COMPILATION_UNIT);
397 // enclosingType= cu.findPrimaryType();
403 // if (elem.getElementType() == IJavaElement.TYPE) {
404 // type= (IType)elem;
405 // if (type.exists()) {
406 // String superName= JavaModelUtil.getFullyQualifiedName(type);
407 // if (type.isInterface()) {
408 // initSuperinterfaces.add(superName);
410 // initSuperclass= superName;
414 // } catch (JavaModelException e) {
415 // PHPeclipsePlugin.log(e);
416 // // ignore this exception now
420 setPackageFragment(pack, true);
421 // setEnclosingType(enclosingType, true);
422 setEnclosingTypeSelection(false, true);
424 setTypeName("", true); //$NON-NLS-1$
425 setSuperClass(initSuperclass, true);
426 setSuperInterfaces(initSuperinterfaces, true);
429 // -------- UI Creation ---------
432 * Creates a separator line. Expects a <code>GridLayout</code> with at
436 * the parent composite
438 * number of columns to span
440 protected void createSeparator(Composite composite, int nColumns) {
441 (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(
442 composite, nColumns, convertHeightInCharsToPixels(1));
446 * Creates the controls for the package name field. Expects a
447 * <code>GridLayout</code> with at least 4 columns.
450 * the parent composite
452 * number of columns to span
454 protected void createPackageControls(Composite composite, int nColumns) {
455 fPackageDialogField.doFillIntoGrid(composite, nColumns);
456 LayoutUtil.setWidthHint(fPackageDialogField.getTextControl(null),
458 LayoutUtil.setHorizontalGrabbing(fPackageDialogField
459 .getTextControl(null));
463 * Creates the controls for the enclosing type name field. Expects a
464 * <code>GridLayout</code> with at least 4 columns.
467 * the parent composite
469 * number of columns to span
471 protected void createEnclosingTypeControls(Composite composite, int nColumns) {
473 Composite tabGroup = new Composite(composite, SWT.NONE);
474 GridLayout layout = new GridLayout();
475 layout.marginWidth = 0;
476 layout.marginHeight = 0;
477 tabGroup.setLayout(layout);
479 fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1);
481 Control c = fEnclosingTypeDialogField.getTextControl(composite);
482 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
483 gd.widthHint = getMaxFieldWidth();
484 gd.horizontalSpan = 2;
487 Button button = fEnclosingTypeDialogField.getChangeControl(composite);
488 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
489 gd.heightHint = SWTUtil.getButtonHeightHint(button);
490 gd.widthHint = SWTUtil.getButtonWidthHint(button);
491 button.setLayoutData(gd);
495 * Creates the controls for the type name field. Expects a
496 * <code>GridLayout</code> with at least 2 columns.
499 * the parent composite
501 * number of columns to span
503 protected void createTypeNameControls(Composite composite, int nColumns) {
504 fTypeNameDialogField.doFillIntoGrid(composite, nColumns - 1);
505 DialogField.createEmptySpace(composite);
507 LayoutUtil.setWidthHint(fTypeNameDialogField.getTextControl(null),
512 * Creates the controls for the modifiers radio/ceckbox buttons. Expects a
513 * <code>GridLayout</code> with at least 3 columns.
516 * the parent composite
518 * number of columns to span
520 protected void createModifierControls(Composite composite, int nColumns) {
521 LayoutUtil.setHorizontalSpan(fAccMdfButtons.getLabelControl(composite),
524 Control control = fAccMdfButtons.getSelectionButtonsGroup(composite);
525 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
526 gd.horizontalSpan = nColumns - 2;
527 control.setLayoutData(gd);
529 DialogField.createEmptySpace(composite);
531 DialogField.createEmptySpace(composite);
533 control = fOtherMdfButtons.getSelectionButtonsGroup(composite);
534 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
535 gd.horizontalSpan = nColumns - 2;
536 control.setLayoutData(gd);
538 DialogField.createEmptySpace(composite);
542 * Creates the controls for the superclass name field. Expects a
543 * <code>GridLayout</code> with at least 3 columns.
546 * the parent composite
548 * number of columns to span
550 protected void createSuperClassControls(Composite composite, int nColumns) {
551 fSuperClassDialogField.doFillIntoGrid(composite, nColumns);
552 LayoutUtil.setWidthHint(fSuperClassDialogField.getTextControl(null),
557 * Creates the controls for the superclass name field. Expects a
558 * <code>GridLayout</code> with at least 3 columns.
561 * the parent composite
563 * number of columns to span
565 protected void createSuperInterfacesControls(Composite composite,
567 fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns);
568 GridData gd = (GridData) fSuperInterfacesDialogField.getListControl(
569 null).getLayoutData();
571 gd.heightHint = convertHeightInCharsToPixels(3);
573 gd.heightHint = convertHeightInCharsToPixels(6);
575 gd.grabExcessVerticalSpace = false;
576 gd.widthHint = getMaxFieldWidth();
580 * Sets the focus on the type name input field.
582 protected void setFocus() {
583 fTypeNameDialogField.setFocus();
586 // -------- TypeFieldsAdapter --------
588 private class TypeFieldsAdapter implements IStringButtonAdapter,
589 IDialogFieldListener, IListAdapter {
591 // -------- IStringButtonAdapter
592 public void changeControlPressed(DialogField field) {
593 // typePageChangeControlPressed(field);
596 // -------- IListAdapter
597 public void customButtonPressed(ListDialogField field, int index) {
598 // typePageCustomButtonPressed(field, index);
601 public void selectionChanged(ListDialogField field) {
604 // -------- IDialogFieldListener
605 public void dialogFieldChanged(DialogField field) {
606 typePageDialogFieldChanged(field);
609 public void doubleClicked(ListDialogField field) {
613 // private void typePageChangeControlPressed(DialogField field) {
614 // if (field == fPackageDialogField) {
615 // IPackageFragment pack= choosePackage();
616 // if (pack != null) {
617 // fPackageDialogField.setText(pack.getElementName());
619 // } else if (field == fEnclosingTypeDialogField) {
620 // IType type= chooseEnclosingType();
621 // if (type != null) {
622 // fEnclosingTypeDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
624 // } else if (field == fSuperClassDialogField) {
625 // IType type= chooseSuperType();
626 // if (type != null) {
627 // fSuperClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
632 // private void typePageCustomButtonPressed(DialogField field, int index) {
633 // if (field == fSuperInterfacesDialogField) {
634 // chooseSuperInterfaces();
639 * A field on the type has changed. The fields' status and all dependend
640 * status are updated.
642 private void typePageDialogFieldChanged(DialogField field) {
643 String fieldName = null;
644 if (field == fPackageDialogField) {
645 fPackageStatus = packageChanged();
646 updatePackageStatusLabel();
647 fTypeNameStatus = typeNameChanged();
648 // fSuperClassStatus= superClassChanged();
650 } else if (field == fEnclosingTypeDialogField) {
651 // fEnclosingTypeStatus= enclosingTypeChanged();
652 fTypeNameStatus = typeNameChanged();
653 // fSuperClassStatus= superClassChanged();
654 fieldName = ENCLOSING;
655 } else if (field == fEnclosingTypeSelection) {
657 boolean isEnclosedType = isEnclosingTypeSelected();
658 if (!isEnclosedType) {
659 if (fAccMdfButtons.isSelected(PRIVATE_INDEX)
660 || fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
661 fAccMdfButtons.setSelection(PRIVATE_INDEX, false);
662 fAccMdfButtons.setSelection(PROTECTED_INDEX, false);
663 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
665 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
666 fOtherMdfButtons.setSelection(fStaticMdfIndex, false);
669 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, isEnclosedType
671 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX,
672 isEnclosedType && fIsClass);
673 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex,
675 fTypeNameStatus = typeNameChanged();
676 // fSuperClassStatus= superClassChanged();
677 fieldName = ENCLOSINGSELECTION;
678 } else if (field == fTypeNameDialogField) {
679 fTypeNameStatus = typeNameChanged();
680 fieldName = TYPENAME;
681 } else if (field == fSuperClassDialogField) {
682 // fSuperClassStatus= superClassChanged();
684 } else if (field == fSuperInterfacesDialogField) {
685 // fSuperInterfacesStatus= superInterfacesChanged();
686 fieldName = INTERFACES;
687 } else if (field == fOtherMdfButtons) {
688 fModifierStatus = modifiersChanged();
689 fieldName = MODIFIERS;
694 handleFieldChanged(fieldName);
697 // -------- update message ----------------
700 * @see net.sourceforge.phpdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
702 protected void handleFieldChanged(String fieldName) {
703 super.handleFieldChanged(fieldName);
704 if (fieldName == CONTAINER) {
705 fPackageStatus = packageChanged();
706 // fEnclosingTypeStatus= enclosingTypeChanged();
707 fTypeNameStatus = typeNameChanged();
708 // fSuperClassStatus= superClassChanged();
709 // fSuperInterfacesStatus= superInterfacesChanged();
713 // ---- set / get ----------------
716 * Returns the text of the package input field.
718 * @return the text of the package input field
720 public String getPackageText() {
721 return fPackageDialogField.getText();
725 * Returns the text of the enclosing type input field.
727 * @return the text of the enclosing type input field
729 public String getEnclosingTypeText() {
730 return fEnclosingTypeDialogField.getText();
734 * Returns the package fragment corresponding to the current input.
736 * @return a package fragement or <code>null</code> if the input could not
739 public IPackageFragment getPackageFragment() {
740 if (!isEnclosingTypeSelected()) {
743 // if (fCurrEnclosingType != null) {
744 // return fCurrEnclosingType.getPackageFragment();
751 * Sets the package fragment to the given value. The method updates the
752 * model and the text of the control.
755 * the package fragement to be set
756 * @param canBeModified
757 * if <code>true</code> the package fragment is editable;
758 * otherwise it is read-only.
760 public void setPackageFragment(IPackageFragment pack, boolean canBeModified) {
762 fCanModifyPackage = canBeModified;
763 String str = (pack == null) ? "" : pack.getElementName(); //$NON-NLS-1$
764 fPackageDialogField.setText(str);
769 * Returns the enclosing type corresponding to the current input.
771 * @return the enclosing type or <code>null</code> if the enclosing type
772 * is not selected or the input could not be resolved
774 public IType getEnclosingType() {
775 // if (isEnclosingTypeSelected()) {
776 // return fCurrEnclosingType;
782 * Sets the enclosing type. The method updates the underlying model and the
783 * text of the control.
787 * @param canBeModified
788 * if <code>true</code> the enclosing type field is editable;
789 * otherwise it is read-only.
791 // public void setEnclosingType(IType type, boolean canBeModified) {
792 // fCurrEnclosingType= type;
793 // fCanModifyEnclosingType= canBeModified;
794 // String str= (type == null) ? "" :
795 // JavaModelUtil.getFullyQualifiedName(type); //$NON-NLS-1$
796 // fEnclosingTypeDialogField.setText(str);
797 // updateEnableState();
800 * Returns the selection state of the enclosing type checkbox.
802 * @return the seleciton state of the enclosing type checkbox
804 public boolean isEnclosingTypeSelected() {
805 return fEnclosingTypeSelection.isSelected();
809 * Sets the enclosing type checkbox's selection state.
812 * the checkbox's selection state
813 * @param canBeModified
814 * if <code>true</code> the enclosing type checkbox is
815 * modifiable; otherwise it is read-only.
817 public void setEnclosingTypeSelection(boolean isSelected,
818 boolean canBeModified) {
819 fEnclosingTypeSelection.setSelection(isSelected);
820 fEnclosingTypeSelection.setEnabled(canBeModified);
825 * Returns the type name entered into the type input field.
827 * @return the type name
829 public String getTypeName() {
830 return fTypeNameDialogField.getText();
834 * Sets the type name input field's text to the given value. Method doesn't
839 * @param canBeModified
840 * if <code>true</code> the enclosing type name field is
841 * editable; otherwise it is read-only.
843 public void setTypeName(String name, boolean canBeModified) {
844 fTypeNameDialogField.setText(name);
845 fTypeNameDialogField.setEnabled(canBeModified);
849 * Returns the selected modifiers.
851 * @return the selected modifiers
854 public int getModifiers() {
856 if (fAccMdfButtons.isSelected(PUBLIC_INDEX)) {
858 } else if (fAccMdfButtons.isSelected(PRIVATE_INDEX)) {
860 } else if (fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
863 // if (fOtherMdfButtons.isSelected(ABSTRACT_INDEX) && (fStaticMdfIndex
867 if (fOtherMdfButtons.isSelected(FINAL_INDEX)) {
870 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
877 * Sets the modifiers.
880 * <code>F_PUBLIC</code>, <code>F_PRIVATE</code>,
881 * <code>F_PROTECTED</code>, <code>F_ABSTRACT, F_FINAL</code>
882 * or <code>F_STATIC</code> or, a valid combination.
883 * @param canBeModified
884 * if <code>true</code> the modifier fields are editable;
885 * otherwise they are read-only
888 public void setModifiers(int modifiers, boolean canBeModified) {
889 if (Flags.isPublic(modifiers)) {
890 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
891 } else if (Flags.isPrivate(modifiers)) {
892 fAccMdfButtons.setSelection(PRIVATE_INDEX, true);
893 } else if (Flags.isProtected(modifiers)) {
894 fAccMdfButtons.setSelection(PROTECTED_INDEX, true);
896 fAccMdfButtons.setSelection(DEFAULT_INDEX, true);
898 // if (Flags.isAbstract(modifiers)) {
899 // fOtherMdfButtons.setSelection(ABSTRACT_INDEX, true);
901 if (Flags.isFinal(modifiers)) {
902 fOtherMdfButtons.setSelection(FINAL_INDEX, true);
904 if (Flags.isStatic(modifiers)) {
905 fOtherMdfButtons.setSelection(fStaticMdfIndex, true);
908 fAccMdfButtons.setEnabled(canBeModified);
909 fOtherMdfButtons.setEnabled(canBeModified);
913 * Returns the content of the superclass input field.
915 * @return the superclass name
917 public String getSuperClass() {
918 return fSuperClassDialogField.getText();
922 * Sets the super class name.
925 * the new superclass name
926 * @param canBeModified
927 * if <code>true</code> the superclass name field is editable;
928 * otherwise it is read-only.
930 public void setSuperClass(String name, boolean canBeModified) {
931 fSuperClassDialogField.setText(name);
932 fSuperClassDialogField.setEnabled(canBeModified);
936 * Returns the chosen super interfaces.
938 * @return a list of chosen super interfaces. The list's elements are of
939 * type <code>String</code>
941 public List getSuperInterfaces() {
942 return fSuperInterfacesDialogField.getElements();
946 * Sets the super interfaces.
948 * @param interfacesNames
949 * a list of super interface. The method requires that the list's
950 * elements are of type <code>String</code>
951 * @param canBeModified
952 * if <code>true</code> the super interface field is editable;
953 * otherwise it is read-only.
955 public void setSuperInterfaces(List interfacesNames, boolean canBeModified) {
956 fSuperInterfacesDialogField.setElements(interfacesNames);
957 fSuperInterfacesDialogField.setEnabled(canBeModified);
960 // ----------- validation ----------
963 * A hook method that gets called when the package field has changed. The
964 * method validates the package name and returns the status of the
965 * validation. The validation also updates the package fragment model.
967 * Subclasses may extend this method to perform their own validation.
970 * @return the status of the validation
972 protected IStatus packageChanged() {
973 StatusInfo status = new StatusInfo();
974 fPackageDialogField.enableButton(getPackageFragmentRoot() != null);
976 // String packName= getPackageText();
977 // if (packName.length() > 0) {
978 // IStatus val= JavaConventions.validatePackageName(packName);
979 // if (val.getSeverity() == IStatus.ERROR) {
980 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidPackageName",
981 // val.getMessage())); //$NON-NLS-1$
983 // } else if (val.getSeverity() == IStatus.WARNING) {
984 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.DiscouragedPackageName",
985 // val.getMessage())); //$NON-NLS-1$
990 // IPackageFragmentRoot root= getPackageFragmentRoot();
991 // if (root != null) {
992 // if (root.getJavaProject().exists() && packName.length() > 0) {
994 // IPath rootPath= root.getPath();
995 // IPath outputPath= root.getJavaProject().getOutputLocation();
996 // if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath))
998 // // if the bin folder is inside of our root, dont allow to name a
1000 // // like the bin folder
1001 // IPath packagePath= rootPath.append(packName.replace('.', '/'));
1002 // if (outputPath.isPrefixOf(packagePath)) {
1003 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ClashOutputLocation"));
1008 // } catch (JavaModelException e) {
1009 // PHPeclipsePlugin.log(e);
1014 // fCurrPackage= root.getPackageFragment(packName);
1016 // status.setError(""); //$NON-NLS-1$
1022 * Updates the 'default' label next to the package field.
1024 private void updatePackageStatusLabel() {
1025 String packName = getPackageText();
1027 if (packName.length() == 0) {
1028 fPackageDialogField.setStatus(NewWizardMessages
1029 .getString("NewTypeWizardPage.default")); //$NON-NLS-1$
1031 fPackageDialogField.setStatus(""); //$NON-NLS-1$
1036 * Updates the enable state of buttons related to the enclosing type
1037 * selection checkbox.
1039 private void updateEnableState() {
1040 boolean enclosing = isEnclosingTypeSelected();
1041 fPackageDialogField.setEnabled(fCanModifyPackage && !enclosing);
1042 fEnclosingTypeDialogField.setEnabled(fCanModifyEnclosingType
1047 * Hook method that gets called when the enclosing type name has changed.
1048 * The method validates the enclosing type and returns the status of the
1049 * validation. It also updates the enclosing type model.
1051 * Subclasses may extend this method to perform their own validation.
1054 * @return the status of the validation
1056 // protected IStatus enclosingTypeChanged() {
1057 // StatusInfo status= new StatusInfo();
1058 // fCurrEnclosingType= null;
1060 // IPackageFragmentRoot root= getPackageFragmentRoot();
1062 // fEnclosingTypeDialogField.enableButton(root != null);
1063 // if (root == null) {
1064 // status.setError(""); //$NON-NLS-1$
1068 // String enclName= getEnclosingTypeText();
1069 // if (enclName.length() == 0) {
1070 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeEnterName"));
1075 // IType type= findType(root.getJavaProject(), enclName);
1076 // if (type == null) {
1077 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists"));
1082 // if (type.getCompilationUnit() == null) {
1083 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotInCU"));
1087 // if (!JavaModelUtil.isEditable(type.getCompilationUnit())) {
1088 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotEditable"));
1093 // fCurrEnclosingType= type;
1094 // IPackageFragmentRoot enclosingRoot=
1095 // JavaModelUtil.getPackageFragmentRoot(type);
1096 // if (!enclosingRoot.equals(root)) {
1097 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.EnclosingNotInSourceFolder"));
1101 // } catch (JavaModelException e) {
1102 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists"));
1104 // PHPeclipsePlugin.log(e);
1109 * Hook method that gets called when the type name has changed. The method
1110 * validates the type name and returns the status of the validation.
1112 * Subclasses may extend this method to perform their own validation.
1115 * @return the status of the validation
1117 protected IStatus typeNameChanged() {
1118 StatusInfo status = new StatusInfo();
1119 String typeName = getTypeName();
1120 // must not be empty
1121 if (typeName.length() == 0) {
1122 status.setError(NewWizardMessages
1123 .getString("NewTypeWizardPage.error.EnterTypeName")); //$NON-NLS-1$
1126 if (typeName.indexOf('.') != -1) {
1127 status.setError(NewWizardMessages
1128 .getString("NewTypeWizardPage.error.QualifiedName")); //$NON-NLS-1$
1131 // IStatus val= JavaConventions.validateJavaTypeName(typeName);
1132 // if (val.getSeverity() == IStatus.ERROR) {
1133 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidTypeName",
1134 // val.getMessage())); //$NON-NLS-1$
1136 // } else if (val.getSeverity() == IStatus.WARNING) {
1137 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.TypeNameDiscouraged",
1138 // val.getMessage())); //$NON-NLS-1$
1139 // // continue checking
1143 if (!isEnclosingTypeSelected()) {
1144 IPackageFragment pack = getPackageFragment();
1146 ICompilationUnit cu = pack.getCompilationUnit(typeName
1147 + ".java"); //$NON-NLS-1$
1148 if (cu.getResource().exists()) {
1150 .setError(NewWizardMessages
1151 .getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1156 IType type = getEnclosingType();
1158 IType member = type.getType(typeName);
1159 if (member.exists()) {
1161 .setError(NewWizardMessages
1162 .getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1171 * Hook method that gets called when the superclass name has changed. The
1172 * method validates the superclass name and returns the status of the
1175 * Subclasses may extend this method to perform their own validation.
1178 * @return the status of the validation
1180 // protected IStatus superClassChanged() {
1181 // StatusInfo status= new StatusInfo();
1182 // IPackageFragmentRoot root= getPackageFragmentRoot();
1183 // fSuperClassDialogField.enableButton(root != null);
1185 // fSuperClass= null;
1187 // String sclassName= getSuperClass();
1188 // if (sclassName.length() == 0) {
1189 // // accept the empty field (stands for java.lang.Object)
1192 // IStatus val= JavaConventions.validateJavaTypeName(sclassName);
1193 // if (val.getSeverity() == IStatus.ERROR) {
1194 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName"));
1198 // if (root != null) {
1200 // IType type= resolveSuperTypeName(root.getJavaProject(), sclassName);
1201 // if (type == null) {
1202 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.SuperClassNotExists"));
1206 // if (type.isInterface()) {
1207 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotClass",
1208 // sclassName)); //$NON-NLS-1$
1211 // int flags= type.getFlags();
1212 // if (Flags.isFinal(flags)) {
1213 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsFinal",
1214 // sclassName)); //$NON-NLS-1$
1216 // } else if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1217 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotVisible",
1218 // sclassName)); //$NON-NLS-1$
1222 // fSuperClass= type;
1223 // } catch (JavaModelException e) {
1224 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName"));
1226 // PHPeclipsePlugin.log(e);
1229 // status.setError(""); //$NON-NLS-1$
1234 // private IType resolveSuperTypeName(IJavaProject jproject, String
1235 // sclassName) throws JavaModelException {
1236 // if (!jproject.exists()) {
1239 // IType type= null;
1240 // if (isEnclosingTypeSelected()) {
1241 // // search in the context of the enclosing type
1242 // IType enclosingType= getEnclosingType();
1243 // if (enclosingType != null) {
1244 // String[][] res= enclosingType.resolveType(sclassName);
1245 // if (res != null && res.length > 0) {
1246 // type= jproject.findType(res[0][0], res[0][1]);
1250 // IPackageFragment currPack= getPackageFragment();
1251 // if (type == null && currPack != null) {
1252 // String packName= currPack.getElementName();
1253 // // search in own package
1254 // if (!currPack.isDefaultPackage()) {
1255 // type= jproject.findType(packName, sclassName);
1257 // // search in java.lang
1258 // if (type == null && !"java.lang".equals(packName)) { //$NON-NLS-1$
1259 // type= jproject.findType("java.lang", sclassName); //$NON-NLS-1$
1262 // // search fully qualified
1263 // if (type == null) {
1264 // type= jproject.findType(sclassName);
1269 // private IType findType(IJavaProject project, String typeName) throws
1270 // JavaModelException {
1271 // if (project.exists()) {
1272 // return project.findType(typeName);
1277 * Hook method that gets called when the list of super interface has
1278 * changed. The method validates the superinterfaces and returns the status
1279 * of the validation.
1281 * Subclasses may extend this method to perform their own validation.
1284 * @return the status of the validation
1286 // protected IStatus superInterfacesChanged() {
1287 // StatusInfo status= new StatusInfo();
1289 // IPackageFragmentRoot root= getPackageFragmentRoot();
1290 // fSuperInterfacesDialogField.enableButton(0, root != null);
1292 // if (root != null) {
1293 // List elements= fSuperInterfacesDialogField.getElements();
1294 // int nElements= elements.size();
1295 // for (int i= 0; i < nElements; i++) {
1296 // String intfname= (String)elements.get(i);
1298 // IType type= findType(root.getJavaProject(), intfname);
1299 // if (type == null) {
1300 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceNotExists",
1301 // intfname)); //$NON-NLS-1$
1304 // if (type.isClass()) {
1305 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotInterface",
1306 // intfname)); //$NON-NLS-1$
1309 // if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1310 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotVisible",
1311 // intfname)); //$NON-NLS-1$
1315 // } catch (JavaModelException e) {
1316 // PHPeclipsePlugin.log(e);
1317 // // let pass, checking is an extra
1324 * Hook method that gets called when the modifiers have changed. The method
1325 * validates the modifiers and returns the status of the validation.
1327 * Subclasses may extend this method to perform their own validation.
1330 * @return the status of the validation
1332 protected IStatus modifiersChanged() {
1333 StatusInfo status = new StatusInfo();
1334 int modifiers = getModifiers();
1335 // if (Flags.isFinal(modifiers) && Flags.isAbstract(modifiers)) {
1336 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ModifiersFinalAndAbstract"));
1342 // selection dialogs
1344 // private IPackageFragment choosePackage() {
1345 // IPackageFragmentRoot froot= getPackageFragmentRoot();
1346 // IJavaElement[] packages= null;
1348 // if (froot != null && froot.exists()) {
1349 // packages= froot.getChildren();
1351 // } catch (JavaModelException e) {
1352 // PHPeclipsePlugin.log(e);
1354 // if (packages == null) {
1355 // packages= new IJavaElement[0];
1358 // ElementListSelectionDialog dialog= new
1359 // ElementListSelectionDialog(getShell(), new
1360 // JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
1361 // dialog.setIgnoreCase(false);
1362 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.title"));
1364 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.description"));
1366 // dialog.setEmptyListMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.empty"));
1368 // dialog.setElements(packages);
1369 // IPackageFragment pack= getPackageFragment();
1370 // if (pack != null) {
1371 // dialog.setInitialSelections(new Object[] { pack });
1374 // if (dialog.open() == ElementListSelectionDialog.OK) {
1375 // return (IPackageFragment) dialog.getFirstResult();
1380 // private IType chooseEnclosingType() {
1381 // IPackageFragmentRoot root= getPackageFragmentRoot();
1382 // if (root == null) {
1386 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new
1387 // IJavaElement[] { root });
1389 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(),
1390 // getWizard().getContainer(), IJavaSearchConstants.TYPE, scope);
1391 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.title"));
1393 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.description"));
1395 // dialog.setFilter(Signature.getSimpleName(getEnclosingTypeText()));
1397 // if (dialog.open() == TypeSelectionDialog.OK) {
1398 // return (IType) dialog.getFirstResult();
1403 // private IType chooseSuperType() {
1404 // IPackageFragmentRoot root= getPackageFragmentRoot();
1405 // if (root == null) {
1409 // IJavaElement[] elements= new IJavaElement[] { root.getJavaProject() };
1410 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
1412 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(),
1413 // getWizard().getContainer(), IJavaSearchConstants.CLASS, scope);
1414 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.title"));
1416 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.message"));
1418 // dialog.setFilter(getSuperClass());
1420 // if (dialog.open() == TypeSelectionDialog.OK) {
1421 // return (IType) dialog.getFirstResult();
1426 // private void chooseSuperInterfaces() {
1427 // IPackageFragmentRoot root= getPackageFragmentRoot();
1428 // if (root == null) {
1432 // IJavaProject project= root.getJavaProject();
1433 // SuperInterfaceSelectionDialog dialog= new
1434 // SuperInterfaceSelectionDialog(getShell(), getWizard().getContainer(),
1435 // fSuperInterfacesDialogField, project);
1436 // dialog.setTitle(fIsClass ?
1437 // NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.class.title")
1439 // NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.interface.title"));
1440 // //$NON-NLS-1$ //$NON-NLS-2$
1441 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.message"));
1447 // ---- creation ----------------
1450 * Creates the new type using the entered field values.
1453 * a progress monitor to report progress.
1455 public void createType(IProgressMonitor monitor) throws CoreException,
1456 InterruptedException {
1457 if (monitor == null) {
1458 monitor = new NullProgressMonitor();
1461 monitor.beginTask(NewWizardMessages
1462 .getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$
1463 ICompilationUnit createdWorkingCopy = null;
1465 // IPackageFragmentRoot root = getPackageFragmentRoot();
1466 // IPackageFragment pack = getPackageFragment();
1467 // if (pack == null) {
1468 // pack = root.getPackageFragment(""); //$NON-NLS-1$
1471 // if (!pack.exists()) {
1472 // String packName = pack.getElementName();
1473 // pack = root.createPackageFragment(packName, true, null);
1478 String clName = getTypeName();
1480 boolean isInnerClass = isEnclosingTypeSelected();
1483 // ImportsStructure imports;
1486 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
1487 // String[] prefOrder =
1488 // JavaPreferencesSettings.getImportOrderPreference(store);
1490 // JavaPreferencesSettings.getImportNumberThreshold(store);
1492 String lineDelimiter = null;
1493 // if (!isInnerClass) {
1494 lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1496 // ICompilationUnit parentCU = pack.createCompilationUnit(clName +
1497 // ".php", "", false, new SubProgressMonitor(monitor, 2));
1498 // //$NON-NLS-1$ //$NON-NLS-2$
1499 // createdWorkingCopy = (ICompilationUnit)
1500 // parentCU.getSharedWorkingCopy(null, JavaUI.getBufferFactory(),
1503 // imports = new ImportsStructure(createdWorkingCopy, prefOrder,
1504 // threshold, false);
1505 // // add an import that will be removed again. Having this import
1507 // imports.addImport(pack.getElementName(), getTypeName());
1509 String typeContent = constructTypeStub(lineDelimiter);// new
1510 // ImportsManager(imports),
1513 // String cuContent = constructCUContent(parentCU, typeContent,
1516 // createdWorkingCopy.getBuffer().setContents(cuContent);
1518 createdType = createdWorkingCopy.getType(clName);
1520 // IType enclosingType = getEnclosingType();
1522 // // if we are working on a enclosed type that is open in an
1524 // // then replace the enclosing type with its working copy
1525 // IType workingCopy = (IType)
1526 // EditorUtility.getWorkingCopy(enclosingType);
1527 // if (workingCopy != null) {
1528 // enclosingType = workingCopy;
1531 // ICompilationUnit parentCU = enclosingType.getCompilationUnit();
1532 // imports = new ImportsStructure(parentCU, prefOrder, threshold,
1535 // // add imports that will be removed again. Having the imports
1537 // IType[] topLevelTypes = parentCU.getTypes();
1538 // for (int i = 0; i < topLevelTypes.length; i++) {
1539 // imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
1542 // lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
1543 // StringBuffer content = new StringBuffer();
1544 // String comment = getTypeComment(parentCU);
1545 // if (comment != null) {
1546 // content.append(comment);
1547 // content.append(lineDelimiter);
1549 // content.append(constructTypeStub(new ImportsManager(imports),
1551 // IJavaElement[] elems = enclosingType.getChildren();
1552 // IJavaElement sibling = elems.length > 0 ? elems[0] : null;
1554 // createdType = enclosingType.createType(content.toString(),
1555 // sibling, false, new SubProgressMonitor(monitor, 1));
1557 // indent = StubUtility.getIndentUsed(enclosingType) + 1;
1560 // // add imports for superclass/interfaces, so types can be
1561 // resolved correctly
1562 // imports.create(false, new SubProgressMonitor(monitor, 1));
1564 ICompilationUnit cu = createdType.getCompilationUnit();
1568 // createTypeMembers(createdType, new ImportsManager(imports), new
1569 // SubProgressMonitor(monitor, 1));
1572 // imports.create(false, new SubProgressMonitor(monitor, 1));
1577 ISourceRange range = createdType.getSourceRange();
1579 IBuffer buf = cu.getBuffer();
1580 String originalContent = buf.getText(range.getOffset(), range
1582 String formattedContent = StubUtility.codeFormat(originalContent,
1583 indent, lineDelimiter);
1584 buf.replace(range.getOffset(), range.getLength(), formattedContent);
1585 if (!isInnerClass) {
1586 String fileComment = getFileComment(cu);
1587 if (fileComment != null && fileComment.length() > 0) {
1588 buf.replace(0, 0, fileComment + lineDelimiter);
1590 cu.commit(false, new SubProgressMonitor(monitor, 1));
1594 fCreatedType = createdType;
1596 if (createdWorkingCopy != null) {
1597 createdWorkingCopy.destroy();
1604 * Uses the New Java file template from the code template page to generate a
1605 * compilation unit with the given type content.
1608 * The new created compilation unit
1609 * @param typeContent
1610 * The content of the type, including signature and type body.
1611 * @param lineDelimiter
1612 * The line delimiter to be used.
1613 * @return String Returns the result of evaluating the new file template
1614 * with the given type content.
1615 * @throws CoreException
1618 // protected String constructCUContent(ICompilationUnit cu, String
1619 // typeContent, String lineDelimiter) throws CoreException {
1620 // StringBuffer typeQualifiedName= new StringBuffer();
1621 // if (isEnclosingTypeSelected()) {
1622 // typeQualifiedName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1624 // typeQualifiedName.append(getTypeName());
1625 // String typeComment= CodeGeneration.getTypeComment(cu,
1626 // typeQualifiedName.toString(), lineDelimiter);
1627 // IPackageFragment pack= (IPackageFragment) cu.getParent();
1628 // String content= CodeGeneration.getCompilationUnitContent(cu, typeComment,
1629 // typeContent, lineDelimiter);
1630 // if (content != null) {
1631 // CompilationUnit unit= AST.parseCompilationUnit(content.toCharArray());
1632 // if ((pack.isDefaultPackage() || unit.getPackage() != null) &&
1633 // !unit.types().isEmpty()) {
1637 // StringBuffer buf= new StringBuffer();
1638 // if (!pack.isDefaultPackage()) {
1639 // buf.append("package ").append(pack.getElementName()).append(';');
1642 // buf.append(lineDelimiter).append(lineDelimiter);
1643 // if (typeComment != null) {
1644 // buf.append(typeComment).append(lineDelimiter);
1646 // buf.append(typeContent);
1647 // return buf.toString();
1650 * Returns the created type. The method only returns a valid type after
1651 * <code>createType</code> has been called.
1653 * @return the created type
1654 * @see #createType(IProgressMonitor)
1656 public IType getCreatedType() {
1657 return fCreatedType;
1660 // ---- construct cu body----------------
1662 // private void writeSuperClass(StringBuffer buf, ImportsManager imports) {
1663 // String typename= getSuperClass();
1664 // if (fIsClass && typename.length() > 0 &&
1665 // !"java.lang.Object".equals(typename)) { //$NON-NLS-1$
1666 // buf.append(" extends "); //$NON-NLS-1$
1668 // String qualifiedName= fSuperClass != null ?
1669 // JavaModelUtil.getFullyQualifiedName(fSuperClass) : typename;
1670 // buf.append(imports.addImport(qualifiedName));
1674 // private void writeSuperInterfaces(StringBuffer buf, ImportsManager
1676 // List interfaces= getSuperInterfaces();
1677 // int last= interfaces.size() - 1;
1680 // buf.append(" implements "); //$NON-NLS-1$
1682 // buf.append(" extends "); //$NON-NLS-1$
1684 // for (int i= 0; i <= last; i++) {
1685 // String typename= (String) interfaces.get(i);
1686 // buf.append(imports.addImport(typename));
1695 * Called from createType to construct the source for this type
1697 private String constructTypeStub(String lineDelimiter) { // ImportsManager
1702 StringBuffer buf = new StringBuffer();
1704 int modifiers = getModifiers();
1705 buf.append(Flags.toString(modifiers));
1706 if (modifiers != 0) {
1709 buf.append(fIsClass ? "class " : "interface "); //$NON-NLS-2$ //$NON-NLS-1$
1710 buf.append(getTypeName());
1711 // writeSuperClass(buf, imports);
1712 // writeSuperInterfaces(buf, imports);
1714 buf.append(lineDelimiter);
1715 buf.append(lineDelimiter);
1717 buf.append(lineDelimiter);
1718 return buf.toString();
1722 * @deprecated Overwrite createTypeMembers(IType, IImportsManager,
1723 * IProgressMonitor) instead
1725 // protected void createTypeMembers(IType newType, IImportsStructure
1726 // imports, IProgressMonitor monitor) throws CoreException {
1730 * Hook method that gets called from <code>createType</code> to support
1731 * adding of unanticipated methods, fields, and inner types to the created
1734 * Implementers can use any methods defined on <code>IType</code> to
1735 * manipulate the new type.
1738 * The source code of the new type will be formtted using the platform's
1739 * formatter. Needed imports are added by the wizard at the end of the type
1740 * creation process using the given import manager.
1744 * the new type created via <code>createType</code>
1746 * an import manager which can be used to add new imports
1748 * a progress monitor to report progress. Must not be
1751 * @see #createType(IProgressMonitor)
1753 // protected void createTypeMembers(IType newType, ImportsManager imports,
1754 // IProgressMonitor monitor) throws CoreException {
1755 // // call for compatibility
1756 // createTypeMembers(newType,
1757 // ((ImportsManager)imports).getImportsStructure(), monitor);
1759 // // default implementation does nothing
1760 // // example would be
1761 // // String mainMathod= "public void foo(Vector vec) {}"
1762 // // createdType.createMethod(main, null, false, null);
1763 // // imports.addImport("java.lang.Vector");
1766 * @deprecated Instead of file templates, the new type code template
1767 * specifies the stub for a compilation unit.
1769 protected String getFileComment(ICompilationUnit parentCU) {
1773 private boolean isValidComment(String template) {
1774 IScanner scanner = ToolFactory.createScanner(true, false, false); // ,
1776 scanner.setSource(template.toCharArray());
1778 int next = scanner.getNextToken();
1779 while (next == ITerminalSymbols.TokenNameCOMMENT_LINE
1780 || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
1781 || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
1782 next = scanner.getNextToken();
1784 return next == ITerminalSymbols.TokenNameEOF;
1785 } catch (InvalidInputException e) {
1791 * Hook method that gets called from <code>createType</code> to retrieve a
1792 * type comment. This default implementation returns the content of the
1793 * 'typecomment' template.
1795 * @return the type comment or <code>null</code> if a type comment is not
1798 protected String getTypeComment(ICompilationUnit parentCU) {
1799 if (PreferenceConstants.getPreferenceStore().getBoolean(
1800 PreferenceConstants.CODEGEN_ADD_COMMENTS)) {
1802 StringBuffer typeName = new StringBuffer();
1803 if (isEnclosingTypeSelected()) {
1806 .getTypeQualifiedName(getEnclosingType()))
1809 typeName.append(getTypeName());
1810 String comment = CodeGeneration.getTypeComment(parentCU,
1811 typeName.toString(), String.valueOf('\n'));
1812 if (comment != null && isValidComment(comment)) {
1815 } catch (CoreException e) {
1816 PHPeclipsePlugin.log(e);
1823 * @deprecated Use getTemplate(String,ICompilationUnit,int)
1825 protected String getTemplate(String name, ICompilationUnit parentCU) {
1826 return getTemplate(name, parentCU, 0);
1830 * Returns the string resulting from evaluation the given template in the
1831 * context of the given compilation unit. This accesses the normal template
1832 * page, not the code templates. To use code templates use
1833 * <code>constructCUContent</code> to construct a compilation unit stub or
1834 * getTypeComment for the comment of the type.
1837 * the template to be evaluated
1839 * the templates evaluation context
1841 * a source offset into the parent compilation unit. The template
1842 * is evalutated at the given source offset
1844 protected String getTemplate(String name, ICompilationUnit parentCU, int pos) {
1846 Template[] templates = Templates.getInstance().getTemplates(name);
1847 if (templates.length > 0) {
1849 .evaluateTemplate(templates[0], parentCU, pos);
1851 } catch (CoreException e) {
1852 PHPeclipsePlugin.log(e);
1853 } catch (BadLocationException e1) {
1854 // TODO Auto-generated catch block
1855 e1.printStackTrace();
1856 } catch (TemplateException e1) {
1857 // TODO Auto-generated catch block
1858 e1.printStackTrace();
1865 * createInheritedMethods(IType,boolean,boolean,IImportsManager,IProgressMonitor)
1867 // protected IMethod[] createInheritedMethods(IType type, boolean
1868 // doConstructors, boolean doUnimplementedMethods, IImportsStructure
1869 // imports, IProgressMonitor monitor) throws CoreException {
1870 // return createInheritedMethods(type, doConstructors,
1871 // doUnimplementedMethods, new ImportsManager(imports), monitor);
1874 * Creates the bodies of all unimplemented methods and constructors and adds
1875 * them to the type. Method is typically called by implementers of
1876 * <code>NewTypeWizardPage</code> to add needed method and constructors.
1879 * the type for which the new methods and constructor are to be
1881 * @param doConstructors
1882 * if <code>true</code> unimplemented constructors are created
1883 * @param doUnimplementedMethods
1884 * if <code>true</code> unimplemented methods are created
1886 * an import manager to add all neded import statements
1888 * a progress monitor to report progress
1890 // protected IMethod[] createInheritedMethods(IType type, boolean
1891 // doConstructors, boolean doUnimplementedMethods, ImportsManager imports,
1892 // IProgressMonitor monitor) throws CoreException {
1893 // ArrayList newMethods= new ArrayList();
1894 // ITypeHierarchy hierarchy= null;
1895 // CodeGenerationSettings settings=
1896 // JavaPreferencesSettings.getCodeGenerationSettings();
1898 // if (doConstructors) {
1899 // hierarchy= type.newSupertypeHierarchy(monitor);
1900 // IType superclass= hierarchy.getSuperclass(type);
1901 // if (superclass != null) {
1902 // String[] constructors= StubUtility.evalConstructors(type, superclass,
1903 // settings, imports.getImportsStructure());
1904 // if (constructors != null) {
1905 // for (int i= 0; i < constructors.length; i++) {
1906 // newMethods.add(constructors[i]);
1912 // if (doUnimplementedMethods) {
1913 // if (hierarchy == null) {
1914 // hierarchy= type.newSupertypeHierarchy(monitor);
1916 // String[] unimplemented= StubUtility.evalUnimplementedMethods(type,
1917 // hierarchy, false, settings, null, imports.getImportsStructure());
1918 // if (unimplemented != null) {
1919 // for (int i= 0; i < unimplemented.length; i++) {
1920 // newMethods.add(unimplemented[i]);
1924 // IMethod[] createdMethods= new IMethod[newMethods.size()];
1925 // for (int i= 0; i < newMethods.size(); i++) {
1926 // String content= (String) newMethods.get(i) + '\n'; // content will be
1927 // formatted, ok to use \n
1928 // createdMethods[i]= type.createMethod(content, null, false, null);
1930 // return createdMethods;
1932 // ---- creation ----------------
1934 * Returns the runnable that creates the type using the current settings.
1935 * The returned runnable must be executed in the UI thread.
1937 * @return the runnable to create the new type
1939 // public IRunnableWithProgress getRunnable() {
1940 // return new IRunnableWithProgress() {
1941 // public void run(IProgressMonitor monitor) throws
1942 // InvocationTargetException, InterruptedException {
1944 // if (monitor == null) {
1945 // monitor= new NullProgressMonitor();
1947 // createType(monitor);
1948 // } catch (CoreException e) {
1949 // throw new InvocationTargetException(e);