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.ICompilationUnit;
18 import net.sourceforge.phpdt.core.IJavaElement;
19 import net.sourceforge.phpdt.core.IPackageFragment;
20 import net.sourceforge.phpdt.core.IType;
21 import net.sourceforge.phpdt.core.ToolFactory;
22 import net.sourceforge.phpdt.core.compiler.IScanner;
23 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
24 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
25 import net.sourceforge.phpdt.externaltools.internal.ui.StatusInfo;
26 import net.sourceforge.phpdt.internal.corext.template.Template;
27 import net.sourceforge.phpdt.internal.corext.template.Templates;
28 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
29 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
30 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
31 import net.sourceforge.phpdt.internal.ui.wizards.NewWizardMessages;
32 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
33 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
34 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IListAdapter;
35 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
36 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
37 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ListDialogField;
38 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
39 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
40 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.Separator;
41 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
42 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField;
43 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringDialogField;
44 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
46 import org.eclipse.core.runtime.CoreException;
47 import org.eclipse.core.runtime.IStatus;
48 import org.eclipse.jface.viewers.LabelProvider;
49 import org.eclipse.swt.SWT;
50 import org.eclipse.swt.graphics.Image;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Control;
58 * The class <code>NewTypeWizardPage</code> contains controls and validation routines
59 * for a 'New Type WizardPage'. Implementors decide which components to add and to enable.
60 * Implementors can also customize the validation code. <code>NewTypeWizardPage</code>
61 * is intended to serve as base class of all wizards that create types like applets, servlets, classes,
64 * See <code>NewClassWizardPage</code> or <code>NewInterfaceWizardPage</code> for an
65 * example usage of <code>NewTypeWizardPage</code>.
68 * @see org.eclipse.jdt.ui.wizards.NewClassWizardPage
69 * @see org.eclipse.jdt.ui.wizards.NewInterfaceWizardPage
73 public abstract class NewTypeWizardPage extends NewContainerWizardPage {
76 * Class used in stub creation routines to add needed imports to a
79 // public static class ImportsManager {
81 // private IImportsStructure fImportsStructure;
83 // /* package */ ImportsManager(IImportsStructure structure) {
84 // fImportsStructure= structure;
87 // /* package */ IImportsStructure getImportsStructure() {
88 // return fImportsStructure;
92 // * Adds a new import declaration that is sorted in the existing imports.
93 // * If an import already exists or the import would conflict with another import
94 // * of an other type with the same simple name the import is not added.
96 // * @param qualifiedTypeName The fully qualified name of the type to import
98 // * @return Returns the simple type name that can be used in the code or the
99 // * fully qualified type name if an import conflict prevented the import
101 // public String addImport(String qualifiedTypeName) {
102 // return fImportsStructure.addImport(qualifiedTypeName);
106 /** Public access flag. See The Java Virtual Machine Specification for more details. */
107 public int F_PUBLIC = Flags.AccPublic;
108 /** Private access flag. See The Java Virtual Machine Specification for more details. */
109 public int F_PRIVATE = Flags.AccPrivate;
110 /** Protected access flag. See The Java Virtual Machine Specification for more details. */
111 public int F_PROTECTED = Flags.AccProtected;
112 /** Static access flag. See The Java Virtual Machine Specification for more details. */
113 public int F_STATIC = Flags.AccStatic;
114 /** Final access flag. See The Java Virtual Machine Specification for more details. */
115 public int F_FINAL = Flags.AccFinal;
116 /** Abstract property flag. See The Java Virtual Machine Specification for more details. */
117 // public int F_ABSTRACT = Flags.AccAbstract;
119 private final static String PAGE_NAME = "NewTypeWizardPage"; //$NON-NLS-1$
121 /** Field ID of the package input field */
122 protected final static String PACKAGE = PAGE_NAME + ".package"; //$NON-NLS-1$
123 /** Field ID of the eclosing type input field */
124 protected final static String ENCLOSING = PAGE_NAME + ".enclosing"; //$NON-NLS-1$
125 /** Field ID of the enclosing type checkbox */
126 protected final static String ENCLOSINGSELECTION = ENCLOSING + ".selection"; //$NON-NLS-1$
127 /** Field ID of the type name input field */
128 protected final static String TYPENAME = PAGE_NAME + ".typename"; //$NON-NLS-1$
129 /** Field ID of the super type input field */
130 protected final static String SUPER = PAGE_NAME + ".superclass"; //$NON-NLS-1$
131 /** Field ID of the super interfaces input field */
132 protected final static String INTERFACES = PAGE_NAME + ".interfaces"; //$NON-NLS-1$
133 /** Field ID of the modifier checkboxes */
134 protected final static String MODIFIERS = PAGE_NAME + ".modifiers"; //$NON-NLS-1$
135 /** Field ID of the method stubs checkboxes */
136 protected final static String METHODS = PAGE_NAME + ".methods"; //$NON-NLS-1$
138 private class InterfacesListLabelProvider extends LabelProvider {
140 private Image fInterfaceImage;
142 public InterfacesListLabelProvider() {
144 fInterfaceImage = PHPUiImages.get(PHPUiImages.IMG_OBJS_INTERFACE);
147 public Image getImage(Object element) {
148 return fInterfaceImage;
152 private StringButtonStatusDialogField fPackageDialogField;
154 private SelectionButtonDialogField fEnclosingTypeSelection;
155 private StringButtonDialogField fEnclosingTypeDialogField;
157 private boolean fCanModifyPackage;
158 private boolean fCanModifyEnclosingType;
160 private IPackageFragment fCurrPackage;
162 // private IType fCurrEnclosingType;
163 private StringDialogField fTypeNameDialogField;
165 private StringButtonDialogField fSuperClassDialogField;
166 private ListDialogField fSuperInterfacesDialogField;
168 // private IType fSuperClass;
170 private SelectionButtonDialogFieldGroup fAccMdfButtons;
171 private SelectionButtonDialogFieldGroup fOtherMdfButtons;
173 // private IType fCreatedType;
175 protected IStatus fEnclosingTypeStatus;
176 protected IStatus fPackageStatus;
177 protected IStatus fTypeNameStatus;
178 // protected IStatus fSuperClassStatus;
179 protected IStatus fModifierStatus;
180 // protected IStatus fSuperInterfacesStatus;
182 private boolean fIsClass;
183 private int fStaticMdfIndex;
185 private final int PUBLIC_INDEX = 0, DEFAULT_INDEX = 1, PRIVATE_INDEX = 2, PROTECTED_INDEX = 3;
186 private final int ABSTRACT_INDEX = 0, FINAL_INDEX = 1;
189 * Creates a new <code>NewTypeWizardPage</code>
191 * @param isClass <code>true</code> if a new class is to be created; otherwise
192 * an interface is to be created
193 * @param pageName the wizard page's name
195 public NewTypeWizardPage(boolean isClass, String pageName) {
197 // fCreatedType= null;
201 TypeFieldsAdapter adapter = new TypeFieldsAdapter();
203 fPackageDialogField = new StringButtonStatusDialogField(adapter);
204 fPackageDialogField.setDialogFieldListener(adapter);
205 fPackageDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.package.label")); //$NON-NLS-1$
206 fPackageDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.package.button")); //$NON-NLS-1$
207 fPackageDialogField.setStatusWidthHint(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
209 fEnclosingTypeSelection = new SelectionButtonDialogField(SWT.CHECK);
210 fEnclosingTypeSelection.setDialogFieldListener(adapter);
211 fEnclosingTypeSelection.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.enclosing.selection.label")); //$NON-NLS-1$
213 fEnclosingTypeDialogField = new StringButtonDialogField(adapter);
214 fEnclosingTypeDialogField.setDialogFieldListener(adapter);
215 fEnclosingTypeDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.enclosing.button")); //$NON-NLS-1$
217 fTypeNameDialogField = new StringDialogField();
218 fTypeNameDialogField.setDialogFieldListener(adapter);
219 fTypeNameDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.typename.label")); //$NON-NLS-1$
221 fSuperClassDialogField = new StringButtonDialogField(adapter);
222 fSuperClassDialogField.setDialogFieldListener(adapter);
223 fSuperClassDialogField.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.superclass.label")); //$NON-NLS-1$
224 fSuperClassDialogField.setButtonLabel(NewWizardMessages.getString("NewTypeWizardPage.superclass.button")); //$NON-NLS-1$
226 String[] addButtons = new String[] {
228 NewWizardMessages.getString("NewTypeWizardPage.interfaces.add"), //$NON-NLS-1$
232 NewWizardMessages.getString("NewTypeWizardPage.interfaces.remove") //$NON-NLS-1$
234 fSuperInterfacesDialogField = new ListDialogField(adapter, addButtons, new InterfacesListLabelProvider());
235 fSuperInterfacesDialogField.setDialogFieldListener(adapter);
236 String interfaceLabel = fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.interfaces.class.label") : NewWizardMessages.getString("NewTypeWizardPage.interfaces.ifc.label"); //$NON-NLS-1$ //$NON-NLS-2$
237 fSuperInterfacesDialogField.setLabelText(interfaceLabel);
238 fSuperInterfacesDialogField.setRemoveButtonIndex(2);
240 String[] buttonNames1 = new String[] {
241 /* 0 == PUBLIC_INDEX */
242 NewWizardMessages.getString("NewTypeWizardPage.modifiers.public"), //$NON-NLS-1$
243 /* 1 == DEFAULT_INDEX */
244 NewWizardMessages.getString("NewTypeWizardPage.modifiers.default"), //$NON-NLS-1$
245 /* 2 == PRIVATE_INDEX */
246 NewWizardMessages.getString("NewTypeWizardPage.modifiers.private"), //$NON-NLS-1$
247 /* 3 == PROTECTED_INDEX*/
248 NewWizardMessages.getString("NewTypeWizardPage.modifiers.protected") //$NON-NLS-1$
250 fAccMdfButtons = new SelectionButtonDialogFieldGroup(SWT.RADIO, buttonNames1, 4);
251 fAccMdfButtons.setDialogFieldListener(adapter);
252 fAccMdfButtons.setLabelText(NewWizardMessages.getString("NewTypeWizardPage.modifiers.acc.label")); //$NON-NLS-1$
253 fAccMdfButtons.setSelection(0, true);
255 String[] buttonNames2;
257 buttonNames2 = new String[] {
258 /* 0 == ABSTRACT_INDEX */
259 NewWizardMessages.getString("NewTypeWizardPage.modifiers.abstract"), //$NON-NLS-1$
260 /* 1 == FINAL_INDEX */
261 NewWizardMessages.getString("NewTypeWizardPage.modifiers.final"), //$NON-NLS-1$
263 NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
265 fStaticMdfIndex = 2; // index of the static checkbox is 2
267 buttonNames2 = new String[] { NewWizardMessages.getString("NewTypeWizardPage.modifiers.static") //$NON-NLS-1$
269 fStaticMdfIndex = 0; // index of the static checkbox is 0
272 fOtherMdfButtons = new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames2, 4);
273 fOtherMdfButtons.setDialogFieldListener(adapter);
275 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, false);
276 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, false);
277 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, false);
279 fPackageStatus = new StatusInfo();
280 fEnclosingTypeStatus = new StatusInfo();
282 fCanModifyPackage = true;
283 fCanModifyEnclosingType = true;
286 fTypeNameStatus = new StatusInfo();
287 // fSuperClassStatus= new StatusInfo();
288 // fSuperInterfacesStatus= new StatusInfo();
289 fModifierStatus = new StatusInfo();
293 * Initializes all fields provided by the page with a given selection.
295 * @param elem the selection used to intialize this page or <code>
296 * null</code> if no selection was available
298 protected void initTypePage(IJavaElement elem) {
299 String initSuperclass = "java.lang.Object"; //$NON-NLS-1$
300 ArrayList initSuperinterfaces = new ArrayList(5);
302 IPackageFragment pack = null;
303 IType enclosingType = null;
306 // evaluate the enclosing type
307 pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
308 IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
309 if (typeInCU != null) {
310 if (typeInCU.getCompilationUnit() != null) {
311 enclosingType = typeInCU;
314 ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
316 // enclosingType= cu.findPrimaryType();
322 // if (elem.getElementType() == IJavaElement.TYPE) {
323 // type= (IType)elem;
324 // if (type.exists()) {
325 // String superName= JavaModelUtil.getFullyQualifiedName(type);
326 // if (type.isInterface()) {
327 // initSuperinterfaces.add(superName);
329 // initSuperclass= superName;
333 // } catch (JavaModelException e) {
334 // PHPeclipsePlugin.log(e);
335 // // ignore this exception now
339 setPackageFragment(pack, true);
340 // setEnclosingType(enclosingType, true);
341 setEnclosingTypeSelection(false, true);
343 setTypeName("", true); //$NON-NLS-1$
344 setSuperClass(initSuperclass, true);
345 setSuperInterfaces(initSuperinterfaces, true);
348 // -------- UI Creation ---------
351 * Creates a separator line. Expects a <code>GridLayout</code> with at least 1 column.
353 * @param composite the parent composite
354 * @param nColumns number of columns to span
356 protected void createSeparator(Composite composite, int nColumns) {
357 (new Separator(SWT.SEPARATOR | SWT.HORIZONTAL)).doFillIntoGrid(composite, nColumns, convertHeightInCharsToPixels(1));
361 * Creates the controls for the package name field. Expects a <code>GridLayout</code> with at
364 * @param composite the parent composite
365 * @param nColumns number of columns to span
367 protected void createPackageControls(Composite composite, int nColumns) {
368 fPackageDialogField.doFillIntoGrid(composite, nColumns);
369 LayoutUtil.setWidthHint(fPackageDialogField.getTextControl(null), getMaxFieldWidth());
370 LayoutUtil.setHorizontalGrabbing(fPackageDialogField.getTextControl(null));
374 * Creates the controls for the enclosing type 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 createEnclosingTypeControls(Composite composite, int nColumns) {
382 Composite tabGroup = new Composite(composite, SWT.NONE);
383 GridLayout layout = new GridLayout();
384 layout.marginWidth = 0;
385 layout.marginHeight = 0;
386 tabGroup.setLayout(layout);
388 fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1);
390 Control c = fEnclosingTypeDialogField.getTextControl(composite);
391 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
392 gd.widthHint = getMaxFieldWidth();
393 gd.horizontalSpan = 2;
396 Button button = fEnclosingTypeDialogField.getChangeControl(composite);
397 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
398 gd.heightHint = SWTUtil.getButtonHeigthHint(button);
399 gd.widthHint = SWTUtil.getButtonWidthHint(button);
400 button.setLayoutData(gd);
404 * Creates the controls for the type name field. Expects a <code>GridLayout</code> with at
407 * @param composite the parent composite
408 * @param nColumns number of columns to span
410 protected void createTypeNameControls(Composite composite, int nColumns) {
411 fTypeNameDialogField.doFillIntoGrid(composite, nColumns - 1);
412 DialogField.createEmptySpace(composite);
414 LayoutUtil.setWidthHint(fTypeNameDialogField.getTextControl(null), getMaxFieldWidth());
418 * Creates the controls for the modifiers radio/ceckbox buttons. Expects a
419 * <code>GridLayout</code> with at least 3 columns.
421 * @param composite the parent composite
422 * @param nColumns number of columns to span
424 protected void createModifierControls(Composite composite, int nColumns) {
425 LayoutUtil.setHorizontalSpan(fAccMdfButtons.getLabelControl(composite), 1);
427 Control control = fAccMdfButtons.getSelectionButtonsGroup(composite);
428 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
429 gd.horizontalSpan = nColumns - 2;
430 control.setLayoutData(gd);
432 DialogField.createEmptySpace(composite);
434 DialogField.createEmptySpace(composite);
436 control = fOtherMdfButtons.getSelectionButtonsGroup(composite);
437 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
438 gd.horizontalSpan = nColumns - 2;
439 control.setLayoutData(gd);
441 DialogField.createEmptySpace(composite);
445 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code>
446 * with at least 3 columns.
448 * @param composite the parent composite
449 * @param nColumns number of columns to span
451 protected void createSuperClassControls(Composite composite, int nColumns) {
452 fSuperClassDialogField.doFillIntoGrid(composite, nColumns);
453 LayoutUtil.setWidthHint(fSuperClassDialogField.getTextControl(null), getMaxFieldWidth());
457 * Creates the controls for the superclass name field. Expects a <code>GridLayout</code> with
458 * at least 3 columns.
460 * @param composite the parent composite
461 * @param nColumns number of columns to span
463 protected void createSuperInterfacesControls(Composite composite, int nColumns) {
464 fSuperInterfacesDialogField.doFillIntoGrid(composite, nColumns);
465 GridData gd = (GridData) fSuperInterfacesDialogField.getListControl(null).getLayoutData();
467 gd.heightHint = convertHeightInCharsToPixels(3);
469 gd.heightHint = convertHeightInCharsToPixels(6);
471 gd.grabExcessVerticalSpace = false;
472 gd.widthHint = getMaxFieldWidth();
476 * Sets the focus on the type name input field.
478 protected void setFocus() {
479 fTypeNameDialogField.setFocus();
482 // -------- TypeFieldsAdapter --------
484 private class TypeFieldsAdapter implements IStringButtonAdapter, IDialogFieldListener, IListAdapter {
486 // -------- IStringButtonAdapter
487 public void changeControlPressed(DialogField field) {
488 // typePageChangeControlPressed(field);
491 // -------- IListAdapter
492 public void customButtonPressed(ListDialogField field, int index) {
493 // typePageCustomButtonPressed(field, index);
496 public void selectionChanged(ListDialogField field) {
499 // -------- IDialogFieldListener
500 public void dialogFieldChanged(DialogField field) {
501 typePageDialogFieldChanged(field);
504 public void doubleClicked(ListDialogField field) {
508 // private void typePageChangeControlPressed(DialogField field) {
509 // if (field == fPackageDialogField) {
510 // IPackageFragment pack= choosePackage();
511 // if (pack != null) {
512 // fPackageDialogField.setText(pack.getElementName());
514 // } else if (field == fEnclosingTypeDialogField) {
515 // IType type= chooseEnclosingType();
516 // if (type != null) {
517 // fEnclosingTypeDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
519 // } else if (field == fSuperClassDialogField) {
520 // IType type= chooseSuperType();
521 // if (type != null) {
522 // fSuperClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
527 // private void typePageCustomButtonPressed(DialogField field, int index) {
528 // if (field == fSuperInterfacesDialogField) {
529 // chooseSuperInterfaces();
534 * A field on the type has changed. The fields' status and all dependend
535 * status are updated.
537 private void typePageDialogFieldChanged(DialogField field) {
538 String fieldName = null;
539 if (field == fPackageDialogField) {
540 fPackageStatus = packageChanged();
541 updatePackageStatusLabel();
542 fTypeNameStatus = typeNameChanged();
543 // fSuperClassStatus= superClassChanged();
545 } else if (field == fEnclosingTypeDialogField) {
546 // fEnclosingTypeStatus= enclosingTypeChanged();
547 fTypeNameStatus = typeNameChanged();
548 // fSuperClassStatus= superClassChanged();
549 fieldName = ENCLOSING;
550 } else if (field == fEnclosingTypeSelection) {
552 boolean isEnclosedType = isEnclosingTypeSelected();
553 if (!isEnclosedType) {
554 if (fAccMdfButtons.isSelected(PRIVATE_INDEX) || fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
555 fAccMdfButtons.setSelection(PRIVATE_INDEX, false);
556 fAccMdfButtons.setSelection(PROTECTED_INDEX, false);
557 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
559 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
560 fOtherMdfButtons.setSelection(fStaticMdfIndex, false);
563 fAccMdfButtons.enableSelectionButton(PRIVATE_INDEX, isEnclosedType && fIsClass);
564 fAccMdfButtons.enableSelectionButton(PROTECTED_INDEX, isEnclosedType && fIsClass);
565 fOtherMdfButtons.enableSelectionButton(fStaticMdfIndex, isEnclosedType);
566 fTypeNameStatus = typeNameChanged();
567 // fSuperClassStatus= superClassChanged();
568 fieldName = ENCLOSINGSELECTION;
569 } else if (field == fTypeNameDialogField) {
570 fTypeNameStatus = typeNameChanged();
571 fieldName = TYPENAME;
572 } else if (field == fSuperClassDialogField) {
573 // fSuperClassStatus= superClassChanged();
575 } else if (field == fSuperInterfacesDialogField) {
576 // fSuperInterfacesStatus= superInterfacesChanged();
577 fieldName = INTERFACES;
578 } else if (field == fOtherMdfButtons) {
579 fModifierStatus = modifiersChanged();
580 fieldName = MODIFIERS;
585 handleFieldChanged(fieldName);
588 // -------- update message ----------------
591 * @see org.eclipse.jdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
593 protected void handleFieldChanged(String fieldName) {
594 super.handleFieldChanged(fieldName);
595 if (fieldName == CONTAINER) {
596 fPackageStatus = packageChanged();
597 // fEnclosingTypeStatus= enclosingTypeChanged();
598 fTypeNameStatus = typeNameChanged();
599 // fSuperClassStatus= superClassChanged();
600 // fSuperInterfacesStatus= superInterfacesChanged();
604 // ---- set / get ----------------
607 * Returns the text of the package input field.
609 * @return the text of the package input field
611 public String getPackageText() {
612 return fPackageDialogField.getText();
616 * Returns the text of the enclosing type input field.
618 * @return the text of the enclosing type input field
620 public String getEnclosingTypeText() {
621 return fEnclosingTypeDialogField.getText();
625 * Returns the package fragment corresponding to the current input.
627 * @return a package fragement or <code>null</code> if the input
628 * could not be resolved.
630 public IPackageFragment getPackageFragment() {
631 if (!isEnclosingTypeSelected()) {
634 // if (fCurrEnclosingType != null) {
635 // return fCurrEnclosingType.getPackageFragment();
642 * Sets the package fragment to the given value. The method updates the model
643 * and the text of the control.
645 * @param pack the package fragement to be set
646 * @param canBeModified if <code>true</code> the package fragment is
647 * editable; otherwise it is read-only.
649 public void setPackageFragment(IPackageFragment pack, boolean canBeModified) {
651 fCanModifyPackage = canBeModified;
652 String str = (pack == null) ? "" : pack.getElementName(); //$NON-NLS-1$
653 fPackageDialogField.setText(str);
658 * Returns the enclosing type corresponding to the current input.
660 * @return the enclosing type or <code>null</code> if the enclosing type is
661 * not selected or the input could not be resolved
663 public IType getEnclosingType() {
664 // if (isEnclosingTypeSelected()) {
665 // return fCurrEnclosingType;
671 * Sets the enclosing type. The method updates the underlying model
672 * and the text of the control.
674 * @param type the enclosing type
675 * @param canBeModified if <code>true</code> the enclosing type field is
676 * editable; otherwise it is read-only.
678 // public void setEnclosingType(IType type, boolean canBeModified) {
679 // fCurrEnclosingType= type;
680 // fCanModifyEnclosingType= canBeModified;
681 // String str= (type == null) ? "" : JavaModelUtil.getFullyQualifiedName(type); //$NON-NLS-1$
682 // fEnclosingTypeDialogField.setText(str);
683 // updateEnableState();
687 * Returns the selection state of the enclosing type checkbox.
689 * @return the seleciton state of the enclosing type checkbox
691 public boolean isEnclosingTypeSelected() {
692 return fEnclosingTypeSelection.isSelected();
696 * Sets the enclosing type checkbox's selection state.
698 * @param isSelected the checkbox's selection state
699 * @param canBeModified if <code>true</code> the enclosing type checkbox is
700 * modifiable; otherwise it is read-only.
702 public void setEnclosingTypeSelection(boolean isSelected, boolean canBeModified) {
703 fEnclosingTypeSelection.setSelection(isSelected);
704 fEnclosingTypeSelection.setEnabled(canBeModified);
709 * Returns the type name entered into the type input field.
711 * @return the type name
713 public String getTypeName() {
714 return fTypeNameDialogField.getText();
718 * Sets the type name input field's text to the given value. Method doesn't update
721 * @param name the new type name
722 * @param canBeModified if <code>true</code> the enclosing type name field is
723 * editable; otherwise it is read-only.
725 public void setTypeName(String name, boolean canBeModified) {
726 fTypeNameDialogField.setText(name);
727 fTypeNameDialogField.setEnabled(canBeModified);
731 * Returns the selected modifiers.
733 * @return the selected modifiers
736 public int getModifiers() {
738 if (fAccMdfButtons.isSelected(PUBLIC_INDEX)) {
740 } else if (fAccMdfButtons.isSelected(PRIVATE_INDEX)) {
742 } else if (fAccMdfButtons.isSelected(PROTECTED_INDEX)) {
745 // if (fOtherMdfButtons.isSelected(ABSTRACT_INDEX) && (fStaticMdfIndex != 0)) {
748 if (fOtherMdfButtons.isSelected(FINAL_INDEX)) {
751 if (fOtherMdfButtons.isSelected(fStaticMdfIndex)) {
758 * Sets the modifiers.
760 * @param modifiers <code>F_PUBLIC</code>, <code>F_PRIVATE</code>,
761 * <code>F_PROTECTED</code>, <code>F_ABSTRACT, F_FINAL</code>
762 * or <code>F_STATIC</code> or, a valid combination.
763 * @param canBeModified if <code>true</code> the modifier fields are
764 * editable; otherwise they are read-only
767 public void setModifiers(int modifiers, boolean canBeModified) {
768 if (Flags.isPublic(modifiers)) {
769 fAccMdfButtons.setSelection(PUBLIC_INDEX, true);
770 } else if (Flags.isPrivate(modifiers)) {
771 fAccMdfButtons.setSelection(PRIVATE_INDEX, true);
772 } else if (Flags.isProtected(modifiers)) {
773 fAccMdfButtons.setSelection(PROTECTED_INDEX, true);
775 fAccMdfButtons.setSelection(DEFAULT_INDEX, true);
777 // if (Flags.isAbstract(modifiers)) {
778 // fOtherMdfButtons.setSelection(ABSTRACT_INDEX, true);
780 if (Flags.isFinal(modifiers)) {
781 fOtherMdfButtons.setSelection(FINAL_INDEX, true);
783 if (Flags.isStatic(modifiers)) {
784 fOtherMdfButtons.setSelection(fStaticMdfIndex, true);
787 fAccMdfButtons.setEnabled(canBeModified);
788 fOtherMdfButtons.setEnabled(canBeModified);
792 * Returns the content of the superclass input field.
794 * @return the superclass name
796 public String getSuperClass() {
797 return fSuperClassDialogField.getText();
801 * Sets the super class name.
803 * @param name the new superclass name
804 * @param canBeModified if <code>true</code> the superclass name field is
805 * editable; otherwise it is read-only.
807 public void setSuperClass(String name, boolean canBeModified) {
808 fSuperClassDialogField.setText(name);
809 fSuperClassDialogField.setEnabled(canBeModified);
813 * Returns the chosen super interfaces.
815 * @return a list of chosen super interfaces. The list's elements
816 * are of type <code>String</code>
818 public List getSuperInterfaces() {
819 return fSuperInterfacesDialogField.getElements();
823 * Sets the super interfaces.
825 * @param interfacesNames a list of super interface. The method requires that
826 * the list's elements are of type <code>String</code>
827 * @param canBeModified if <code>true</code> the super interface field is
828 * editable; otherwise it is read-only.
830 public void setSuperInterfaces(List interfacesNames, boolean canBeModified) {
831 fSuperInterfacesDialogField.setElements(interfacesNames);
832 fSuperInterfacesDialogField.setEnabled(canBeModified);
835 // ----------- validation ----------
838 * A hook method that gets called when the package field has changed. The method
839 * validates the package name and returns the status of the validation. The validation
840 * also updates the package fragment model.
842 * Subclasses may extend this method to perform their own validation.
845 * @return the status of the validation
847 protected IStatus packageChanged() {
848 StatusInfo status = new StatusInfo();
849 fPackageDialogField.enableButton(getPackageFragmentRoot() != null);
851 // String packName= getPackageText();
852 // if (packName.length() > 0) {
853 // IStatus val= JavaConventions.validatePackageName(packName);
854 // if (val.getSeverity() == IStatus.ERROR) {
855 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidPackageName", val.getMessage())); //$NON-NLS-1$
857 // } else if (val.getSeverity() == IStatus.WARNING) {
858 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.DiscouragedPackageName", val.getMessage())); //$NON-NLS-1$
863 // IPackageFragmentRoot root= getPackageFragmentRoot();
864 // if (root != null) {
865 // if (root.getJavaProject().exists() && packName.length() > 0) {
867 // IPath rootPath= root.getPath();
868 // IPath outputPath= root.getJavaProject().getOutputLocation();
869 // if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
870 // // if the bin folder is inside of our root, dont allow to name a package
871 // // like the bin folder
872 // IPath packagePath= rootPath.append(packName.replace('.', '/'));
873 // if (outputPath.isPrefixOf(packagePath)) {
874 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ClashOutputLocation")); //$NON-NLS-1$
878 // } catch (JavaModelException e) {
879 // PHPeclipsePlugin.log(e);
884 // fCurrPackage= root.getPackageFragment(packName);
886 // status.setError(""); //$NON-NLS-1$
892 * Updates the 'default' label next to the package field.
894 private void updatePackageStatusLabel() {
895 String packName = getPackageText();
897 if (packName.length() == 0) {
898 fPackageDialogField.setStatus(NewWizardMessages.getString("NewTypeWizardPage.default")); //$NON-NLS-1$
900 fPackageDialogField.setStatus(""); //$NON-NLS-1$
905 * Updates the enable state of buttons related to the enclosing type selection checkbox.
907 private void updateEnableState() {
908 boolean enclosing = isEnclosingTypeSelected();
909 fPackageDialogField.setEnabled(fCanModifyPackage && !enclosing);
910 fEnclosingTypeDialogField.setEnabled(fCanModifyEnclosingType && enclosing);
914 * Hook method that gets called when the enclosing type name has changed. The method
915 * validates the enclosing type and returns the status of the validation. It also updates the
916 * enclosing type model.
918 * Subclasses may extend this method to perform their own validation.
921 * @return the status of the validation
923 // protected IStatus enclosingTypeChanged() {
924 // StatusInfo status= new StatusInfo();
925 // fCurrEnclosingType= null;
927 // IPackageFragmentRoot root= getPackageFragmentRoot();
929 // fEnclosingTypeDialogField.enableButton(root != null);
930 // if (root == null) {
931 // status.setError(""); //$NON-NLS-1$
935 // String enclName= getEnclosingTypeText();
936 // if (enclName.length() == 0) {
937 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeEnterName")); //$NON-NLS-1$
941 // IType type= findType(root.getJavaProject(), enclName);
942 // if (type == null) {
943 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
947 // if (type.getCompilationUnit() == null) {
948 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotInCU")); //$NON-NLS-1$
951 // if (!JavaModelUtil.isEditable(type.getCompilationUnit())) {
952 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingNotEditable")); //$NON-NLS-1$
956 // fCurrEnclosingType= type;
957 // IPackageFragmentRoot enclosingRoot= JavaModelUtil.getPackageFragmentRoot(type);
958 // if (!enclosingRoot.equals(root)) {
959 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.EnclosingNotInSourceFolder")); //$NON-NLS-1$
962 // } catch (JavaModelException e) {
963 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnclosingTypeNotExists")); //$NON-NLS-1$
964 // PHPeclipsePlugin.log(e);
970 * Hook method that gets called when the type name has changed. The method validates the
971 * type name and returns the status of the validation.
973 * Subclasses may extend this method to perform their own validation.
976 * @return the status of the validation
978 protected IStatus typeNameChanged() {
979 StatusInfo status = new StatusInfo();
980 String typeName = getTypeName();
982 if (typeName.length() == 0) {
983 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.EnterTypeName")); //$NON-NLS-1$
986 if (typeName.indexOf('.') != -1) {
987 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.QualifiedName")); //$NON-NLS-1$
990 // IStatus val= JavaConventions.validateJavaTypeName(typeName);
991 // if (val.getSeverity() == IStatus.ERROR) {
992 // status.setError(NewWizardMessages.getFormattedString("NewTypeWizardPage.error.InvalidTypeName", val.getMessage())); //$NON-NLS-1$
994 // } else if (val.getSeverity() == IStatus.WARNING) {
995 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.TypeNameDiscouraged", val.getMessage())); //$NON-NLS-1$
996 // // continue checking
1000 if (!isEnclosingTypeSelected()) {
1001 IPackageFragment pack = getPackageFragment();
1003 ICompilationUnit cu = pack.getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
1004 if (cu.getResource().exists()) {
1005 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1010 IType type = getEnclosingType();
1012 IType member = type.getType(typeName);
1013 if (member.exists()) {
1014 status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.TypeNameExists")); //$NON-NLS-1$
1023 * Hook method that gets called when the superclass name has changed. The method
1024 * validates the superclass name and returns the status of the validation.
1026 * Subclasses may extend this method to perform their own validation.
1029 * @return the status of the validation
1031 // protected IStatus superClassChanged() {
1032 // StatusInfo status= new StatusInfo();
1033 // IPackageFragmentRoot root= getPackageFragmentRoot();
1034 // fSuperClassDialogField.enableButton(root != null);
1036 // fSuperClass= null;
1038 // String sclassName= getSuperClass();
1039 // if (sclassName.length() == 0) {
1040 // // accept the empty field (stands for java.lang.Object)
1043 // IStatus val= JavaConventions.validateJavaTypeName(sclassName);
1044 // if (val.getSeverity() == IStatus.ERROR) {
1045 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1048 // if (root != null) {
1050 // IType type= resolveSuperTypeName(root.getJavaProject(), sclassName);
1051 // if (type == null) {
1052 // status.setWarning(NewWizardMessages.getString("NewTypeWizardPage.warning.SuperClassNotExists")); //$NON-NLS-1$
1055 // if (type.isInterface()) {
1056 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotClass", sclassName)); //$NON-NLS-1$
1059 // int flags= type.getFlags();
1060 // if (Flags.isFinal(flags)) {
1061 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsFinal", sclassName)); //$NON-NLS-1$
1063 // } else if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1064 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.SuperClassIsNotVisible", sclassName)); //$NON-NLS-1$
1068 // fSuperClass= type;
1069 // } catch (JavaModelException e) {
1070 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.InvalidSuperClassName")); //$NON-NLS-1$
1071 // PHPeclipsePlugin.log(e);
1074 // status.setError(""); //$NON-NLS-1$
1080 // private IType resolveSuperTypeName(IJavaProject jproject, String sclassName) throws JavaModelException {
1081 // if (!jproject.exists()) {
1084 // IType type= null;
1085 // if (isEnclosingTypeSelected()) {
1086 // // search in the context of the enclosing type
1087 // IType enclosingType= getEnclosingType();
1088 // if (enclosingType != null) {
1089 // String[][] res= enclosingType.resolveType(sclassName);
1090 // if (res != null && res.length > 0) {
1091 // type= jproject.findType(res[0][0], res[0][1]);
1095 // IPackageFragment currPack= getPackageFragment();
1096 // if (type == null && currPack != null) {
1097 // String packName= currPack.getElementName();
1098 // // search in own package
1099 // if (!currPack.isDefaultPackage()) {
1100 // type= jproject.findType(packName, sclassName);
1102 // // search in java.lang
1103 // if (type == null && !"java.lang".equals(packName)) { //$NON-NLS-1$
1104 // type= jproject.findType("java.lang", sclassName); //$NON-NLS-1$
1107 // // search fully qualified
1108 // if (type == null) {
1109 // type= jproject.findType(sclassName);
1115 // private IType findType(IJavaProject project, String typeName) throws JavaModelException {
1116 // if (project.exists()) {
1117 // return project.findType(typeName);
1123 * Hook method that gets called when the list of super interface has changed. The method
1124 * validates the superinterfaces and returns the status of the validation.
1126 * Subclasses may extend this method to perform their own validation.
1129 * @return the status of the validation
1131 // protected IStatus superInterfacesChanged() {
1132 // StatusInfo status= new StatusInfo();
1134 // IPackageFragmentRoot root= getPackageFragmentRoot();
1135 // fSuperInterfacesDialogField.enableButton(0, root != null);
1137 // if (root != null) {
1138 // List elements= fSuperInterfacesDialogField.getElements();
1139 // int nElements= elements.size();
1140 // for (int i= 0; i < nElements; i++) {
1141 // String intfname= (String)elements.get(i);
1143 // IType type= findType(root.getJavaProject(), intfname);
1144 // if (type == null) {
1145 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceNotExists", intfname)); //$NON-NLS-1$
1148 // if (type.isClass()) {
1149 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotInterface", intfname)); //$NON-NLS-1$
1152 // if (!JavaModelUtil.isVisible(type, getPackageFragment())) {
1153 // status.setWarning(NewWizardMessages.getFormattedString("NewTypeWizardPage.warning.InterfaceIsNotVisible", intfname)); //$NON-NLS-1$
1157 // } catch (JavaModelException e) {
1158 // PHPeclipsePlugin.log(e);
1159 // // let pass, checking is an extra
1167 * Hook method that gets called when the modifiers have changed. The method validates
1168 * the modifiers and returns the status of the validation.
1170 * Subclasses may extend this method to perform their own validation.
1173 * @return the status of the validation
1175 protected IStatus modifiersChanged() {
1176 StatusInfo status = new StatusInfo();
1177 int modifiers = getModifiers();
1178 // if (Flags.isFinal(modifiers) && Flags.isAbstract(modifiers)) {
1179 // status.setError(NewWizardMessages.getString("NewTypeWizardPage.error.ModifiersFinalAndAbstract")); //$NON-NLS-1$
1184 // selection dialogs
1186 // private IPackageFragment choosePackage() {
1187 // IPackageFragmentRoot froot= getPackageFragmentRoot();
1188 // IJavaElement[] packages= null;
1190 // if (froot != null && froot.exists()) {
1191 // packages= froot.getChildren();
1193 // } catch (JavaModelException e) {
1194 // PHPeclipsePlugin.log(e);
1196 // if (packages == null) {
1197 // packages= new IJavaElement[0];
1200 // ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
1201 // dialog.setIgnoreCase(false);
1202 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.title")); //$NON-NLS-1$
1203 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.description")); //$NON-NLS-1$
1204 // dialog.setEmptyListMessage(NewWizardMessages.getString("NewTypeWizardPage.ChoosePackageDialog.empty")); //$NON-NLS-1$
1205 // dialog.setElements(packages);
1206 // IPackageFragment pack= getPackageFragment();
1207 // if (pack != null) {
1208 // dialog.setInitialSelections(new Object[] { pack });
1211 // if (dialog.open() == ElementListSelectionDialog.OK) {
1212 // return (IPackageFragment) dialog.getFirstResult();
1217 // private IType chooseEnclosingType() {
1218 // IPackageFragmentRoot root= getPackageFragmentRoot();
1219 // if (root == null) {
1223 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { root });
1225 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.TYPE, scope);
1226 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.title")); //$NON-NLS-1$
1227 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.ChooseEnclosingTypeDialog.description")); //$NON-NLS-1$
1228 // dialog.setFilter(Signature.getSimpleName(getEnclosingTypeText()));
1230 // if (dialog.open() == TypeSelectionDialog.OK) {
1231 // return (IType) dialog.getFirstResult();
1236 // private IType chooseSuperType() {
1237 // IPackageFragmentRoot root= getPackageFragmentRoot();
1238 // if (root == null) {
1242 // IJavaElement[] elements= new IJavaElement[] { root.getJavaProject() };
1243 // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
1245 // TypeSelectionDialog dialog= new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.CLASS, scope);
1246 // dialog.setTitle(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.title")); //$NON-NLS-1$
1247 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.SuperClassDialog.message")); //$NON-NLS-1$
1248 // dialog.setFilter(getSuperClass());
1250 // if (dialog.open() == TypeSelectionDialog.OK) {
1251 // return (IType) dialog.getFirstResult();
1256 // private void chooseSuperInterfaces() {
1257 // IPackageFragmentRoot root= getPackageFragmentRoot();
1258 // if (root == null) {
1262 // IJavaProject project= root.getJavaProject();
1263 // SuperInterfaceSelectionDialog dialog= new SuperInterfaceSelectionDialog(getShell(), getWizard().getContainer(), fSuperInterfacesDialogField, project);
1264 // dialog.setTitle(fIsClass ? NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.class.title") : NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.interface.title")); //$NON-NLS-1$ //$NON-NLS-2$
1265 // dialog.setMessage(NewWizardMessages.getString("NewTypeWizardPage.InterfacesDialog.message")); //$NON-NLS-1$
1270 // ---- creation ----------------
1273 * Creates the new type using the entered field values.
1275 * @param monitor a progress monitor to report progress.
1277 // public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException {
1278 // if (monitor == null) {
1279 // monitor = new NullProgressMonitor();
1282 // monitor.beginTask(NewWizardMessages.getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$
1283 // ICompilationUnit createdWorkingCopy = null;
1285 // IPackageFragmentRoot root = getPackageFragmentRoot();
1286 // IPackageFragment pack = getPackageFragment();
1287 // if (pack == null) {
1288 // pack = root.getPackageFragment(""); //$NON-NLS-1$
1291 // if (!pack.exists()) {
1292 // String packName = pack.getElementName();
1293 // pack = root.createPackageFragment(packName, true, null);
1296 // monitor.worked(1);
1298 // String clName = getTypeName();
1300 // boolean isInnerClass = isEnclosingTypeSelected();
1302 // IType createdType;
1303 // ImportsStructure imports;
1306 // IPreferenceStore store = PreferenceConstants.getPreferenceStore();
1307 // String[] prefOrder = JavaPreferencesSettings.getImportOrderPreference(store);
1308 // int threshold = JavaPreferencesSettings.getImportNumberThreshold(store);
1310 // String lineDelimiter = null;
1311 // if (!isInnerClass) {
1312 // lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1314 // ICompilationUnit parentCU = pack.createCompilationUnit(clName + ".java", "", false, new SubProgressMonitor(monitor, 2)); //$NON-NLS-1$ //$NON-NLS-2$
1315 // createdWorkingCopy = (ICompilationUnit) parentCU.getSharedWorkingCopy(null, JavaUI.getBufferFactory(), null);
1317 // imports = new ImportsStructure(createdWorkingCopy, prefOrder, threshold, false);
1318 // // add an import that will be removed again. Having this import solves 14661
1319 // imports.addImport(pack.getElementName(), getTypeName());
1321 // String typeContent = constructTypeStub(new ImportsManager(imports), lineDelimiter);
1323 // String cuContent = constructCUContent(parentCU, typeContent, lineDelimiter);
1325 // createdWorkingCopy.getBuffer().setContents(cuContent);
1327 // createdType = createdWorkingCopy.getType(clName);
1329 // IType enclosingType = getEnclosingType();
1331 // // if we are working on a enclosed type that is open in an editor,
1332 // // then replace the enclosing type with its working copy
1333 // IType workingCopy = (IType) EditorUtility.getWorkingCopy(enclosingType);
1334 // if (workingCopy != null) {
1335 // enclosingType = workingCopy;
1338 // ICompilationUnit parentCU = enclosingType.getCompilationUnit();
1339 // imports = new ImportsStructure(parentCU, prefOrder, threshold, true);
1341 // // add imports that will be removed again. Having the imports solves 14661
1342 // IType[] topLevelTypes = parentCU.getTypes();
1343 // for (int i = 0; i < topLevelTypes.length; i++) {
1344 // imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
1347 // lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
1348 // StringBuffer content = new StringBuffer();
1349 // String comment = getTypeComment(parentCU);
1350 // if (comment != null) {
1351 // content.append(comment);
1352 // content.append(lineDelimiter);
1354 // content.append(constructTypeStub(new ImportsManager(imports), lineDelimiter));
1355 // IJavaElement[] elems = enclosingType.getChildren();
1356 // IJavaElement sibling = elems.length > 0 ? elems[0] : null;
1358 // createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitor, 1));
1360 // indent = StubUtility.getIndentUsed(enclosingType) + 1;
1363 // // add imports for superclass/interfaces, so types can be resolved correctly
1364 // imports.create(false, new SubProgressMonitor(monitor, 1));
1366 // ICompilationUnit cu = createdType.getCompilationUnit();
1367 // synchronized (cu) {
1370 // createTypeMembers(createdType, new ImportsManager(imports), new SubProgressMonitor(monitor, 1));
1373 // imports.create(false, new SubProgressMonitor(monitor, 1));
1375 // synchronized (cu) {
1378 // ISourceRange range = createdType.getSourceRange();
1380 // IBuffer buf = cu.getBuffer();
1381 // String originalContent = buf.getText(range.getOffset(), range.getLength());
1382 // String formattedContent = StubUtility.codeFormat(originalContent, indent, lineDelimiter);
1383 // buf.replace(range.getOffset(), range.getLength(), formattedContent);
1384 // if (!isInnerClass) {
1385 // String fileComment = getFileComment(cu);
1386 // if (fileComment != null && fileComment.length() > 0) {
1387 // buf.replace(0, 0, fileComment + lineDelimiter);
1389 // cu.commit(false, new SubProgressMonitor(monitor, 1));
1391 // monitor.worked(1);
1393 // fCreatedType = createdType;
1395 // if (createdWorkingCopy != null) {
1396 // createdWorkingCopy.destroy();
1403 * Uses the New Java file template from the code template page to generate a
1404 * compilation unit with the given type content.
1405 * @param cu The new created compilation unit
1406 * @param typeContent The content of the type, including signature and type
1408 * @param lineDelimiter The line delimiter to be used.
1409 * @return String Returns the result of evaluating the new file template
1410 * with the given type content.
1411 * @throws CoreException
1414 // protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
1415 // StringBuffer typeQualifiedName= new StringBuffer();
1416 // if (isEnclosingTypeSelected()) {
1417 // typeQualifiedName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1419 // typeQualifiedName.append(getTypeName());
1420 // String typeComment= CodeGeneration.getTypeComment(cu, typeQualifiedName.toString(), lineDelimiter);
1421 // IPackageFragment pack= (IPackageFragment) cu.getParent();
1422 // String content= CodeGeneration.getCompilationUnitContent(cu, typeComment, typeContent, lineDelimiter);
1423 // if (content != null) {
1424 // CompilationUnit unit= AST.parseCompilationUnit(content.toCharArray());
1425 // if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
1429 // StringBuffer buf= new StringBuffer();
1430 // if (!pack.isDefaultPackage()) {
1431 // buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
1433 // buf.append(lineDelimiter).append(lineDelimiter);
1434 // if (typeComment != null) {
1435 // buf.append(typeComment).append(lineDelimiter);
1437 // buf.append(typeContent);
1438 // return buf.toString();
1442 * Returns the created type. The method only returns a valid type
1443 * after <code>createType</code> has been called.
1445 * @return the created type
1446 * @see #createType(IProgressMonitor)
1448 // public IType getCreatedType() {
1449 // return fCreatedType;
1452 // ---- construct cu body----------------
1454 // private void writeSuperClass(StringBuffer buf, ImportsManager imports) {
1455 // String typename= getSuperClass();
1456 // if (fIsClass && typename.length() > 0 && !"java.lang.Object".equals(typename)) { //$NON-NLS-1$
1457 // buf.append(" extends "); //$NON-NLS-1$
1459 // String qualifiedName= fSuperClass != null ? JavaModelUtil.getFullyQualifiedName(fSuperClass) : typename;
1460 // buf.append(imports.addImport(qualifiedName));
1464 // private void writeSuperInterfaces(StringBuffer buf, ImportsManager imports) {
1465 // List interfaces= getSuperInterfaces();
1466 // int last= interfaces.size() - 1;
1469 // buf.append(" implements "); //$NON-NLS-1$
1471 // buf.append(" extends "); //$NON-NLS-1$
1473 // for (int i= 0; i <= last; i++) {
1474 // String typename= (String) interfaces.get(i);
1475 // buf.append(imports.addImport(typename));
1484 * Called from createType to construct the source for this type
1486 // private String constructTypeStub(ImportsManager imports, String lineDelimiter) {
1487 // StringBuffer buf= new StringBuffer();
1489 // int modifiers= getModifiers();
1490 // buf.append(Flags.toString(modifiers));
1491 // if (modifiers != 0) {
1494 // buf.append(fIsClass ? "class " : "interface "); //$NON-NLS-2$ //$NON-NLS-1$
1495 // buf.append(getTypeName());
1496 // writeSuperClass(buf, imports);
1497 // writeSuperInterfaces(buf, imports);
1499 // buf.append(lineDelimiter);
1500 // buf.append(lineDelimiter);
1502 // buf.append(lineDelimiter);
1503 // return buf.toString();
1507 * @deprecated Overwrite createTypeMembers(IType, IImportsManager, IProgressMonitor) instead
1509 // protected void createTypeMembers(IType newType, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1514 * Hook method that gets called from <code>createType</code> to support adding of
1515 * unanticipated methods, fields, and inner types to the created type.
1517 * Implementers can use any methods defined on <code>IType</code> to manipulate the
1521 * The source code of the new type will be formtted using the platform's formatter. Needed
1522 * imports are added by the wizard at the end of the type creation process using the given
1526 * @param newType the new type created via <code>createType</code>
1527 * @param imports an import manager which can be used to add new imports
1528 * @param monitor a progress monitor to report progress. Must not be <code>null</code>
1530 * @see #createType(IProgressMonitor)
1532 // protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1533 // // call for compatibility
1534 // createTypeMembers(newType, ((ImportsManager)imports).getImportsStructure(), monitor);
1536 // // default implementation does nothing
1537 // // example would be
1538 // // String mainMathod= "public void foo(Vector vec) {}"
1539 // // createdType.createMethod(main, null, false, null);
1540 // // imports.addImport("java.lang.Vector");
1544 * @deprecated Instead of file templates, the new type code template
1545 * specifies the stub for a compilation unit.
1547 protected String getFileComment(ICompilationUnit parentCU) {
1551 private boolean isValidComment(String template) {
1552 IScanner scanner = ToolFactory.createScanner(true, false, false); //, false);
1553 scanner.setSource(template.toCharArray());
1555 int next = scanner.getNextToken();
1556 while (next == ITerminalSymbols.TokenNameCOMMENT_LINE
1557 || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
1558 || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
1559 next = scanner.getNextToken();
1561 return next == ITerminalSymbols.TokenNameEOF;
1562 } catch (InvalidInputException e) {
1568 * Hook method that gets called from <code>createType</code> to retrieve
1569 * a type comment. This default implementation returns the content of the
1570 * 'typecomment' template.
1572 * @return the type comment or <code>null</code> if a type comment
1575 // protected String getTypeComment(ICompilationUnit parentCU) {
1576 // if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_ADD_COMMENTS)) {
1578 // StringBuffer typeName= new StringBuffer();
1579 // if (isEnclosingTypeSelected()) {
1580 // typeName.append(JavaModelUtil.getTypeQualifiedName(getEnclosingType())).append('.');
1582 // typeName.append(getTypeName());
1583 // String comment= CodeGeneration.getTypeComment(parentCU, typeName.toString(), String.valueOf('\n'));
1584 // if (comment != null && isValidComment(comment)) {
1587 // } catch (CoreException e) {
1588 // PHPeclipsePlugin.log(e);
1595 * @deprecated Use getTemplate(String,ICompilationUnit,int)
1597 protected String getTemplate(String name, ICompilationUnit parentCU) {
1598 return getTemplate(name, parentCU, 0);
1602 * Returns the string resulting from evaluation the given template in
1603 * the context of the given compilation unit. This accesses the normal
1604 * template page, not the code templates. To use code templates use
1605 * <code>constructCUContent</code> to construct a compilation unit stub or
1606 * getTypeComment for the comment of the type.
1608 * @param name the template to be evaluated
1609 * @param parentCU the templates evaluation context
1610 * @param pos a source offset into the parent compilation unit. The
1611 * template is evalutated at the given source offset
1613 protected String getTemplate(String name, ICompilationUnit parentCU, int pos) {
1615 Template[] templates = Templates.getInstance().getTemplates(name);
1616 if (templates.length > 0) {
1617 return JavaContext.evaluateTemplate(templates[0], parentCU, pos);
1619 } catch (CoreException e) {
1620 PHPeclipsePlugin.log(e);
1626 * @deprecated Use createInheritedMethods(IType,boolean,boolean,IImportsManager,IProgressMonitor)
1628 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, IImportsStructure imports, IProgressMonitor monitor) throws CoreException {
1629 // return createInheritedMethods(type, doConstructors, doUnimplementedMethods, new ImportsManager(imports), monitor);
1633 * Creates the bodies of all unimplemented methods and constructors and adds them to the type.
1634 * Method is typically called by implementers of <code>NewTypeWizardPage</code> to add
1635 * needed method and constructors.
1637 * @param type the type for which the new methods and constructor are to be created
1638 * @param doConstructors if <code>true</code> unimplemented constructors are created
1639 * @param doUnimplementedMethods if <code>true</code> unimplemented methods are created
1640 * @param imports an import manager to add all neded import statements
1641 * @param monitor a progress monitor to report progress
1643 // protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
1644 // ArrayList newMethods= new ArrayList();
1645 // ITypeHierarchy hierarchy= null;
1646 // CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings();
1648 // if (doConstructors) {
1649 // hierarchy= type.newSupertypeHierarchy(monitor);
1650 // IType superclass= hierarchy.getSuperclass(type);
1651 // if (superclass != null) {
1652 // String[] constructors= StubUtility.evalConstructors(type, superclass, settings, imports.getImportsStructure());
1653 // if (constructors != null) {
1654 // for (int i= 0; i < constructors.length; i++) {
1655 // newMethods.add(constructors[i]);
1661 // if (doUnimplementedMethods) {
1662 // if (hierarchy == null) {
1663 // hierarchy= type.newSupertypeHierarchy(monitor);
1665 // String[] unimplemented= StubUtility.evalUnimplementedMethods(type, hierarchy, false, settings, null, imports.getImportsStructure());
1666 // if (unimplemented != null) {
1667 // for (int i= 0; i < unimplemented.length; i++) {
1668 // newMethods.add(unimplemented[i]);
1672 // IMethod[] createdMethods= new IMethod[newMethods.size()];
1673 // for (int i= 0; i < newMethods.size(); i++) {
1674 // String content= (String) newMethods.get(i) + '\n'; // content will be formatted, ok to use \n
1675 // createdMethods[i]= type.createMethod(content, null, false, null);
1677 // return createdMethods;
1680 // ---- creation ----------------
1683 * Returns the runnable that creates the type using the current settings.
1684 * The returned runnable must be executed in the UI thread.
1686 * @return the runnable to create the new type
1688 // public IRunnableWithProgress getRunnable() {
1689 // return new IRunnableWithProgress() {
1690 // public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
1692 // if (monitor == null) {
1693 // monitor= new NullProgressMonitor();
1695 // createType(monitor);
1696 // } catch (CoreException e) {
1697 // throw new InvocationTargetException(e);