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.ITerminalSymbols.TokenName;
27 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
29 //import net.sourceforge.phpdt.externaltools.internal.ui.StatusInfo;
30 import net.sourceforge.phpeclipse.ui.StatusInfo;
31 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
32 //import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
33 //import net.sourceforge.phpdt.internal.corext.template.php.Templates;
34 //import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
35 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
36 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
37 import net.sourceforge.phpdt.internal.ui.wizards.NewWizardMessages;
38 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
39 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
40 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IListAdapter;
41 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
42 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
43 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ListDialogField;
44 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
45 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
46 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.Separator;
47 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
48 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField;
49 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringDialogField;
50 //import net.sourceforge.phpdt.ui.CodeGeneration;
51 //import net.sourceforge.phpdt.ui.PreferenceConstants;
52 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
54 import org.eclipse.core.runtime.CoreException;
55 import org.eclipse.core.runtime.IProgressMonitor;
56 import org.eclipse.core.runtime.IStatus;
57 import org.eclipse.core.runtime.NullProgressMonitor;
58 import org.eclipse.core.runtime.SubProgressMonitor;
59 //import org.eclipse.jface.preference.IPreferenceStore;
60 //import org.eclipse.jface.text.BadLocationException;
61 //import org.eclipse.jface.text.templates.Template;
62 //import org.eclipse.jface.text.templates.TemplateException;
63 import org.eclipse.jface.viewers.LabelProvider;
64 import org.eclipse.swt.SWT;
65 import org.eclipse.swt.graphics.Image;
66 import org.eclipse.swt.layout.GridData;
67 import org.eclipse.swt.layout.GridLayout;
68 import org.eclipse.swt.widgets.Button;
69 import org.eclipse.swt.widgets.Composite;
70 import org.eclipse.swt.widgets.Control;
73 * The class <code>NewTypeWizardPage</code> contains controls and validation
74 * routines for a 'New Type WizardPage'. Implementors decide which components to
75 * add and to enable. Implementors can also customize the validation code.
76 * <code>NewTypeWizardPage</code> is intended to serve as base class of all
77 * wizards that create types like applets, servlets, classes, interfaces, etc.
79 * See <code>NewClassWizardPage</code> or <code>NewInterfaceWizardPage</code>
80 * for an example usage of <code>NewTypeWizardPage</code>.
83 * @see net.sourceforge.phpdt.ui.wizards.NewClassWizardPage
84 * @see net.sourceforge.phpdt.ui.wizards.NewInterfaceWizardPage
88 public abstract class NewTypeWizardPage extends NewContainerWizardPage {
91 * Class used in stub creation routines to add needed imports to a
94 // public static class ImportsManager {
96 // private IImportsStructure fImportsStructure;
98 // /* package */ ImportsManager(IImportsStructure structure) {
99 // fImportsStructure= structure;
102 // /* package */ IImportsStructure getImportsStructure() {
103 // return fImportsStructure;
107 // * Adds a new import declaration that is sorted in the existing imports.
108 // * If an import already exists or the import would conflict with another
110 // * of an other type with the same simple name the import is not added.
112 // * @param qualifiedTypeName The fully qualified name of the type to import
114 // * @return Returns the simple type name that can be used in the code or
116 // * fully qualified type name if an import conflict prevented the import
118 // public String addImport(String qualifiedTypeName) {
119 // return fImportsStructure.addImport(qualifiedTypeName);
123 * Public access flag. See The Java Virtual Machine Specification for more
126 public int F_PUBLIC = Flags.AccPublic;
129 * Private access flag. See The Java Virtual Machine Specification for more
132 public int F_PRIVATE = Flags.AccPrivate;
135 * Protected access flag. See The Java Virtual Machine Specification for
138 public int F_PROTECTED = Flags.AccProtected;
141 * Static access flag. See The Java Virtual Machine Specification for more
144 public int F_STATIC = Flags.AccStatic;
147 * Final access flag. See The Java Virtual Machine Specification for more
150 public int F_FINAL = Flags.AccFinal;
153 * Abstract property flag. See The Java Virtual Machine Specification for
156 // public int F_ABSTRACT = Flags.AccAbstract;
157 private final static String PAGE_NAME = "NewTypeWizardPage"; //$NON-NLS-1$
159 /** Field ID of the package input field */
160 protected final static String PACKAGE = PAGE_NAME + ".package"; //$NON-NLS-1$
162 /** Field ID of the eclosing type input field */
163 protected final static String ENCLOSING = PAGE_NAME + ".enclosing"; //$NON-NLS-1$
165 /** Field ID of the enclosing type checkbox */
166 protected final static String ENCLOSINGSELECTION = ENCLOSING + ".selection"; //$NON-NLS-1$
168 /** Field ID of the type name input field */
169 protected final static String TYPENAME = PAGE_NAME + ".typename"; //$NON-NLS-1$
171 /** Field ID of the super type input field */
172 protected final static String SUPER = PAGE_NAME + ".superclass"; //$NON-NLS-1$
174 /** Field ID of the super interfaces input field */
175 protected final static String INTERFACES = PAGE_NAME + ".interfaces"; //$NON-NLS-1$
177 /** Field ID of the modifier checkboxes */
178 protected final static String MODIFIERS = PAGE_NAME + ".modifiers"; //$NON-NLS-1$
180 /** Field ID of the method stubs checkboxes */
181 protected final static String METHODS = PAGE_NAME + ".methods"; //$NON-NLS-1$
183 private class InterfacesListLabelProvider extends LabelProvider {
185 private Image fInterfaceImage;
187 public InterfacesListLabelProvider() {
189 fInterfaceImage = PHPUiImages.get(PHPUiImages.IMG_OBJS_INTERFACE);
192 public Image getImage(Object element) {
193 return fInterfaceImage;
197 private StringButtonStatusDialogField fPackageDialogField;
199 private SelectionButtonDialogField fEnclosingTypeSelection;
201 private StringButtonDialogField fEnclosingTypeDialogField;
203 private boolean fCanModifyPackage;
205 private boolean fCanModifyEnclosingType;
207 private IPackageFragment fCurrPackage;
209 // private IType fCurrEnclosingType;
210 private StringDialogField fTypeNameDialogField;
212 private StringButtonDialogField fSuperClassDialogField;
214 private ListDialogField fSuperInterfacesDialogField;
216 // private IType fSuperClass;
218 private SelectionButtonDialogFieldGroup fAccMdfButtons;
220 private SelectionButtonDialogFieldGroup fOtherMdfButtons;
222 private IType fCreatedType;
224 protected IStatus fEnclosingTypeStatus;
226 protected IStatus fPackageStatus;
228 protected IStatus fTypeNameStatus;
230 // protected IStatus fSuperClassStatus;
231 protected IStatus fModifierStatus;
233 // protected IStatus fSuperInterfacesStatus;
235 private boolean fIsClass;
237 private int fStaticMdfIndex;
239 private final int PUBLIC_INDEX = 0, DEFAULT_INDEX = 1, PRIVATE_INDEX = 2,
242 private final int ABSTRACT_INDEX = 0, FINAL_INDEX = 1;
245 * Creates a new <code>NewTypeWizardPage</code>
248 * <code>true</code> if a new class is to be created; otherwise
249 * an interface is to be created
251 * the wizard page's name
253 public NewTypeWizardPage(boolean isClass, String pageName) {
259 TypeFieldsAdapter adapter = new TypeFieldsAdapter();
261 fPackageDialogField = new StringButtonStatusDialogField(adapter);
262 fPackageDialogField.setDialogFieldListener(adapter);
263 fPackageDialogField.setLabelText(NewWizardMessages
264 .getString("NewTypeWizardPage.package.label")); //$NON-NLS-1$
265 fPackageDialogField.setButtonLabel(NewWizardMessages
266 .getString("NewTypeWizardPage.package.button")); //$NON-NLS-1$
267 fPackageDialogField.setStatusWidthHint(NewWizardMessages
268 .getString("NewTypeWizardPage.default")); //$NON-NLS-1$
270 fEnclosingTypeSelection = new SelectionButtonDialogField(SWT.CHECK);
271 fEnclosingTypeSelection.setDialogFieldListener(adapter);
272 fEnclosingTypeSelection.setLabelText(NewWizardMessages
273 .getString("NewTypeWizardPage.enclosing.selection.label")); //$NON-NLS-1$
275 fEnclosingTypeDialogField = new StringButtonDialogField(adapter);
276 fEnclosingTypeDialogField.setDialogFieldListener(adapter);
277 fEnclosingTypeDialogField.setButtonLabel(NewWizardMessages
278 .getString("NewTypeWizardPage.enclosing.button")); //$NON-NLS-1$
280 fTypeNameDialogField = new StringDialogField();
281 fTypeNameDialogField.setDialogFieldListener(adapter);
282 fTypeNameDialogField.setLabelText(NewWizardMessages
283 .getString("NewTypeWizardPage.typename.label")); //$NON-NLS-1$
285 fSuperClassDialogField = new StringButtonDialogField(adapter);
286 fSuperClassDialogField.setDialogFieldListener(adapter);
287 fSuperClassDialogField.setLabelText(NewWizardMessages
288 .getString("NewTypeWizardPage.superclass.label")); //$NON-NLS-1$
289 fSuperClassDialogField.setButtonLabel(NewWizardMessages
290 .getString("NewTypeWizardPage.superclass.button")); //$NON-NLS-1$
292 String[] addButtons = new String[] {
294 NewWizardMessages.getString("NewTypeWizardPage.interfaces.add"), //$NON-NLS-1$
299 .getString("NewTypeWizardPage.interfaces.remove") //$NON-NLS-1$
301 fSuperInterfacesDialogField = new ListDialogField(adapter, addButtons,
302 new InterfacesListLabelProvider());
303 fSuperInterfacesDialogField.setDialogFieldListener(adapter);
304 String interfaceLabel = fIsClass ? NewWizardMessages
305 .getString("NewTypeWizardPage.interfaces.class.label") : NewWizardMessages.getString("NewTypeWizardPage.interfaces.ifc.label"); //$NON-NLS-1$ //$NON-NLS-2$
306 fSuperInterfacesDialogField.setLabelText(interfaceLabel);
307 fSuperInterfacesDialogField.setRemoveButtonIndex(2);
309 String[] buttonNames1 = new String[] {
310 /* 0 == PUBLIC_INDEX */
312 .getString("NewTypeWizardPage.modifiers.public"), //$NON-NLS-1$
313 /* 1 == DEFAULT_INDEX */
315 .getString("NewTypeWizardPage.modifiers.default"), //$NON-NLS-1$
316 /* 2 == PRIVATE_INDEX */
318 .getString("NewTypeWizardPage.modifiers.private"), //$NON-NLS-1$
319 /* 3 == PROTECTED_INDEX */
321 .getString("NewTypeWizardPage.modifiers.protected") //$NON-NLS-1$
323 fAccMdfButtons = new SelectionButtonDialogFieldGroup(SWT.RADIO,
325 fAccMdfButtons.setDialogFieldListener(adapter);
326 fAccMdfButtons.setLabelText(NewWizardMessages
327 .getString("NewTypeWizardPage.modifiers.acc.label")); //$NON-NLS-1$
328 fAccMdfButtons.setSelection(0, true);
330 String[] buttonNames2;
332 buttonNames2 = new String[] {
333 /* 0 == ABSTRACT_INDEX */
335 .getString("NewTypeWizardPage.modifiers.abstract"), //$NON-NLS-1$
336 /* 1 == FINAL_INDEX */
338 .getString("NewTypeWizardPage.modifiers.final"), //$NON-NLS-1$
341 .getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
343 fStaticMdfIndex = 2; // index of the static checkbox is 2
345 buttonNames2 = new String[] { NewWizardMessages
346 .getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
348 fStaticMdfIndex = 0; // index of the static checkbox is 0
351 fOtherMdfButtons = new SelectionButtonDialogFieldGroup(SWT.CHECK,
353 fOtherMdfButtons.setDialogFieldListener(adapter);
355 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, false);
356 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, false);
357 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, false);
359 fPackageStatus = new StatusInfo();
360 fEnclosingTypeStatus = new StatusInfo();
362 fCanModifyPackage = true;
363 fCanModifyEnclosingType = true;
366 fTypeNameStatus = new StatusInfo();
367 // fSuperClassStatus= new StatusInfo();
368 // fSuperInterfacesStatus= new StatusInfo();
369 fModifierStatus = new StatusInfo();
373 * Initializes all fields provided by the page with a given selection.
376 * the selection used to intialize this page or <code>
378 * if no selection was available
380 protected void initTypePage(IJavaElement elem) {
381 String initSuperclass = "java.lang.Object"; //$NON-NLS-1$
382 ArrayList initSuperinterfaces = new ArrayList(5);
384 IPackageFragment pack = null;
385 IType enclosingType = null;
388 // evaluate the enclosing type
389 pack = (IPackageFragment) elem
390 .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
391 IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
392 if (typeInCU != null) {
393 if (typeInCU.getCompilationUnit() != null) {
394 enclosingType = typeInCU;
397 ICompilationUnit cu = (ICompilationUnit) elem
398 .getAncestor(IJavaElement.COMPILATION_UNIT);
400 // enclosingType= cu.findPrimaryType();
406 // if (elem.getElementType() == IJavaElement.TYPE) {
407 // type= (IType)elem;
408 // if (type.exists()) {
409 // String superName= JavaModelUtil.getFullyQualifiedName(type);
410 // if (type.isInterface()) {
411 // initSuperinterfaces.add(superName);
413 // initSuperclass= superName;
417 // } catch (JavaModelException e) {
418 // PHPeclipsePlugin.log(e);
419 // // ignore this exception now
423 setPackageFragment(pack, true);
424 // setEnclosingType(enclosingType, true);
425 setEnclosingTypeSelection(false, true);
427 setTypeName("", true); //$NON-NLS-1$
428 setSuperClass(initSuperclass, true);
429 setSuperInterfaces(initSuperinterfaces, true);
432 // -------- UI Creation ---------
435 * Creates a separator line. Expects a <code>GridLayout</code> with at
439 * the parent composite
441 * number of columns to span
443 protected void createSeparator(Composite composite, int nColumns) {
444 (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(
445 composite, nColumns, convertHeightInCharsToPixels(1));
449 * Creates the controls for the package name field. Expects a
450 * <code>GridLayout</code> with at least 4 columns.
453 * the parent composite
455 * number of columns to span
457 protected void createPackageControls(Composite composite, int nColumns) {
458 fPackageDialogField.doFillIntoGrid(composite, nColumns);
459 LayoutUtil.setWidthHint(fPackageDialogField.getTextControl(null),
461 LayoutUtil.setHorizontalGrabbing(fPackageDialogField
462 .getTextControl(null));
466 * Creates the controls for the enclosing type name field. Expects a
467 * <code>GridLayout</code> with at least 4 columns.
470 * the parent composite
472 * number of columns to span
474 protected void createEnclosingTypeControls(Composite composite, int nColumns) {
476 Composite tabGroup = new Composite(composite, SWT.NONE);
477 GridLayout layout = new GridLayout();
478 layout.marginWidth = 0;
479 layout.marginHeight = 0;
480 tabGroup.setLayout(layout);
482 fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1);
484 Control c = fEnclosingTypeDialogField.getTextControl(composite);
485 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
486 gd.widthHint = getMaxFieldWidth();
487 gd.horizontalSpan = 2;
490 Button button = fEnclosingTypeDialogField.getChangeControl(composite);
491 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
492 gd.heightHint = SWTUtil.getButtonHeightHint(button);
493 gd.widthHint = SWTUtil.getButtonWidthHint(button);
494 button.setLayoutData(gd);
498 * Creates the controls for the type name field. Expects a
499 * <code>GridLayout</code> with at least 2 columns.
502 * the parent composite
504 * number of columns to span
506 protected void createTypeNameControls(Composite composite, int nColumns) {
507 fTypeNameDialogField.doFillIntoGrid(composite, nColumns - 1);
508 DialogField.createEmptySpace(composite);
510 LayoutUtil.setWidthHint(fTypeNameDialogField.getTextControl(null),
515 * Creates the controls for the modifiers radio/ceckbox buttons. Expects a
516 * <code>GridLayout</code> with at least 3 columns.
519 * the parent composite
521 * number of columns to span
523 protected void createModifierControls(Composite composite, int nColumns) {
524 LayoutUtil.setHorizontalSpan(fAccMdfButtons.getLabelControl(composite),
527 Control control = fAccMdfButtons.getSelectionButtonsGroup(composite);
528 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
529 gd.horizontalSpan = nColumns - 2;
530 control.setLayoutData(gd);
532 DialogField.createEmptySpace(composite);
534 DialogField.createEmptySpace(composite);
536 control = fOtherMdfButtons.getSelectionButtonsGroup(composite);
537 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
538 gd.horizontalSpan = nColumns - 2;
539 control.setLayoutData(gd);
541 DialogField.createEmptySpace(composite);
545 * Creates the controls for the superclass name field. Expects a
546 * <code>GridLayout</code> with at least 3 columns.
549 * the parent composite
551 * number of columns to span
553 protected void createSuperClassControls(Composite composite, int nColumns) {
554 fSuperClassDialogField.doFillIntoGrid(composite, nColumns);
555 LayoutUtil.setWidthHint(fSuperClassDialogField.getTextControl(null),
560 * Creates the controls for the superclass name field. Expects a
561 * <code>GridLayout</code> with at least 3 columns.
564 * the parent composite
566 * number of columns to span
568 protected void createSuperInterfacesControls(Composite composite,
570 fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns);
571 GridData gd = (GridData) fSuperInterfacesDialogField.getListControl(
572 null).getLayoutData();
574 gd.heightHint = convertHeightInCharsToPixels(3);
576 gd.heightHint = convertHeightInCharsToPixels(6);
578 gd.grabExcessVerticalSpace = false;
579 gd.widthHint = getMaxFieldWidth();
583 * Sets the focus on the type name input field.
585 protected void setFocus() {
586 fTypeNameDialogField.setFocus();
589 // -------- TypeFieldsAdapter --------
591 private class TypeFieldsAdapter implements IStringButtonAdapter,
592 IDialogFieldListener, IListAdapter {
594 // -------- IStringButtonAdapter
595 public void changeControlPressed(DialogField field) {
596 // typePageChangeControlPressed(field);
599 // -------- IListAdapter
600 public void customButtonPressed(ListDialogField field, int index) {
601 // typePageCustomButtonPressed(field, index);
604 public void selectionChanged(ListDialogField field) {
607 // -------- IDialogFieldListener
608 public void dialogFieldChanged(DialogField field) {
609 typePageDialogFieldChanged(field);
612 public void doubleClicked(ListDialogField field) {
616 // private void typePageChangeControlPressed(DialogField field) {
617 // if (field == fPackageDialogField) {
618 // IPackageFragment pack= choosePackage();
619 // if (pack != null) {
620 // fPackageDialogField.setText(pack.getElementName());
622 // } else if (field == fEnclosingTypeDialogField) {
623 // IType type= chooseEnclosingType();
624 // if (type != null) {
625 // fEnclosingTypeDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
627 // } else if (field == fSuperClassDialogField) {
628 // IType type= chooseSuperType();
629 // if (type != null) {
630 // fSuperClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
635 // private void typePageCustomButtonPressed(DialogField field, int index) {
636 // if (field == fSuperInterfacesDialogField) {
637 // chooseSuperInterfaces();
642 * A field on the type has changed. The fields' status and all dependend
643 * status are updated.
645 private void typePageDialogFieldChanged(DialogField field) {
646 String fieldName = null;
647 if (field == fPackageDialogField) {
648 fPackageStatus = packageChanged();
649 updatePackageStatusLabel();
650 fTypeNameStatus = typeNameChanged();
651 // fSuperClassStatus= superClassChanged();
653 } else if (field == fEnclosingTypeDialogField) {
654 // fEnclosingTypeStatus= enclosingTypeChanged();
655 fTypeNameStatus = typeNameChanged();
656 // fSuperClassStatus= superClassChanged();
657 fieldName = ENCLOSING;
658 } else if (field == fEnclosingTypeSelection) {
660 boolean isEnclosedType = isEnclosingTypeSelected();
661 if (!isEnclosedType) {
662 if (fAccMdfButtons.isSelected(PRIVATE_INDEX)
663 || fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
664 fAccMdfButtons.setSelection(PRIVATE_INDEX, false);
665 fAccMdfButtons.setSelection(PROTECTED_INDEX, false);
666 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
668 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
669 fOtherMdfButtons.setSelection(fStaticMdfIndex, false);
672 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, isEnclosedType
674 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX,
675 isEnclosedType && fIsClass);
676 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex,
678 fTypeNameStatus = typeNameChanged();
679 // fSuperClassStatus= superClassChanged();
680 fieldName = ENCLOSINGSELECTION;
681 } else if (field == fTypeNameDialogField) {
682 fTypeNameStatus = typeNameChanged();
683 fieldName = TYPENAME;
684 } else if (field == fSuperClassDialogField) {
685 // fSuperClassStatus= superClassChanged();
687 } else if (field == fSuperInterfacesDialogField) {
688 // fSuperInterfacesStatus= superInterfacesChanged();
689 fieldName = INTERFACES;
690 } else if (field == fOtherMdfButtons) {
691 fModifierStatus = modifiersChanged();
692 fieldName = MODIFIERS;
697 handleFieldChanged(fieldName);
700 // -------- update message ----------------
703 * @see net.sourceforge.phpdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
705 protected void handleFieldChanged(String fieldName) {
706 //super.handleFieldChanged(fieldName);
707 if (fieldName == CONTAINER) {
708 fPackageStatus = packageChanged();
709 // fEnclosingTypeStatus= enclosingTypeChanged();
710 fTypeNameStatus = typeNameChanged();
711 // fSuperClassStatus= superClassChanged();
712 // fSuperInterfacesStatus= superInterfacesChanged();
716 // ---- set / get ----------------
719 * Returns the text of the package input field.
721 * @return the text of the package input field
723 public String getPackageText() {
724 return fPackageDialogField.getText();
728 * Returns the text of the enclosing type input field.
730 * @return the text of the enclosing type input field
732 // public String getEnclosingTypeText() {
733 // return fEnclosingTypeDialogField.getText();
737 * Returns the package fragment corresponding to the current input.
739 * @return a package fragement or <code>null</code> if the input could not
742 public IPackageFragment getPackageFragment() {
743 if (!isEnclosingTypeSelected()) {
746 // if (fCurrEnclosingType != null) {
747 // return fCurrEnclosingType.getPackageFragment();
754 * Sets the package fragment to the given value. The method updates the
755 * model and the text of the control.
758 * the package fragement to be set
759 * @param canBeModified
760 * if <code>true</code> the package fragment is editable;
761 * otherwise it is read-only.
763 public void setPackageFragment(IPackageFragment pack, boolean canBeModified) {
765 fCanModifyPackage = canBeModified;
766 String str = (pack == null) ? "" : pack.getElementName(); //$NON-NLS-1$
767 fPackageDialogField.setText(str);
772 * Returns the enclosing type corresponding to the current input.
774 * @return the enclosing type or <code>null</code> if the enclosing type
775 * is not selected or the input could not be resolved
777 public IType getEnclosingType() {
778 // if (isEnclosingTypeSelected()) {
779 // return fCurrEnclosingType;
785 * Sets the enclosing type. The method updates the underlying model and the
786 * text of the control.
790 * @param canBeModified
791 * if <code>true</code> the enclosing type field is editable;
792 * otherwise it is read-only.
794 // public void setEnclosingType(IType type, boolean canBeModified) {
795 // fCurrEnclosingType= type;
796 // fCanModifyEnclosingType= canBeModified;
797 // String str= (type == null) ? "" :
798 // JavaModelUtil.getFullyQualifiedName(type); //$NON-NLS-1$
799 // fEnclosingTypeDialogField.setText(str);
800 // updateEnableState();
803 * Returns the selection state of the enclosing type checkbox.
805 * @return the seleciton state of the enclosing type checkbox
807 public boolean isEnclosingTypeSelected() {
808 return fEnclosingTypeSelection.isSelected();
812 * Sets the enclosing type checkbox's selection state.
815 * the checkbox's selection state
816 * @param canBeModified
817 * if <code>true</code> the enclosing type checkbox is
818 * modifiable; otherwise it is read-only.
820 public void setEnclosingTypeSelection(boolean isSelected,
821 boolean canBeModified) {
822 fEnclosingTypeSelection.setSelection(isSelected);
823 fEnclosingTypeSelection.setEnabled(canBeModified);
828 * Returns the type name entered into the type input field.
830 * @return the type name
832 public String getTypeName() {
833 return fTypeNameDialogField.getText();
837 * Sets the type name input field's text to the given value. Method doesn't
842 * @param canBeModified
843 * if <code>true</code> the enclosing type name field is
844 * editable; otherwise it is read-only.
846 public void setTypeName(String name, boolean canBeModified) {
847 fTypeNameDialogField.setText(name);
848 fTypeNameDialogField.setEnabled(canBeModified);
852 * Returns the selected modifiers.
854 * @return the selected modifiers
857 // public int getModifiers() {
859 // if (fAccMdfButtons.isSelected(PUBLIC_INDEX)) {
861 // } else if (fAccMdfButtons.isSelected(PRIVATE_INDEX)) {
863 // } else if (fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
864 // mdf += F_PROTECTED;
866 // // if (fOtherMdfButtons.isSelected(ABSTRACT_INDEX) && (fStaticMdfIndex
868 // // mdf+= F_ABSTRACT;
870 // if (fOtherMdfButtons.isSelected(FINAL_INDEX)) {
873 // if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
880 * Sets the modifiers.
883 * <code>F_PUBLIC</code>, <code>F_PRIVATE</code>,
884 * <code>F_PROTECTED</code>, <code>F_ABSTRACT, F_FINAL</code>
885 * or <code>F_STATIC</code> or, a valid combination.
886 * @param canBeModified
887 * if <code>true</code> the modifier fields are editable;
888 * otherwise they are read-only
891 // public void setModifiers(int modifiers, boolean canBeModified) {
892 // if (Flags.isPublic(modifiers)) {
893 // fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
894 // } else if (Flags.isPrivate(modifiers)) {
895 // fAccMdfButtons.setSelection(PRIVATE_INDEX, true);
896 // } else if (Flags.isProtected(modifiers)) {
897 // fAccMdfButtons.setSelection(PROTECTED_INDEX, true);
899 // fAccMdfButtons.setSelection(DEFAULT_INDEX, true);
901 // // if (Flags.isAbstract(modifiers)) {
902 // // fOtherMdfButtons.setSelection(ABSTRACT_INDEX, true);
904 // if (Flags.isFinal(modifiers)) {
905 // fOtherMdfButtons.setSelection(FINAL_INDEX, true);
907 // if (Flags.isStatic(modifiers)) {
908 // fOtherMdfButtons.setSelection(fStaticMdfIndex, true);
911 // fAccMdfButtons.setEnabled(canBeModified);
912 // fOtherMdfButtons.setEnabled(canBeModified);
916 * Returns the content of the superclass input field.
918 * @return the superclass name
920 // public String getSuperClass() {
921 // return fSuperClassDialogField.getText();
925 * Sets the super class name.
928 * the new superclass name
929 * @param canBeModified
930 * if <code>true</code> the superclass name field is editable;
931 * otherwise it is read-only.
933 public void setSuperClass(String name, boolean canBeModified) {
934 fSuperClassDialogField.setText(name);
935 fSuperClassDialogField.setEnabled(canBeModified);
939 * Returns the chosen super interfaces.
941 * @return a list of chosen super interfaces. The list's elements are of
942 * type <code>String</code>
944 // public List getSuperInterfaces() {
945 // return fSuperInterfacesDialogField.getElements();
949 * Sets the super interfaces.
951 * @param interfacesNames
952 * a list of super interface. The method requires that the list's
953 * elements are of type <code>String</code>
954 * @param canBeModified
955 * if <code>true</code> the super interface field is editable;
956 * otherwise it is read-only.
958 public void setSuperInterfaces(List interfacesNames, boolean canBeModified) {
959 fSuperInterfacesDialogField.setElements(interfacesNames);
960 fSuperInterfacesDialogField.setEnabled(canBeModified);
963 // ----------- validation ----------
966 * A hook method that gets called when the package field has changed. The
967 * method validates the package name and returns the status of the
968 * validation. The validation also updates the package fragment model.
970 * Subclasses may extend this method to perform their own validation.
973 * @return the status of the validation
975 protected IStatus packageChanged() {
976 StatusInfo status = new StatusInfo();
977 fPackageDialogField.enableButton(getPackageFragmentRoot() != null);
979 // String packName= getPackageText();
980 // if (packName.length() > 0) {
981 // IStatus val= JavaConventions.validatePackageName(packName);
982 // if (val.getSeverity() == IStatus.ERROR) {
983 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidPackageName",
984 // val.getMessage())); //$NON-NLS-1$
986 // } else if (val.getSeverity() == IStatus.WARNING) {
987 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.DiscouragedPackageName",
988 // val.getMessage())); //$NON-NLS-1$
993 // IPackageFragmentRoot root= getPackageFragmentRoot();
994 // if (root != null) {
995 // if (root.getJavaProject().exists() && packName.length() > 0) {
997 // IPath rootPath= root.getPath();
998 // IPath outputPath= root.getJavaProject().getOutputLocation();
999 // if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath))
1001 // // if the bin folder is inside of our root, dont allow to name a
1003 // // like the bin folder
1004 // IPath packagePath= rootPath.append(packName.replace('.', '/'));
1005 // if (outputPath.isPrefixOf(packagePath)) {
1006 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ClashOutputLocation"));
1011 // } catch (JavaModelException e) {
1012 // PHPeclipsePlugin.log(e);
1017 // fCurrPackage= root.getPackageFragment(packName);
1019 // status.setError(""); //$NON-NLS-1$
1025 * Updates the 'default' label next to the package field.
1027 private void updatePackageStatusLabel() {
1028 String packName = getPackageText();
1030 if (packName.length() == 0) {
1031 fPackageDialogField.setStatus(NewWizardMessages
1032 .getString("NewTypeWizardPage.default")); //$NON-NLS-1$
1034 fPackageDialogField.setStatus(""); //$NON-NLS-1$
1039 * Updates the enable state of buttons related to the enclosing type
1040 * selection checkbox.
1042 private void updateEnableState() {
1043 boolean enclosing = isEnclosingTypeSelected();
1044 fPackageDialogField.setEnabled(fCanModifyPackage && !enclosing);
1045 fEnclosingTypeDialogField.setEnabled(fCanModifyEnclosingType
1050 * Hook method that gets called when the enclosing type name has changed.
1051 * The method validates the enclosing type and returns the status of the
1052 * validation. It also updates the enclosing type model.
1054 * Subclasses may extend this method to perform their own validation.
1057 * @return the status of the validation
1059 // protected IStatus enclosingTypeChanged() {
1060 // StatusInfo status= new StatusInfo();
1061 // fCurrEnclosingType= null;
1063 // IPackageFragmentRoot root= getPackageFragmentRoot();
1065 // fEnclosingTypeDialogField.enableButton(root != null);
1066 // if (root == null) {
1067 // status.setError(""); //$NON-NLS-1$
1071 // String enclName= getEnclosingTypeText();
1072 // if (enclName.length() == 0) {
1073 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeEnterName"));
1078 // IType type= findType(root.getJavaProject(), enclName);
1079 // if (type == null) {
1080 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists"));
1085 // if (type.getCompilationUnit() == null) {
1086 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotInCU"));
1090 // if (!JavaModelUtil.isEditable(type.getCompilationUnit())) {
1091 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotEditable"));
1096 // fCurrEnclosingType= type;
1097 // IPackageFragmentRoot enclosingRoot=
1098 // JavaModelUtil.getPackageFragmentRoot(type);
1099 // if (!enclosingRoot.equals(root)) {
1100 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.EnclosingNotInSourceFolder"));
1104 // } catch (JavaModelException e) {
1105 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists"));
1107 // PHPeclipsePlugin.log(e);
1112 * Hook method that gets called when the type name has changed. The method
1113 * validates the type name and returns the status of the validation.
1115 * Subclasses may extend this method to perform their own validation.
1118 * @return the status of the validation
1120 protected IStatus typeNameChanged() {
1121 StatusInfo status = new StatusInfo();
1122 String typeName = getTypeName();
1123 // must not be empty
1124 if (typeName.length() == 0) {
1125 status.setError(NewWizardMessages
1126 .getString("NewTypeWizardPage.error.EnterTypeName")); //$NON-NLS-1$
1129 if (typeName.indexOf('.') != -1) {
1130 status.setError(NewWizardMessages
1131 .getString("NewTypeWizardPage.error.QualifiedName")); //$NON-NLS-1$
1134 // IStatus val= JavaConventions.validateJavaTypeName(typeName);
1135 // if (val.getSeverity() == IStatus.ERROR) {
1136 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidTypeName",
1137 // val.getMessage())); //$NON-NLS-1$
1139 // } else if (val.getSeverity() == IStatus.WARNING) {
1140 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.TypeNameDiscouraged",
1141 // val.getMessage())); //$NON-NLS-1$
1142 // // continue checking
1146 if (!isEnclosingTypeSelected()) {
1147 IPackageFragment pack = getPackageFragment();
1149 ICompilationUnit cu = pack.getCompilationUnit(typeName
1150 + ".java"); //$NON-NLS-1$
1151 if (cu.getResource().exists()) {
1153 .setError(NewWizardMessages
1154 .getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1159 IType type = getEnclosingType();
1161 IType member = type.getType(typeName);
1162 if (member.exists()) {
1164 .setError(NewWizardMessages
1165 .getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1174 * Hook method that gets called when the superclass name has changed. The
1175 * method validates the superclass name and returns the status of the
1178 * Subclasses may extend this method to perform their own validation.
1181 * @return the status of the validation
1183 // protected IStatus superClassChanged() {
1184 // StatusInfo status= new StatusInfo();
1185 // IPackageFragmentRoot root= getPackageFragmentRoot();
1186 // fSuperClassDialogField.enableButton(root != null);
1188 // fSuperClass= null;
1190 // String sclassName= getSuperClass();
1191 // if (sclassName.length() == 0) {
1192 // // accept the empty field (stands for java.lang.Object)
1195 // IStatus val= JavaConventions.validateJavaTypeName(sclassName);
1196 // if (val.getSeverity() == IStatus.ERROR) {
1197 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName"));
1201 // if (root != null) {
1203 // IType type= resolveSuperTypeName(root.getJavaProject(), sclassName);
1204 // if (type == null) {
1205 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.SuperClassNotExists"));
1209 // if (type.isInterface()) {
1210 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotClass",
1211 // sclassName)); //$NON-NLS-1$
1214 // int flags= type.getFlags();
1215 // if (Flags.isFinal(flags)) {
1216 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsFinal",
1217 // sclassName)); //$NON-NLS-1$
1219 // } else if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1220 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotVisible",
1221 // sclassName)); //$NON-NLS-1$
1225 // fSuperClass= type;
1226 // } catch (JavaModelException e) {
1227 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName"));
1229 // PHPeclipsePlugin.log(e);
1232 // status.setError(""); //$NON-NLS-1$
1237 // private IType resolveSuperTypeName(IJavaProject jproject, String
1238 // sclassName) throws JavaModelException {
1239 // if (!jproject.exists()) {
1242 // IType type= null;
1243 // if (isEnclosingTypeSelected()) {
1244 // // search in the context of the enclosing type
1245 // IType enclosingType= getEnclosingType();
1246 // if (enclosingType != null) {
1247 // String[][] res= enclosingType.resolveType(sclassName);
1248 // if (res != null && res.length > 0) {
1249 // type= jproject.findType(res[0][0], res[0][1]);
1253 // IPackageFragment currPack= getPackageFragment();
1254 // if (type == null && currPack != null) {
1255 // String packName= currPack.getElementName();
1256 // // search in own package
1257 // if (!currPack.isDefaultPackage()) {
1258 // type= jproject.findType(packName, sclassName);
1260 // // search in java.lang
1261 // if (type == null && !"java.lang".equals(packName)) { //$NON-NLS-1$
1262 // type= jproject.findType("java.lang", sclassName); //$NON-NLS-1$
1265 // // search fully qualified
1266 // if (type == null) {
1267 // type= jproject.findType(sclassName);
1272 // private IType findType(IJavaProject project, String typeName) throws
1273 // JavaModelException {
1274 // if (project.exists()) {
1275 // return project.findType(typeName);
1280 * Hook method that gets called when the list of super interface has
1281 * changed. The method validates the superinterfaces and returns the status
1282 * of the validation.
1284 * Subclasses may extend this method to perform their own validation.
1287 * @return the status of the validation
1289 // protected IStatus superInterfacesChanged() {
1290 // StatusInfo status= new StatusInfo();
1292 // IPackageFragmentRoot root= getPackageFragmentRoot();
1293 // fSuperInterfacesDialogField.enableButton(0, root != null);
1295 // if (root != null) {
1296 // List elements= fSuperInterfacesDialogField.getElements();
1297 // int nElements= elements.size();
1298 // for (int i= 0; i < nElements; i++) {
1299 // String intfname= (String)elements.get(i);
1301 // IType type= findType(root.getJavaProject(), intfname);
1302 // if (type == null) {
1303 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceNotExists",
1304 // intfname)); //$NON-NLS-1$
1307 // if (type.isClass()) {
1308 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotInterface",
1309 // intfname)); //$NON-NLS-1$
1312 // if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1313 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotVisible",
1314 // intfname)); //$NON-NLS-1$
1318 // } catch (JavaModelException e) {
1319 // PHPeclipsePlugin.log(e);
1320 // // let pass, checking is an extra
1327 * Hook method that gets called when the modifiers have changed. The method
1328 * validates the modifiers and returns the status of the validation.
1330 * Subclasses may extend this method to perform their own validation.
1333 * @return the status of the validation
1335 protected IStatus modifiersChanged() {
1336 StatusInfo status = new StatusInfo();
1337 //int modifiers = getModifiers();
1339 // if (Flags.isFinal(modifiers) && Flags.isAbstract(modifiers)) {
1340 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ModifiersFinalAndAbstract"));
1346 // selection dialogs
1348 // private IPackageFragment choosePackage() {
1349 // IPackageFragmentRoot froot= getPackageFragmentRoot();
1350 // IJavaElement[] packages= null;
1352 // if (froot != null && froot.exists()) {
1353 // packages= froot.getChildren();
1355 // } catch (JavaModelException e) {
1356 // PHPeclipsePlugin.log(e);
1358 // if (packages == null) {
1359 // packages= new IJavaElement[0];
1362 // ElementListSelectionDialog dialog= new
1363 // ElementListSelectionDialog(getShell(), new
1364 // JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
1365 // dialog.setIgnoreCase(false);
1366 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.title"));
1368 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.description"));
1370 // dialog.setEmptyListMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.empty"));
1372 // dialog.setElements(packages);
1373 // IPackageFragment pack= getPackageFragment();
1374 // if (pack != null) {
1375 // dialog.setInitialSelections(new Object[] { pack });
1378 // if (dialog.open() == ElementListSelectionDialog.OK) {
1379 // return (IPackageFragment) dialog.getFirstResult();
1384 // private IType chooseEnclosingType() {
1385 // IPackageFragmentRoot root= getPackageFragmentRoot();
1386 // if (root == null) {
1390 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new
1391 // IJavaElement[] { root });
1393 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(),
1394 // getWizard().getContainer(), IJavaSearchConstants.TYPE, scope);
1395 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.title"));
1397 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.description"));
1399 // dialog.setFilter(Signature.getSimpleName(getEnclosingTypeText()));
1401 // if (dialog.open() == TypeSelectionDialog.OK) {
1402 // return (IType) dialog.getFirstResult();
1407 // private IType chooseSuperType() {
1408 // IPackageFragmentRoot root= getPackageFragmentRoot();
1409 // if (root == null) {
1413 // IJavaElement[] elements= new IJavaElement[] { root.getJavaProject() };
1414 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
1416 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(),
1417 // getWizard().getContainer(), IJavaSearchConstants.CLASS, scope);
1418 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.title"));
1420 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.message"));
1422 // dialog.setFilter(getSuperClass());
1424 // if (dialog.open() == TypeSelectionDialog.OK) {
1425 // return (IType) dialog.getFirstResult();
1430 // private void chooseSuperInterfaces() {
1431 // IPackageFragmentRoot root= getPackageFragmentRoot();
1432 // if (root == null) {
1436 // IJavaProject project= root.getJavaProject();
1437 // SuperInterfaceSelectionDialog dialog= new
1438 // SuperInterfaceSelectionDialog(getShell(), getWizard().getContainer(),
1439 // fSuperInterfacesDialogField, project);
1440 // dialog.setTitle(fIsClass ?
1441 // NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.class.title")
1443 // NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.interface.title"));
1444 // //$NON-NLS-1$ //$NON-NLS-2$
1445 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.message"));
1451 // ---- creation ----------------
1454 * Creates the new type using the entered field values.
1457 * a progress monitor to report progress.
1459 public void createType(IProgressMonitor monitor) throws CoreException,
1460 InterruptedException {
1461 if (monitor == null) {
1462 monitor = new NullProgressMonitor();
1465 monitor.beginTask(NewWizardMessages
1466 .getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$
1467 ICompilationUnit createdWorkingCopy = null;
1469 // IPackageFragmentRoot root = getPackageFragmentRoot();
1470 // IPackageFragment pack = getPackageFragment();
1471 // if (pack == null) {
1472 // pack = root.getPackageFragment(""); //$NON-NLS-1$
1475 // if (!pack.exists()) {
1476 // String packName = pack.getElementName();
1477 // pack = root.createPackageFragment(packName, true, null);
1482 String clName = getTypeName();
1484 boolean isInnerClass = isEnclosingTypeSelected();
1487 // ImportsStructure imports;
1490 //IPreferenceStore store = PreferenceConstants.getPreferenceStore();
1491 // String[] prefOrder =
1492 // JavaPreferencesSettings.getImportOrderPreference(store);
1494 // JavaPreferencesSettings.getImportNumberThreshold(store);
1496 String lineDelimiter = null;
1497 // if (!isInnerClass) {
1498 lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1500 // ICompilationUnit parentCU = pack.createCompilationUnit(clName +
1501 // ".php", "", false, new SubProgressMonitor(monitor, 2));
1502 // //$NON-NLS-1$ //$NON-NLS-2$
1503 // createdWorkingCopy = (ICompilationUnit)
1504 // parentCU.getSharedWorkingCopy(null, JavaUI.getBufferFactory(),
1507 // imports = new ImportsStructure(createdWorkingCopy, prefOrder,
1508 // threshold, false);
1509 // // add an import that will be removed again. Having this import
1511 // imports.addImport(pack.getElementName(), getTypeName());
1513 //String typeContent = constructTypeStub(lineDelimiter);// new
1514 // ImportsManager(imports),
1517 // String cuContent = constructCUContent(parentCU, typeContent,
1520 // createdWorkingCopy.getBuffer().setContents(cuContent);
1522 createdType = createdWorkingCopy.getType(clName);
1524 // IType enclosingType = getEnclosingType();
1526 // // if we are working on a enclosed type that is open in an
1528 // // then replace the enclosing type with its working copy
1529 // IType workingCopy = (IType)
1530 // EditorUtility.getWorkingCopy(enclosingType);
1531 // if (workingCopy != null) {
1532 // enclosingType = workingCopy;
1535 // ICompilationUnit parentCU = enclosingType.getCompilationUnit();
1536 // imports = new ImportsStructure(parentCU, prefOrder, threshold,
1539 // // add imports that will be removed again. Having the imports
1541 // IType[] topLevelTypes = parentCU.getTypes();
1542 // for (int i = 0; i < topLevelTypes.length; i++) {
1543 // imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
1546 // lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
1547 // StringBuffer content = new StringBuffer();
1548 // String comment = getTypeComment(parentCU);
1549 // if (comment != null) {
1550 // content.append(comment);
1551 // content.append(lineDelimiter);
1553 // content.append(constructTypeStub(new ImportsManager(imports),
1555 // IJavaElement[] elems = enclosingType.getChildren();
1556 // IJavaElement sibling = elems.length > 0 ? elems[0] : null;
1558 // createdType = enclosingType.createType(content.toString(),
1559 // sibling, false, new SubProgressMonitor(monitor, 1));
1561 // indent = StubUtility.getIndentUsed(enclosingType) + 1;
1564 // // add imports for superclass/interfaces, so types can be
1565 // resolved correctly
1566 // imports.create(false, new SubProgressMonitor(monitor, 1));
1568 ICompilationUnit cu = createdType.getCompilationUnit();
1572 // createTypeMembers(createdType, new ImportsManager(imports), new
1573 // SubProgressMonitor(monitor, 1));
1576 // imports.create(false, new SubProgressMonitor(monitor, 1));
1581 ISourceRange range = createdType.getSourceRange();
1583 IBuffer buf = cu.getBuffer();
1584 String originalContent = buf.getText(range.getOffset(), range
1586 String formattedContent = StubUtility.codeFormat(originalContent,
1587 indent, lineDelimiter);
1588 buf.replace(range.getOffset(), range.getLength(), formattedContent);
1589 if (!isInnerClass) {
1590 String fileComment = getFileComment(cu);
1591 if (fileComment != null && fileComment.length() > 0) {
1592 buf.replace(0, 0, fileComment + lineDelimiter);
1594 cu.commit(false, new SubProgressMonitor(monitor, 1));
1598 fCreatedType = createdType;
1600 if (createdWorkingCopy != null) {
1601 createdWorkingCopy.destroy();
1608 * Uses the New Java file template from the code template page to generate a
1609 * compilation unit with the given type content.
1612 * The new created compilation unit
1613 * @param typeContent
1614 * The content of the type, including signature and type body.
1615 * @param lineDelimiter
1616 * The line delimiter to be used.
1617 * @return String Returns the result of evaluating the new file template
1618 * with the given type content.
1619 * @throws CoreException
1622 // protected String constructCUContent(ICompilationUnit cu, String
1623 // typeContent, String lineDelimiter) throws CoreException {
1624 // StringBuffer typeQualifiedName= new StringBuffer();
1625 // if (isEnclosingTypeSelected()) {
1626 // typeQualifiedName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1628 // typeQualifiedName.append(getTypeName());
1629 // String typeComment= CodeGeneration.getTypeComment(cu,
1630 // typeQualifiedName.toString(), lineDelimiter);
1631 // IPackageFragment pack= (IPackageFragment) cu.getParent();
1632 // String content= CodeGeneration.getCompilationUnitContent(cu, typeComment,
1633 // typeContent, lineDelimiter);
1634 // if (content != null) {
1635 // CompilationUnit unit= AST.parseCompilationUnit(content.toCharArray());
1636 // if ((pack.isDefaultPackage() || unit.getPackage() != null) &&
1637 // !unit.types().isEmpty()) {
1641 // StringBuffer buf= new StringBuffer();
1642 // if (!pack.isDefaultPackage()) {
1643 // buf.append("package ").append(pack.getElementName()).append(';');
1646 // buf.append(lineDelimiter).append(lineDelimiter);
1647 // if (typeComment != null) {
1648 // buf.append(typeComment).append(lineDelimiter);
1650 // buf.append(typeContent);
1651 // return buf.toString();
1654 * Returns the created type. The method only returns a valid type after
1655 * <code>createType</code> has been called.
1657 * @return the created type
1658 * @see #createType(IProgressMonitor)
1660 public IType getCreatedType() {
1661 return fCreatedType;
1664 // ---- construct cu body----------------
1666 // private void writeSuperClass(StringBuffer buf, ImportsManager imports) {
1667 // String typename= getSuperClass();
1668 // if (fIsClass && typename.length() > 0 &&
1669 // !"java.lang.Object".equals(typename)) { //$NON-NLS-1$
1670 // buf.append(" extends "); //$NON-NLS-1$
1672 // String qualifiedName= fSuperClass != null ?
1673 // JavaModelUtil.getFullyQualifiedName(fSuperClass) : typename;
1674 // buf.append(imports.addImport(qualifiedName));
1678 // private void writeSuperInterfaces(StringBuffer buf, ImportsManager
1680 // List interfaces= getSuperInterfaces();
1681 // int last= interfaces.size() - 1;
1684 // buf.append(" implements "); //$NON-NLS-1$
1686 // buf.append(" extends "); //$NON-NLS-1$
1688 // for (int i= 0; i <= last; i++) {
1689 // String typename= (String) interfaces.get(i);
1690 // buf.append(imports.addImport(typename));
1699 * Called from createType to construct the source for this type
1701 // private String constructTypeStub(String lineDelimiter) { // ImportsManager
1704 // // lineDelimiter)
1706 // StringBuffer buf = new StringBuffer();
1708 // int modifiers = getModifiers();
1709 // buf.append(Flags.toString(modifiers));
1710 // if (modifiers != 0) {
1713 // buf.append(fIsClass ? "class " : "interface "); //$NON-NLS-2$ //$NON-NLS-1$
1714 // buf.append(getTypeName());
1715 // // writeSuperClass(buf, imports);
1716 // // writeSuperInterfaces(buf, imports);
1718 // buf.append(lineDelimiter);
1719 // buf.append(lineDelimiter);
1721 // buf.append(lineDelimiter);
1722 // return buf.toString();
1726 * @deprecated Overwrite createTypeMembers(IType, IImportsManager,
1727 * IProgressMonitor) instead
1729 // protected void createTypeMembers(IType newType, IImportsStructure
1730 // imports, IProgressMonitor monitor) throws CoreException {
1734 * Hook method that gets called from <code>createType</code> to support
1735 * adding of unanticipated methods, fields, and inner types to the created
1738 * Implementers can use any methods defined on <code>IType</code> to
1739 * manipulate the new type.
1742 * The source code of the new type will be formtted using the platform's
1743 * formatter. Needed imports are added by the wizard at the end of the type
1744 * creation process using the given import manager.
1748 * the new type created via <code>createType</code>
1750 * an import manager which can be used to add new imports
1752 * a progress monitor to report progress. Must not be
1755 * @see #createType(IProgressMonitor)
1757 // protected void createTypeMembers(IType newType, ImportsManager imports,
1758 // IProgressMonitor monitor) throws CoreException {
1759 // // call for compatibility
1760 // createTypeMembers(newType,
1761 // ((ImportsManager)imports).getImportsStructure(), monitor);
1763 // // default implementation does nothing
1764 // // example would be
1765 // // String mainMathod= "public void foo(Vector vec) {}"
1766 // // createdType.createMethod(main, null, false, null);
1767 // // imports.addImport("java.lang.Vector");
1770 * @deprecated Instead of file templates, the new type code template
1771 * specifies the stub for a compilation unit.
1773 protected String getFileComment(ICompilationUnit parentCU) {
1777 private boolean isValidComment(String template) {
1778 IScanner scanner = ToolFactory.createScanner(true, false, false); // ,
1780 scanner.setSource(template.toCharArray());
1782 TokenName next = scanner.getNextToken();
1783 while (next == ITerminalSymbols.TokenName.COMMENT_LINE
1784 || next == ITerminalSymbols.TokenName.COMMENT_PHPDOC
1785 || next == ITerminalSymbols.TokenName.COMMENT_BLOCK) {
1786 next = scanner.getNextToken();
1788 return next == ITerminalSymbols.TokenName.EOF;
1789 } catch (InvalidInputException e) {
1795 * Hook method that gets called from <code>createType</code> to retrieve a
1796 * type comment. This default implementation returns the content of the
1797 * 'typecomment' template.
1799 * @return the type comment or <code>null</code> if a type comment is not
1802 // protected String getTypeComment(ICompilationUnit parentCU) {
1803 // if (PreferenceConstants.getPreferenceStore().getBoolean(
1804 // PreferenceConstants.CODEGEN_ADD_COMMENTS)) {
1806 // StringBuffer typeName = new StringBuffer();
1807 // if (isEnclosingTypeSelected()) {
1810 // .getTypeQualifiedName(getEnclosingType()))
1813 // typeName.append(getTypeName());
1814 // String comment = CodeGeneration.getTypeComment(parentCU,
1815 // typeName.toString(), String.valueOf('\n'));
1816 // if (comment != null && isValidComment(comment)) {
1819 // } catch (CoreException e) {
1820 // PHPeclipsePlugin.log(e);
1827 * @deprecated Use getTemplate(String,ICompilationUnit,int)
1829 // protected String getTemplate(String name, ICompilationUnit parentCU) {
1830 // return getTemplate(name, parentCU, 0);
1834 * Returns the string resulting from evaluation the given template in the
1835 * context of the given compilation unit. This accesses the normal template
1836 * page, not the code templates. To use code templates use
1837 * <code>constructCUContent</code> to construct a compilation unit stub or
1838 * getTypeComment for the comment of the type.
1841 * the template to be evaluated
1843 * the templates evaluation context
1845 * a source offset into the parent compilation unit. The template
1846 * is evalutated at the given source offset
1848 // protected String getTemplate(String name, ICompilationUnit parentCU, int pos) {
1850 // Template[] templates = Templates.getInstance().getTemplates(name);
1851 // if (templates.length > 0) {
1852 // return JavaContext
1853 // .evaluateTemplate(templates[0], parentCU, pos);
1855 // } catch (CoreException e) {
1856 // PHPeclipsePlugin.log(e);
1857 // } catch (BadLocationException e1) {
1858 // // TODO Auto-generated catch block
1859 // e1.printStackTrace();
1860 // } catch (TemplateException e1) {
1861 // // TODO Auto-generated catch block
1862 // e1.printStackTrace();
1869 * createInheritedMethods(IType,boolean,boolean,IImportsManager,IProgressMonitor)
1871 // protected IMethod[] createInheritedMethods(IType type, boolean
1872 // doConstructors, boolean doUnimplementedMethods, IImportsStructure
1873 // imports, IProgressMonitor monitor) throws CoreException {
1874 // return createInheritedMethods(type, doConstructors,
1875 // doUnimplementedMethods, new ImportsManager(imports), monitor);
1878 * Creates the bodies of all unimplemented methods and constructors and adds
1879 * them to the type. Method is typically called by implementers of
1880 * <code>NewTypeWizardPage</code> to add needed method and constructors.
1883 * the type for which the new methods and constructor are to be
1885 * @param doConstructors
1886 * if <code>true</code> unimplemented constructors are created
1887 * @param doUnimplementedMethods
1888 * if <code>true</code> unimplemented methods are created
1890 * an import manager to add all neded import statements
1892 * a progress monitor to report progress
1894 // protected IMethod[] createInheritedMethods(IType type, boolean
1895 // doConstructors, boolean doUnimplementedMethods, ImportsManager imports,
1896 // IProgressMonitor monitor) throws CoreException {
1897 // ArrayList newMethods= new ArrayList();
1898 // ITypeHierarchy hierarchy= null;
1899 // CodeGenerationSettings settings=
1900 // JavaPreferencesSettings.getCodeGenerationSettings();
1902 // if (doConstructors) {
1903 // hierarchy= type.newSupertypeHierarchy(monitor);
1904 // IType superclass= hierarchy.getSuperclass(type);
1905 // if (superclass != null) {
1906 // String[] constructors= StubUtility.evalConstructors(type, superclass,
1907 // settings, imports.getImportsStructure());
1908 // if (constructors != null) {
1909 // for (int i= 0; i < constructors.length; i++) {
1910 // newMethods.add(constructors[i]);
1916 // if (doUnimplementedMethods) {
1917 // if (hierarchy == null) {
1918 // hierarchy= type.newSupertypeHierarchy(monitor);
1920 // String[] unimplemented= StubUtility.evalUnimplementedMethods(type,
1921 // hierarchy, false, settings, null, imports.getImportsStructure());
1922 // if (unimplemented != null) {
1923 // for (int i= 0; i < unimplemented.length; i++) {
1924 // newMethods.add(unimplemented[i]);
1928 // IMethod[] createdMethods= new IMethod[newMethods.size()];
1929 // for (int i= 0; i < newMethods.size(); i++) {
1930 // String content= (String) newMethods.get(i) + '\n'; // content will be
1931 // formatted, ok to use \n
1932 // createdMethods[i]= type.createMethod(content, null, false, null);
1934 // return createdMethods;
1936 // ---- creation ----------------
1938 * Returns the runnable that creates the type using the current settings.
1939 * The returned runnable must be executed in the UI thread.
1941 * @return the runnable to create the new type
1943 // public IRunnableWithProgress getRunnable() {
1944 // return new IRunnableWithProgress() {
1945 // public void run(IProgressMonitor monitor) throws
1946 // InvocationTargetException, InterruptedException {
1948 // if (monitor == null) {
1949 // monitor= new NullProgressMonitor();
1951 // createType(monitor);
1952 // } catch (CoreException e) {
1953 // throw new InvocationTargetException(e);