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.php.JavaContext;
31 import net.sourceforge.phpdt.internal.corext.template.php.Templates;
32 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
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.CodeGeneration;
49 import net.sourceforge.phpdt.ui.PreferenceConstants;
50 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
52 import org.eclipse.core.runtime.CoreException;
53 import org.eclipse.core.runtime.IProgressMonitor;
54 import org.eclipse.core.runtime.IStatus;
55 import org.eclipse.core.runtime.NullProgressMonitor;
56 import org.eclipse.core.runtime.SubProgressMonitor;
57 import org.eclipse.jface.preference.IPreferenceStore;
58 import org.eclipse.jface.text.BadLocationException;
59 import org.eclipse.jface.text.templates.Template;
60 import org.eclipse.jface.text.templates.TemplateException;
61 import org.eclipse.jface.viewers.LabelProvider;
62 import org.eclipse.swt.SWT;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.layout.GridData;
65 import org.eclipse.swt.layout.GridLayout;
66 import org.eclipse.swt.widgets.Button;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Control;
71 * The class <code>NewTypeWizardPage</code> contains controls and validation routines
72 * for a 'New Type WizardPage'. Implementors decide which components to add and to enable.
73 * Implementors can also customize the validation code. <code>NewTypeWizardPage</code>
74 * is intended to serve as base class of all wizards that create types like applets, servlets, classes,
77 * See <code>NewClassWizardPage</code> or <code>NewInterfaceWizardPage</code> for an
78 * example usage of <code>NewTypeWizardPage</code>.
81 * @see net.sourceforge.phpdt.ui.wizards.NewClassWizardPage
82 * @see net.sourceforge.phpdt.ui.wizards.NewInterfaceWizardPage
86 public abstract class NewTypeWizardPage extends NewContainerWizardPage {
89 * Class used in stub creation routines to add needed imports to a
92 // public static class ImportsManager {
94 // private IImportsStructure fImportsStructure;
96 // /* package */ ImportsManager(IImportsStructure structure) {
97 // fImportsStructure= structure;
100 // /* package */ IImportsStructure getImportsStructure() {
101 // return fImportsStructure;
105 // * Adds a new import declaration that is sorted in the existing imports.
106 // * If an import already exists or the import would conflict with another import
107 // * of an other type with the same simple name the import is not added.
109 // * @param qualifiedTypeName The fully qualified name of the type to import
111 // * @return Returns the simple type name that can be used in the code or the
112 // * fully qualified type name if an import conflict prevented the import
114 // public String addImport(String qualifiedTypeName) {
115 // return fImportsStructure.addImport(qualifiedTypeName);
119 /** Public access flag. See The Java Virtual Machine Specification for more details. */
120 public int F_PUBLIC = Flags.AccPublic;
121 /** Private access flag. See The Java Virtual Machine Specification for more details. */
122 public int F_PRIVATE = Flags.AccPrivate;
123 /** Protected access flag. See The Java Virtual Machine Specification for more details. */
124 public int F_PROTECTED = Flags.AccProtected;
125 /** Static access flag. See The Java Virtual Machine Specification for more details. */
126 public int F_STATIC = Flags.AccStatic;
127 /** Final access flag. See The Java Virtual Machine Specification for more details. */
128 public int F_FINAL = Flags.AccFinal;
129 /** Abstract property flag. See The Java Virtual Machine Specification for more details. */
130 // public int F_ABSTRACT = Flags.AccAbstract;
132 private final static String PAGE_NAME = "NewTypeWizardPage"; //$NON-NLS-1$
134 /** Field ID of the package input field */
135 protected final static String PACKAGE = PAGE_NAME + ".package"; //$NON-NLS-1$
136 /** Field ID of the eclosing type input field */
137 protected final static String ENCLOSING = PAGE_NAME + ".enclosing"; //$NON-NLS-1$
138 /** Field ID of the enclosing type checkbox */
139 protected final static String ENCLOSINGSELECTION = ENCLOSING + ".selection"; //$NON-NLS-1$
140 /** Field ID of the type name input field */
141 protected final static String TYPENAME = PAGE_NAME + ".typename"; //$NON-NLS-1$
142 /** Field ID of the super type input field */
143 protected final static String SUPER = PAGE_NAME + ".superclass"; //$NON-NLS-1$
144 /** Field ID of the super interfaces input field */
145 protected final static String INTERFACES = PAGE_NAME + ".interfaces"; //$NON-NLS-1$
146 /** Field ID of the modifier checkboxes */
147 protected final static String MODIFIERS = PAGE_NAME + ".modifiers"; //$NON-NLS-1$
148 /** Field ID of the method stubs checkboxes */
149 protected final static String METHODS = PAGE_NAME + ".methods"; //$NON-NLS-1$
151 private class InterfacesListLabelProvider extends LabelProvider {
153 private Image fInterfaceImage;
155 public InterfacesListLabelProvider() {
157 fInterfaceImage = PHPUiImages.get(PHPUiImages.IMG_OBJS_INTERFACE);
160 public Image getImage(Object element) {
161 return fInterfaceImage;
165 private StringButtonStatusDialogField fPackageDialogField;
167 private SelectionButtonDialogField fEnclosingTypeSelection;
168 private StringButtonDialogField fEnclosingTypeDialogField;
170 private boolean fCanModifyPackage;
171 private boolean fCanModifyEnclosingType;
173 private IPackageFragment fCurrPackage;
175 // private IType fCurrEnclosingType;
176 private StringDialogField fTypeNameDialogField;
178 private StringButtonDialogField fSuperClassDialogField;
179 private ListDialogField fSuperInterfacesDialogField;
181 // private IType fSuperClass;
183 private SelectionButtonDialogFieldGroup fAccMdfButtons;
184 private SelectionButtonDialogFieldGroup fOtherMdfButtons;
186 private IType fCreatedType;
188 protected IStatus fEnclosingTypeStatus;
189 protected IStatus fPackageStatus;
190 protected IStatus fTypeNameStatus;
191 // protected IStatus fSuperClassStatus;
192 protected IStatus fModifierStatus;
193 // protected IStatus fSuperInterfacesStatus;
195 private boolean fIsClass;
196 private int fStaticMdfIndex;
198 private final int PUBLIC_INDEX = 0, DEFAULT_INDEX = 1, PRIVATE_INDEX = 2, PROTECTED_INDEX = 3;
199 private final int ABSTRACT_INDEX = 0, FINAL_INDEX = 1;
202 * Creates a new <code>NewTypeWizardPage</code>
204 * @param isClass <code>true</code> if a new class is to be created; otherwise
205 * an interface is to be created
206 * @param pageName the wizard page's name
208 public NewTypeWizardPage(boolean isClass, String pageName) {
214 TypeFieldsAdapter adapter = new TypeFieldsAdapter();
216 fPackageDialogField = new StringButtonStatusDialogField(adapter);
217 fPackageDialogField.setDialogFieldListener(adapter);
218 fPackageDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.package.label")); //$NON-NLS-1$
219 fPackageDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.package.button")); //$NON-NLS-1$
220 fPackageDialogField.setStatusWidthHint(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
222 fEnclosingTypeSelection = new SelectionButtonDialogField(SWT.CHECK);
223 fEnclosingTypeSelection.setDialogFieldListener(adapter);
224 fEnclosingTypeSelection.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.enclosing.selection.label")); //$NON-NLS-1$
226 fEnclosingTypeDialogField = new StringButtonDialogField(adapter);
227 fEnclosingTypeDialogField.setDialogFieldListener(adapter);
228 fEnclosingTypeDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.enclosing.button")); //$NON-NLS-1$
230 fTypeNameDialogField = new StringDialogField();
231 fTypeNameDialogField.setDialogFieldListener(adapter);
232 fTypeNameDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.typename.label")); //$NON-NLS-1$
234 fSuperClassDialogField = new StringButtonDialogField(adapter);
235 fSuperClassDialogField.setDialogFieldListener(adapter);
236 fSuperClassDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.superclass.label")); //$NON-NLS-1$
237 fSuperClassDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.superclass.button")); //$NON-NLS-1$
239 String[] addButtons = new String[] {
241 NewWizardMessages.getString("NewTypeWizardPage.interfaces.add"), //$NON-NLS-1$
245 NewWizardMessages.getString("NewTypeWizardPage.interfaces.remove") //$NON-NLS-1$
247 fSuperInterfacesDialogField = new ListDialogField(adapter, addButtons, new InterfacesListLabelProvider());
248 fSuperInterfacesDialogField.setDialogFieldListener(adapter);
249 String interfaceLabel = fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.interfaces.class.label") : NewWizardMessages.getString("NewTypeWizardPage.interfaces.ifc.label"); //$NON-NLS-1$ //$NON-NLS-2$
250 fSuperInterfacesDialogField.setLabelText(interfaceLabel);
251 fSuperInterfacesDialogField.setRemoveButtonIndex(2);
253 String[] buttonNames1 = new String[] {
254 /* 0 == PUBLIC_INDEX */
255 NewWizardMessages.getString("NewTypeWizardPage.modifiers.public"), //$NON-NLS-1$
256 /* 1 == DEFAULT_INDEX */
257 NewWizardMessages.getString("NewTypeWizardPage.modifiers.default"), //$NON-NLS-1$
258 /* 2 == PRIVATE_INDEX */
259 NewWizardMessages.getString("NewTypeWizardPage.modifiers.private"), //$NON-NLS-1$
260 /* 3 == PROTECTED_INDEX*/
261 NewWizardMessages.getString("NewTypeWizardPage.modifiers.protected") //$NON-NLS-1$
263 fAccMdfButtons = new SelectionButtonDialogFieldGroup(SWT.RADIO, buttonNames1, 4);
264 fAccMdfButtons.setDialogFieldListener(adapter);
265 fAccMdfButtons.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.modifiers.acc.label")); //$NON-NLS-1$
266 fAccMdfButtons.setSelection(0, true);
268 String[] buttonNames2;
270 buttonNames2 = new String[] {
271 /* 0 == ABSTRACT_INDEX */
272 NewWizardMessages.getString("NewTypeWizardPage.modifiers.abstract"), //$NON-NLS-1$
273 /* 1 == FINAL_INDEX */
274 NewWizardMessages.getString("NewTypeWizardPage.modifiers.final"), //$NON-NLS-1$
276 NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
278 fStaticMdfIndex = 2; // index of the static checkbox is 2
280 buttonNames2 = new String[] { NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
282 fStaticMdfIndex = 0; // index of the static checkbox is 0
285 fOtherMdfButtons = new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames2, 4);
286 fOtherMdfButtons.setDialogFieldListener(adapter);
288 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, false);
289 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, false);
290 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, false);
292 fPackageStatus = new StatusInfo();
293 fEnclosingTypeStatus = new StatusInfo();
295 fCanModifyPackage = true;
296 fCanModifyEnclosingType = true;
299 fTypeNameStatus = new StatusInfo();
300 // fSuperClassStatus= new StatusInfo();
301 // fSuperInterfacesStatus= new StatusInfo();
302 fModifierStatus = new StatusInfo();
306 * Initializes all fields provided by the page with a given selection.
308 * @param elem the selection used to intialize this page or <code>
309 * null</code> if no selection was available
311 protected void initTypePage(IJavaElement elem) {
312 String initSuperclass = "java.lang.Object"; //$NON-NLS-1$
313 ArrayList initSuperinterfaces = new ArrayList(5);
315 IPackageFragment pack = null;
316 IType enclosingType = null;
319 // evaluate the enclosing type
320 pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
321 IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
322 if (typeInCU != null) {
323 if (typeInCU.getCompilationUnit() != null) {
324 enclosingType = typeInCU;
327 ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
329 // enclosingType= cu.findPrimaryType();
335 // if (elem.getElementType() == IJavaElement.TYPE) {
336 // type= (IType)elem;
337 // if (type.exists()) {
338 // String superName= JavaModelUtil.getFullyQualifiedName(type);
339 // if (type.isInterface()) {
340 // initSuperinterfaces.add(superName);
342 // initSuperclass= superName;
346 // } catch (JavaModelException e) {
347 // PHPeclipsePlugin.log(e);
348 // // ignore this exception now
352 setPackageFragment(pack, true);
353 // setEnclosingType(enclosingType, true);
354 setEnclosingTypeSelection(false, true);
356 setTypeName("", true); //$NON-NLS-1$
357 setSuperClass(initSuperclass, true);
358 setSuperInterfaces(initSuperinterfaces, true);
361 // -------- UI Creation ---------
364 * Creates a separator line. Expects a <code>GridLayout</code> with at least 1 column.
366 * @param composite the parent composite
367 * @param nColumns number of columns to span
369 protected void createSeparator(Composite composite, int nColumns) {
370 (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(composite, nColumns, convertHeightInCharsToPixels(1));
374 * Creates the controls for the package name field. Expects a <code>GridLayout</code> with at
377 * @param composite the parent composite
378 * @param nColumns number of columns to span
380 protected void createPackageControls(Composite composite, int nColumns) {
381 fPackageDialogField.doFillIntoGrid(composite, nColumns);
382 LayoutUtil.setWidthHint(fPackageDialogField.getTextControl(null), getMaxFieldWidth());
383 LayoutUtil.setHorizontalGrabbing(fPackageDialogField.getTextControl(null));
387 * Creates the controls for the enclosing type name field. Expects a <code>GridLayout</code> with at
390 * @param composite the parent composite
391 * @param nColumns number of columns to span
393 protected void createEnclosingTypeControls(Composite composite, int nColumns) {
395 Composite tabGroup = new Composite(composite, SWT.NONE);
396 GridLayout layout = new GridLayout();
397 layout.marginWidth = 0;
398 layout.marginHeight = 0;
399 tabGroup.setLayout(layout);
401 fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1);
403 Control c = fEnclosingTypeDialogField.getTextControl(composite);
404 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
405 gd.widthHint = getMaxFieldWidth();
406 gd.horizontalSpan = 2;
409 Button button = fEnclosingTypeDialogField.getChangeControl(composite);
410 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
411 gd.heightHint = SWTUtil.getButtonHeightHint(button);
412 gd.widthHint = SWTUtil.getButtonWidthHint(button);
413 button.setLayoutData(gd);
417 * Creates the controls for the type name field. Expects a <code>GridLayout</code> with at
420 * @param composite the parent composite
421 * @param nColumns number of columns to span
423 protected void createTypeNameControls(Composite composite, int nColumns) {
424 fTypeNameDialogField.doFillIntoGrid(composite, nColumns - 1);
425 DialogField.createEmptySpace(composite);
427 LayoutUtil.setWidthHint(fTypeNameDialogField.getTextControl(null), getMaxFieldWidth());
431 * Creates the controls for the modifiers radio/ceckbox buttons. Expects a
432 * <code>GridLayout</code> with at least 3 columns.
434 * @param composite the parent composite
435 * @param nColumns number of columns to span
437 protected void createModifierControls(Composite composite, int nColumns) {
438 LayoutUtil.setHorizontalSpan(fAccMdfButtons.getLabelControl(composite), 1);
440 Control control = fAccMdfButtons.getSelectionButtonsGroup(composite);
441 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
442 gd.horizontalSpan = nColumns - 2;
443 control.setLayoutData(gd);
445 DialogField.createEmptySpace(composite);
447 DialogField.createEmptySpace(composite);
449 control = fOtherMdfButtons.getSelectionButtonsGroup(composite);
450 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
451 gd.horizontalSpan = nColumns - 2;
452 control.setLayoutData(gd);
454 DialogField.createEmptySpace(composite);
458 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code>
459 * with at least 3 columns.
461 * @param composite the parent composite
462 * @param nColumns number of columns to span
464 protected void createSuperClassControls(Composite composite, int nColumns) {
465 fSuperClassDialogField.doFillIntoGrid(composite, nColumns);
466 LayoutUtil.setWidthHint(fSuperClassDialogField.getTextControl(null), getMaxFieldWidth());
470 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code> with
471 * at least 3 columns.
473 * @param composite the parent composite
474 * @param nColumns number of columns to span
476 protected void createSuperInterfacesControls(Composite composite, int nColumns) {
477 fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns);
478 GridData gd = (GridData) fSuperInterfacesDialogField.getListControl(null).getLayoutData();
480 gd.heightHint = convertHeightInCharsToPixels(3);
482 gd.heightHint = convertHeightInCharsToPixels(6);
484 gd.grabExcessVerticalSpace = false;
485 gd.widthHint = getMaxFieldWidth();
489 * Sets the focus on the type name input field.
491 protected void setFocus() {
492 fTypeNameDialogField.setFocus();
495 // -------- TypeFieldsAdapter --------
497 private class TypeFieldsAdapter implements IStringButtonAdapter, IDialogFieldListener, IListAdapter {
499 // -------- IStringButtonAdapter
500 public void changeControlPressed(DialogField field) {
501 // typePageChangeControlPressed(field);
504 // -------- IListAdapter
505 public void customButtonPressed(ListDialogField field, int index) {
506 // typePageCustomButtonPressed(field, index);
509 public void selectionChanged(ListDialogField field) {
512 // -------- IDialogFieldListener
513 public void dialogFieldChanged(DialogField field) {
514 typePageDialogFieldChanged(field);
517 public void doubleClicked(ListDialogField field) {
521 // private void typePageChangeControlPressed(DialogField field) {
522 // if (field == fPackageDialogField) {
523 // IPackageFragment pack= choosePackage();
524 // if (pack != null) {
525 // fPackageDialogField.setText(pack.getElementName());
527 // } else if (field == fEnclosingTypeDialogField) {
528 // IType type= chooseEnclosingType();
529 // if (type != null) {
530 // fEnclosingTypeDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
532 // } else if (field == fSuperClassDialogField) {
533 // IType type= chooseSuperType();
534 // if (type != null) {
535 // fSuperClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
540 // private void typePageCustomButtonPressed(DialogField field, int index) {
541 // if (field == fSuperInterfacesDialogField) {
542 // chooseSuperInterfaces();
547 * A field on the type has changed. The fields' status and all dependend
548 * status are updated.
550 private void typePageDialogFieldChanged(DialogField field) {
551 String fieldName = null;
552 if (field == fPackageDialogField) {
553 fPackageStatus = packageChanged();
554 updatePackageStatusLabel();
555 fTypeNameStatus = typeNameChanged();
556 // fSuperClassStatus= superClassChanged();
558 } else if (field == fEnclosingTypeDialogField) {
559 // fEnclosingTypeStatus= enclosingTypeChanged();
560 fTypeNameStatus = typeNameChanged();
561 // fSuperClassStatus= superClassChanged();
562 fieldName = ENCLOSING;
563 } else if (field == fEnclosingTypeSelection) {
565 boolean isEnclosedType = isEnclosingTypeSelected();
566 if (!isEnclosedType) {
567 if (fAccMdfButtons.isSelected(PRIVATE_INDEX) || fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
568 fAccMdfButtons.setSelection(PRIVATE_INDEX, false);
569 fAccMdfButtons.setSelection(PROTECTED_INDEX, false);
570 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
572 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
573 fOtherMdfButtons.setSelection(fStaticMdfIndex, false);
576 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, isEnclosedType && fIsClass);
577 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, isEnclosedType && fIsClass);
578 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, isEnclosedType);
579 fTypeNameStatus = typeNameChanged();
580 // fSuperClassStatus= superClassChanged();
581 fieldName = ENCLOSINGSELECTION;
582 } else if (field == fTypeNameDialogField) {
583 fTypeNameStatus = typeNameChanged();
584 fieldName = TYPENAME;
585 } else if (field == fSuperClassDialogField) {
586 // fSuperClassStatus= superClassChanged();
588 } else if (field == fSuperInterfacesDialogField) {
589 // fSuperInterfacesStatus= superInterfacesChanged();
590 fieldName = INTERFACES;
591 } else if (field == fOtherMdfButtons) {
592 fModifierStatus = modifiersChanged();
593 fieldName = MODIFIERS;
598 handleFieldChanged(fieldName);
601 // -------- update message ----------------
604 * @see net.sourceforge.phpdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
606 protected void handleFieldChanged(String fieldName) {
607 super.handleFieldChanged(fieldName);
608 if (fieldName == CONTAINER) {
609 fPackageStatus = packageChanged();
610 // fEnclosingTypeStatus= enclosingTypeChanged();
611 fTypeNameStatus = typeNameChanged();
612 // fSuperClassStatus= superClassChanged();
613 // fSuperInterfacesStatus= superInterfacesChanged();
617 // ---- set / get ----------------
620 * Returns the text of the package input field.
622 * @return the text of the package input field
624 public String getPackageText() {
625 return fPackageDialogField.getText();
629 * Returns the text of the enclosing type input field.
631 * @return the text of the enclosing type input field
633 public String getEnclosingTypeText() {
634 return fEnclosingTypeDialogField.getText();
638 * Returns the package fragment corresponding to the current input.
640 * @return a package fragement or <code>null</code> if the input
641 * could not be resolved.
643 public IPackageFragment getPackageFragment() {
644 if (!isEnclosingTypeSelected()) {
647 // if (fCurrEnclosingType != null) {
648 // return fCurrEnclosingType.getPackageFragment();
655 * Sets the package fragment to the given value. The method updates the model
656 * and the text of the control.
658 * @param pack the package fragement to be set
659 * @param canBeModified if <code>true</code> the package fragment is
660 * editable; otherwise it is read-only.
662 public void setPackageFragment(IPackageFragment pack, boolean canBeModified) {
664 fCanModifyPackage = canBeModified;
665 String str = (pack == null) ? "" : pack.getElementName(); //$NON-NLS-1$
666 fPackageDialogField.setText(str);
671 * Returns the enclosing type corresponding to the current input.
673 * @return the enclosing type or <code>null</code> if the enclosing type is
674 * not selected or the input could not be resolved
676 public IType getEnclosingType() {
677 // if (isEnclosingTypeSelected()) {
678 // return fCurrEnclosingType;
684 * Sets the enclosing type. The method updates the underlying model
685 * and the text of the control.
687 * @param type the enclosing type
688 * @param canBeModified if <code>true</code> the enclosing type field is
689 * editable; otherwise it is read-only.
691 // public void setEnclosingType(IType type, boolean canBeModified) {
692 // fCurrEnclosingType= type;
693 // fCanModifyEnclosingType= canBeModified;
694 // String str= (type == null) ? "" : JavaModelUtil.getFullyQualifiedName(type); //$NON-NLS-1$
695 // fEnclosingTypeDialogField.setText(str);
696 // updateEnableState();
700 * Returns the selection state of the enclosing type checkbox.
702 * @return the seleciton state of the enclosing type checkbox
704 public boolean isEnclosingTypeSelected() {
705 return fEnclosingTypeSelection.isSelected();
709 * Sets the enclosing type checkbox's selection state.
711 * @param isSelected the checkbox's selection state
712 * @param canBeModified if <code>true</code> the enclosing type checkbox is
713 * modifiable; otherwise it is read-only.
715 public void setEnclosingTypeSelection(boolean isSelected, boolean canBeModified) {
716 fEnclosingTypeSelection.setSelection(isSelected);
717 fEnclosingTypeSelection.setEnabled(canBeModified);
722 * Returns the type name entered into the type input field.
724 * @return the type name
726 public String getTypeName() {
727 return fTypeNameDialogField.getText();
731 * Sets the type name input field's text to the given value. Method doesn't update
734 * @param name the new type name
735 * @param canBeModified if <code>true</code> the enclosing type name field is
736 * editable; otherwise it is read-only.
738 public void setTypeName(String name, boolean canBeModified) {
739 fTypeNameDialogField.setText(name);
740 fTypeNameDialogField.setEnabled(canBeModified);
744 * Returns the selected modifiers.
746 * @return the selected modifiers
749 public int getModifiers() {
751 if (fAccMdfButtons.isSelected(PUBLIC_INDEX)) {
753 } else if (fAccMdfButtons.isSelected(PRIVATE_INDEX)) {
755 } else if (fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
758 // if (fOtherMdfButtons.isSelected(ABSTRACT_INDEX) && (fStaticMdfIndex != 0)) {
761 if (fOtherMdfButtons.isSelected(FINAL_INDEX)) {
764 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
771 * Sets the modifiers.
773 * @param modifiers <code>F_PUBLIC</code>, <code>F_PRIVATE</code>,
774 * <code>F_PROTECTED</code>, <code>F_ABSTRACT, F_FINAL</code>
775 * or <code>F_STATIC</code> or, a valid combination.
776 * @param canBeModified if <code>true</code> the modifier fields are
777 * editable; otherwise they are read-only
780 public void setModifiers(int modifiers, boolean canBeModified) {
781 if (Flags.isPublic(modifiers)) {
782 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
783 } else if (Flags.isPrivate(modifiers)) {
784 fAccMdfButtons.setSelection(PRIVATE_INDEX, true);
785 } else if (Flags.isProtected(modifiers)) {
786 fAccMdfButtons.setSelection(PROTECTED_INDEX, true);
788 fAccMdfButtons.setSelection(DEFAULT_INDEX, true);
790 // if (Flags.isAbstract(modifiers)) {
791 // fOtherMdfButtons.setSelection(ABSTRACT_INDEX, true);
793 if (Flags.isFinal(modifiers)) {
794 fOtherMdfButtons.setSelection(FINAL_INDEX, true);
796 if (Flags.isStatic(modifiers)) {
797 fOtherMdfButtons.setSelection(fStaticMdfIndex, true);
800 fAccMdfButtons.setEnabled(canBeModified);
801 fOtherMdfButtons.setEnabled(canBeModified);
805 * Returns the content of the superclass input field.
807 * @return the superclass name
809 public String getSuperClass() {
810 return fSuperClassDialogField.getText();
814 * Sets the super class name.
816 * @param name the new superclass name
817 * @param canBeModified if <code>true</code> the superclass name field is
818 * editable; otherwise it is read-only.
820 public void setSuperClass(String name, boolean canBeModified) {
821 fSuperClassDialogField.setText(name);
822 fSuperClassDialogField.setEnabled(canBeModified);
826 * Returns the chosen super interfaces.
828 * @return a list of chosen super interfaces. The list's elements
829 * are of type <code>String</code>
831 public List getSuperInterfaces() {
832 return fSuperInterfacesDialogField.getElements();
836 * Sets the super interfaces.
838 * @param interfacesNames a list of super interface. The method requires that
839 * the list's elements are of type <code>String</code>
840 * @param canBeModified if <code>true</code> the super interface field is
841 * editable; otherwise it is read-only.
843 public void setSuperInterfaces(List interfacesNames, boolean canBeModified) {
844 fSuperInterfacesDialogField.setElements(interfacesNames);
845 fSuperInterfacesDialogField.setEnabled(canBeModified);
848 // ----------- validation ----------
851 * A hook method that gets called when the package field has changed. The method
852 * validates the package name and returns the status of the validation. The validation
853 * also updates the package fragment model.
855 * Subclasses may extend this method to perform their own validation.
858 * @return the status of the validation
860 protected IStatus packageChanged() {
861 StatusInfo status = new StatusInfo();
862 fPackageDialogField.enableButton(getPackageFragmentRoot() != null);
864 // String packName= getPackageText();
865 // if (packName.length() > 0) {
866 // IStatus val= JavaConventions.validatePackageName(packName);
867 // if (val.getSeverity() == IStatus.ERROR) {
868 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidPackageName", val.getMessage())); //$NON-NLS-1$
870 // } else if (val.getSeverity() == IStatus.WARNING) {
871 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.DiscouragedPackageName", val.getMessage())); //$NON-NLS-1$
876 // IPackageFragmentRoot root= getPackageFragmentRoot();
877 // if (root != null) {
878 // if (root.getJavaProject().exists() && packName.length() > 0) {
880 // IPath rootPath= root.getPath();
881 // IPath outputPath= root.getJavaProject().getOutputLocation();
882 // if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
883 // // if the bin folder is inside of our root, dont allow to name a package
884 // // like the bin folder
885 // IPath packagePath= rootPath.append(packName.replace('.', '/'));
886 // if (outputPath.isPrefixOf(packagePath)) {
887 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ClashOutputLocation")); //$NON-NLS-1$
891 // } catch (JavaModelException e) {
892 // PHPeclipsePlugin.log(e);
897 // fCurrPackage= root.getPackageFragment(packName);
899 // status.setError(""); //$NON-NLS-1$
905 * Updates the 'default' label next to the package field.
907 private void updatePackageStatusLabel() {
908 String packName = getPackageText();
910 if (packName.length() == 0) {
911 fPackageDialogField.setStatus(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
913 fPackageDialogField.setStatus(""); //$NON-NLS-1$
918 * Updates the enable state of buttons related to the enclosing type selection checkbox.
920 private void updateEnableState() {
921 boolean enclosing = isEnclosingTypeSelected();
922 fPackageDialogField.setEnabled(fCanModifyPackage && !enclosing);
923 fEnclosingTypeDialogField.setEnabled(fCanModifyEnclosingType && enclosing);
927 * Hook method that gets called when the enclosing type name has changed. The method
928 * validates the enclosing type and returns the status of the validation. It also updates the
929 * enclosing type model.
931 * Subclasses may extend this method to perform their own validation.
934 * @return the status of the validation
936 // protected IStatus enclosingTypeChanged() {
937 // StatusInfo status= new StatusInfo();
938 // fCurrEnclosingType= null;
940 // IPackageFragmentRoot root= getPackageFragmentRoot();
942 // fEnclosingTypeDialogField.enableButton(root != null);
943 // if (root == null) {
944 // status.setError(""); //$NON-NLS-1$
948 // String enclName= getEnclosingTypeText();
949 // if (enclName.length() == 0) {
950 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeEnterName")); //$NON-NLS-1$
954 // IType type= findType(root.getJavaProject(), enclName);
955 // if (type == null) {
956 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
960 // if (type.getCompilationUnit() == null) {
961 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotInCU")); //$NON-NLS-1$
964 // if (!JavaModelUtil.isEditable(type.getCompilationUnit())) {
965 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotEditable")); //$NON-NLS-1$
969 // fCurrEnclosingType= type;
970 // IPackageFragmentRoot enclosingRoot= JavaModelUtil.getPackageFragmentRoot(type);
971 // if (!enclosingRoot.equals(root)) {
972 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.EnclosingNotInSourceFolder")); //$NON-NLS-1$
975 // } catch (JavaModelException e) {
976 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
977 // PHPeclipsePlugin.log(e);
983 * Hook method that gets called when the type name has changed. The method validates the
984 * type name and returns the status of the validation.
986 * Subclasses may extend this method to perform their own validation.
989 * @return the status of the validation
991 protected IStatus typeNameChanged() {
992 StatusInfo status = new StatusInfo();
993 String typeName = getTypeName();
995 if (typeName.length() == 0) {
996 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnterTypeName")); //$NON-NLS-1$
999 if (typeName.indexOf('.') != -1) {
1000 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.QualifiedName")); //$NON-NLS-1$
1003 // IStatus val= JavaConventions.validateJavaTypeName(typeName);
1004 // if (val.getSeverity() == IStatus.ERROR) {
1005 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidTypeName", val.getMessage())); //$NON-NLS-1$
1007 // } else if (val.getSeverity() == IStatus.WARNING) {
1008 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.TypeNameDiscouraged", val.getMessage())); //$NON-NLS-1$
1009 // // continue checking
1013 if (!isEnclosingTypeSelected()) {
1014 IPackageFragment pack = getPackageFragment();
1016 ICompilationUnit cu = pack.getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
1017 if (cu.getResource().exists()) {
1018 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1023 IType type = getEnclosingType();
1025 IType member = type.getType(typeName);
1026 if (member.exists()) {
1027 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1036 * Hook method that gets called when the superclass name has changed. The method
1037 * validates the superclass name and returns the status of the validation.
1039 * Subclasses may extend this method to perform their own validation.
1042 * @return the status of the validation
1044 // protected IStatus superClassChanged() {
1045 // StatusInfo status= new StatusInfo();
1046 // IPackageFragmentRoot root= getPackageFragmentRoot();
1047 // fSuperClassDialogField.enableButton(root != null);
1049 // fSuperClass= null;
1051 // String sclassName= getSuperClass();
1052 // if (sclassName.length() == 0) {
1053 // // accept the empty field (stands for java.lang.Object)
1056 // IStatus val= JavaConventions.validateJavaTypeName(sclassName);
1057 // if (val.getSeverity() == IStatus.ERROR) {
1058 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1061 // if (root != null) {
1063 // IType type= resolveSuperTypeName(root.getJavaProject(), sclassName);
1064 // if (type == null) {
1065 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.SuperClassNotExists")); //$NON-NLS-1$
1068 // if (type.isInterface()) {
1069 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotClass", sclassName)); //$NON-NLS-1$
1072 // int flags= type.getFlags();
1073 // if (Flags.isFinal(flags)) {
1074 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsFinal", sclassName)); //$NON-NLS-1$
1076 // } else if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1077 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotVisible", sclassName)); //$NON-NLS-1$
1081 // fSuperClass= type;
1082 // } catch (JavaModelException e) {
1083 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1084 // PHPeclipsePlugin.log(e);
1087 // status.setError(""); //$NON-NLS-1$
1093 // private IType resolveSuperTypeName(IJavaProject jproject, String sclassName) throws JavaModelException {
1094 // if (!jproject.exists()) {
1097 // IType type= null;
1098 // if (isEnclosingTypeSelected()) {
1099 // // search in the context of the enclosing type
1100 // IType enclosingType= getEnclosingType();
1101 // if (enclosingType != null) {
1102 // String[][] res= enclosingType.resolveType(sclassName);
1103 // if (res != null && res.length > 0) {
1104 // type= jproject.findType(res[0][0], res[0][1]);
1108 // IPackageFragment currPack= getPackageFragment();
1109 // if (type == null && currPack != null) {
1110 // String packName= currPack.getElementName();
1111 // // search in own package
1112 // if (!currPack.isDefaultPackage()) {
1113 // type= jproject.findType(packName, sclassName);
1115 // // search in java.lang
1116 // if (type == null && !"java.lang".equals(packName)) { //$NON-NLS-1$
1117 // type= jproject.findType("java.lang", sclassName); //$NON-NLS-1$
1120 // // search fully qualified
1121 // if (type == null) {
1122 // type= jproject.findType(sclassName);
1128 // private IType findType(IJavaProject project, String typeName) throws JavaModelException {
1129 // if (project.exists()) {
1130 // return project.findType(typeName);
1136 * Hook method that gets called when the list of super interface has changed. The method
1137 * validates the superinterfaces and returns the status of the validation.
1139 * Subclasses may extend this method to perform their own validation.
1142 * @return the status of the validation
1144 // protected IStatus superInterfacesChanged() {
1145 // StatusInfo status= new StatusInfo();
1147 // IPackageFragmentRoot root= getPackageFragmentRoot();
1148 // fSuperInterfacesDialogField.enableButton(0, root != null);
1150 // if (root != null) {
1151 // List elements= fSuperInterfacesDialogField.getElements();
1152 // int nElements= elements.size();
1153 // for (int i= 0; i < nElements; i++) {
1154 // String intfname= (String)elements.get(i);
1156 // IType type= findType(root.getJavaProject(), intfname);
1157 // if (type == null) {
1158 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceNotExists", intfname)); //$NON-NLS-1$
1161 // if (type.isClass()) {
1162 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotInterface", intfname)); //$NON-NLS-1$
1165 // if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1166 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotVisible", intfname)); //$NON-NLS-1$
1170 // } catch (JavaModelException e) {
1171 // PHPeclipsePlugin.log(e);
1172 // // let pass, checking is an extra
1180 * Hook method that gets called when the modifiers have changed. The method validates
1181 * the modifiers and returns the status of the validation.
1183 * Subclasses may extend this method to perform their own validation.
1186 * @return the status of the validation
1188 protected IStatus modifiersChanged() {
1189 StatusInfo status = new StatusInfo();
1190 int modifiers = getModifiers();
1191 // if (Flags.isFinal(modifiers) && Flags.isAbstract(modifiers)) {
1192 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ModifiersFinalAndAbstract")); //$NON-NLS-1$
1197 // selection dialogs
1199 // private IPackageFragment choosePackage() {
1200 // IPackageFragmentRoot froot= getPackageFragmentRoot();
1201 // IJavaElement[] packages= null;
1203 // if (froot != null && froot.exists()) {
1204 // packages= froot.getChildren();
1206 // } catch (JavaModelException e) {
1207 // PHPeclipsePlugin.log(e);
1209 // if (packages == null) {
1210 // packages= new IJavaElement[0];
1213 // ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
1214 // dialog.setIgnoreCase(false);
1215 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.title")); //$NON-NLS-1$
1216 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.description")); //$NON-NLS-1$
1217 // dialog.setEmptyListMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.empty")); //$NON-NLS-1$
1218 // dialog.setElements(packages);
1219 // IPackageFragment pack= getPackageFragment();
1220 // if (pack != null) {
1221 // dialog.setInitialSelections(new Object[] { pack });
1224 // if (dialog.open() == ElementListSelectionDialog.OK) {
1225 // return (IPackageFragment) dialog.getFirstResult();
1230 // private IType chooseEnclosingType() {
1231 // IPackageFragmentRoot root= getPackageFragmentRoot();
1232 // if (root == null) {
1236 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { root });
1238 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.TYPE, scope);
1239 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.title")); //$NON-NLS-1$
1240 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.description")); //$NON-NLS-1$
1241 // dialog.setFilter(Signature.getSimpleName(getEnclosingTypeText()));
1243 // if (dialog.open() == TypeSelectionDialog.OK) {
1244 // return (IType) dialog.getFirstResult();
1249 // private IType chooseSuperType() {
1250 // IPackageFragmentRoot root= getPackageFragmentRoot();
1251 // if (root == null) {
1255 // IJavaElement[] elements= new IJavaElement[] { root.getJavaProject() };
1256 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
1258 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.CLASS, scope);
1259 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.title")); //$NON-NLS-1$
1260 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.message")); //$NON-NLS-1$
1261 // dialog.setFilter(getSuperClass());
1263 // if (dialog.open() == TypeSelectionDialog.OK) {
1264 // return (IType) dialog.getFirstResult();
1269 // private void chooseSuperInterfaces() {
1270 // IPackageFragmentRoot root= getPackageFragmentRoot();
1271 // if (root == null) {
1275 // IJavaProject project= root.getJavaProject();
1276 // SuperInterfaceSelectionDialog dialog= new SuperInterfaceSelectionDialog(getShell(), getWizard().getContainer(), fSuperInterfacesDialogField, project);
1277 // dialog.setTitle(fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.class.title") : NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.interface.title")); //$NON-NLS-1$ //$NON-NLS-2$
1278 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.message")); //$NON-NLS-1$
1283 // ---- creation ----------------
1286 * Creates the new type using the entered field values.
1288 * @param monitor a progress monitor to report progress.
1290 public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException {
1291 if (monitor == null) {
1292 monitor = new NullProgressMonitor();
1295 monitor.beginTask(NewWizardMessages.getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$
1296 ICompilationUnit createdWorkingCopy = null;
1298 // IPackageFragmentRoot root = getPackageFragmentRoot();
1299 // IPackageFragment pack = getPackageFragment();
1300 // if (pack == null) {
1301 // pack = root.getPackageFragment(""); //$NON-NLS-1$
1304 // if (!pack.exists()) {
1305 // String packName = pack.getElementName();
1306 // pack = root.createPackageFragment(packName, true, null);
1311 String clName = getTypeName();
1313 boolean isInnerClass = isEnclosingTypeSelected();
1316 // ImportsStructure imports;
1319 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
1320 // String[] prefOrder = JavaPreferencesSettings.getImportOrderPreference(store);
1321 // int threshold = JavaPreferencesSettings.getImportNumberThreshold(store);
1323 String lineDelimiter = null;
1324 // if (!isInnerClass) {
1325 lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1327 // ICompilationUnit parentCU = pack.createCompilationUnit(clName + ".php", "", false, new SubProgressMonitor(monitor, 2)); //$NON-NLS-1$ //$NON-NLS-2$
1328 // createdWorkingCopy = (ICompilationUnit) parentCU.getSharedWorkingCopy(null, JavaUI.getBufferFactory(), null);
1330 // imports = new ImportsStructure(createdWorkingCopy, prefOrder, threshold, false);
1331 // // add an import that will be removed again. Having this import solves 14661
1332 // imports.addImport(pack.getElementName(), getTypeName());
1334 String typeContent = constructTypeStub(lineDelimiter);//new ImportsManager(imports), lineDelimiter);
1336 // String cuContent = constructCUContent(parentCU, typeContent, lineDelimiter);
1338 // createdWorkingCopy.getBuffer().setContents(cuContent);
1340 createdType = createdWorkingCopy.getType(clName);
1342 // IType enclosingType = getEnclosingType();
1344 // // if we are working on a enclosed type that is open in an editor,
1345 // // then replace the enclosing type with its working copy
1346 // IType workingCopy = (IType) EditorUtility.getWorkingCopy(enclosingType);
1347 // if (workingCopy != null) {
1348 // enclosingType = workingCopy;
1351 // ICompilationUnit parentCU = enclosingType.getCompilationUnit();
1352 // imports = new ImportsStructure(parentCU, prefOrder, threshold, true);
1354 // // add imports that will be removed again. Having the imports solves 14661
1355 // IType[] topLevelTypes = parentCU.getTypes();
1356 // for (int i = 0; i < topLevelTypes.length; i++) {
1357 // imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
1360 // lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
1361 // StringBuffer content = new StringBuffer();
1362 // String comment = getTypeComment(parentCU);
1363 // if (comment != null) {
1364 // content.append(comment);
1365 // content.append(lineDelimiter);
1367 // content.append(constructTypeStub(new ImportsManager(imports), lineDelimiter));
1368 // IJavaElement[] elems = enclosingType.getChildren();
1369 // IJavaElement sibling = elems.length > 0 ? elems[0] : null;
1371 // createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitor, 1));
1373 // indent = StubUtility.getIndentUsed(enclosingType) + 1;
1376 // // add imports for superclass/interfaces, so types can be resolved correctly
1377 // imports.create(false, new SubProgressMonitor(monitor, 1));
1379 ICompilationUnit cu = createdType.getCompilationUnit();
1383 // createTypeMembers(createdType, new ImportsManager(imports), new SubProgressMonitor(monitor, 1));
1386 // imports.create(false, new SubProgressMonitor(monitor, 1));
1391 ISourceRange range = createdType.getSourceRange();
1393 IBuffer buf = cu.getBuffer();
1394 String originalContent = buf.getText(range.getOffset(), range.getLength());
1395 String formattedContent = StubUtility.codeFormat(originalContent, indent, lineDelimiter);
1396 buf.replace(range.getOffset(), range.getLength(), formattedContent);
1397 if (!isInnerClass) {
1398 String fileComment = getFileComment(cu);
1399 if (fileComment != null && fileComment.length() > 0) {
1400 buf.replace(0, 0, fileComment + lineDelimiter);
1402 cu.commit(false, new SubProgressMonitor(monitor, 1));
1406 fCreatedType = createdType;
1408 if (createdWorkingCopy != null) {
1409 createdWorkingCopy.destroy();
1416 * Uses the New Java file template from the code template page to generate a
1417 * compilation unit with the given type content.
1418 * @param cu The new created compilation unit
1419 * @param typeContent The content of the type, including signature and type
1421 * @param lineDelimiter The line delimiter to be used.
1422 * @return String Returns the result of evaluating the new file template
1423 * with the given type content.
1424 * @throws CoreException
1427 // protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
1428 // StringBuffer typeQualifiedName= new StringBuffer();
1429 // if (isEnclosingTypeSelected()) {
1430 // typeQualifiedName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1432 // typeQualifiedName.append(getTypeName());
1433 // String typeComment= CodeGeneration.getTypeComment(cu, typeQualifiedName.toString(), lineDelimiter);
1434 // IPackageFragment pack= (IPackageFragment) cu.getParent();
1435 // String content= CodeGeneration.getCompilationUnitContent(cu, typeComment, typeContent, lineDelimiter);
1436 // if (content != null) {
1437 // CompilationUnit unit= AST.parseCompilationUnit(content.toCharArray());
1438 // if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
1442 // StringBuffer buf= new StringBuffer();
1443 // if (!pack.isDefaultPackage()) {
1444 // buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
1446 // buf.append(lineDelimiter).append(lineDelimiter);
1447 // if (typeComment != null) {
1448 // buf.append(typeComment).append(lineDelimiter);
1450 // buf.append(typeContent);
1451 // return buf.toString();
1455 * Returns the created type. The method only returns a valid type
1456 * after <code>createType</code> has been called.
1458 * @return the created type
1459 * @see #createType(IProgressMonitor)
1461 public IType getCreatedType() {
1462 return fCreatedType;
1465 // ---- construct cu body----------------
1467 // private void writeSuperClass(StringBuffer buf, ImportsManager imports) {
1468 // String typename= getSuperClass();
1469 // if (fIsClass && typename.length() > 0 && !"java.lang.Object".equals(typename)) { //$NON-NLS-1$
1470 // buf.append(" extends "); //$NON-NLS-1$
1472 // String qualifiedName= fSuperClass != null ? JavaModelUtil.getFullyQualifiedName(fSuperClass) : typename;
1473 // buf.append(imports.addImport(qualifiedName));
1477 // private void writeSuperInterfaces(StringBuffer buf, ImportsManager imports) {
1478 // List interfaces= getSuperInterfaces();
1479 // int last= interfaces.size() - 1;
1482 // buf.append(" implements "); //$NON-NLS-1$
1484 // buf.append(" extends "); //$NON-NLS-1$
1486 // for (int i= 0; i <= last; i++) {
1487 // String typename= (String) interfaces.get(i);
1488 // buf.append(imports.addImport(typename));
1497 * Called from createType to construct the source for this type
1499 private String constructTypeStub(String lineDelimiter) { // ImportsManager imports, String lineDelimiter) {
1500 StringBuffer buf= new StringBuffer();
1502 int modifiers= getModifiers();
1503 buf.append(Flags.toString(modifiers));
1504 if (modifiers != 0) {
1507 buf.append(fIsClass ? "class " : "interface "); //$NON-NLS-2$ //$NON-NLS-1$
1508 buf.append(getTypeName());
1509 // writeSuperClass(buf, imports);
1510 // writeSuperInterfaces(buf, imports);
1512 buf.append(lineDelimiter);
1513 buf.append(lineDelimiter);
1515 buf.append(lineDelimiter);
1516 return buf.toString();
1520 * @deprecated Overwrite createTypeMembers(IType, IImportsManager, IProgressMonitor) instead
1522 // protected void createTypeMembers(IType newType, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1527 * Hook method that gets called from <code>createType</code> to support adding of
1528 * unanticipated methods, fields, and inner types to the created type.
1530 * Implementers can use any methods defined on <code>IType</code> to manipulate the
1534 * The source code of the new type will be formtted using the platform's formatter. Needed
1535 * imports are added by the wizard at the end of the type creation process using the given
1539 * @param newType the new type created via <code>createType</code>
1540 * @param imports an import manager which can be used to add new imports
1541 * @param monitor a progress monitor to report progress. Must not be <code>null</code>
1543 * @see #createType(IProgressMonitor)
1545 // protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1546 // // call for compatibility
1547 // createTypeMembers(newType, ((ImportsManager)imports).getImportsStructure(), monitor);
1549 // // default implementation does nothing
1550 // // example would be
1551 // // String mainMathod= "public void foo(Vector vec) {}"
1552 // // createdType.createMethod(main, null, false, null);
1553 // // imports.addImport("java.lang.Vector");
1557 * @deprecated Instead of file templates, the new type code template
1558 * specifies the stub for a compilation unit.
1560 protected String getFileComment(ICompilationUnit parentCU) {
1564 private boolean isValidComment(String template) {
1565 IScanner scanner = ToolFactory.createScanner(true, false, false); //, false);
1566 scanner.setSource(template.toCharArray());
1568 int next = scanner.getNextToken();
1569 while (next == ITerminalSymbols.TokenNameCOMMENT_LINE
1570 || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
1571 || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
1572 next = scanner.getNextToken();
1574 return next == ITerminalSymbols.TokenNameEOF;
1575 } catch (InvalidInputException e) {
1581 * Hook method that gets called from <code>createType</code> to retrieve
1582 * a type comment. This default implementation returns the content of the
1583 * 'typecomment' template.
1585 * @return the type comment or <code>null</code> if a type comment
1588 protected String getTypeComment(ICompilationUnit parentCU) {
1589 if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_ADD_COMMENTS)) {
1591 StringBuffer typeName= new StringBuffer();
1592 if (isEnclosingTypeSelected()) {
1593 typeName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1595 typeName.append(getTypeName());
1596 String comment= CodeGeneration.getTypeComment(parentCU, typeName.toString(), String.valueOf('\n'));
1597 if (comment != null && isValidComment(comment)) {
1600 } catch (CoreException e) {
1601 PHPeclipsePlugin.log(e);
1608 * @deprecated Use getTemplate(String,ICompilationUnit,int)
1610 protected String getTemplate(String name, ICompilationUnit parentCU) {
1611 return getTemplate(name, parentCU, 0);
1615 * Returns the string resulting from evaluation the given template in
1616 * the context of the given compilation unit. This accesses the normal
1617 * template page, not the code templates. To use code templates use
1618 * <code>constructCUContent</code> to construct a compilation unit stub or
1619 * getTypeComment for the comment of the type.
1621 * @param name the template to be evaluated
1622 * @param parentCU the templates evaluation context
1623 * @param pos a source offset into the parent compilation unit. The
1624 * template is evalutated at the given source offset
1626 protected String getTemplate(String name, ICompilationUnit parentCU, int pos) {
1628 Template[] templates = Templates.getInstance().getTemplates(name);
1629 if (templates.length > 0) {
1630 return JavaContext.evaluateTemplate(templates[0], parentCU, pos);
1632 } catch (CoreException e) {
1633 PHPeclipsePlugin.log(e);
1634 } catch (BadLocationException e1) {
1635 // TODO Auto-generated catch block
1636 e1.printStackTrace();
1637 } catch (TemplateException e1) {
1638 // TODO Auto-generated catch block
1639 e1.printStackTrace();
1645 * @deprecated Use createInheritedMethods(IType,boolean,boolean,IImportsManager,IProgressMonitor)
1647 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1648 // return createInheritedMethods(type, doConstructors, doUnimplementedMethods, new ImportsManager(imports), monitor);
1652 * Creates the bodies of all unimplemented methods and constructors and adds them to the type.
1653 * Method is typically called by implementers of <code>NewTypeWizardPage</code> to add
1654 * needed method and constructors.
1656 * @param type the type for which the new methods and constructor are to be created
1657 * @param doConstructors if <code>true</code> unimplemented constructors are created
1658 * @param doUnimplementedMethods if <code>true</code> unimplemented methods are created
1659 * @param imports an import manager to add all neded import statements
1660 * @param monitor a progress monitor to report progress
1662 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1663 // ArrayList newMethods= new ArrayList();
1664 // ITypeHierarchy hierarchy= null;
1665 // CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings();
1667 // if (doConstructors) {
1668 // hierarchy= type.newSupertypeHierarchy(monitor);
1669 // IType superclass= hierarchy.getSuperclass(type);
1670 // if (superclass != null) {
1671 // String[] constructors= StubUtility.evalConstructors(type, superclass, settings, imports.getImportsStructure());
1672 // if (constructors != null) {
1673 // for (int i= 0; i < constructors.length; i++) {
1674 // newMethods.add(constructors[i]);
1680 // if (doUnimplementedMethods) {
1681 // if (hierarchy == null) {
1682 // hierarchy= type.newSupertypeHierarchy(monitor);
1684 // String[] unimplemented= StubUtility.evalUnimplementedMethods(type, hierarchy, false, settings, null, imports.getImportsStructure());
1685 // if (unimplemented != null) {
1686 // for (int i= 0; i < unimplemented.length; i++) {
1687 // newMethods.add(unimplemented[i]);
1691 // IMethod[] createdMethods= new IMethod[newMethods.size()];
1692 // for (int i= 0; i < newMethods.size(); i++) {
1693 // String content= (String) newMethods.get(i) + '\n'; // content will be formatted, ok to use \n
1694 // createdMethods[i]= type.createMethod(content, null, false, null);
1696 // return createdMethods;
1699 // ---- creation ----------------
1702 * Returns the runnable that creates the type using the current settings.
1703 * The returned runnable must be executed in the UI thread.
1705 * @return the runnable to create the new type
1707 // public IRunnableWithProgress getRunnable() {
1708 // return new IRunnableWithProgress() {
1709 // public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
1711 // if (monitor == null) {
1712 // monitor= new NullProgressMonitor();
1714 // createType(monitor);
1715 // } catch (CoreException e) {
1716 // throw new InvocationTargetException(e);