RCP界面---wizard完整的例子

    技术2024-10-29  59

    1.WizardDialog是承载的主窗体; Wizard是中间的管理类,它管理着4个WizardPage; 而WizardPage是真正的窗体中的可控制的UI界面,它填充了dialogArea区域,并且4个WizardPage都进行了初始化,并且都是setVisual(false)它们在WizardDialog使用特殊的布局器,进行布局。2.WizardDialog持有Wizard + currentWizardPage.Wizard持有WizardDialog和4个WizardPage.WizardPage持有Wizard--->所以,它也可以间接通过Wizard得到WizardDialog.(但是一般,不这样做,一般直接调用它的setMessage()来更新WizardDialog上的界面)3.WizardPage之间,WizardPage与Wizard之间的信息交互有三种方式:1).在WizardPage中定义username/password,让每个页面存储自己的模型信息。2).在Wizard中定义一个javabean来封装各个页面的模型,每个页面不再自己存储信息了,都来统一访问Wizard中的bean.3).利用Wizard里面的DialogSettings来作为通用存储4.如何控制WizardPage的isPageComplete信息,对于Text要注意addModifyListener。同时由于页面的username改变了,所以有必要及时的update WizardDialog的buttons.

    MyWizardPage1.this.getContainer().updateButtons(); 

    5.在UI(eclipse)中,父子类是经过精心设计的,但是仍然会有一些设计的问题。如果直接操作父类得不到API时,就及时向下转型。

    Composite composite = (Composite)parent; ((Wizard) newWizard).setNeedsProgressMonitor(true); address = ((Text) e.getSource()).getText(); 

    6.关于进度条与IRunnableWithProgress + IProgressMonitor的作用,参见例子:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/JFacesProgressMonitorDialog.htm

    ((Wizard) newWizard).setNeedsProgressMonitor(true); getContainer().run(true, true, new LongRunningOperation(false)); 

     

    //App.java package org.wizard; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.jface.wizard.Wizard; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; 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.Display; import org.eclipse.swt.widgets.Shell; public class App extends ApplicationWindow { @Override protected void configureShell(Shell shell) { super.configureShell(shell); shell.setText("应用程序"); shell.setSize(400, 300); } @Override protected Control createContents(Composite parent) { Composite contents = (Composite) super.createContents(parent); contents.setLayout(new GridLayout(1, false)); Button button = new Button(contents, SWT.PUSH); button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); button.setText("click"); button.addSelectionListener(new SelectionListener() { @Override public void widgetDefaultSelected(SelectionEvent e) { } @Override public void widgetSelected(SelectionEvent e) { Wizard myWizard = new MyWizard(); WizardDialog wdialog = new MyWizardDialog(App.this.getShell(), myWizard); myWizard.setWindowTitle("导入工程2"); wdialog.open(); } }); return contents; } public App() { super(null); } public void run() { this.setBlockOnOpen(true); this.open(); // 注意,这里调用dispose很重要,因为open中没有进行dispose操作。getCurrent是OK的。 Display.getCurrent().dispose(); } public static void main(String[] args) { new App().run(); } } 

     

    //MyWizardDialog.java package org.wizard; import org.eclipse.jface.wizard.IWizard; import org.eclipse.jface.wizard.Wizard; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.widgets.Shell; public class MyWizardDialog extends WizardDialog { @Override protected void configureShell(Shell newShell) { super.configureShell(newShell); // 这个windowTitle会被myWizard.setWindowTitle("导入工程2")覆盖 newShell.setText("导入工程1"); newShell.setSize(400, 300); newShell.setMinimumSize(300, 270); } public MyWizardDialog(Shell parentShell, IWizard newWizard) { super(parentShell, newWizard); ((Wizard) newWizard).setNeedsProgressMonitor(true); } } 

     

    //MyWizard.java package org.wizard; import java.lang.reflect.InvocationTargetException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.dialogs.DialogSettings; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.wizard.Wizard; public class MyWizard extends Wizard { public MyWizard() { this.setDialogSettings(new DialogSettings("导入工程")); } @Override public void addPages() { this.addPage(new MyWizardPage1()); this.addPage(new MyWizardPage2()); } @Override public boolean performFinish() { IDialogSettings dialogSettings2 = this.getDialogSettings(); final String username = dialogSettings2.get("用户名"); String password = dialogSettings2.get("密 码"); String city = dialogSettings2.get("城 市"); String address = dialogSettings2.get("地 址"); IRunnableWithProgress op = new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException { try { MessageDialog.openConfirm(MyWizard.this.getShell(), "请确认以下信息", "username=" + username); } catch (Exception e) { throw new InvocationTargetException(e); } finally { monitor.done(); } } }; try { getContainer().run(true, true, new LongRunningOperation(false)); } catch (InterruptedException e) { return false; } catch (InvocationTargetException e) { Throwable realException = e.getTargetException(); MessageDialog.openError(getShell(), "Error", realException .getMessage()); return false; } return true; } } class LongRunningOperation implements IRunnableWithProgress { // The total sleep time private static final int TOTAL_TIME = 10000; // The increment sleep time private static final int INCREMENT = 500; private boolean indeterminate; public LongRunningOperation(boolean indeterminate) { this.indeterminate = indeterminate; } public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { monitor.beginTask("Running long running operation", indeterminate ? IProgressMonitor.UNKNOWN : TOTAL_TIME); // monitor.subTask("Doing first half"); for (int total = 0; total < TOTAL_TIME && !monitor.isCanceled(); total += INCREMENT) { Thread.sleep(INCREMENT); monitor.worked(INCREMENT); if (total == TOTAL_TIME / 2) monitor.subTask("Doing second half"); } monitor.done(); if (monitor.isCanceled()) throw new InterruptedException( "The long running operation was cancelled"); } } 

     

    //MyWizardPage1.java package org.wizard; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; public class MyWizardPage1 extends WizardPage { private String username = ""; private String password = ""; protected MyWizardPage1() { super("MyWizardPage1"); } @Override public void createControl(Composite parent) { // 在生成UI之前,先设为未完成 // this.setPageComplete(false); Composite composite = new Composite(parent, SWT.NONE); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); composite.setLayout(new GridLayout(2, false)); Label label = new Label(composite, SWT.NONE); label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); label.setText("用户名:"); Text text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER); text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); text.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { // 把数据存在自己手里,对外提供get API username = ((Text) e.getSource()).getText(); // 把数据统一交给wizard里面的通用存储器DialogSettings来存值储 MyWizardPage1.this.getWizard().getDialogSettings().put("用户名", ((Text) e.getSource()).getText()); // 因为模型改变了,所以要及时更改界面 MyWizardPage1.this.getContainer().updateButtons(); } }); label = new Label(composite, SWT.NONE); label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); label.setText("密 码:"); text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER); text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); text.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { password = ((Text) e.getSource()).getText(); MyWizardPage1.this.getWizard().getDialogSettings().put("密 码", ((Text) e.getSource()).getText()); // 因为模型改变了,所以要及时更改界面 MyWizardPage1.this.getContainer().updateButtons(); } }); this.setTitle("导入TOS工程"); this.setMessage("请输入用户名和密码,进行工程导入操作"); this.setControl(composite); } @Override // 重写这个方法,不再使用以前的flag public boolean isPageComplete() { return username.length() > 0 && password.length() > 0; } } 

     

    //MyWizardPage2.java package org.wizard; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; public class MyWizardPage2 extends WizardPage { private String city = ""; private String address = ""; protected MyWizardPage2() { super("MyWizardPage2"); } @Override public void createControl(Composite parent) { // 在生成UI之前,先设为未完成 // this.setPageComplete(false); Composite composite = new Composite(parent, SWT.NONE); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); composite.setLayout(new GridLayout(2, false)); Label label = new Label(composite, SWT.NONE); label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); label.setText("城 市:"); Text text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER); text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); text.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { city = ((Text) e.getSource()).getText(); MyWizardPage2.this.getWizard().getDialogSettings().put("城 市", ((Text) e.getSource()).getText()); // 因为模型改变了,所以要及时更改界面 MyWizardPage2.this.getContainer().updateButtons(); } }); label = new Label(composite, SWT.NONE); label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); label.setText("地 址:"); text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER); text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); text.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { address = ((Text) e.getSource()).getText(); MyWizardPage2.this.getWizard().getDialogSettings().put("地 址", ((Text) e.getSource()).getText()); // 因为模型改变了,所以要及时更改界面 MyWizardPage2.this.getContainer().updateButtons(); } }); this.setTitle("导入TOS工程"); this.setMessage("请输入用户名和密码,进行工程导入操作"); this.setControl(composite); } @Override public boolean isPageComplete() { // TODO Auto-generated method stub return city.length() > 0 && address.length() > 0; } } 

    最新回复(0)