插件项目中做一个弹出框通过类名包名新建Java类,就重构了下eclipse 新建Java向导类。
随便修改了下:
弹出代码:
CustomNewClassCreationWizard wizard = new CustomNewClassCreationWizard(); wizard.init(PlatformUI.getWorkbench(), wizard.getSelection()); wizard.setForcePreviousAndNextButtons(true); MyWizardDialog.createAndOpenDialog(Display.getDefault().getActiveShell(),wizard);这里的CustomNewClassCreateWizard重构了NewElementWizard。
package com.sunsheen.jfids.studio.composite.dialog.JavaClassDIalog; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.JavaPluginImages; import org.eclipse.jdt.internal.ui.wizards.NewElementWizard; import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; import org.eclipse.jdt.ui.wizards.NewClassWizardPage; /*** * * 重写Java创建向导 * @author minner * */ public class CustomNewClassCreationWizard extends NewElementWizard{ private CustomNewClassWizardPage fPage; private boolean fOpenEditorOnFinish; public CustomNewClassCreationWizard(CustomNewClassWizardPage page, boolean openEditorOnFinish) { setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_NEWCLASS); setDialogSettings(JavaPlugin.getDefault().getDialogSettings()); setWindowTitle("新建一个关联服务类"); fPage= page; fOpenEditorOnFinish= openEditorOnFinish; } public CustomNewClassCreationWizard() { this(null, true); } /* * @see Wizard#createPages */ @Override public void addPages() { super.addPages(); if (fPage == null) { fPage= new CustomNewClassWizardPage(); fPage.init(getSelection()); } addPage(fPage); } /*(non-Javadoc) * @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#canRunForked() */ @Override protected boolean canRunForked() { return !fPage.isEnclosingTypeSelected(); } /* (non-Javadoc) * @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#finishPage(org.eclipse.core.runtime.IProgressMonitor) */ @Override protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException { fPage.createType(monitor); // use the full progress monitor } /* (non-Javadoc) * @see org.eclipse.jface.wizard.IWizard#performFinish() */ @Override public boolean performFinish() { warnAboutTypeCommentDeprecation(); boolean res= super.performFinish(); if (res) { IResource resource= fPage.getModifiedResource(); if (resource != null) { selectAndReveal(resource); if (fOpenEditorOnFinish) { openResource((IFile) resource); } } } return res; } /* (non-Javadoc) * @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#getCreatedElement() */ @Override public IJavaElement getCreatedElement() { return fPage.getCreatedType(); } }
对话框页面的内容,也是一个重构类
/******************************************************************************* * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation * John Kaplan, johnkaplantech@gmail.com - 108071 [code templates] template for body of newly created class *******************************************************************************/ package com.sunsheen.jfids.studio.composite.dialog.JavaClassDIalog; import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Item; import org.eclipse.swt.widgets.Link; import org.eclipse.swt.widgets.Text; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.text.edits.TextEdit; import org.eclipse.jface.contentassist.SubjectControlContentAssistant; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.preference.PreferenceDialog; import org.eclipse.jface.viewers.CellEditor; import org.eclipse.jface.viewers.ICellModifier; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.window.Window; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.templates.Template; import org.eclipse.jface.text.templates.TemplateException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.contentassist.ContentAssistHandler; import org.eclipse.ui.dialogs.ElementListSelectionDialog; import org.eclipse.ui.dialogs.PreferencesUtil; import org.eclipse.jdt.core.Flags; import org.eclipse.jdt.core.IBuffer; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.ISourceRange; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaConventions; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.Signature; import org.eclipse.jdt.core.ToolFactory; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.compiler.IScanner; import org.eclipse.jdt.core.compiler.ITerminalSymbols; import org.eclipse.jdt.core.compiler.InvalidInputException; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.ImportDeclaration; import org.eclipse.jdt.core.dom.ParameterizedType; import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jdt.core.search.IJavaSearchConstants; import org.eclipse.jdt.core.search.IJavaSearchScope; import org.eclipse.jdt.core.search.SearchEngine; import org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedConstructorsOperation; import org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedMethodsOperation; import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; import org.eclipse.jdt.internal.corext.dom.ASTNodes; import org.eclipse.jdt.internal.corext.dom.TokenScanner; import org.eclipse.jdt.internal.corext.refactoring.StubTypeContext; import org.eclipse.jdt.internal.corext.refactoring.TypeContextChecker; import org.eclipse.jdt.internal.corext.template.java.JavaContext; import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.internal.corext.util.Resources; import org.eclipse.jdt.internal.corext.util.Strings; import org.eclipse.jdt.ui.CodeGeneration; import org.eclipse.jdt.ui.JavaElementLabelProvider; import org.eclipse.jdt.ui.wizards.NewContainerWizardPage; import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; import org.eclipse.jdt.ui.wizards.NewTypeWizardPage.ImportsManager; import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.JavaPluginImages; import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog; import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; import org.eclipse.jdt.internal.ui.dialogs.TableTextCellEditor; import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler; import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider; import org.eclipse.jdt.internal.ui.preferences.CodeTemplatePreferencePage; import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; import org.eclipse.jdt.internal.ui.refactoring.contentassist.CompletionContextRequestor; import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper; import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaPackageCompletionProcessor; import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor; import org.eclipse.jdt.internal.ui.util.SWTUtil; import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; import org.eclipse.jdt.internal.ui.wizards.SuperInterfaceSelectionDialog; import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter; import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter; import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup; import org.eclipse.jdt.internal.ui.wizards.dialogfields.Separator; import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField; import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField; import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; /** * 重写新建类向导 * The class <code>NewTypeWizardPage</code> contains controls and validation routines * for a 'New Type WizardPage'. Implementors decide which components to add and to enable. * Implementors can also customize the validation code. <code>NewTypeWizardPage</code> * is intended to serve as base class of all wizards that create types like applets, servlets, classes, * interfaces, etc. * <p> * See {@link NewClassWizardPage} or {@link NewInterfaceWizardPage} for an * example usage of the <code>NewTypeWizardPage</code>. * </p> * * @see org.eclipse.jdt.ui.wizards.NewClassWizardPage * @see org.eclipse.jdt.ui.wizards.NewInterfaceWizardPage * @see org.eclipse.jdt.ui.wizards.NewEnumWizardPage * @see org.eclipse.jdt.ui.wizards.NewAnnotationWizardPage * * @since 2.0 */ public class CustomNewClassWizardPage extends NewTypeWizardPage { private final static String PAGE_NAME= "NewClassWizardPage"; //$NON-NLS-1$ private final static String SETTINGS_CREATEMAIN= "create_main"; //$NON-NLS-1$ private final static String SETTINGS_CREATECONSTR= "create_constructor"; //$NON-NLS-1$ private final static String SETTINGS_CREATEUNIMPLEMENTED= "create_unimplemented"; //$NON-NLS-1$ private SelectionButtonDialogFieldGroup fMethodStubsButtons; /** * Creates a new <code>NewClassWizardPage</code> */ public CustomNewClassWizardPage() { super(true, PAGE_NAME); setTitle(NewWizardMessages.NewClassWizardPage_title); setDescription(NewWizardMessages.NewClassWizardPage_description); String[] buttonNames3= new String[] { NewWizardMessages.NewClassWizardPage_methods_main, NewWizardMessages.NewClassWizardPage_methods_constructors, NewWizardMessages.NewClassWizardPage_methods_inherited }; fMethodStubsButtons= new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames3, 1); fMethodStubsButtons.setLabelText(NewWizardMessages.NewClassWizardPage_methods_label); } // -------- Initialization --------- /** * The wizard owning this page is responsible for calling this method with the * current selection. The selection is used to initialize the fields of the wizard * page. * * @param selection used to initialize the fields */ public void init(IStructuredSelection selection) { IJavaElement jelem= getInitialJavaElement(selection); initContainerPage(jelem); initTypePage(jelem); doStatusUpdate(); boolean createMain= false; boolean createConstructors= false; boolean createUnimplemented= true; IDialogSettings dialogSettings= getDialogSettings(); if (dialogSettings != null) { IDialogSettings section= dialogSettings.getSection(PAGE_NAME); if (section != null) { createMain= section.getBoolean(SETTINGS_CREATEMAIN); createConstructors= section.getBoolean(SETTINGS_CREATECONSTR); createUnimplemented= section.getBoolean(SETTINGS_CREATEUNIMPLEMENTED); } } setMethodStubSelection(createMain, createConstructors, createUnimplemented, true); } // ------ validation -------- private void doStatusUpdate() { // status of all used components IStatus[] status= new IStatus[] { fContainerStatus, isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus, fTypeNameStatus, fModifierStatus, fSuperClassStatus, fSuperInterfacesStatus }; // the mode severe status will be displayed and the OK button enabled/disabled. updateStatus(status); } /* * @see NewContainerWizardPage#handleFieldChanged */ @Override protected void handleFieldChanged(String fieldName) { super.handleFieldChanged(fieldName); doStatusUpdate(); } // ------ UI -------- /* * @see WizardPage#createControl */ public void createControl(Composite parent) { initializeDialogUnits(parent); Composite composite= new Composite(parent, SWT.NONE); composite.setFont(parent.getFont()); int nColumns= 4; GridLayout layout= new GridLayout(); layout.numColumns= nColumns; composite.setLayout(layout); // pick & choose the wanted UI components createContainerControls(composite, nColumns); createPackageControls(composite, nColumns); // createEnclosingTypeControls(composite, nColumns); createSeparator(composite, nColumns); createTypeNameControls(composite, nColumns); // createModifierControls(composite, nColumns); // createSuperClassControls(composite, nColumns); createSuperInterfacesControls(composite, nColumns); // createMethodStubSelectionControls(composite, nColumns); // // createCommentControls(composite, nColumns); // enableCommentControl(true); setControl(composite); Dialog.applyDialogFont(composite); PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_CLASS_WIZARD_PAGE); } /* * @see WizardPage#becomesVisible */ @Override public void setVisible(boolean visible) { super.setVisible(visible); if (visible) { setFocus(); } else { IDialogSettings dialogSettings= getDialogSettings(); if (dialogSettings != null) { IDialogSettings section= dialogSettings.getSection(PAGE_NAME); if (section == null) { section= dialogSettings.addNewSection(PAGE_NAME); } section.put(SETTINGS_CREATEMAIN, isCreateMain()); section.put(SETTINGS_CREATECONSTR, isCreateConstructors()); section.put(SETTINGS_CREATEUNIMPLEMENTED, isCreateInherited()); } } } private void createMethodStubSelectionControls(Composite composite, int nColumns) { Control labelControl= fMethodStubsButtons.getLabelControl(composite); LayoutUtil.setHorizontalSpan(labelControl, nColumns); DialogField.createEmptySpace(composite); Control buttonGroup= fMethodStubsButtons.getSelectionButtonsGroup(composite); LayoutUtil.setHorizontalSpan(buttonGroup, nColumns - 1); } /** * Returns the current selection state of the 'Create Main' checkbox. * * @return the selection state of the 'Create Main' checkbox */ public boolean isCreateMain() { return fMethodStubsButtons.isSelected(0); } /** * Returns the current selection state of the 'Create Constructors' checkbox. * * @return the selection state of the 'Create Constructors' checkbox */ public boolean isCreateConstructors() { return fMethodStubsButtons.isSelected(1); } /** * Returns the current selection state of the 'Create inherited abstract methods' * checkbox. * * @return the selection state of the 'Create inherited abstract methods' checkbox */ public boolean isCreateInherited() { return fMethodStubsButtons.isSelected(2); } /** * Sets the selection state of the method stub checkboxes. * * @param createMain initial selection state of the 'Create Main' checkbox. * @param createConstructors initial selection state of the 'Create Constructors' checkbox. * @param createInherited initial selection state of the 'Create inherited abstract methods' checkbox. * @param canBeModified if <code>true</code> the method stub checkboxes can be changed by * the user. If <code>false</code> the buttons are "read-only" */ public void setMethodStubSelection(boolean createMain, boolean createConstructors, boolean createInherited, boolean canBeModified) { fMethodStubsButtons.setSelection(0, createMain); fMethodStubsButtons.setSelection(1, createConstructors); fMethodStubsButtons.setSelection(2, createInherited); fMethodStubsButtons.setEnabled(canBeModified); } // ---- creation ---------------- /* * @see NewTypeWizardPage#createTypeMembers */ @Override protected void createTypeMembers(IType type, ImportsManager imports, IProgressMonitor monitor) throws CoreException { boolean doMain= isCreateMain(); boolean doConstr= isCreateConstructors(); boolean doInherited= isCreateInherited(); createInheritedMethods(type, doConstr, doInherited, imports, new SubProgressMonitor(monitor, 1)); if (doMain) { StringBuffer buf= new StringBuffer(); final String lineDelim= "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$ String comment= CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", new String[] {"args"}, new String[0], Signature.createTypeSignature("void", true), null, lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ if (comment != null) { buf.append(comment); buf.append(lineDelim); } buf.append("public static void main("); //$NON-NLS-1$ buf.append(imports.addImport("java.lang.String")); //$NON-NLS-1$ buf.append("[] args) {"); //$NON-NLS-1$ buf.append(lineDelim); final String content= CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ if (content != null && content.length() != 0) buf.append(content); buf.append(lineDelim); buf.append("}"); //$NON-NLS-1$ type.createMethod(buf.toString(), null, false, null); } if (monitor != null) { monitor.done(); } } }
对比下NewTypeWizardPage就知道做了什么改变。
这里还想做一个操作,将接口实现一个指定的类,并且不可move。
找到这个类的
/* * @see WizardPage#createControl */ public void createControl(Composite parent) {
这个方法,顾名思义,则是一个创建组件的地方
看到里面的 createSuperInterfacesControls(composite, nColumns);
这个方法,这就是如何创建父接口的地方啦。
跟进去..