1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.ui.wizards;
13 import java.util.ArrayList;
14 import java.util.List;
16 import net.sourceforge.phpdt.core.Flags;
17 import net.sourceforge.phpdt.core.IBuffer;
18 import net.sourceforge.phpdt.core.ICompilationUnit;
19 import net.sourceforge.phpdt.core.IJavaElement;
20 import net.sourceforge.phpdt.core.IPackageFragment;
21 import net.sourceforge.phpdt.core.ISourceRange;
22 import net.sourceforge.phpdt.core.IType;
23 import net.sourceforge.phpdt.core.ToolFactory;
24 import net.sourceforge.phpdt.core.compiler.IScanner;
25 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
26 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
27 import net.sourceforge.phpdt.externaltools.internal.ui.StatusInfo;
28 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
29 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
30 import net.sourceforge.phpdt.internal.corext.template.php.Templates;
31 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
32 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
33 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
34 import net.sourceforge.phpdt.internal.ui.wizards.NewWizardMessages;
35 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
36 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
37 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IListAdapter;
38 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
39 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
40 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ListDialogField;
41 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
42 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
43 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.Separator;
44 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
45 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField;
46 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringDialogField;
47 import net.sourceforge.phpdt.ui.CodeGeneration;
48 import net.sourceforge.phpdt.ui.PreferenceConstants;
49 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
51 import org.eclipse.core.runtime.CoreException;
52 import org.eclipse.core.runtime.IProgressMonitor;
53 import org.eclipse.core.runtime.IStatus;
54 import org.eclipse.core.runtime.NullProgressMonitor;
55 import org.eclipse.core.runtime.SubProgressMonitor;
56 import org.eclipse.jface.preference.IPreferenceStore;
57 import org.eclipse.jface.text.BadLocationException;
58 import org.eclipse.jface.text.templates.Template;
59 import org.eclipse.jface.text.templates.TemplateException;
60 import org.eclipse.jface.viewers.LabelProvider;
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.graphics.Image;
63 import org.eclipse.swt.layout.GridData;
64 import org.eclipse.swt.layout.GridLayout;
65 import org.eclipse.swt.widgets.Button;
66 import org.eclipse.swt.widgets.Composite;
67 import org.eclipse.swt.widgets.Control;
70 * The class <code>NewTypeWizardPage</code> contains controls and validation routines
71 * for a 'New Type WizardPage'. Implementors decide which components to add and to enable.
72 * Implementors can also customize the validation code. <code>NewTypeWizardPage</code>
73 * is intended to serve as base class of all wizards that create types like applets, servlets, classes,
76 * See <code>NewClassWizardPage</code> or <code>NewInterfaceWizardPage</code> for an
77 * example usage of <code>NewTypeWizardPage</code>.
80 * @see net.sourceforge.phpdt.ui.wizards.NewClassWizardPage
81 * @see net.sourceforge.phpdt.ui.wizards.NewInterfaceWizardPage
85 public abstract class NewTypeWizardPage extends NewContainerWizardPage {
88 * Class used in stub creation routines to add needed imports to a
91 // public static class ImportsManager {
93 // private IImportsStructure fImportsStructure;
95 // /* package */ ImportsManager(IImportsStructure structure) {
96 // fImportsStructure= structure;
99 // /* package */ IImportsStructure getImportsStructure() {
100 // return fImportsStructure;
104 // * Adds a new import declaration that is sorted in the existing imports.
105 // * If an import already exists or the import would conflict with another import
106 // * of an other type with the same simple name the import is not added.
108 // * @param qualifiedTypeName The fully qualified name of the type to import
110 // * @return Returns the simple type name that can be used in the code or the
111 // * fully qualified type name if an import conflict prevented the import
113 // public String addImport(String qualifiedTypeName) {
114 // return fImportsStructure.addImport(qualifiedTypeName);
118 /** Public access flag. See The Java Virtual Machine Specification for more details. */
119 public int F_PUBLIC = Flags.AccPublic;
120 /** Private access flag. See The Java Virtual Machine Specification for more details. */
121 public int F_PRIVATE = Flags.AccPrivate;
122 /** Protected access flag. See The Java Virtual Machine Specification for more details. */
123 public int F_PROTECTED = Flags.AccProtected;
124 /** Static access flag. See The Java Virtual Machine Specification for more details. */
125 public int F_STATIC = Flags.AccStatic;
126 /** Final access flag. See The Java Virtual Machine Specification for more details. */
127 public int F_FINAL = Flags.AccFinal;
128 /** Abstract property flag. See The Java Virtual Machine Specification for more details. */
129 // public int F_ABSTRACT = Flags.AccAbstract;
131 private final static String PAGE_NAME = "NewTypeWizardPage"; //$NON-NLS-1$
133 /** Field ID of the package input field */
134 protected final static String PACKAGE = PAGE_NAME + ".package"; //$NON-NLS-1$
135 /** Field ID of the eclosing type input field */
136 protected final static String ENCLOSING = PAGE_NAME + ".enclosing"; //$NON-NLS-1$
137 /** Field ID of the enclosing type checkbox */
138 protected final static String ENCLOSINGSELECTION = ENCLOSING + ".selection"; //$NON-NLS-1$
139 /** Field ID of the type name input field */
140 protected final static String TYPENAME = PAGE_NAME + ".typename"; //$NON-NLS-1$
141 /** Field ID of the super type input field */
142 protected final static String SUPER = PAGE_NAME + ".superclass"; //$NON-NLS-1$
143 /** Field ID of the super interfaces input field */
144 protected final static String INTERFACES = PAGE_NAME + ".interfaces"; //$NON-NLS-1$
145 /** Field ID of the modifier checkboxes */
146 protected final static String MODIFIERS = PAGE_NAME + ".modifiers"; //$NON-NLS-1$
147 /** Field ID of the method stubs checkboxes */
148 protected final static String METHODS = PAGE_NAME + ".methods"; //$NON-NLS-1$
150 private class InterfacesListLabelProvider extends LabelProvider {
152 private Image fInterfaceImage;
154 public InterfacesListLabelProvider() {
156 fInterfaceImage = PHPUiImages.get(PHPUiImages.IMG_OBJS_INTERFACE);
159 public Image getImage(Object element) {
160 return fInterfaceImage;
164 private StringButtonStatusDialogField fPackageDialogField;
166 private SelectionButtonDialogField fEnclosingTypeSelection;
167 private StringButtonDialogField fEnclosingTypeDialogField;
169 private boolean fCanModifyPackage;
170 private boolean fCanModifyEnclosingType;
172 private IPackageFragment fCurrPackage;
174 // private IType fCurrEnclosingType;
175 private StringDialogField fTypeNameDialogField;
177 private StringButtonDialogField fSuperClassDialogField;
178 private ListDialogField fSuperInterfacesDialogField;
180 // private IType fSuperClass;
182 private SelectionButtonDialogFieldGroup fAccMdfButtons;
183 private SelectionButtonDialogFieldGroup fOtherMdfButtons;
185 private IType fCreatedType;
187 protected IStatus fEnclosingTypeStatus;
188 protected IStatus fPackageStatus;
189 protected IStatus fTypeNameStatus;
190 // protected IStatus fSuperClassStatus;
191 protected IStatus fModifierStatus;
192 // protected IStatus fSuperInterfacesStatus;
194 private boolean fIsClass;
195 private int fStaticMdfIndex;
197 private final int PUBLIC_INDEX = 0, DEFAULT_INDEX = 1, PRIVATE_INDEX = 2, PROTECTED_INDEX = 3;
198 private final int ABSTRACT_INDEX = 0, FINAL_INDEX = 1;
201 * Creates a new <code>NewTypeWizardPage</code>
203 * @param isClass <code>true</code> if a new class is to be created; otherwise
204 * an interface is to be created
205 * @param pageName the wizard page's name
207 public NewTypeWizardPage(boolean isClass, String pageName) {
213 TypeFieldsAdapter adapter = new TypeFieldsAdapter();
215 fPackageDialogField = new StringButtonStatusDialogField(adapter);
216 fPackageDialogField.setDialogFieldListener(adapter);
217 fPackageDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.package.label")); //$NON-NLS-1$
218 fPackageDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.package.button")); //$NON-NLS-1$
219 fPackageDialogField.setStatusWidthHint(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
221 fEnclosingTypeSelection = new SelectionButtonDialogField(SWT.CHECK);
222 fEnclosingTypeSelection.setDialogFieldListener(adapter);
223 fEnclosingTypeSelection.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.enclosing.selection.label")); //$NON-NLS-1$
225 fEnclosingTypeDialogField = new StringButtonDialogField(adapter);
226 fEnclosingTypeDialogField.setDialogFieldListener(adapter);
227 fEnclosingTypeDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.enclosing.button")); //$NON-NLS-1$
229 fTypeNameDialogField = new StringDialogField();
230 fTypeNameDialogField.setDialogFieldListener(adapter);
231 fTypeNameDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.typename.label")); //$NON-NLS-1$
233 fSuperClassDialogField = new StringButtonDialogField(adapter);
234 fSuperClassDialogField.setDialogFieldListener(adapter);
235 fSuperClassDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.superclass.label")); //$NON-NLS-1$
236 fSuperClassDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.superclass.button")); //$NON-NLS-1$
238 String[] addButtons = new String[] {
240 NewWizardMessages.getString("NewTypeWizardPage.interfaces.add"), //$NON-NLS-1$
244 NewWizardMessages.getString("NewTypeWizardPage.interfaces.remove") //$NON-NLS-1$
246 fSuperInterfacesDialogField = new ListDialogField(adapter, addButtons, new InterfacesListLabelProvider());
247 fSuperInterfacesDialogField.setDialogFieldListener(adapter);
248 String interfaceLabel = fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.interfaces.class.label") : NewWizardMessages.getString("NewTypeWizardPage.interfaces.ifc.label"); //$NON-NLS-1$ //$NON-NLS-2$
249 fSuperInterfacesDialogField.setLabelText(interfaceLabel);
250 fSuperInterfacesDialogField.setRemoveButtonIndex(2);
252 String[] buttonNames1 = new String[] {
253 /* 0 == PUBLIC_INDEX */
254 NewWizardMessages.getString("NewTypeWizardPage.modifiers.public"), //$NON-NLS-1$
255 /* 1 == DEFAULT_INDEX */
256 NewWizardMessages.getString("NewTypeWizardPage.modifiers.default"), //$NON-NLS-1$
257 /* 2 == PRIVATE_INDEX */
258 NewWizardMessages.getString("NewTypeWizardPage.modifiers.private"), //$NON-NLS-1$
259 /* 3 == PROTECTED_INDEX*/
260 NewWizardMessages.getString("NewTypeWizardPage.modifiers.protected") //$NON-NLS-1$
262 fAccMdfButtons = new SelectionButtonDialogFieldGroup(SWT.RADIO, buttonNames1, 4);
263 fAccMdfButtons.setDialogFieldListener(adapter);
264 fAccMdfButtons.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.modifiers.acc.label")); //$NON-NLS-1$
265 fAccMdfButtons.setSelection(0, true);
267 String[] buttonNames2;
269 buttonNames2 = new String[] {
270 /* 0 == ABSTRACT_INDEX */
271 NewWizardMessages.getString("NewTypeWizardPage.modifiers.abstract"), //$NON-NLS-1$
272 /* 1 == FINAL_INDEX */
273 NewWizardMessages.getString("NewTypeWizardPage.modifiers.final"), //$NON-NLS-1$
275 NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
277 fStaticMdfIndex = 2; // index of the static checkbox is 2
279 buttonNames2 = new String[] { NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
281 fStaticMdfIndex = 0; // index of the static checkbox is 0
284 fOtherMdfButtons = new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames2, 4);
285 fOtherMdfButtons.setDialogFieldListener(adapter);
287 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, false);
288 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, false);
289 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, false);
291 fPackageStatus = new StatusInfo();
292 fEnclosingTypeStatus = new StatusInfo();
294 fCanModifyPackage = true;
295 fCanModifyEnclosingType = true;
298 fTypeNameStatus = new StatusInfo();
299 // fSuperClassStatus= new StatusInfo();
300 // fSuperInterfacesStatus= new StatusInfo();
301 fModifierStatus = new StatusInfo();
305 * Initializes all fields provided by the page with a given selection.
307 * @param elem the selection used to intialize this page or <code>
308 * null</code> if no selection was available
310 protected void initTypePage(IJavaElement elem) {
311 String initSuperclass = "java.lang.Object"; //$NON-NLS-1$
312 ArrayList initSuperinterfaces = new ArrayList(5);
314 IPackageFragment pack = null;
315 IType enclosingType = null;
318 // evaluate the enclosing type
319 pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
320 IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
321 if (typeInCU != null) {
322 if (typeInCU.getCompilationUnit() != null) {
323 enclosingType = typeInCU;
326 ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
328 // enclosingType= cu.findPrimaryType();
334 // if (elem.getElementType() == IJavaElement.TYPE) {
335 // type= (IType)elem;
336 // if (type.exists()) {
337 // String superName= JavaModelUtil.getFullyQualifiedName(type);
338 // if (type.isInterface()) {
339 // initSuperinterfaces.add(superName);
341 // initSuperclass= superName;
345 // } catch (JavaModelException e) {
346 // PHPeclipsePlugin.log(e);
347 // // ignore this exception now
351 setPackageFragment(pack, true);
352 // setEnclosingType(enclosingType, true);
353 setEnclosingTypeSelection(false, true);
355 setTypeName("", true); //$NON-NLS-1$
356 setSuperClass(initSuperclass, true);
357 setSuperInterfaces(initSuperinterfaces, true);
360 // -------- UI Creation ---------
363 * Creates a separator line. Expects a <code>GridLayout</code> with at least 1 column.
365 * @param composite the parent composite
366 * @param nColumns number of columns to span
368 protected void createSeparator(Composite composite, int nColumns) {
369 (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(composite, nColumns, convertHeightInCharsToPixels(1));
373 * Creates the controls for the package name field. Expects a <code>GridLayout</code> with at
376 * @param composite the parent composite
377 * @param nColumns number of columns to span
379 protected void createPackageControls(Composite composite, int nColumns) {
380 fPackageDialogField.doFillIntoGrid(composite, nColumns);
381 LayoutUtil.setWidthHint(fPackageDialogField.getTextControl(null), getMaxFieldWidth());
382 LayoutUtil.setHorizontalGrabbing(fPackageDialogField.getTextControl(null));
386 * Creates the controls for the enclosing type name field. Expects a <code>GridLayout</code> with at
389 * @param composite the parent composite
390 * @param nColumns number of columns to span
392 protected void createEnclosingTypeControls(Composite composite, int nColumns) {
394 Composite tabGroup = new Composite(composite, SWT.NONE);
395 GridLayout layout = new GridLayout();
396 layout.marginWidth = 0;
397 layout.marginHeight = 0;
398 tabGroup.setLayout(layout);
400 fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1);
402 Control c = fEnclosingTypeDialogField.getTextControl(composite);
403 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
404 gd.widthHint = getMaxFieldWidth();
405 gd.horizontalSpan = 2;
408 Button button = fEnclosingTypeDialogField.getChangeControl(composite);
409 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
410 gd.heightHint = SWTUtil.getButtonHeightHint(button);
411 gd.widthHint = SWTUtil.getButtonWidthHint(button);
412 button.setLayoutData(gd);
416 * Creates the controls for the type name field. Expects a <code>GridLayout</code> with at
419 * @param composite the parent composite
420 * @param nColumns number of columns to span
422 protected void createTypeNameControls(Composite composite, int nColumns) {
423 fTypeNameDialogField.doFillIntoGrid(composite, nColumns - 1);
424 DialogField.createEmptySpace(composite);
426 LayoutUtil.setWidthHint(fTypeNameDialogField.getTextControl(null), getMaxFieldWidth());
430 * Creates the controls for the modifiers radio/ceckbox buttons. Expects a
431 * <code>GridLayout</code> with at least 3 columns.
433 * @param composite the parent composite
434 * @param nColumns number of columns to span
436 protected void createModifierControls(Composite composite, int nColumns) {
437 LayoutUtil.setHorizontalSpan(fAccMdfButtons.getLabelControl(composite), 1);
439 Control control = fAccMdfButtons.getSelectionButtonsGroup(composite);
440 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
441 gd.horizontalSpan = nColumns - 2;
442 control.setLayoutData(gd);
444 DialogField.createEmptySpace(composite);
446 DialogField.createEmptySpace(composite);
448 control = fOtherMdfButtons.getSelectionButtonsGroup(composite);
449 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
450 gd.horizontalSpan = nColumns - 2;
451 control.setLayoutData(gd);
453 DialogField.createEmptySpace(composite);
457 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code>
458 * with at least 3 columns.
460 * @param composite the parent composite
461 * @param nColumns number of columns to span
463 protected void createSuperClassControls(Composite composite, int nColumns) {
464 fSuperClassDialogField.doFillIntoGrid(composite, nColumns);
465 LayoutUtil.setWidthHint(fSuperClassDialogField.getTextControl(null), getMaxFieldWidth());
469 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code> with
470 * at least 3 columns.
472 * @param composite the parent composite
473 * @param nColumns number of columns to span
475 protected void createSuperInterfacesControls(Composite composite, int nColumns) {
476 fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns);
477 GridData gd = (GridData) fSuperInterfacesDialogField.getListControl(null).getLayoutData();
479 gd.heightHint = convertHeightInCharsToPixels(3);
481 gd.heightHint = convertHeightInCharsToPixels(6);
483 gd.grabExcessVerticalSpace = false;
484 gd.widthHint = getMaxFieldWidth();
488 * Sets the focus on the type name input field.
490 protected void setFocus() {
491 fTypeNameDialogField.setFocus();
494 // -------- TypeFieldsAdapter --------
496 private class TypeFieldsAdapter implements IStringButtonAdapter, IDialogFieldListener, IListAdapter {
498 // -------- IStringButtonAdapter
499 public void changeControlPressed(DialogField field) {
500 // typePageChangeControlPressed(field);
503 // -------- IListAdapter
504 public void customButtonPressed(ListDialogField field, int index) {
505 // typePageCustomButtonPressed(field, index);
508 public void selectionChanged(ListDialogField field) {
511 // -------- IDialogFieldListener
512 public void dialogFieldChanged(DialogField field) {
513 typePageDialogFieldChanged(field);
516 public void doubleClicked(ListDialogField field) {
520 // private void typePageChangeControlPressed(DialogField field) {
521 // if (field == fPackageDialogField) {
522 // IPackageFragment pack= choosePackage();
523 // if (pack != null) {
524 // fPackageDialogField.setText(pack.getElementName());
526 // } else if (field == fEnclosingTypeDialogField) {
527 // IType type= chooseEnclosingType();
528 // if (type != null) {
529 // fEnclosingTypeDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
531 // } else if (field == fSuperClassDialogField) {
532 // IType type= chooseSuperType();
533 // if (type != null) {
534 // fSuperClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
539 // private void typePageCustomButtonPressed(DialogField field, int index) {
540 // if (field == fSuperInterfacesDialogField) {
541 // chooseSuperInterfaces();
546 * A field on the type has changed. The fields' status and all dependend
547 * status are updated.
549 private void typePageDialogFieldChanged(DialogField field) {
550 String fieldName = null;
551 if (field == fPackageDialogField) {
552 fPackageStatus = packageChanged();
553 updatePackageStatusLabel();
554 fTypeNameStatus = typeNameChanged();
555 // fSuperClassStatus= superClassChanged();
557 } else if (field == fEnclosingTypeDialogField) {
558 // fEnclosingTypeStatus= enclosingTypeChanged();
559 fTypeNameStatus = typeNameChanged();
560 // fSuperClassStatus= superClassChanged();
561 fieldName = ENCLOSING;
562 } else if (field == fEnclosingTypeSelection) {
564 boolean isEnclosedType = isEnclosingTypeSelected();
565 if (!isEnclosedType) {
566 if (fAccMdfButtons.isSelected(PRIVATE_INDEX) || fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
567 fAccMdfButtons.setSelection(PRIVATE_INDEX, false);
568 fAccMdfButtons.setSelection(PROTECTED_INDEX, false);
569 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
571 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
572 fOtherMdfButtons.setSelection(fStaticMdfIndex, false);
575 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, isEnclosedType && fIsClass);
576 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, isEnclosedType && fIsClass);
577 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, isEnclosedType);
578 fTypeNameStatus = typeNameChanged();
579 // fSuperClassStatus= superClassChanged();
580 fieldName = ENCLOSINGSELECTION;
581 } else if (field == fTypeNameDialogField) {
582 fTypeNameStatus = typeNameChanged();
583 fieldName = TYPENAME;
584 } else if (field == fSuperClassDialogField) {
585 // fSuperClassStatus= superClassChanged();
587 } else if (field == fSuperInterfacesDialogField) {
588 // fSuperInterfacesStatus= superInterfacesChanged();
589 fieldName = INTERFACES;
590 } else if (field == fOtherMdfButtons) {
591 fModifierStatus = modifiersChanged();
592 fieldName = MODIFIERS;
597 handleFieldChanged(fieldName);
600 // -------- update message ----------------
603 * @see net.sourceforge.phpdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
605 protected void handleFieldChanged(String fieldName) {
606 super.handleFieldChanged(fieldName);
607 if (fieldName == CONTAINER) {
608 fPackageStatus = packageChanged();
609 // fEnclosingTypeStatus= enclosingTypeChanged();
610 fTypeNameStatus = typeNameChanged();
611 // fSuperClassStatus= superClassChanged();
612 // fSuperInterfacesStatus= superInterfacesChanged();
616 // ---- set / get ----------------
619 * Returns the text of the package input field.
621 * @return the text of the package input field
623 public String getPackageText() {
624 return fPackageDialogField.getText();
628 * Returns the text of the enclosing type input field.
630 * @return the text of the enclosing type input field
632 public String getEnclosingTypeText() {
633 return fEnclosingTypeDialogField.getText();
637 * Returns the package fragment corresponding to the current input.
639 * @return a package fragement or <code>null</code> if the input
640 * could not be resolved.
642 public IPackageFragment getPackageFragment() {
643 if (!isEnclosingTypeSelected()) {
646 // if (fCurrEnclosingType != null) {
647 // return fCurrEnclosingType.getPackageFragment();
654 * Sets the package fragment to the given value. The method updates the model
655 * and the text of the control.
657 * @param pack the package fragement to be set
658 * @param canBeModified if <code>true</code> the package fragment is
659 * editable; otherwise it is read-only.
661 public void setPackageFragment(IPackageFragment pack, boolean canBeModified) {
663 fCanModifyPackage = canBeModified;
664 String str = (pack == null) ? "" : pack.getElementName(); //$NON-NLS-1$
665 fPackageDialogField.setText(str);
670 * Returns the enclosing type corresponding to the current input.
672 * @return the enclosing type or <code>null</code> if the enclosing type is
673 * not selected or the input could not be resolved
675 public IType getEnclosingType() {
676 // if (isEnclosingTypeSelected()) {
677 // return fCurrEnclosingType;
683 * Sets the enclosing type. The method updates the underlying model
684 * and the text of the control.
686 * @param type the enclosing type
687 * @param canBeModified if <code>true</code> the enclosing type field is
688 * editable; otherwise it is read-only.
690 // public void setEnclosingType(IType type, boolean canBeModified) {
691 // fCurrEnclosingType= type;
692 // fCanModifyEnclosingType= canBeModified;
693 // String str= (type == null) ? "" : JavaModelUtil.getFullyQualifiedName(type); //$NON-NLS-1$
694 // fEnclosingTypeDialogField.setText(str);
695 // updateEnableState();
699 * Returns the selection state of the enclosing type checkbox.
701 * @return the seleciton state of the enclosing type checkbox
703 public boolean isEnclosingTypeSelected() {
704 return fEnclosingTypeSelection.isSelected();
708 * Sets the enclosing type checkbox's selection state.
710 * @param isSelected the checkbox's selection state
711 * @param canBeModified if <code>true</code> the enclosing type checkbox is
712 * modifiable; otherwise it is read-only.
714 public void setEnclosingTypeSelection(boolean isSelected, boolean canBeModified) {
715 fEnclosingTypeSelection.setSelection(isSelected);
716 fEnclosingTypeSelection.setEnabled(canBeModified);
721 * Returns the type name entered into the type input field.
723 * @return the type name
725 public String getTypeName() {
726 return fTypeNameDialogField.getText();
730 * Sets the type name input field's text to the given value. Method doesn't update
733 * @param name the new type name
734 * @param canBeModified if <code>true</code> the enclosing type name field is
735 * editable; otherwise it is read-only.
737 public void setTypeName(String name, boolean canBeModified) {
738 fTypeNameDialogField.setText(name);
739 fTypeNameDialogField.setEnabled(canBeModified);
743 * Returns the selected modifiers.
745 * @return the selected modifiers
748 public int getModifiers() {
750 if (fAccMdfButtons.isSelected(PUBLIC_INDEX)) {
752 } else if (fAccMdfButtons.isSelected(PRIVATE_INDEX)) {
754 } else if (fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
757 // if (fOtherMdfButtons.isSelected(ABSTRACT_INDEX) && (fStaticMdfIndex != 0)) {
760 if (fOtherMdfButtons.isSelected(FINAL_INDEX)) {
763 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
770 * Sets the modifiers.
772 * @param modifiers <code>F_PUBLIC</code>, <code>F_PRIVATE</code>,
773 * <code>F_PROTECTED</code>, <code>F_ABSTRACT, F_FINAL</code>
774 * or <code>F_STATIC</code> or, a valid combination.
775 * @param canBeModified if <code>true</code> the modifier fields are
776 * editable; otherwise they are read-only
779 public void setModifiers(int modifiers, boolean canBeModified) {
780 if (Flags.isPublic(modifiers)) {
781 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
782 } else if (Flags.isPrivate(modifiers)) {
783 fAccMdfButtons.setSelection(PRIVATE_INDEX, true);
784 } else if (Flags.isProtected(modifiers)) {
785 fAccMdfButtons.setSelection(PROTECTED_INDEX, true);
787 fAccMdfButtons.setSelection(DEFAULT_INDEX, true);
789 // if (Flags.isAbstract(modifiers)) {
790 // fOtherMdfButtons.setSelection(ABSTRACT_INDEX, true);
792 if (Flags.isFinal(modifiers)) {
793 fOtherMdfButtons.setSelection(FINAL_INDEX, true);
795 if (Flags.isStatic(modifiers)) {
796 fOtherMdfButtons.setSelection(fStaticMdfIndex, true);
799 fAccMdfButtons.setEnabled(canBeModified);
800 fOtherMdfButtons.setEnabled(canBeModified);
804 * Returns the content of the superclass input field.
806 * @return the superclass name
808 public String getSuperClass() {
809 return fSuperClassDialogField.getText();
813 * Sets the super class name.
815 * @param name the new superclass name
816 * @param canBeModified if <code>true</code> the superclass name field is
817 * editable; otherwise it is read-only.
819 public void setSuperClass(String name, boolean canBeModified) {
820 fSuperClassDialogField.setText(name);
821 fSuperClassDialogField.setEnabled(canBeModified);
825 * Returns the chosen super interfaces.
827 * @return a list of chosen super interfaces. The list's elements
828 * are of type <code>String</code>
830 public List getSuperInterfaces() {
831 return fSuperInterfacesDialogField.getElements();
835 * Sets the super interfaces.
837 * @param interfacesNames a list of super interface. The method requires that
838 * the list's elements are of type <code>String</code>
839 * @param canBeModified if <code>true</code> the super interface field is
840 * editable; otherwise it is read-only.
842 public void setSuperInterfaces(List interfacesNames, boolean canBeModified) {
843 fSuperInterfacesDialogField.setElements(interfacesNames);
844 fSuperInterfacesDialogField.setEnabled(canBeModified);
847 // ----------- validation ----------
850 * A hook method that gets called when the package field has changed. The method
851 * validates the package name and returns the status of the validation. The validation
852 * also updates the package fragment model.
854 * Subclasses may extend this method to perform their own validation.
857 * @return the status of the validation
859 protected IStatus packageChanged() {
860 StatusInfo status = new StatusInfo();
861 fPackageDialogField.enableButton(getPackageFragmentRoot() != null);
863 // String packName= getPackageText();
864 // if (packName.length() > 0) {
865 // IStatus val= JavaConventions.validatePackageName(packName);
866 // if (val.getSeverity() == IStatus.ERROR) {
867 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidPackageName", val.getMessage())); //$NON-NLS-1$
869 // } else if (val.getSeverity() == IStatus.WARNING) {
870 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.DiscouragedPackageName", val.getMessage())); //$NON-NLS-1$
875 // IPackageFragmentRoot root= getPackageFragmentRoot();
876 // if (root != null) {
877 // if (root.getJavaProject().exists() && packName.length() > 0) {
879 // IPath rootPath= root.getPath();
880 // IPath outputPath= root.getJavaProject().getOutputLocation();
881 // if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
882 // // if the bin folder is inside of our root, dont allow to name a package
883 // // like the bin folder
884 // IPath packagePath= rootPath.append(packName.replace('.', '/'));
885 // if (outputPath.isPrefixOf(packagePath)) {
886 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ClashOutputLocation")); //$NON-NLS-1$
890 // } catch (JavaModelException e) {
891 // PHPeclipsePlugin.log(e);
896 // fCurrPackage= root.getPackageFragment(packName);
898 // status.setError(""); //$NON-NLS-1$
904 * Updates the 'default' label next to the package field.
906 private void updatePackageStatusLabel() {
907 String packName = getPackageText();
909 if (packName.length() == 0) {
910 fPackageDialogField.setStatus(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
912 fPackageDialogField.setStatus(""); //$NON-NLS-1$
917 * Updates the enable state of buttons related to the enclosing type selection checkbox.
919 private void updateEnableState() {
920 boolean enclosing = isEnclosingTypeSelected();
921 fPackageDialogField.setEnabled(fCanModifyPackage && !enclosing);
922 fEnclosingTypeDialogField.setEnabled(fCanModifyEnclosingType && enclosing);
926 * Hook method that gets called when the enclosing type name has changed. The method
927 * validates the enclosing type and returns the status of the validation. It also updates the
928 * enclosing type model.
930 * Subclasses may extend this method to perform their own validation.
933 * @return the status of the validation
935 // protected IStatus enclosingTypeChanged() {
936 // StatusInfo status= new StatusInfo();
937 // fCurrEnclosingType= null;
939 // IPackageFragmentRoot root= getPackageFragmentRoot();
941 // fEnclosingTypeDialogField.enableButton(root != null);
942 // if (root == null) {
943 // status.setError(""); //$NON-NLS-1$
947 // String enclName= getEnclosingTypeText();
948 // if (enclName.length() == 0) {
949 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeEnterName")); //$NON-NLS-1$
953 // IType type= findType(root.getJavaProject(), enclName);
954 // if (type == null) {
955 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
959 // if (type.getCompilationUnit() == null) {
960 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotInCU")); //$NON-NLS-1$
963 // if (!JavaModelUtil.isEditable(type.getCompilationUnit())) {
964 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotEditable")); //$NON-NLS-1$
968 // fCurrEnclosingType= type;
969 // IPackageFragmentRoot enclosingRoot= JavaModelUtil.getPackageFragmentRoot(type);
970 // if (!enclosingRoot.equals(root)) {
971 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.EnclosingNotInSourceFolder")); //$NON-NLS-1$
974 // } catch (JavaModelException e) {
975 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
976 // PHPeclipsePlugin.log(e);
982 * Hook method that gets called when the type name has changed. The method validates the
983 * type name and returns the status of the validation.
985 * Subclasses may extend this method to perform their own validation.
988 * @return the status of the validation
990 protected IStatus typeNameChanged() {
991 StatusInfo status = new StatusInfo();
992 String typeName = getTypeName();
994 if (typeName.length() == 0) {
995 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnterTypeName")); //$NON-NLS-1$
998 if (typeName.indexOf('.') != -1) {
999 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.QualifiedName")); //$NON-NLS-1$
1002 // IStatus val= JavaConventions.validateJavaTypeName(typeName);
1003 // if (val.getSeverity() == IStatus.ERROR) {
1004 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidTypeName", val.getMessage())); //$NON-NLS-1$
1006 // } else if (val.getSeverity() == IStatus.WARNING) {
1007 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.TypeNameDiscouraged", val.getMessage())); //$NON-NLS-1$
1008 // // continue checking
1012 if (!isEnclosingTypeSelected()) {
1013 IPackageFragment pack = getPackageFragment();
1015 ICompilationUnit cu = pack.getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
1016 if (cu.getResource().exists()) {
1017 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1022 IType type = getEnclosingType();
1024 IType member = type.getType(typeName);
1025 if (member.exists()) {
1026 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1035 * Hook method that gets called when the superclass name has changed. The method
1036 * validates the superclass name and returns the status of the validation.
1038 * Subclasses may extend this method to perform their own validation.
1041 * @return the status of the validation
1043 // protected IStatus superClassChanged() {
1044 // StatusInfo status= new StatusInfo();
1045 // IPackageFragmentRoot root= getPackageFragmentRoot();
1046 // fSuperClassDialogField.enableButton(root != null);
1048 // fSuperClass= null;
1050 // String sclassName= getSuperClass();
1051 // if (sclassName.length() == 0) {
1052 // // accept the empty field (stands for java.lang.Object)
1055 // IStatus val= JavaConventions.validateJavaTypeName(sclassName);
1056 // if (val.getSeverity() == IStatus.ERROR) {
1057 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1060 // if (root != null) {
1062 // IType type= resolveSuperTypeName(root.getJavaProject(), sclassName);
1063 // if (type == null) {
1064 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.SuperClassNotExists")); //$NON-NLS-1$
1067 // if (type.isInterface()) {
1068 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotClass", sclassName)); //$NON-NLS-1$
1071 // int flags= type.getFlags();
1072 // if (Flags.isFinal(flags)) {
1073 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsFinal", sclassName)); //$NON-NLS-1$
1075 // } else if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1076 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotVisible", sclassName)); //$NON-NLS-1$
1080 // fSuperClass= type;
1081 // } catch (JavaModelException e) {
1082 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1083 // PHPeclipsePlugin.log(e);
1086 // status.setError(""); //$NON-NLS-1$
1092 // private IType resolveSuperTypeName(IJavaProject jproject, String sclassName) throws JavaModelException {
1093 // if (!jproject.exists()) {
1096 // IType type= null;
1097 // if (isEnclosingTypeSelected()) {
1098 // // search in the context of the enclosing type
1099 // IType enclosingType= getEnclosingType();
1100 // if (enclosingType != null) {
1101 // String[][] res= enclosingType.resolveType(sclassName);
1102 // if (res != null && res.length > 0) {
1103 // type= jproject.findType(res[0][0], res[0][1]);
1107 // IPackageFragment currPack= getPackageFragment();
1108 // if (type == null && currPack != null) {
1109 // String packName= currPack.getElementName();
1110 // // search in own package
1111 // if (!currPack.isDefaultPackage()) {
1112 // type= jproject.findType(packName, sclassName);
1114 // // search in java.lang
1115 // if (type == null && !"java.lang".equals(packName)) { //$NON-NLS-1$
1116 // type= jproject.findType("java.lang", sclassName); //$NON-NLS-1$
1119 // // search fully qualified
1120 // if (type == null) {
1121 // type= jproject.findType(sclassName);
1127 // private IType findType(IJavaProject project, String typeName) throws JavaModelException {
1128 // if (project.exists()) {
1129 // return project.findType(typeName);
1135 * Hook method that gets called when the list of super interface has changed. The method
1136 * validates the superinterfaces and returns the status of the validation.
1138 * Subclasses may extend this method to perform their own validation.
1141 * @return the status of the validation
1143 // protected IStatus superInterfacesChanged() {
1144 // StatusInfo status= new StatusInfo();
1146 // IPackageFragmentRoot root= getPackageFragmentRoot();
1147 // fSuperInterfacesDialogField.enableButton(0, root != null);
1149 // if (root != null) {
1150 // List elements= fSuperInterfacesDialogField.getElements();
1151 // int nElements= elements.size();
1152 // for (int i= 0; i < nElements; i++) {
1153 // String intfname= (String)elements.get(i);
1155 // IType type= findType(root.getJavaProject(), intfname);
1156 // if (type == null) {
1157 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceNotExists", intfname)); //$NON-NLS-1$
1160 // if (type.isClass()) {
1161 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotInterface", intfname)); //$NON-NLS-1$
1164 // if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1165 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotVisible", intfname)); //$NON-NLS-1$
1169 // } catch (JavaModelException e) {
1170 // PHPeclipsePlugin.log(e);
1171 // // let pass, checking is an extra
1179 * Hook method that gets called when the modifiers have changed. The method validates
1180 * the modifiers and returns the status of the validation.
1182 * Subclasses may extend this method to perform their own validation.
1185 * @return the status of the validation
1187 protected IStatus modifiersChanged() {
1188 StatusInfo status = new StatusInfo();
1189 int modifiers = getModifiers();
1190 // if (Flags.isFinal(modifiers) && Flags.isAbstract(modifiers)) {
1191 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ModifiersFinalAndAbstract")); //$NON-NLS-1$
1196 // selection dialogs
1198 // private IPackageFragment choosePackage() {
1199 // IPackageFragmentRoot froot= getPackageFragmentRoot();
1200 // IJavaElement[] packages= null;
1202 // if (froot != null && froot.exists()) {
1203 // packages= froot.getChildren();
1205 // } catch (JavaModelException e) {
1206 // PHPeclipsePlugin.log(e);
1208 // if (packages == null) {
1209 // packages= new IJavaElement[0];
1212 // ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
1213 // dialog.setIgnoreCase(false);
1214 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.title")); //$NON-NLS-1$
1215 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.description")); //$NON-NLS-1$
1216 // dialog.setEmptyListMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.empty")); //$NON-NLS-1$
1217 // dialog.setElements(packages);
1218 // IPackageFragment pack= getPackageFragment();
1219 // if (pack != null) {
1220 // dialog.setInitialSelections(new Object[] { pack });
1223 // if (dialog.open() == ElementListSelectionDialog.OK) {
1224 // return (IPackageFragment) dialog.getFirstResult();
1229 // private IType chooseEnclosingType() {
1230 // IPackageFragmentRoot root= getPackageFragmentRoot();
1231 // if (root == null) {
1235 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { root });
1237 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.TYPE, scope);
1238 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.title")); //$NON-NLS-1$
1239 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.description")); //$NON-NLS-1$
1240 // dialog.setFilter(Signature.getSimpleName(getEnclosingTypeText()));
1242 // if (dialog.open() == TypeSelectionDialog.OK) {
1243 // return (IType) dialog.getFirstResult();
1248 // private IType chooseSuperType() {
1249 // IPackageFragmentRoot root= getPackageFragmentRoot();
1250 // if (root == null) {
1254 // IJavaElement[] elements= new IJavaElement[] { root.getJavaProject() };
1255 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
1257 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.CLASS, scope);
1258 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.title")); //$NON-NLS-1$
1259 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.message")); //$NON-NLS-1$
1260 // dialog.setFilter(getSuperClass());
1262 // if (dialog.open() == TypeSelectionDialog.OK) {
1263 // return (IType) dialog.getFirstResult();
1268 // private void chooseSuperInterfaces() {
1269 // IPackageFragmentRoot root= getPackageFragmentRoot();
1270 // if (root == null) {
1274 // IJavaProject project= root.getJavaProject();
1275 // SuperInterfaceSelectionDialog dialog= new SuperInterfaceSelectionDialog(getShell(), getWizard().getContainer(), fSuperInterfacesDialogField, project);
1276 // dialog.setTitle(fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.class.title") : NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.interface.title")); //$NON-NLS-1$ //$NON-NLS-2$
1277 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.message")); //$NON-NLS-1$
1282 // ---- creation ----------------
1285 * Creates the new type using the entered field values.
1287 * @param monitor a progress monitor to report progress.
1289 public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException {
1290 if (monitor == null) {
1291 monitor = new NullProgressMonitor();
1294 monitor.beginTask(NewWizardMessages.getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$
1295 ICompilationUnit createdWorkingCopy = null;
1297 // IPackageFragmentRoot root = getPackageFragmentRoot();
1298 // IPackageFragment pack = getPackageFragment();
1299 // if (pack == null) {
1300 // pack = root.getPackageFragment(""); //$NON-NLS-1$
1303 // if (!pack.exists()) {
1304 // String packName = pack.getElementName();
1305 // pack = root.createPackageFragment(packName, true, null);
1310 String clName = getTypeName();
1312 boolean isInnerClass = isEnclosingTypeSelected();
1315 // ImportsStructure imports;
1318 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
1319 // String[] prefOrder = JavaPreferencesSettings.getImportOrderPreference(store);
1320 // int threshold = JavaPreferencesSettings.getImportNumberThreshold(store);
1322 String lineDelimiter = null;
1323 // if (!isInnerClass) {
1324 lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1326 // ICompilationUnit parentCU = pack.createCompilationUnit(clName + ".php", "", false, new SubProgressMonitor(monitor, 2)); //$NON-NLS-1$ //$NON-NLS-2$
1327 // createdWorkingCopy = (ICompilationUnit) parentCU.getSharedWorkingCopy(null, JavaUI.getBufferFactory(), null);
1329 // imports = new ImportsStructure(createdWorkingCopy, prefOrder, threshold, false);
1330 // // add an import that will be removed again. Having this import solves 14661
1331 // imports.addImport(pack.getElementName(), getTypeName());
1333 String typeContent = constructTypeStub(lineDelimiter);//new ImportsManager(imports), lineDelimiter);
1335 // String cuContent = constructCUContent(parentCU, typeContent, lineDelimiter);
1337 // createdWorkingCopy.getBuffer().setContents(cuContent);
1339 createdType = createdWorkingCopy.getType(clName);
1341 // IType enclosingType = getEnclosingType();
1343 // // if we are working on a enclosed type that is open in an editor,
1344 // // then replace the enclosing type with its working copy
1345 // IType workingCopy = (IType) EditorUtility.getWorkingCopy(enclosingType);
1346 // if (workingCopy != null) {
1347 // enclosingType = workingCopy;
1350 // ICompilationUnit parentCU = enclosingType.getCompilationUnit();
1351 // imports = new ImportsStructure(parentCU, prefOrder, threshold, true);
1353 // // add imports that will be removed again. Having the imports solves 14661
1354 // IType[] topLevelTypes = parentCU.getTypes();
1355 // for (int i = 0; i < topLevelTypes.length; i++) {
1356 // imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
1359 // lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
1360 // StringBuffer content = new StringBuffer();
1361 // String comment = getTypeComment(parentCU);
1362 // if (comment != null) {
1363 // content.append(comment);
1364 // content.append(lineDelimiter);
1366 // content.append(constructTypeStub(new ImportsManager(imports), lineDelimiter));
1367 // IJavaElement[] elems = enclosingType.getChildren();
1368 // IJavaElement sibling = elems.length > 0 ? elems[0] : null;
1370 // createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitor, 1));
1372 // indent = StubUtility.getIndentUsed(enclosingType) + 1;
1375 // // add imports for superclass/interfaces, so types can be resolved correctly
1376 // imports.create(false, new SubProgressMonitor(monitor, 1));
1378 ICompilationUnit cu = createdType.getCompilationUnit();
1382 // createTypeMembers(createdType, new ImportsManager(imports), new SubProgressMonitor(monitor, 1));
1385 // imports.create(false, new SubProgressMonitor(monitor, 1));
1390 ISourceRange range = createdType.getSourceRange();
1392 IBuffer buf = cu.getBuffer();
1393 String originalContent = buf.getText(range.getOffset(), range.getLength());
1394 String formattedContent = StubUtility.codeFormat(originalContent, indent, lineDelimiter);
1395 buf.replace(range.getOffset(), range.getLength(), formattedContent);
1396 if (!isInnerClass) {
1397 String fileComment = getFileComment(cu);
1398 if (fileComment != null && fileComment.length() > 0) {
1399 buf.replace(0, 0, fileComment + lineDelimiter);
1401 cu.commit(false, new SubProgressMonitor(monitor, 1));
1405 fCreatedType = createdType;
1407 if (createdWorkingCopy != null) {
1408 createdWorkingCopy.destroy();
1415 * Uses the New Java file template from the code template page to generate a
1416 * compilation unit with the given type content.
1417 * @param cu The new created compilation unit
1418 * @param typeContent The content of the type, including signature and type
1420 * @param lineDelimiter The line delimiter to be used.
1421 * @return String Returns the result of evaluating the new file template
1422 * with the given type content.
1423 * @throws CoreException
1426 // protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
1427 // StringBuffer typeQualifiedName= new StringBuffer();
1428 // if (isEnclosingTypeSelected()) {
1429 // typeQualifiedName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1431 // typeQualifiedName.append(getTypeName());
1432 // String typeComment= CodeGeneration.getTypeComment(cu, typeQualifiedName.toString(), lineDelimiter);
1433 // IPackageFragment pack= (IPackageFragment) cu.getParent();
1434 // String content= CodeGeneration.getCompilationUnitContent(cu, typeComment, typeContent, lineDelimiter);
1435 // if (content != null) {
1436 // CompilationUnit unit= AST.parseCompilationUnit(content.toCharArray());
1437 // if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
1441 // StringBuffer buf= new StringBuffer();
1442 // if (!pack.isDefaultPackage()) {
1443 // buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
1445 // buf.append(lineDelimiter).append(lineDelimiter);
1446 // if (typeComment != null) {
1447 // buf.append(typeComment).append(lineDelimiter);
1449 // buf.append(typeContent);
1450 // return buf.toString();
1454 * Returns the created type. The method only returns a valid type
1455 * after <code>createType</code> has been called.
1457 * @return the created type
1458 * @see #createType(IProgressMonitor)
1460 public IType getCreatedType() {
1461 return fCreatedType;
1464 // ---- construct cu body----------------
1466 // private void writeSuperClass(StringBuffer buf, ImportsManager imports) {
1467 // String typename= getSuperClass();
1468 // if (fIsClass && typename.length() > 0 && !"java.lang.Object".equals(typename)) { //$NON-NLS-1$
1469 // buf.append(" extends "); //$NON-NLS-1$
1471 // String qualifiedName= fSuperClass != null ? JavaModelUtil.getFullyQualifiedName(fSuperClass) : typename;
1472 // buf.append(imports.addImport(qualifiedName));
1476 // private void writeSuperInterfaces(StringBuffer buf, ImportsManager imports) {
1477 // List interfaces= getSuperInterfaces();
1478 // int last= interfaces.size() - 1;
1481 // buf.append(" implements "); //$NON-NLS-1$
1483 // buf.append(" extends "); //$NON-NLS-1$
1485 // for (int i= 0; i <= last; i++) {
1486 // String typename= (String) interfaces.get(i);
1487 // buf.append(imports.addImport(typename));
1496 * Called from createType to construct the source for this type
1498 private String constructTypeStub(String lineDelimiter) { // ImportsManager imports, String lineDelimiter) {
1499 StringBuffer buf= new StringBuffer();
1501 int modifiers= getModifiers();
1502 buf.append(Flags.toString(modifiers));
1503 if (modifiers != 0) {
1506 buf.append(fIsClass ? "class " : "interface "); //$NON-NLS-2$ //$NON-NLS-1$
1507 buf.append(getTypeName());
1508 // writeSuperClass(buf, imports);
1509 // writeSuperInterfaces(buf, imports);
1511 buf.append(lineDelimiter);
1512 buf.append(lineDelimiter);
1514 buf.append(lineDelimiter);
1515 return buf.toString();
1519 * @deprecated Overwrite createTypeMembers(IType, IImportsManager, IProgressMonitor) instead
1521 // protected void createTypeMembers(IType newType, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1526 * Hook method that gets called from <code>createType</code> to support adding of
1527 * unanticipated methods, fields, and inner types to the created type.
1529 * Implementers can use any methods defined on <code>IType</code> to manipulate the
1533 * The source code of the new type will be formtted using the platform's formatter. Needed
1534 * imports are added by the wizard at the end of the type creation process using the given
1538 * @param newType the new type created via <code>createType</code>
1539 * @param imports an import manager which can be used to add new imports
1540 * @param monitor a progress monitor to report progress. Must not be <code>null</code>
1542 * @see #createType(IProgressMonitor)
1544 // protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1545 // // call for compatibility
1546 // createTypeMembers(newType, ((ImportsManager)imports).getImportsStructure(), monitor);
1548 // // default implementation does nothing
1549 // // example would be
1550 // // String mainMathod= "public void foo(Vector vec) {}"
1551 // // createdType.createMethod(main, null, false, null);
1552 // // imports.addImport("java.lang.Vector");
1556 * @deprecated Instead of file templates, the new type code template
1557 * specifies the stub for a compilation unit.
1559 protected String getFileComment(ICompilationUnit parentCU) {
1563 private boolean isValidComment(String template) {
1564 IScanner scanner = ToolFactory.createScanner(true, false, false); //, false);
1565 scanner.setSource(template.toCharArray());
1567 int next = scanner.getNextToken();
1568 while (next == ITerminalSymbols.TokenNameCOMMENT_LINE
1569 || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
1570 || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
1571 next = scanner.getNextToken();
1573 return next == ITerminalSymbols.TokenNameEOF;
1574 } catch (InvalidInputException e) {
1580 * Hook method that gets called from <code>createType</code> to retrieve
1581 * a type comment. This default implementation returns the content of the
1582 * 'typecomment' template.
1584 * @return the type comment or <code>null</code> if a type comment
1587 protected String getTypeComment(ICompilationUnit parentCU) {
1588 if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_ADD_COMMENTS)) {
1590 StringBuffer typeName= new StringBuffer();
1591 if (isEnclosingTypeSelected()) {
1592 typeName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1594 typeName.append(getTypeName());
1595 String comment= CodeGeneration.getTypeComment(parentCU, typeName.toString(), String.valueOf('\n'));
1596 if (comment != null && isValidComment(comment)) {
1599 } catch (CoreException e) {
1600 PHPeclipsePlugin.log(e);
1607 * @deprecated Use getTemplate(String,ICompilationUnit,int)
1609 protected String getTemplate(String name, ICompilationUnit parentCU) {
1610 return getTemplate(name, parentCU, 0);
1614 * Returns the string resulting from evaluation the given template in
1615 * the context of the given compilation unit. This accesses the normal
1616 * template page, not the code templates. To use code templates use
1617 * <code>constructCUContent</code> to construct a compilation unit stub or
1618 * getTypeComment for the comment of the type.
1620 * @param name the template to be evaluated
1621 * @param parentCU the templates evaluation context
1622 * @param pos a source offset into the parent compilation unit. The
1623 * template is evalutated at the given source offset
1625 protected String getTemplate(String name, ICompilationUnit parentCU, int pos) {
1627 Template[] templates = Templates.getInstance().getTemplates(name);
1628 if (templates.length > 0) {
1629 return JavaContext.evaluateTemplate(templates[0], parentCU, pos);
1631 } catch (CoreException e) {
1632 PHPeclipsePlugin.log(e);
1633 } catch (BadLocationException e1) {
1634 // TODO Auto-generated catch block
1635 e1.printStackTrace();
1636 } catch (TemplateException e1) {
1637 // TODO Auto-generated catch block
1638 e1.printStackTrace();
1644 * @deprecated Use createInheritedMethods(IType,boolean,boolean,IImportsManager,IProgressMonitor)
1646 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1647 // return createInheritedMethods(type, doConstructors, doUnimplementedMethods, new ImportsManager(imports), monitor);
1651 * Creates the bodies of all unimplemented methods and constructors and adds them to the type.
1652 * Method is typically called by implementers of <code>NewTypeWizardPage</code> to add
1653 * needed method and constructors.
1655 * @param type the type for which the new methods and constructor are to be created
1656 * @param doConstructors if <code>true</code> unimplemented constructors are created
1657 * @param doUnimplementedMethods if <code>true</code> unimplemented methods are created
1658 * @param imports an import manager to add all neded import statements
1659 * @param monitor a progress monitor to report progress
1661 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1662 // ArrayList newMethods= new ArrayList();
1663 // ITypeHierarchy hierarchy= null;
1664 // CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings();
1666 // if (doConstructors) {
1667 // hierarchy= type.newSupertypeHierarchy(monitor);
1668 // IType superclass= hierarchy.getSuperclass(type);
1669 // if (superclass != null) {
1670 // String[] constructors= StubUtility.evalConstructors(type, superclass, settings, imports.getImportsStructure());
1671 // if (constructors != null) {
1672 // for (int i= 0; i < constructors.length; i++) {
1673 // newMethods.add(constructors[i]);
1679 // if (doUnimplementedMethods) {
1680 // if (hierarchy == null) {
1681 // hierarchy= type.newSupertypeHierarchy(monitor);
1683 // String[] unimplemented= StubUtility.evalUnimplementedMethods(type, hierarchy, false, settings, null, imports.getImportsStructure());
1684 // if (unimplemented != null) {
1685 // for (int i= 0; i < unimplemented.length; i++) {
1686 // newMethods.add(unimplemented[i]);
1690 // IMethod[] createdMethods= new IMethod[newMethods.size()];
1691 // for (int i= 0; i < newMethods.size(); i++) {
1692 // String content= (String) newMethods.get(i) + '\n'; // content will be formatted, ok to use \n
1693 // createdMethods[i]= type.createMethod(content, null, false, null);
1695 // return createdMethods;
1698 // ---- creation ----------------
1701 * Returns the runnable that creates the type using the current settings.
1702 * The returned runnable must be executed in the UI thread.
1704 * @return the runnable to create the new type
1706 // public IRunnableWithProgress getRunnable() {
1707 // return new IRunnableWithProgress() {
1708 // public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
1710 // if (monitor == null) {
1711 // monitor= new NullProgressMonitor();
1713 // createType(monitor);
1714 // } catch (CoreException e) {
1715 // throw new InvocationTargetException(e);