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

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.

[java]  view plain copy
  1. MyWizardPage1.this.getContainer().updateButtons();  
 


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

[java]  view plain copy
  1. Composite composite = (Composite)parent;  
  2. ((Wizard) newWizard).setNeedsProgressMonitor(true);  
  3. address = ((Text) e.getSource()).getText();  
 


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

[java]  view plain copy
  1. ((Wizard) newWizard).setNeedsProgressMonitor(true);  
  2. getContainer().run(truetruenew LongRunningOperation(false));  
 

 

[c-sharp]  view plain copy
  1. //App.java  
  2. package org.wizard;  
  3. import org.eclipse.jface.window.ApplicationWindow;  
  4. import org.eclipse.jface.wizard.Wizard;  
  5. import org.eclipse.jface.wizard.WizardDialog;  
  6. import org.eclipse.swt.SWT;  
  7. import org.eclipse.swt.events.SelectionEvent;  
  8. import org.eclipse.swt.events.SelectionListener;  
  9. import org.eclipse.swt.layout.GridData;  
  10. import org.eclipse.swt.layout.GridLayout;  
  11. import org.eclipse.swt.widgets.Button;  
  12. import org.eclipse.swt.widgets.Composite;  
  13. import org.eclipse.swt.widgets.Control;  
  14. import org.eclipse.swt.widgets.Display;  
  15. import org.eclipse.swt.widgets.Shell;  
  16. public class App extends ApplicationWindow {  
  17.     @Override  
  18.     protected void configureShell(Shell shell) {  
  19.         super.configureShell(shell);  
  20.         shell.setText("应用程序");  
  21.         shell.setSize(400, 300);  
  22.     }  
  23.     @Override  
  24.     protected Control createContents(Composite parent) {  
  25.         Composite contents = (Composite) super.createContents(parent);  
  26.         contents.setLayout(new GridLayout(1, false));  
  27.         Button button = new Button(contents, SWT.PUSH);  
  28.         button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,  
  29.                 false));  
  30.         button.setText("click");  
  31.         button.addSelectionListener(new SelectionListener() {  
  32.             @Override  
  33.             public void widgetDefaultSelected(SelectionEvent e) {  
  34.             }  
  35.             @Override  
  36.             public void widgetSelected(SelectionEvent e) {  
  37.                 Wizard myWizard = new MyWizard();  
  38.                 WizardDialog wdialog = new MyWizardDialog(App.this.getShell(),  
  39.                         myWizard);  
  40.                 myWizard.setWindowTitle("导入工程2");  
  41.                 wdialog.open();  
  42.             }  
  43.         });  
  44.         return contents;  
  45.     }  
  46.     public App() {  
  47.         super(null);  
  48.     }  
  49.     public void run() {  
  50.         this.setBlockOnOpen(true);  
  51.         this.open();  
  52.         // 注意,这里调用dispose很重要,因为open中没有进行dispose操作。getCurrent是OK的。  
  53.         Display.getCurrent().dispose();  
  54.     }  
  55.     public static void main(String[] args) {  
  56.         new App().run();  
  57.     }  
  58. }  
 

 

[java]  view plain copy
  1. //MyWizardDialog.java  
  2. package org.wizard;  
  3. import org.eclipse.jface.wizard.IWizard;  
  4. import org.eclipse.jface.wizard.Wizard;  
  5. import org.eclipse.jface.wizard.WizardDialog;  
  6. import org.eclipse.swt.widgets.Shell;  
  7. public class MyWizardDialog extends WizardDialog {  
  8.     @Override  
  9.     protected void configureShell(Shell newShell) {  
  10.         super.configureShell(newShell);  
  11.         // 这个windowTitle会被myWizard.setWindowTitle("导入工程2")覆盖  
  12.         newShell.setText("导入工程1");  
  13.         newShell.setSize(400300);  
  14.         newShell.setMinimumSize(300270);  
  15.     }  
  16.     public MyWizardDialog(Shell parentShell, IWizard newWizard) {  
  17.         super(parentShell, newWizard);  
  18.         ((Wizard) newWizard).setNeedsProgressMonitor(true);  
  19.     }  
  20. }  
 

 

[java]  view plain copy
  1. //MyWizard.java  
  2. package org.wizard;  
  3. import java.lang.reflect.InvocationTargetException;  
  4. import org.eclipse.core.runtime.IProgressMonitor;  
  5. import org.eclipse.jface.dialogs.DialogSettings;  
  6. import org.eclipse.jface.dialogs.IDialogSettings;  
  7. import org.eclipse.jface.dialogs.MessageDialog;  
  8. import org.eclipse.jface.operation.IRunnableWithProgress;  
  9. import org.eclipse.jface.wizard.Wizard;  
  10. public class MyWizard extends Wizard {  
  11.     public MyWizard() {  
  12.         this.setDialogSettings(new DialogSettings("导入工程"));  
  13.     }  
  14.     @Override  
  15.     public void addPages() {  
  16.         this.addPage(new MyWizardPage1());  
  17.         this.addPage(new MyWizardPage2());  
  18.     }  
  19.     @Override  
  20.     public boolean performFinish() {  
  21.         IDialogSettings dialogSettings2 = this.getDialogSettings();  
  22.         final String username = dialogSettings2.get("用户名");  
  23.         String password = dialogSettings2.get("密  码");  
  24.         String city = dialogSettings2.get("城  市");  
  25.         String address = dialogSettings2.get("地  址");  
  26.         IRunnableWithProgress op = new IRunnableWithProgress() {  
  27.             public void run(IProgressMonitor monitor)  
  28.                     throws InvocationTargetException {  
  29.                 try {  
  30.                     MessageDialog.openConfirm(MyWizard.this.getShell(),  
  31.                             "请确认以下信息""username=" + username);  
  32.                 } catch (Exception e) {  
  33.                     throw new InvocationTargetException(e);  
  34.                 } finally {  
  35.                     monitor.done();  
  36.                 }  
  37.             }  
  38.         };  
  39.         try {  
  40.             getContainer().run(truetruenew LongRunningOperation(false));  
  41.         } catch (InterruptedException e) {  
  42.             return false;  
  43.         } catch (InvocationTargetException e) {  
  44.             Throwable realException = e.getTargetException();  
  45.             MessageDialog.openError(getShell(), "Error", realException  
  46.                     .getMessage());  
  47.             return false;  
  48.         }  
  49.         return true;  
  50.     }  
  51. }  
  52.   
  53. class LongRunningOperation implements IRunnableWithProgress {  
  54.     // The total sleep time  
  55.     private static final int TOTAL_TIME = 10000;  
  56.     // The increment sleep time  
  57.     private static final int INCREMENT = 500;  
  58.     private boolean indeterminate;  
  59.      
  60.     public LongRunningOperation(boolean indeterminate) {  
  61.         this.indeterminate = indeterminate;  
  62.     }  
  63.      
  64.     public void run(IProgressMonitor monitor) throws InvocationTargetException,  
  65.             InterruptedException {  
  66.         monitor.beginTask("Running long running operation",  
  67.                 indeterminate ? IProgressMonitor.UNKNOWN : TOTAL_TIME);  
  68.         // monitor.subTask("Doing first half");  
  69.         for (int total = 0; total < TOTAL_TIME && !monitor.isCanceled(); total += INCREMENT) {  
  70.             Thread.sleep(INCREMENT);  
  71.             monitor.worked(INCREMENT);  
  72.             if (total == TOTAL_TIME / 2)  
  73.                 monitor.subTask("Doing second half");  
  74.         }  
  75.         monitor.done();  
  76.         if (monitor.isCanceled())  
  77.             throw new InterruptedException(  
  78.                     "The long running operation was cancelled");  
  79.     }  
  80. }  
 

 

[java]  view plain copy
  1. //MyWizardPage1.java  
  2. package org.wizard;  
  3. import org.eclipse.jface.wizard.WizardPage;  
  4. import org.eclipse.swt.SWT;  
  5. import org.eclipse.swt.events.ModifyEvent;  
  6. import org.eclipse.swt.events.ModifyListener;  
  7. import org.eclipse.swt.layout.GridData;  
  8. import org.eclipse.swt.layout.GridLayout;  
  9. import org.eclipse.swt.widgets.Composite;  
  10. import org.eclipse.swt.widgets.Label;  
  11. import org.eclipse.swt.widgets.Text;  
  12. public class MyWizardPage1 extends WizardPage {  
  13.     private String username = "";  
  14.     private String password = "";  
  15.     protected MyWizardPage1() {  
  16.         super("MyWizardPage1");  
  17.     }  
  18.     @Override  
  19.     public void createControl(Composite parent) {  
  20.         // 在生成UI之前,先设为未完成  
  21.         // this.setPageComplete(false);  
  22.         Composite composite = new Composite(parent, SWT.NONE);  
  23.         composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, truetrue));  
  24.         composite.setLayout(new GridLayout(2false));  
  25.         Label label = new Label(composite, SWT.NONE);  
  26.         label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,  
  27.                 false));  
  28.         label.setText("用户名:");  
  29.         Text text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);  
  30.         text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, truefalse));  
  31.         text.addModifyListener(new ModifyListener() {  
  32.             @Override  
  33.             public void modifyText(ModifyEvent e) {  
  34.                 // 把数据存在自己手里,对外提供get API  
  35.                 username = ((Text) e.getSource()).getText();  
  36.                 // 把数据统一交给wizard里面的通用存储器DialogSettings来存值储  
  37.                 MyWizardPage1.this.getWizard().getDialogSettings().put("用户名",  
  38.                         ((Text) e.getSource()).getText());  
  39.                 // 因为模型改变了,所以要及时更改界面  
  40.                 MyWizardPage1.this.getContainer().updateButtons();  
  41.             }  
  42.         });  
  43.         label = new Label(composite, SWT.NONE);  
  44.         label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,  
  45.                 false));  
  46.         label.setText("密  码:");  
  47.         text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);  
  48.         text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, truefalse));  
  49.         text.addModifyListener(new ModifyListener() {  
  50.             @Override  
  51.             public void modifyText(ModifyEvent e) {  
  52.                 password = ((Text) e.getSource()).getText();  
  53.                 MyWizardPage1.this.getWizard().getDialogSettings().put("密  码",  
  54.                         ((Text) e.getSource()).getText());  
  55.                 // 因为模型改变了,所以要及时更改界面  
  56.                 MyWizardPage1.this.getContainer().updateButtons();  
  57.             }  
  58.         });  
  59.         this.setTitle("导入TOS工程");  
  60.         this.setMessage("请输入用户名和密码,进行工程导入操作");  
  61.         this.setControl(composite);  
  62.     }  
  63.     @Override  
  64.     // 重写这个方法,不再使用以前的flag  
  65.     public boolean isPageComplete() {  
  66.         return username.length() > 0 && password.length() > 0;  
  67.     }  
  68. }  
 

 

[java]  view plain copy
  1. //MyWizardPage2.java  
  2. package org.wizard;  
  3. import org.eclipse.jface.wizard.WizardPage;  
  4. import org.eclipse.swt.SWT;  
  5. import org.eclipse.swt.events.ModifyEvent;  
  6. import org.eclipse.swt.events.ModifyListener;  
  7. import org.eclipse.swt.layout.GridData;  
  8. import org.eclipse.swt.layout.GridLayout;  
  9. import org.eclipse.swt.widgets.Composite;  
  10. import org.eclipse.swt.widgets.Label;  
  11. import org.eclipse.swt.widgets.Text;  
  12. public class MyWizardPage2 extends WizardPage {  
  13.     private String city = "";  
  14.     private String address = "";  
  15.     protected MyWizardPage2() {  
  16.         super("MyWizardPage2");  
  17.     }  
  18.     @Override  
  19.     public void createControl(Composite parent) {  
  20.         // 在生成UI之前,先设为未完成  
  21.         // this.setPageComplete(false);  
  22.         Composite composite = new Composite(parent, SWT.NONE);  
  23.         composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, truetrue));  
  24.         composite.setLayout(new GridLayout(2false));  
  25.         Label label = new Label(composite, SWT.NONE);  
  26.         label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,  
  27.                 false));  
  28.         label.setText("城  市:");  
  29.         Text text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);  
  30.         text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, truefalse));  
  31.         text.addModifyListener(new ModifyListener() {  
  32.             @Override  
  33.             public void modifyText(ModifyEvent e) {  
  34.                 city = ((Text) e.getSource()).getText();  
  35.                 MyWizardPage2.this.getWizard().getDialogSettings().put("城  市",  
  36.                         ((Text) e.getSource()).getText());  
  37.                 // 因为模型改变了,所以要及时更改界面  
  38.                 MyWizardPage2.this.getContainer().updateButtons();  
  39.             }  
  40.         });  
  41.         label = new Label(composite, SWT.NONE);  
  42.         label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,  
  43.                 false));  
  44.         label.setText("地  址:");  
  45.         text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);  
  46.         text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, truefalse));  
  47.         text.addModifyListener(new ModifyListener() {  
  48.             @Override  
  49.             public void modifyText(ModifyEvent e) {  
  50.                 address = ((Text) e.getSource()).getText();  
  51.                 MyWizardPage2.this.getWizard().getDialogSettings().put("地  址",  
  52.                         ((Text) e.getSource()).getText());  
  53.                 // 因为模型改变了,所以要及时更改界面  
  54.                 MyWizardPage2.this.getContainer().updateButtons();  
  55.             }  
  56.         });  
  57.         this.setTitle("导入TOS工程");  
  58.         this.setMessage("请输入用户名和密码,进行工程导入操作");  
  59.         this.setControl(composite);  
  60.     }  
  61.     @Override  
  62.     public boolean isPageComplete() {  
  63.         // TODO Auto-generated method stub  
  64.         return city.length() > 0 && address.length() > 0;  
  65.     }  
  66. }  
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值