在Eclipse WizardPage中-在页面之间导航时出现的问题

时间:2018-08-24 11:56:11

标签: eclipse-plugin swt eclipse-rcp jface

我写了一个小程序,应该从一个页面导航到另一页面。但是我做不到。

我的要求是

在PageOne上,如果用户输入一个值,则应该对其进行验证,即在这种情况下的简单比较为“ 123”。 如果匹配,请转到下一页,如下所示:

第一页

enter image description here

第二页

enter image description here

在下面抛出对话框:

第一页

enter image description here

第一页代码:

    package testing.importWizards;

    import org.eclipse.jface.dialogs.MessageDialog;
    import org.eclipse.jface.wizard.IWizardPage;
    import org.eclipse.jface.wizard.WizardPage;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.KeyEvent;
    import org.eclipse.swt.events.KeyListener;
    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.Shell;
    import org.eclipse.swt.widgets.Text;

    public class PageOne extends WizardPage {
        private Text text1;
        private Composite container;
        boolean isVisible=false;

        public PageOne() {
            super("Page One");
            setTitle("Page One");
            setDescription("Fake Wizard: Page One");
        }

        @Override
        public void createControl(Composite parent) {
            container = new Composite(parent, SWT.NONE);
            GridLayout layout = new GridLayout();
            container.setLayout(layout);
            layout.numColumns = 2;
            Label label1 = new Label(container, SWT.NONE);
            label1.setText("Put a value here.");

            text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
            text1.setText("");
            text1.addKeyListener(new KeyListener() {

                @Override
                public void keyPressed(KeyEvent e) {
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    if (!text1.getText().isEmpty()) {
                        setPageComplete(true);
                    }
                }
            });
            GridData gd = new GridData(GridData.FILL_HORIZONTAL);
            text1.setLayoutData(gd);
            setControl(container);
            setPageComplete(false);
        }


        @Override
        public IWizardPage getNextPage() {
            // TODO Auto-generated method stub

            Shell shell=getShell();
            return super.getNextPage();
        }

        public String getText1() {
            return text1.getText();
        }
    }

第二页代码:

    package testing.importWizards;


    import org.eclipse.jface.wizard.WizardPage;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.KeyEvent;
    import org.eclipse.swt.events.KeyListener;
    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 PageTwo extends WizardPage {
        private Text text1;
        private Composite container;

        public PageTwo() {
            super("Page Two");
            setTitle("Page Two");
            setDescription("Fake Wizard: Page Two");
        }

        @Override
        public void createControl(Composite parent) {
            container = new Composite(parent, SWT.NONE);
            GridLayout layout = new GridLayout();
            container.setLayout(layout);
            layout.numColumns = 2;
            Label label1 = new Label(container, SWT.NONE);
            label1.setText("Success");

            /*text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
            text1.setText("");
            text1.addKeyListener(new KeyListener() {

                @Override
                public void keyPressed(KeyEvent e) {
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    if (!text1.getText().isEmpty()) {
                        setPageComplete(true);
                    }
                }
            });*/
            GridData gd = new GridData(GridData.FILL_HORIZONTAL);
            //text1.setLayoutData(gd);
            setControl(container);
            //setPageComplete(false);
            setPageComplete(true);
        }

        public String getText1() {
            return text1.getText();
        }
    }

我已经覆盖了setVisible和getNextPage方法,但是我却得到了错误的行为。有人可以解释一下实施验证的逻辑。我的整个方法是错误的吗?

1 个答案:

答案 0 :(得分:2)

向导页面通常不显示对话框,而是使用setErrorMessagesetError在向导顶部显示错误。

通常,每次页面上的数据更改时,进行验证都很方便。对于您的示例页面,类似以下内容:

public class PageOne extends WizardPage
{
  private Text text1;

  public PageOne()
  {
    super("testWizardPage");

    setTitle("Page One");
    setDescription("Fake Wizard: Page One");
  }

  @Override
  public void createControl(final Composite parent)
  {
    Composite container = new Composite(parent, SWT.NONE);
    container.setLayout(new GridLayout(2, false));

    Label label1 = new Label(container, SWT.NONE);
    label1.setText("Put a value here.");

    text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
    text1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    text1.addModifyListener(this::modifyListener);

    setControl(container);

    setPageComplete(false);
  }    

  private void modifyListener(final ModifyEvent event)
  {
    boolean empty = text1.getText().isEmpty();

    if (empty)
      setErrorMessage("Text must be entered");
    else
      setErrorMessage(null);

    setPageComplete(!empty);
  }
}

这是在text1字段上使用“修改监听器”,并在每次更改该字段时调用setErrorMessagesetPageComplete