Eclipse插件RCP桌面应用开发的点点滴滴( 二 )


2.wizardwizardpage添加好后,需要新建一个wizard类来管理它们。

MyWizardpackage de.vogella.rcp.intro.wizards.wizard;import org.eclipse.jface.wizard.Wizard;public class MyWizard extends Wizard {protected MyPageOne one;protected MyPageTwo two;public MyWizard() {super();setNeedsProgressMonitor(true);}@Overridepublic String getWindowTitle() {return "Export My Data";}@Overridepublic void addPages() {one = new MyPageOne();two = new MyPageTwo();addPage(one);addPage(two);}@Overridepublic boolean performFinish() {// Print the result to the consoleSystem.out.println(one.getText1());System.out.println(two.getText1());return true;}}
① 自定义wizard继承 JFACE的 Wizard类。重写addPage()方法,为向导添加向导页 。重写performFinish()方法,指定点击finish按钮后完成的动作.重写canFinish()方法,FINISH按钮是否可以点击,
可以通过这个方法,来判断是否是最后一页,最后一页才可以点FINISH按钮@Override public boolean canFinish() {if (this.getContainer().getCurrentPage() instanceof FilePreprocessingWizardPage) // FilePreprocessingWizardPage为最后一个页面return true;elsereturn false; }
重写getNextPage()方法, 下一页
3.WizardDialogwizardDialog 一般用于管理向导页的按钮,如果你想将原有的next/finish/cancel等按钮重写,就需要新建这个类 。下面是我项目中遇到的代码,需求是:最后一个页不再显示next按钮,而是改为start,并执行相关功能 。第一页finish不可点 (这个由wizard类的canfinish方法控制):
Eclipse插件RCP桌面应用开发的点点滴滴

文章插图
第二页finish可以点、next 变为start
Eclipse插件RCP桌面应用开发的点点滴滴

文章插图
点击查看代码import org.eclipse.jface.dialogs.IDialogConstants;import org.eclipse.jface.wizard.IWizard;import org.eclipse.jface.wizard.IWizardPage;import org.eclipse.jface.wizard.WizardDialog;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Button;public class InputFileWizardDialog extends WizardDialog { private Button startBtn; private Button nextButton; public InputFileWizardDialog(Shell parentShell, IWizard newWizard) {super(parentShell, newWizard); } @Override protected void buttonPressed(int buttonId) {switch (buttonId) {case IDialogConstants.HELP_ID: {helpPressed();break;}case IDialogConstants.BACK_ID: {backPressed();break;}case IDialogConstants.NEXT_ID: {nextPressed();break;}case IDialogConstants.FINISH_ID: {finishPressed();break;}} } @Override protected void nextPressed() {IWizardPage currentPage = getCurrentPage();IWizardPage nextPage = currentPage.getNextPage();if (currentPage instanceof FilePreprocessingWizardPage) {((FilePreprocessingWizardPage) currentPage).startButtonClick();}if (nextPage instanceof FilePreprocessingWizardPage) { // last pageif (nextPage.getControl() != null)nextPage.dispose();showPage(nextPage);startBtn = this.getButton(IDialogConstants.NEXT_ID);startBtn.setText("Start");startBtn.setEnabled(true);} } /*** The Back button has been pressed.*/ @Override protected void backPressed() {IWizardPage page = getCurrentPage().getPreviousPage();super.backPressed();if (!(page instanceof FilePreprocessingWizardPage)) { // last pagenextButton = this.getButton(IDialogConstants.NEXT_ID);nextButton.setText(IDialogConstants.NEXT_LABEL);} }}
①buttonPressed()方法监听按钮被点击后执行的方法②nextPressed()方法,下一页。这里判断当前页面的下一页是否为最后一页,如果是则通过setTest方法将按钮改为start按钮,并将其设为可用状态。如果当前页面已经是最后一页,则执行在最后一页中定义的startbuttonclick方法。③ backPressed()方法,点上一页时,将上个方法中被改变的next按钮复原
4.最后,打开一个wizard一般写在一个按钮监听中 ,或者菜单功能里。

经验总结扩展阅读