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.IPackageFragmentRoot;
22 import net.sourceforge.phpdt.core.ISourceRange;
23 import net.sourceforge.phpdt.core.IType;
24 import net.sourceforge.phpdt.core.ToolFactory;
25 import net.sourceforge.phpdt.core.compiler.IScanner;
26 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
27 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
28 import net.sourceforge.phpdt.externaltools.internal.ui.StatusInfo;
29 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
30 import net.sourceforge.phpdt.internal.corext.template.Template;
31 import net.sourceforge.phpdt.internal.corext.template.Templates;
32 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
33 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
34 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
35 import net.sourceforge.phpdt.internal.ui.wizards.NewWizardMessages;
36 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
37 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
38 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IListAdapter;
39 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
40 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
41 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ListDialogField;
42 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
43 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
44 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.Separator;
45 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
46 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField;
47 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringDialogField;
48 import net.sourceforge.phpdt.ui.JavaUI;
49 import net.sourceforge.phpdt.ui.PreferenceConstants;
50 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
51 import net.sourceforge.phpeclipse.phpeditor.EditorUtility;
53 import org.eclipse.core.runtime.CoreException;
54 import org.eclipse.core.runtime.IProgressMonitor;
55 import org.eclipse.core.runtime.IStatus;
56 import org.eclipse.core.runtime.NullProgressMonitor;
57 import org.eclipse.core.runtime.SubProgressMonitor;
58 import org.eclipse.jface.preference.IPreferenceStore;
59 import org.eclipse.jface.viewers.LabelProvider;
60 import org.eclipse.swt.SWT;
61 import org.eclipse.swt.graphics.Image;
62 import org.eclipse.swt.layout.GridData;
63 import org.eclipse.swt.layout.GridLayout;
64 import org.eclipse.swt.widgets.Button;
65 import org.eclipse.swt.widgets.Composite;
66 import org.eclipse.swt.widgets.Control;
69 * The class <code>NewTypeWizardPage</code> contains controls and validation routines
70 * for a 'New Type WizardPage'. Implementors decide which components to add and to enable.
71 * Implementors can also customize the validation code. <code>NewTypeWizardPage</code>
72 * is intended to serve as base class of all wizards that create types like applets, servlets, classes,
75 * See <code>NewClassWizardPage</code> or <code>NewInterfaceWizardPage</code> for an
76 * example usage of <code>NewTypeWizardPage</code>.
79 * @see org.eclipse.jdt.ui.wizards.NewClassWizardPage
80 * @see org.eclipse.jdt.ui.wizards.NewInterfaceWizardPage
84 public abstract class NewTypeWizardPage extends NewContainerWizardPage {
87 * Class used in stub creation routines to add needed imports to a
90 // public static class ImportsManager {
92 // private IImportsStructure fImportsStructure;
94 // /* package */ ImportsManager(IImportsStructure structure) {
95 // fImportsStructure= structure;
98 // /* package */ IImportsStructure getImportsStructure() {
99 // return fImportsStructure;
103 // * Adds a new import declaration that is sorted in the existing imports.
104 // * If an import already exists or the import would conflict with another import
105 // * of an other type with the same simple name the import is not added.
107 // * @param qualifiedTypeName The fully qualified name of the type to import
109 // * @return Returns the simple type name that can be used in the code or the
110 // * fully qualified type name if an import conflict prevented the import
112 // public String addImport(String qualifiedTypeName) {
113 // return fImportsStructure.addImport(qualifiedTypeName);
117 /** Public access flag. See The Java Virtual Machine Specification for more details. */
118 public int F_PUBLIC = Flags.AccPublic;
119 /** Private access flag. See The Java Virtual Machine Specification for more details. */
120 public int F_PRIVATE = Flags.AccPrivate;
121 /** Protected access flag. See The Java Virtual Machine Specification for more details. */
122 public int F_PROTECTED = Flags.AccProtected;
123 /** Static access flag. See The Java Virtual Machine Specification for more details. */
124 public int F_STATIC = Flags.AccStatic;
125 /** Final access flag. See The Java Virtual Machine Specification for more details. */
126 public int F_FINAL = Flags.AccFinal;
127 /** Abstract property flag. See The Java Virtual Machine Specification for more details. */
128 // public int F_ABSTRACT = Flags.AccAbstract;
130 private final static String PAGE_NAME = "NewTypeWizardPage"; //$NON-NLS-1$
132 /** Field ID of the package input field */
133 protected final static String PACKAGE = PAGE_NAME + ".package"; //$NON-NLS-1$
134 /** Field ID of the eclosing type input field */
135 protected final static String ENCLOSING = PAGE_NAME + ".enclosing"; //$NON-NLS-1$
136 /** Field ID of the enclosing type checkbox */
137 protected final static String ENCLOSINGSELECTION = ENCLOSING + ".selection"; //$NON-NLS-1$
138 /** Field ID of the type name input field */
139 protected final static String TYPENAME = PAGE_NAME + ".typename"; //$NON-NLS-1$
140 /** Field ID of the super type input field */
141 protected final static String SUPER = PAGE_NAME + ".superclass"; //$NON-NLS-1$
142 /** Field ID of the super interfaces input field */
143 protected final static String INTERFACES = PAGE_NAME + ".interfaces"; //$NON-NLS-1$
144 /** Field ID of the modifier checkboxes */
145 protected final static String MODIFIERS = PAGE_NAME + ".modifiers"; //$NON-NLS-1$
146 /** Field ID of the method stubs checkboxes */
147 protected final static String METHODS = PAGE_NAME + ".methods"; //$NON-NLS-1$
149 private class InterfacesListLabelProvider extends LabelProvider {
151 private Image fInterfaceImage;
153 public InterfacesListLabelProvider() {
155 fInterfaceImage = PHPUiImages.get(PHPUiImages.IMG_OBJS_INTERFACE);
158 public Image getImage(Object element) {
159 return fInterfaceImage;
163 private StringButtonStatusDialogField fPackageDialogField;
165 private SelectionButtonDialogField fEnclosingTypeSelection;
166 private StringButtonDialogField fEnclosingTypeDialogField;
168 private boolean fCanModifyPackage;
169 private boolean fCanModifyEnclosingType;
171 private IPackageFragment fCurrPackage;
173 // private IType fCurrEnclosingType;
174 private StringDialogField fTypeNameDialogField;
176 private StringButtonDialogField fSuperClassDialogField;
177 private ListDialogField fSuperInterfacesDialogField;
179 // private IType fSuperClass;
181 private SelectionButtonDialogFieldGroup fAccMdfButtons;
182 private SelectionButtonDialogFieldGroup fOtherMdfButtons;
184 private IType fCreatedType;
186 protected IStatus fEnclosingTypeStatus;
187 protected IStatus fPackageStatus;
188 protected IStatus fTypeNameStatus;
189 // protected IStatus fSuperClassStatus;
190 protected IStatus fModifierStatus;
191 // protected IStatus fSuperInterfacesStatus;
193 private boolean fIsClass;
194 private int fStaticMdfIndex;
196 private final int PUBLIC_INDEX = 0, DEFAULT_INDEX = 1, PRIVATE_INDEX = 2, PROTECTED_INDEX = 3;
197 private final int ABSTRACT_INDEX = 0, FINAL_INDEX = 1;
200 * Creates a new <code>NewTypeWizardPage</code>
202 * @param isClass <code>true</code> if a new class is to be created; otherwise
203 * an interface is to be created
204 * @param pageName the wizard page's name
206 public NewTypeWizardPage(boolean isClass, String pageName) {
212 TypeFieldsAdapter adapter = new TypeFieldsAdapter();
214 fPackageDialogField = new StringButtonStatusDialogField(adapter);
215 fPackageDialogField.setDialogFieldListener(adapter);
216 fPackageDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.package.label")); //$NON-NLS-1$
217 fPackageDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.package.button")); //$NON-NLS-1$
218 fPackageDialogField.setStatusWidthHint(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
220 fEnclosingTypeSelection = new SelectionButtonDialogField(SWT.CHECK);
221 fEnclosingTypeSelection.setDialogFieldListener(adapter);
222 fEnclosingTypeSelection.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.enclosing.selection.label")); //$NON-NLS-1$
224 fEnclosingTypeDialogField = new StringButtonDialogField(adapter);
225 fEnclosingTypeDialogField.setDialogFieldListener(adapter);
226 fEnclosingTypeDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.enclosing.button")); //$NON-NLS-1$
228 fTypeNameDialogField = new StringDialogField();
229 fTypeNameDialogField.setDialogFieldListener(adapter);
230 fTypeNameDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.typename.label")); //$NON-NLS-1$
232 fSuperClassDialogField = new StringButtonDialogField(adapter);
233 fSuperClassDialogField.setDialogFieldListener(adapter);
234 fSuperClassDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.superclass.label")); //$NON-NLS-1$
235 fSuperClassDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.superclass.button")); //$NON-NLS-1$
237 String[] addButtons = new String[] {
239 NewWizardMessages.getString("NewTypeWizardPage.interfaces.add"), //$NON-NLS-1$
243 NewWizardMessages.getString("NewTypeWizardPage.interfaces.remove") //$NON-NLS-1$
245 fSuperInterfacesDialogField = new ListDialogField(adapter, addButtons, new InterfacesListLabelProvider());
246 fSuperInterfacesDialogField.setDialogFieldListener(adapter);
247 String interfaceLabel = fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.interfaces.class.label") : NewWizardMessages.getString("NewTypeWizardPage.interfaces.ifc.label"); //$NON-NLS-1$ //$NON-NLS-2$
248 fSuperInterfacesDialogField.setLabelText(interfaceLabel);
249 fSuperInterfacesDialogField.setRemoveButtonIndex(2);
251 String[] buttonNames1 = new String[] {
252 /* 0 == PUBLIC_INDEX */
253 NewWizardMessages.getString("NewTypeWizardPage.modifiers.public"), //$NON-NLS-1$
254 /* 1 == DEFAULT_INDEX */
255 NewWizardMessages.getString("NewTypeWizardPage.modifiers.default"), //$NON-NLS-1$
256 /* 2 == PRIVATE_INDEX */
257 NewWizardMessages.getString("NewTypeWizardPage.modifiers.private"), //$NON-NLS-1$
258 /* 3 == PROTECTED_INDEX*/
259 NewWizardMessages.getString("NewTypeWizardPage.modifiers.protected") //$NON-NLS-1$
261 fAccMdfButtons = new SelectionButtonDialogFieldGroup(SWT.RADIO, buttonNames1, 4);
262 fAccMdfButtons.setDialogFieldListener(adapter);
263 fAccMdfButtons.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.modifiers.acc.label")); //$NON-NLS-1$
264 fAccMdfButtons.setSelection(0, true);
266 String[] buttonNames2;
268 buttonNames2 = new String[] {
269 /* 0 == ABSTRACT_INDEX */
270 NewWizardMessages.getString("NewTypeWizardPage.modifiers.abstract"), //$NON-NLS-1$
271 /* 1 == FINAL_INDEX */
272 NewWizardMessages.getString("NewTypeWizardPage.modifiers.final"), //$NON-NLS-1$
274 NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
276 fStaticMdfIndex = 2; // index of the static checkbox is 2
278 buttonNames2 = new String[] { NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
280 fStaticMdfIndex = 0; // index of the static checkbox is 0
283 fOtherMdfButtons = new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames2, 4);
284 fOtherMdfButtons.setDialogFieldListener(adapter);
286 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, false);
287 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, false);
288 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, false);
290 fPackageStatus = new StatusInfo();
291 fEnclosingTypeStatus = new StatusInfo();
293 fCanModifyPackage = true;
294 fCanModifyEnclosingType = true;
297 fTypeNameStatus = new StatusInfo();
298 // fSuperClassStatus= new StatusInfo();
299 // fSuperInterfacesStatus= new StatusInfo();
300 fModifierStatus = new StatusInfo();
304 * Initializes all fields provided by the page with a given selection.
306 * @param elem the selection used to intialize this page or <code>
307 * null</code> if no selection was available
309 protected void initTypePage(IJavaElement elem) {
310 String initSuperclass = "java.lang.Object"; //$NON-NLS-1$
311 ArrayList initSuperinterfaces = new ArrayList(5);
313 IPackageFragment pack = null;
314 IType enclosingType = null;
317 // evaluate the enclosing type
318 pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
319 IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
320 if (typeInCU != null) {
321 if (typeInCU.getCompilationUnit() != null) {
322 enclosingType = typeInCU;
325 ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
327 // enclosingType= cu.findPrimaryType();
333 // if (elem.getElementType() == IJavaElement.TYPE) {
334 // type= (IType)elem;
335 // if (type.exists()) {
336 // String superName= JavaModelUtil.getFullyQualifiedName(type);
337 // if (type.isInterface()) {
338 // initSuperinterfaces.add(superName);
340 // initSuperclass= superName;
344 // } catch (JavaModelException e) {
345 // PHPeclipsePlugin.log(e);
346 // // ignore this exception now
350 setPackageFragment(pack, true);
351 // setEnclosingType(enclosingType, true);
352 setEnclosingTypeSelection(false, true);
354 setTypeName("", true); //$NON-NLS-1$
355 setSuperClass(initSuperclass, true);
356 setSuperInterfaces(initSuperinterfaces, true);
359 // -------- UI Creation ---------
362 * Creates a separator line. Expects a <code>GridLayout</code> with at least 1 column.
364 * @param composite the parent composite
365 * @param nColumns number of columns to span
367 protected void createSeparator(Composite composite, int nColumns) {
368 (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(composite, nColumns, convertHeightInCharsToPixels(1));
372 * Creates the controls for the package name field. Expects a <code>GridLayout</code> with at
375 * @param composite the parent composite
376 * @param nColumns number of columns to span
378 protected void createPackageControls(Composite composite, int nColumns) {
379 fPackageDialogField.doFillIntoGrid(composite, nColumns);
380 LayoutUtil.setWidthHint(fPackageDialogField.getTextControl(null), getMaxFieldWidth());
381 LayoutUtil.setHorizontalGrabbing(fPackageDialogField.getTextControl(null));
385 * Creates the controls for the enclosing type name field. Expects a <code>GridLayout</code> with at
388 * @param composite the parent composite
389 * @param nColumns number of columns to span
391 protected void createEnclosingTypeControls(Composite composite, int nColumns) {
393 Composite tabGroup = new Composite(composite, SWT.NONE);
394 GridLayout layout = new GridLayout();
395 layout.marginWidth = 0;
396 layout.marginHeight = 0;
397 tabGroup.setLayout(layout);
399 fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1);
401 Control c = fEnclosingTypeDialogField.getTextControl(composite);
402 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
403 gd.widthHint = getMaxFieldWidth();
404 gd.horizontalSpan = 2;
407 Button button = fEnclosingTypeDialogField.getChangeControl(composite);
408 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
409 gd.heightHint = SWTUtil.getButtonHeigthHint(button);
410 gd.widthHint = SWTUtil.getButtonWidthHint(button);
411 button.setLayoutData(gd);
415 * Creates the controls for the type name field. Expects a <code>GridLayout</code> with at
418 * @param composite the parent composite
419 * @param nColumns number of columns to span
421 protected void createTypeNameControls(Composite composite, int nColumns) {
422 fTypeNameDialogField.doFillIntoGrid(composite, nColumns - 1);
423 DialogField.createEmptySpace(composite);
425 LayoutUtil.setWidthHint(fTypeNameDialogField.getTextControl(null), getMaxFieldWidth());
429 * Creates the controls for the modifiers radio/ceckbox buttons. Expects a
430 * <code>GridLayout</code> with at least 3 columns.
432 * @param composite the parent composite
433 * @param nColumns number of columns to span
435 protected void createModifierControls(Composite composite, int nColumns) {
436 LayoutUtil.setHorizontalSpan(fAccMdfButtons.getLabelControl(composite), 1);
438 Control control = fAccMdfButtons.getSelectionButtonsGroup(composite);
439 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
440 gd.horizontalSpan = nColumns - 2;
441 control.setLayoutData(gd);
443 DialogField.createEmptySpace(composite);
445 DialogField.createEmptySpace(composite);
447 control = fOtherMdfButtons.getSelectionButtonsGroup(composite);
448 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
449 gd.horizontalSpan = nColumns - 2;
450 control.setLayoutData(gd);
452 DialogField.createEmptySpace(composite);
456 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code>
457 * with at least 3 columns.
459 * @param composite the parent composite
460 * @param nColumns number of columns to span
462 protected void createSuperClassControls(Composite composite, int nColumns) {
463 fSuperClassDialogField.doFillIntoGrid(composite, nColumns);
464 LayoutUtil.setWidthHint(fSuperClassDialogField.getTextControl(null), getMaxFieldWidth());
468 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code> with
469 * at least 3 columns.
471 * @param composite the parent composite
472 * @param nColumns number of columns to span
474 protected void createSuperInterfacesControls(Composite composite, int nColumns) {
475 fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns);
476 GridData gd = (GridData) fSuperInterfacesDialogField.getListControl(null).getLayoutData();
478 gd.heightHint = convertHeightInCharsToPixels(3);
480 gd.heightHint = convertHeightInCharsToPixels(6);
482 gd.grabExcessVerticalSpace = false;
483 gd.widthHint = getMaxFieldWidth();
487 * Sets the focus on the type name input field.
489 protected void setFocus() {
490 fTypeNameDialogField.setFocus();
493 // -------- TypeFieldsAdapter --------
495 private class TypeFieldsAdapter implements IStringButtonAdapter, IDialogFieldListener, IListAdapter {
497 // -------- IStringButtonAdapter
498 public void changeControlPressed(DialogField field) {
499 // typePageChangeControlPressed(field);
502 // -------- IListAdapter
503 public void customButtonPressed(ListDialogField field, int index) {
504 // typePageCustomButtonPressed(field, index);
507 public void selectionChanged(ListDialogField field) {
510 // -------- IDialogFieldListener
511 public void dialogFieldChanged(DialogField field) {
512 typePageDialogFieldChanged(field);
515 public void doubleClicked(ListDialogField field) {
519 // private void typePageChangeControlPressed(DialogField field) {
520 // if (field == fPackageDialogField) {
521 // IPackageFragment pack= choosePackage();
522 // if (pack != null) {
523 // fPackageDialogField.setText(pack.getElementName());
525 // } else if (field == fEnclosingTypeDialogField) {
526 // IType type= chooseEnclosingType();
527 // if (type != null) {
528 // fEnclosingTypeDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
530 // } else if (field == fSuperClassDialogField) {
531 // IType type= chooseSuperType();
532 // if (type != null) {
533 // fSuperClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
538 // private void typePageCustomButtonPressed(DialogField field, int index) {
539 // if (field == fSuperInterfacesDialogField) {
540 // chooseSuperInterfaces();
545 * A field on the type has changed. The fields' status and all dependend
546 * status are updated.
548 private void typePageDialogFieldChanged(DialogField field) {
549 String fieldName = null;
550 if (field == fPackageDialogField) {
551 fPackageStatus = packageChanged();
552 updatePackageStatusLabel();
553 fTypeNameStatus = typeNameChanged();
554 // fSuperClassStatus= superClassChanged();
556 } else if (field == fEnclosingTypeDialogField) {
557 // fEnclosingTypeStatus= enclosingTypeChanged();
558 fTypeNameStatus = typeNameChanged();
559 // fSuperClassStatus= superClassChanged();
560 fieldName = ENCLOSING;
561 } else if (field == fEnclosingTypeSelection) {
563 boolean isEnclosedType = isEnclosingTypeSelected();
564 if (!isEnclosedType) {
565 if (fAccMdfButtons.isSelected(PRIVATE_INDEX) || fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
566 fAccMdfButtons.setSelection(PRIVATE_INDEX, false);
567 fAccMdfButtons.setSelection(PROTECTED_INDEX, false);
568 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
570 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
571 fOtherMdfButtons.setSelection(fStaticMdfIndex, false);
574 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, isEnclosedType && fIsClass);
575 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, isEnclosedType && fIsClass);
576 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, isEnclosedType);
577 fTypeNameStatus = typeNameChanged();
578 // fSuperClassStatus= superClassChanged();
579 fieldName = ENCLOSINGSELECTION;
580 } else if (field == fTypeNameDialogField) {
581 fTypeNameStatus = typeNameChanged();
582 fieldName = TYPENAME;
583 } else if (field == fSuperClassDialogField) {
584 // fSuperClassStatus= superClassChanged();
586 } else if (field == fSuperInterfacesDialogField) {
587 // fSuperInterfacesStatus= superInterfacesChanged();
588 fieldName = INTERFACES;
589 } else if (field == fOtherMdfButtons) {
590 fModifierStatus = modifiersChanged();
591 fieldName = MODIFIERS;
596 handleFieldChanged(fieldName);
599 // -------- update message ----------------
602 * @see org.eclipse.jdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
604 protected void handleFieldChanged(String fieldName) {
605 super.handleFieldChanged(fieldName);
606 if (fieldName == CONTAINER) {
607 fPackageStatus = packageChanged();
608 // fEnclosingTypeStatus= enclosingTypeChanged();
609 fTypeNameStatus = typeNameChanged();
610 // fSuperClassStatus= superClassChanged();
611 // fSuperInterfacesStatus= superInterfacesChanged();
615 // ---- set / get ----------------
618 * Returns the text of the package input field.
620 * @return the text of the package input field
622 public String getPackageText() {
623 return fPackageDialogField.getText();
627 * Returns the text of the enclosing type input field.
629 * @return the text of the enclosing type input field
631 public String getEnclosingTypeText() {
632 return fEnclosingTypeDialogField.getText();
636 * Returns the package fragment corresponding to the current input.
638 * @return a package fragement or <code>null</code> if the input
639 * could not be resolved.
641 public IPackageFragment getPackageFragment() {
642 if (!isEnclosingTypeSelected()) {
645 // if (fCurrEnclosingType != null) {
646 // return fCurrEnclosingType.getPackageFragment();
653 * Sets the package fragment to the given value. The method updates the model
654 * and the text of the control.
656 * @param pack the package fragement to be set
657 * @param canBeModified if <code>true</code> the package fragment is
658 * editable; otherwise it is read-only.
660 public void setPackageFragment(IPackageFragment pack, boolean canBeModified) {
662 fCanModifyPackage = canBeModified;
663 String str = (pack == null) ? "" : pack.getElementName(); //$NON-NLS-1$
664 fPackageDialogField.setText(str);
669 * Returns the enclosing type corresponding to the current input.
671 * @return the enclosing type or <code>null</code> if the enclosing type is
672 * not selected or the input could not be resolved
674 public IType getEnclosingType() {
675 // if (isEnclosingTypeSelected()) {
676 // return fCurrEnclosingType;
682 * Sets the enclosing type. The method updates the underlying model
683 * and the text of the control.
685 * @param type the enclosing type
686 * @param canBeModified if <code>true</code> the enclosing type field is
687 * editable; otherwise it is read-only.
689 // public void setEnclosingType(IType type, boolean canBeModified) {
690 // fCurrEnclosingType= type;
691 // fCanModifyEnclosingType= canBeModified;
692 // String str= (type == null) ? "" : JavaModelUtil.getFullyQualifiedName(type); //$NON-NLS-1$
693 // fEnclosingTypeDialogField.setText(str);
694 // updateEnableState();
698 * Returns the selection state of the enclosing type checkbox.
700 * @return the seleciton state of the enclosing type checkbox
702 public boolean isEnclosingTypeSelected() {
703 return fEnclosingTypeSelection.isSelected();
707 * Sets the enclosing type checkbox's selection state.
709 * @param isSelected the checkbox's selection state
710 * @param canBeModified if <code>true</code> the enclosing type checkbox is
711 * modifiable; otherwise it is read-only.
713 public void setEnclosingTypeSelection(boolean isSelected, boolean canBeModified) {
714 fEnclosingTypeSelection.setSelection(isSelected);
715 fEnclosingTypeSelection.setEnabled(canBeModified);
720 * Returns the type name entered into the type input field.
722 * @return the type name
724 public String getTypeName() {
725 return fTypeNameDialogField.getText();
729 * Sets the type name input field's text to the given value. Method doesn't update
732 * @param name the new type name
733 * @param canBeModified if <code>true</code> the enclosing type name field is
734 * editable; otherwise it is read-only.
736 public void setTypeName(String name, boolean canBeModified) {
737 fTypeNameDialogField.setText(name);
738 fTypeNameDialogField.setEnabled(canBeModified);
742 * Returns the selected modifiers.
744 * @return the selected modifiers
747 public int getModifiers() {
749 if (fAccMdfButtons.isSelected(PUBLIC_INDEX)) {
751 } else if (fAccMdfButtons.isSelected(PRIVATE_INDEX)) {
753 } else if (fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
756 // if (fOtherMdfButtons.isSelected(ABSTRACT_INDEX) && (fStaticMdfIndex != 0)) {
759 if (fOtherMdfButtons.isSelected(FINAL_INDEX)) {
762 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
769 * Sets the modifiers.
771 * @param modifiers <code>F_PUBLIC</code>, <code>F_PRIVATE</code>,
772 * <code>F_PROTECTED</code>, <code>F_ABSTRACT, F_FINAL</code>
773 * or <code>F_STATIC</code> or, a valid combination.
774 * @param canBeModified if <code>true</code> the modifier fields are
775 * editable; otherwise they are read-only
778 public void setModifiers(int modifiers, boolean canBeModified) {
779 if (Flags.isPublic(modifiers)) {
780 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
781 } else if (Flags.isPrivate(modifiers)) {
782 fAccMdfButtons.setSelection(PRIVATE_INDEX, true);
783 } else if (Flags.isProtected(modifiers)) {
784 fAccMdfButtons.setSelection(PROTECTED_INDEX, true);
786 fAccMdfButtons.setSelection(DEFAULT_INDEX, true);
788 // if (Flags.isAbstract(modifiers)) {
789 // fOtherMdfButtons.setSelection(ABSTRACT_INDEX, true);
791 if (Flags.isFinal(modifiers)) {
792 fOtherMdfButtons.setSelection(FINAL_INDEX, true);
794 if (Flags.isStatic(modifiers)) {
795 fOtherMdfButtons.setSelection(fStaticMdfIndex, true);
798 fAccMdfButtons.setEnabled(canBeModified);
799 fOtherMdfButtons.setEnabled(canBeModified);
803 * Returns the content of the superclass input field.
805 * @return the superclass name
807 public String getSuperClass() {
808 return fSuperClassDialogField.getText();
812 * Sets the super class name.
814 * @param name the new superclass name
815 * @param canBeModified if <code>true</code> the superclass name field is
816 * editable; otherwise it is read-only.
818 public void setSuperClass(String name, boolean canBeModified) {
819 fSuperClassDialogField.setText(name);
820 fSuperClassDialogField.setEnabled(canBeModified);
824 * Returns the chosen super interfaces.
826 * @return a list of chosen super interfaces. The list's elements
827 * are of type <code>String</code>
829 public List getSuperInterfaces() {
830 return fSuperInterfacesDialogField.getElements();
834 * Sets the super interfaces.
836 * @param interfacesNames a list of super interface. The method requires that
837 * the list's elements are of type <code>String</code>
838 * @param canBeModified if <code>true</code> the super interface field is
839 * editable; otherwise it is read-only.
841 public void setSuperInterfaces(List interfacesNames, boolean canBeModified) {
842 fSuperInterfacesDialogField.setElements(interfacesNames);
843 fSuperInterfacesDialogField.setEnabled(canBeModified);
846 // ----------- validation ----------
849 * A hook method that gets called when the package field has changed. The method
850 * validates the package name and returns the status of the validation. The validation
851 * also updates the package fragment model.
853 * Subclasses may extend this method to perform their own validation.
856 * @return the status of the validation
858 protected IStatus packageChanged() {
859 StatusInfo status = new StatusInfo();
860 fPackageDialogField.enableButton(getPackageFragmentRoot() != null);
862 // String packName= getPackageText();
863 // if (packName.length() > 0) {
864 // IStatus val= JavaConventions.validatePackageName(packName);
865 // if (val.getSeverity() == IStatus.ERROR) {
866 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidPackageName", val.getMessage())); //$NON-NLS-1$
868 // } else if (val.getSeverity() == IStatus.WARNING) {
869 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.DiscouragedPackageName", val.getMessage())); //$NON-NLS-1$
874 // IPackageFragmentRoot root= getPackageFragmentRoot();
875 // if (root != null) {
876 // if (root.getJavaProject().exists() && packName.length() > 0) {
878 // IPath rootPath= root.getPath();
879 // IPath outputPath= root.getJavaProject().getOutputLocation();
880 // if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
881 // // if the bin folder is inside of our root, dont allow to name a package
882 // // like the bin folder
883 // IPath packagePath= rootPath.append(packName.replace('.', '/'));
884 // if (outputPath.isPrefixOf(packagePath)) {
885 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ClashOutputLocation")); //$NON-NLS-1$
889 // } catch (JavaModelException e) {
890 // PHPeclipsePlugin.log(e);
895 // fCurrPackage= root.getPackageFragment(packName);
897 // status.setError(""); //$NON-NLS-1$
903 * Updates the 'default' label next to the package field.
905 private void updatePackageStatusLabel() {
906 String packName = getPackageText();
908 if (packName.length() == 0) {
909 fPackageDialogField.setStatus(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
911 fPackageDialogField.setStatus(""); //$NON-NLS-1$
916 * Updates the enable state of buttons related to the enclosing type selection checkbox.
918 private void updateEnableState() {
919 boolean enclosing = isEnclosingTypeSelected();
920 fPackageDialogField.setEnabled(fCanModifyPackage && !enclosing);
921 fEnclosingTypeDialogField.setEnabled(fCanModifyEnclosingType && enclosing);
925 * Hook method that gets called when the enclosing type name has changed. The method
926 * validates the enclosing type and returns the status of the validation. It also updates the
927 * enclosing type model.
929 * Subclasses may extend this method to perform their own validation.
932 * @return the status of the validation
934 // protected IStatus enclosingTypeChanged() {
935 // StatusInfo status= new StatusInfo();
936 // fCurrEnclosingType= null;
938 // IPackageFragmentRoot root= getPackageFragmentRoot();
940 // fEnclosingTypeDialogField.enableButton(root != null);
941 // if (root == null) {
942 // status.setError(""); //$NON-NLS-1$
946 // String enclName= getEnclosingTypeText();
947 // if (enclName.length() == 0) {
948 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeEnterName")); //$NON-NLS-1$
952 // IType type= findType(root.getJavaProject(), enclName);
953 // if (type == null) {
954 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
958 // if (type.getCompilationUnit() == null) {
959 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotInCU")); //$NON-NLS-1$
962 // if (!JavaModelUtil.isEditable(type.getCompilationUnit())) {
963 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotEditable")); //$NON-NLS-1$
967 // fCurrEnclosingType= type;
968 // IPackageFragmentRoot enclosingRoot= JavaModelUtil.getPackageFragmentRoot(type);
969 // if (!enclosingRoot.equals(root)) {
970 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.EnclosingNotInSourceFolder")); //$NON-NLS-1$
973 // } catch (JavaModelException e) {
974 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
975 // PHPeclipsePlugin.log(e);
981 * Hook method that gets called when the type name has changed. The method validates the
982 * type name and returns the status of the validation.
984 * Subclasses may extend this method to perform their own validation.
987 * @return the status of the validation
989 protected IStatus typeNameChanged() {
990 StatusInfo status = new StatusInfo();
991 String typeName = getTypeName();
993 if (typeName.length() == 0) {
994 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnterTypeName")); //$NON-NLS-1$
997 if (typeName.indexOf('.') != -1) {
998 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.QualifiedName")); //$NON-NLS-1$
1001 // IStatus val= JavaConventions.validateJavaTypeName(typeName);
1002 // if (val.getSeverity() == IStatus.ERROR) {
1003 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidTypeName", val.getMessage())); //$NON-NLS-1$
1005 // } else if (val.getSeverity() == IStatus.WARNING) {
1006 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.TypeNameDiscouraged", val.getMessage())); //$NON-NLS-1$
1007 // // continue checking
1011 if (!isEnclosingTypeSelected()) {
1012 IPackageFragment pack = getPackageFragment();
1014 ICompilationUnit cu = pack.getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
1015 if (cu.getResource().exists()) {
1016 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1021 IType type = getEnclosingType();
1023 IType member = type.getType(typeName);
1024 if (member.exists()) {
1025 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1034 * Hook method that gets called when the superclass name has changed. The method
1035 * validates the superclass name and returns the status of the validation.
1037 * Subclasses may extend this method to perform their own validation.
1040 * @return the status of the validation
1042 // protected IStatus superClassChanged() {
1043 // StatusInfo status= new StatusInfo();
1044 // IPackageFragmentRoot root= getPackageFragmentRoot();
1045 // fSuperClassDialogField.enableButton(root != null);
1047 // fSuperClass= null;
1049 // String sclassName= getSuperClass();
1050 // if (sclassName.length() == 0) {
1051 // // accept the empty field (stands for java.lang.Object)
1054 // IStatus val= JavaConventions.validateJavaTypeName(sclassName);
1055 // if (val.getSeverity() == IStatus.ERROR) {
1056 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1059 // if (root != null) {
1061 // IType type= resolveSuperTypeName(root.getJavaProject(), sclassName);
1062 // if (type == null) {
1063 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.SuperClassNotExists")); //$NON-NLS-1$
1066 // if (type.isInterface()) {
1067 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotClass", sclassName)); //$NON-NLS-1$
1070 // int flags= type.getFlags();
1071 // if (Flags.isFinal(flags)) {
1072 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsFinal", sclassName)); //$NON-NLS-1$
1074 // } else if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1075 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotVisible", sclassName)); //$NON-NLS-1$
1079 // fSuperClass= type;
1080 // } catch (JavaModelException e) {
1081 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1082 // PHPeclipsePlugin.log(e);
1085 // status.setError(""); //$NON-NLS-1$
1091 // private IType resolveSuperTypeName(IJavaProject jproject, String sclassName) throws JavaModelException {
1092 // if (!jproject.exists()) {
1095 // IType type= null;
1096 // if (isEnclosingTypeSelected()) {
1097 // // search in the context of the enclosing type
1098 // IType enclosingType= getEnclosingType();
1099 // if (enclosingType != null) {
1100 // String[][] res= enclosingType.resolveType(sclassName);
1101 // if (res != null && res.length > 0) {
1102 // type= jproject.findType(res[0][0], res[0][1]);
1106 // IPackageFragment currPack= getPackageFragment();
1107 // if (type == null && currPack != null) {
1108 // String packName= currPack.getElementName();
1109 // // search in own package
1110 // if (!currPack.isDefaultPackage()) {
1111 // type= jproject.findType(packName, sclassName);
1113 // // search in java.lang
1114 // if (type == null && !"java.lang".equals(packName)) { //$NON-NLS-1$
1115 // type= jproject.findType("java.lang", sclassName); //$NON-NLS-1$
1118 // // search fully qualified
1119 // if (type == null) {
1120 // type= jproject.findType(sclassName);
1126 // private IType findType(IJavaProject project, String typeName) throws JavaModelException {
1127 // if (project.exists()) {
1128 // return project.findType(typeName);
1134 * Hook method that gets called when the list of super interface has changed. The method
1135 * validates the superinterfaces and returns the status of the validation.
1137 * Subclasses may extend this method to perform their own validation.
1140 * @return the status of the validation
1142 // protected IStatus superInterfacesChanged() {
1143 // StatusInfo status= new StatusInfo();
1145 // IPackageFragmentRoot root= getPackageFragmentRoot();
1146 // fSuperInterfacesDialogField.enableButton(0, root != null);
1148 // if (root != null) {
1149 // List elements= fSuperInterfacesDialogField.getElements();
1150 // int nElements= elements.size();
1151 // for (int i= 0; i < nElements; i++) {
1152 // String intfname= (String)elements.get(i);
1154 // IType type= findType(root.getJavaProject(), intfname);
1155 // if (type == null) {
1156 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceNotExists", intfname)); //$NON-NLS-1$
1159 // if (type.isClass()) {
1160 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotInterface", intfname)); //$NON-NLS-1$
1163 // if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1164 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotVisible", intfname)); //$NON-NLS-1$
1168 // } catch (JavaModelException e) {
1169 // PHPeclipsePlugin.log(e);
1170 // // let pass, checking is an extra
1178 * Hook method that gets called when the modifiers have changed. The method validates
1179 * the modifiers and returns the status of the validation.
1181 * Subclasses may extend this method to perform their own validation.
1184 * @return the status of the validation
1186 protected IStatus modifiersChanged() {
1187 StatusInfo status = new StatusInfo();
1188 int modifiers = getModifiers();
1189 // if (Flags.isFinal(modifiers) && Flags.isAbstract(modifiers)) {
1190 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ModifiersFinalAndAbstract")); //$NON-NLS-1$
1195 // selection dialogs
1197 // private IPackageFragment choosePackage() {
1198 // IPackageFragmentRoot froot= getPackageFragmentRoot();
1199 // IJavaElement[] packages= null;
1201 // if (froot != null && froot.exists()) {
1202 // packages= froot.getChildren();
1204 // } catch (JavaModelException e) {
1205 // PHPeclipsePlugin.log(e);
1207 // if (packages == null) {
1208 // packages= new IJavaElement[0];
1211 // ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
1212 // dialog.setIgnoreCase(false);
1213 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.title")); //$NON-NLS-1$
1214 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.description")); //$NON-NLS-1$
1215 // dialog.setEmptyListMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.empty")); //$NON-NLS-1$
1216 // dialog.setElements(packages);
1217 // IPackageFragment pack= getPackageFragment();
1218 // if (pack != null) {
1219 // dialog.setInitialSelections(new Object[] { pack });
1222 // if (dialog.open() == ElementListSelectionDialog.OK) {
1223 // return (IPackageFragment) dialog.getFirstResult();
1228 // private IType chooseEnclosingType() {
1229 // IPackageFragmentRoot root= getPackageFragmentRoot();
1230 // if (root == null) {
1234 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { root });
1236 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.TYPE, scope);
1237 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.title")); //$NON-NLS-1$
1238 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.description")); //$NON-NLS-1$
1239 // dialog.setFilter(Signature.getSimpleName(getEnclosingTypeText()));
1241 // if (dialog.open() == TypeSelectionDialog.OK) {
1242 // return (IType) dialog.getFirstResult();
1247 // private IType chooseSuperType() {
1248 // IPackageFragmentRoot root= getPackageFragmentRoot();
1249 // if (root == null) {
1253 // IJavaElement[] elements= new IJavaElement[] { root.getJavaProject() };
1254 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
1256 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.CLASS, scope);
1257 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.title")); //$NON-NLS-1$
1258 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.message")); //$NON-NLS-1$
1259 // dialog.setFilter(getSuperClass());
1261 // if (dialog.open() == TypeSelectionDialog.OK) {
1262 // return (IType) dialog.getFirstResult();
1267 // private void chooseSuperInterfaces() {
1268 // IPackageFragmentRoot root= getPackageFragmentRoot();
1269 // if (root == null) {
1273 // IJavaProject project= root.getJavaProject();
1274 // SuperInterfaceSelectionDialog dialog= new SuperInterfaceSelectionDialog(getShell(), getWizard().getContainer(), fSuperInterfacesDialogField, project);
1275 // dialog.setTitle(fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.class.title") : NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.interface.title")); //$NON-NLS-1$ //$NON-NLS-2$
1276 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.message")); //$NON-NLS-1$
1281 // ---- creation ----------------
1284 * Creates the new type using the entered field values.
1286 * @param monitor a progress monitor to report progress.
1288 public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException {
1289 if (monitor == null) {
1290 monitor = new NullProgressMonitor();
1293 monitor.beginTask(NewWizardMessages.getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$
1294 ICompilationUnit createdWorkingCopy = null;
1296 // IPackageFragmentRoot root = getPackageFragmentRoot();
1297 // IPackageFragment pack = getPackageFragment();
1298 // if (pack == null) {
1299 // pack = root.getPackageFragment(""); //$NON-NLS-1$
1302 // if (!pack.exists()) {
1303 // String packName = pack.getElementName();
1304 // pack = root.createPackageFragment(packName, true, null);
1309 String clName = getTypeName();
1311 boolean isInnerClass = isEnclosingTypeSelected();
1314 // ImportsStructure imports;
1317 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
1318 // String[] prefOrder = JavaPreferencesSettings.getImportOrderPreference(store);
1319 // int threshold = JavaPreferencesSettings.getImportNumberThreshold(store);
1321 String lineDelimiter = null;
1322 // if (!isInnerClass) {
1323 lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1325 // ICompilationUnit parentCU = pack.createCompilationUnit(clName + ".php", "", false, new SubProgressMonitor(monitor, 2)); //$NON-NLS-1$ //$NON-NLS-2$
1326 // createdWorkingCopy = (ICompilationUnit) parentCU.getSharedWorkingCopy(null, JavaUI.getBufferFactory(), null);
1328 // imports = new ImportsStructure(createdWorkingCopy, prefOrder, threshold, false);
1329 // // add an import that will be removed again. Having this import solves 14661
1330 // imports.addImport(pack.getElementName(), getTypeName());
1332 // String typeContent = constructTypeStub(new ImportsManager(imports), lineDelimiter);
1334 // String cuContent = constructCUContent(parentCU, typeContent, lineDelimiter);
1336 // createdWorkingCopy.getBuffer().setContents(cuContent);
1338 createdType = createdWorkingCopy.getType(clName);
1340 // IType enclosingType = getEnclosingType();
1342 // // if we are working on a enclosed type that is open in an editor,
1343 // // then replace the enclosing type with its working copy
1344 // IType workingCopy = (IType) EditorUtility.getWorkingCopy(enclosingType);
1345 // if (workingCopy != null) {
1346 // enclosingType = workingCopy;
1349 // ICompilationUnit parentCU = enclosingType.getCompilationUnit();
1350 // imports = new ImportsStructure(parentCU, prefOrder, threshold, true);
1352 // // add imports that will be removed again. Having the imports solves 14661
1353 // IType[] topLevelTypes = parentCU.getTypes();
1354 // for (int i = 0; i < topLevelTypes.length; i++) {
1355 // imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
1358 // lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
1359 // StringBuffer content = new StringBuffer();
1360 // String comment = getTypeComment(parentCU);
1361 // if (comment != null) {
1362 // content.append(comment);
1363 // content.append(lineDelimiter);
1365 // content.append(constructTypeStub(new ImportsManager(imports), lineDelimiter));
1366 // IJavaElement[] elems = enclosingType.getChildren();
1367 // IJavaElement sibling = elems.length > 0 ? elems[0] : null;
1369 // createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitor, 1));
1371 // indent = StubUtility.getIndentUsed(enclosingType) + 1;
1374 // // add imports for superclass/interfaces, so types can be resolved correctly
1375 // imports.create(false, new SubProgressMonitor(monitor, 1));
1377 ICompilationUnit cu = createdType.getCompilationUnit();
1381 // createTypeMembers(createdType, new ImportsManager(imports), new SubProgressMonitor(monitor, 1));
1384 // imports.create(false, new SubProgressMonitor(monitor, 1));
1389 ISourceRange range = createdType.getSourceRange();
1391 IBuffer buf = cu.getBuffer();
1392 String originalContent = buf.getText(range.getOffset(), range.getLength());
1393 String formattedContent = StubUtility.codeFormat(originalContent, indent, lineDelimiter);
1394 buf.replace(range.getOffset(), range.getLength(), formattedContent);
1395 if (!isInnerClass) {
1396 String fileComment = getFileComment(cu);
1397 if (fileComment != null && fileComment.length() > 0) {
1398 buf.replace(0, 0, fileComment + lineDelimiter);
1400 cu.commit(false, new SubProgressMonitor(monitor, 1));
1404 fCreatedType = createdType;
1406 if (createdWorkingCopy != null) {
1407 createdWorkingCopy.destroy();
1414 * Uses the New Java file template from the code template page to generate a
1415 * compilation unit with the given type content.
1416 * @param cu The new created compilation unit
1417 * @param typeContent The content of the type, including signature and type
1419 * @param lineDelimiter The line delimiter to be used.
1420 * @return String Returns the result of evaluating the new file template
1421 * with the given type content.
1422 * @throws CoreException
1425 // protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
1426 // StringBuffer typeQualifiedName= new StringBuffer();
1427 // if (isEnclosingTypeSelected()) {
1428 // typeQualifiedName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1430 // typeQualifiedName.append(getTypeName());
1431 // String typeComment= CodeGeneration.getTypeComment(cu, typeQualifiedName.toString(), lineDelimiter);
1432 // IPackageFragment pack= (IPackageFragment) cu.getParent();
1433 // String content= CodeGeneration.getCompilationUnitContent(cu, typeComment, typeContent, lineDelimiter);
1434 // if (content != null) {
1435 // CompilationUnit unit= AST.parseCompilationUnit(content.toCharArray());
1436 // if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
1440 // StringBuffer buf= new StringBuffer();
1441 // if (!pack.isDefaultPackage()) {
1442 // buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
1444 // buf.append(lineDelimiter).append(lineDelimiter);
1445 // if (typeComment != null) {
1446 // buf.append(typeComment).append(lineDelimiter);
1448 // buf.append(typeContent);
1449 // return buf.toString();
1453 * Returns the created type. The method only returns a valid type
1454 * after <code>createType</code> has been called.
1456 * @return the created type
1457 * @see #createType(IProgressMonitor)
1459 public IType getCreatedType() {
1460 return fCreatedType;
1463 // ---- construct cu body----------------
1465 // private void writeSuperClass(StringBuffer buf, ImportsManager imports) {
1466 // String typename= getSuperClass();
1467 // if (fIsClass && typename.length() > 0 && !"java.lang.Object".equals(typename)) { //$NON-NLS-1$
1468 // buf.append(" extends "); //$NON-NLS-1$
1470 // String qualifiedName= fSuperClass != null ? JavaModelUtil.getFullyQualifiedName(fSuperClass) : typename;
1471 // buf.append(imports.addImport(qualifiedName));
1475 // private void writeSuperInterfaces(StringBuffer buf, ImportsManager imports) {
1476 // List interfaces= getSuperInterfaces();
1477 // int last= interfaces.size() - 1;
1480 // buf.append(" implements "); //$NON-NLS-1$
1482 // buf.append(" extends "); //$NON-NLS-1$
1484 // for (int i= 0; i <= last; i++) {
1485 // String typename= (String) interfaces.get(i);
1486 // buf.append(imports.addImport(typename));
1495 * Called from createType to construct the source for this type
1497 // private String constructTypeStub(ImportsManager imports, String lineDelimiter) {
1498 // StringBuffer buf= new StringBuffer();
1500 // int modifiers= getModifiers();
1501 // buf.append(Flags.toString(modifiers));
1502 // if (modifiers != 0) {
1505 // buf.append(fIsClass ? "class " : "interface "); //$NON-NLS-2$ //$NON-NLS-1$
1506 // buf.append(getTypeName());
1507 // writeSuperClass(buf, imports);
1508 // writeSuperInterfaces(buf, imports);
1510 // buf.append(lineDelimiter);
1511 // buf.append(lineDelimiter);
1513 // buf.append(lineDelimiter);
1514 // return buf.toString();
1518 * @deprecated Overwrite createTypeMembers(IType, IImportsManager, IProgressMonitor) instead
1520 // protected void createTypeMembers(IType newType, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1525 * Hook method that gets called from <code>createType</code> to support adding of
1526 * unanticipated methods, fields, and inner types to the created type.
1528 * Implementers can use any methods defined on <code>IType</code> to manipulate the
1532 * The source code of the new type will be formtted using the platform's formatter. Needed
1533 * imports are added by the wizard at the end of the type creation process using the given
1537 * @param newType the new type created via <code>createType</code>
1538 * @param imports an import manager which can be used to add new imports
1539 * @param monitor a progress monitor to report progress. Must not be <code>null</code>
1541 * @see #createType(IProgressMonitor)
1543 // protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1544 // // call for compatibility
1545 // createTypeMembers(newType, ((ImportsManager)imports).getImportsStructure(), monitor);
1547 // // default implementation does nothing
1548 // // example would be
1549 // // String mainMathod= "public void foo(Vector vec) {}"
1550 // // createdType.createMethod(main, null, false, null);
1551 // // imports.addImport("java.lang.Vector");
1555 * @deprecated Instead of file templates, the new type code template
1556 * specifies the stub for a compilation unit.
1558 protected String getFileComment(ICompilationUnit parentCU) {
1562 private boolean isValidComment(String template) {
1563 IScanner scanner = ToolFactory.createScanner(true, false, false); //, false);
1564 scanner.setSource(template.toCharArray());
1566 int next = scanner.getNextToken();
1567 while (next == ITerminalSymbols.TokenNameCOMMENT_LINE
1568 || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
1569 || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
1570 next = scanner.getNextToken();
1572 return next == ITerminalSymbols.TokenNameEOF;
1573 } catch (InvalidInputException e) {
1579 * Hook method that gets called from <code>createType</code> to retrieve
1580 * a type comment. This default implementation returns the content of the
1581 * 'typecomment' template.
1583 * @return the type comment or <code>null</code> if a type comment
1586 // protected String getTypeComment(ICompilationUnit parentCU) {
1587 // if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_ADD_COMMENTS)) {
1589 // StringBuffer typeName= new StringBuffer();
1590 // if (isEnclosingTypeSelected()) {
1591 // typeName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1593 // typeName.append(getTypeName());
1594 // String comment= CodeGeneration.getTypeComment(parentCU, typeName.toString(), String.valueOf('\n'));
1595 // if (comment != null && isValidComment(comment)) {
1598 // } catch (CoreException e) {
1599 // PHPeclipsePlugin.log(e);
1606 * @deprecated Use getTemplate(String,ICompilationUnit,int)
1608 protected String getTemplate(String name, ICompilationUnit parentCU) {
1609 return getTemplate(name, parentCU, 0);
1613 * Returns the string resulting from evaluation the given template in
1614 * the context of the given compilation unit. This accesses the normal
1615 * template page, not the code templates. To use code templates use
1616 * <code>constructCUContent</code> to construct a compilation unit stub or
1617 * getTypeComment for the comment of the type.
1619 * @param name the template to be evaluated
1620 * @param parentCU the templates evaluation context
1621 * @param pos a source offset into the parent compilation unit. The
1622 * template is evalutated at the given source offset
1624 protected String getTemplate(String name, ICompilationUnit parentCU, int pos) {
1626 Template[] templates = Templates.getInstance().getTemplates(name);
1627 if (templates.length > 0) {
1628 return JavaContext.evaluateTemplate(templates[0], parentCU, pos);
1630 } catch (CoreException e) {
1631 PHPeclipsePlugin.log(e);
1637 * @deprecated Use createInheritedMethods(IType,boolean,boolean,IImportsManager,IProgressMonitor)
1639 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1640 // return createInheritedMethods(type, doConstructors, doUnimplementedMethods, new ImportsManager(imports), monitor);
1644 * Creates the bodies of all unimplemented methods and constructors and adds them to the type.
1645 * Method is typically called by implementers of <code>NewTypeWizardPage</code> to add
1646 * needed method and constructors.
1648 * @param type the type for which the new methods and constructor are to be created
1649 * @param doConstructors if <code>true</code> unimplemented constructors are created
1650 * @param doUnimplementedMethods if <code>true</code> unimplemented methods are created
1651 * @param imports an import manager to add all neded import statements
1652 * @param monitor a progress monitor to report progress
1654 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1655 // ArrayList newMethods= new ArrayList();
1656 // ITypeHierarchy hierarchy= null;
1657 // CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings();
1659 // if (doConstructors) {
1660 // hierarchy= type.newSupertypeHierarchy(monitor);
1661 // IType superclass= hierarchy.getSuperclass(type);
1662 // if (superclass != null) {
1663 // String[] constructors= StubUtility.evalConstructors(type, superclass, settings, imports.getImportsStructure());
1664 // if (constructors != null) {
1665 // for (int i= 0; i < constructors.length; i++) {
1666 // newMethods.add(constructors[i]);
1672 // if (doUnimplementedMethods) {
1673 // if (hierarchy == null) {
1674 // hierarchy= type.newSupertypeHierarchy(monitor);
1676 // String[] unimplemented= StubUtility.evalUnimplementedMethods(type, hierarchy, false, settings, null, imports.getImportsStructure());
1677 // if (unimplemented != null) {
1678 // for (int i= 0; i < unimplemented.length; i++) {
1679 // newMethods.add(unimplemented[i]);
1683 // IMethod[] createdMethods= new IMethod[newMethods.size()];
1684 // for (int i= 0; i < newMethods.size(); i++) {
1685 // String content= (String) newMethods.get(i) + '\n'; // content will be formatted, ok to use \n
1686 // createdMethods[i]= type.createMethod(content, null, false, null);
1688 // return createdMethods;
1691 // ---- creation ----------------
1694 * Returns the runnable that creates the type using the current settings.
1695 * The returned runnable must be executed in the UI thread.
1697 * @return the runnable to create the new type
1699 // public IRunnableWithProgress getRunnable() {
1700 // return new IRunnableWithProgress() {
1701 // public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
1703 // if (monitor == null) {
1704 // monitor= new NullProgressMonitor();
1706 // createType(monitor);
1707 // } catch (CoreException e) {
1708 // throw new InvocationTargetException(e);