从 WizardsPage1 导航到 2 到下一个时,如果单击进度条取消按钮,如何强制用户保持在同一页面上?

When navigating from WizardsPage1 to 2 through next, how to force the user to remain on the same page if Progress Bar cancel button is clicked?

我有一个如下所示的向导页面-

点击下一步按钮后,进度条如下图-

如果进度栏 activity 被取消,我想让用户在单击下一步按钮的同一页面上。我怎样才能做到这一点?

我正在执行以下操作以显示 WizardPage2 的进度条

@Override
    public void setVisible(boolean visible) {
        if (visible) {
            InterfacePage interfacePage = (InterfacePage) (getWizard().getPage("NewInterfacePage"));
            String interfaceId = interfacePage.getInterfaceID();
            
            try {
                getContainer().run(true, true, new IRunnableWithProgress() {

                    @Override
                    public void run(IProgressMonitor arg0) throws InvocationTargetException, InterruptedException {
                        int amount = 10;
                        arg0.beginTask("Fetching details from External System...", 10);
                        for (int i = 0; i < amount; i++) {
                            arg0.internalWorked(1);
                            Thread.sleep(2000);
                            if (arg0.isCanceled()) {
                                cancelled = true;
                                
                                break;
                            }
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
        super.setVisible(visible);
    }

失败的示例代码-

主要精灵-

public class TemplateCreationWizard extends Wizard implements INewWizard, IExecutableExtension {

    private InterfacePage interfacePage;
    private InterfaceDetailsPage interfaceDetailsPage;
    private ErrorPage error;
    private IConfigurationElement _configurationElement;

    private SelectedServerNode iServer;

    public TemplateCreationWizard() {
        setWindowTitle("Auto Development");
    }

    @Override
    public void addPages() {
        super.addPages();

        if (iServer.isConnected()) {
            interfacePage = new InterfacePage(iServer);
            interfaceDetailsPage = new InterfaceDetailsPage(iServer);

            addPage(interfacePage);
            addPage(interfaceDetailsPage);

            setNeedsProgressMonitor(true);
        } else {
            error = new ErrorPage(iServer);
            addPage(error);
        }

    }

    @Override
    public boolean performFinish() {
        // will do something
        return true;
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {

        try {
            TreeSelection treeSelection = (TreeSelection) selection;

            TreePath[] treePaths = treeSelection.getPaths();
            if (null != treePaths && treePaths.length > 0) {
                TreePath treePath = treePaths[0];
                ISServerNode serverNode = (ISServerNode) treePath.getFirstSegment();
                ServerConnection iData = serverNode.getServerConnection();
                if (null != iData) {
                    iServer = createSelectedServerNode(iData);
                    // do domething
                }
            } else {
                // ... logic in case appropriate node is not selected
                System.out.println("<empty selection>");
            }
        } catch (Exception exception) {

        }

    }

    @Override
    public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
            throws CoreException {
        _configurationElement = config;

    }


    @Override
    public IWizardPage getNextPage(final IWizardPage page) {
        if (!(page instanceof InterfacePage))  //Next page from first page?
            return super.getNextPage(page);

         //Moving from first to second page, run the progress

        try {
            getContainer().run(true, true, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor arg0) throws InvocationTargetException, InterruptedException {
                    int amount = 10;
                    arg0.beginTask("Fetching details from External System...", 10);
                    for (int i = 0; i < amount; i++) {
                        arg0.internalWorked(1);
                        Thread.sleep(2000);
                        
                    }
                }
            });
        } catch (InvocationTargetException ex) {
             //TODO deal with error
        } catch (InterruptedException ex) {
             //Cancelled
            return null;  //Stay on same page
        }
        return super.getNextPage(page);
    }
}

向导页面-

第 1 页 -

public class InterfacePage extends WizardPage {
    private SelectedServerNode iServer = null;

    private class CompleteListener implements KeyListener {
        @Override
        public void keyPressed(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
            boolean hasInterfaceID = !"".equals(getTextFrom(interfaceId));
            if (!hasInterfaceID) {
                setMessage("Please enter an Interface ID", IMessageProvider.ERROR);
            }
            if (hasInterfaceID) {
                setMessage(null);
            }
            setPageComplete(hasInterfaceID);
        }
    }

    private Text interfaceId;
    protected InterfacePage(SelectedServerNode iServer) {
        super("InterfacePage");

        this.iServer = iServer;
        setTitle("Some title");
        setMessage("Some message");
    }

    @Override
    public void createControl(Composite parent) {
        Composite page = new Composite(parent, SWT.NONE);
        setControl(page);
        setPageComplete(false);

        // page.setLayout(new GridLayout(2, false));
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        gridLayout.makeColumnsEqualWidth = false;
        page.setLayout(gridLayout);

        page.setLayoutData(new GridData(GridData.FILL_BOTH));

        Label label = new Label(page, SWT.NONE);
        label.setText("Interface ID:");

        interfaceId = new Text(page, SWT.BORDER);
        GridData interfaceIdGridData = new GridData(GridData.FILL_HORIZONTAL);
        interfaceId.setLayoutData(interfaceIdGridData);

        CompleteListener listener = new CompleteListener();
        interfaceId.addKeyListener(listener);

    }
    

    @Override
    public boolean canFlipToNextPage()
    {
      // Default calls getNextPage() which runs the progress just checking page complete is enough here
      return isPageComplete();
    }
    
    
    private String getTextFrom(Text text) {
        return text == null || text.isDisposed() ? null : text.getText();
    }

    public String getInterfaceID() {
        return getTextFrom(interfaceId);
    }

}

第2页-

public class InterfaceDetailsPage extends WizardPage {
    
    private SelectedServerNode iServer;
    
    protected InterfaceDetailsPage(SelectedServerNode iServer) {
        super("InterfaceDetailsPage");
        
        this.iServer = iServer;
        setTitle("Interface ID details");
        setMessage("Provided Interface ID details is shown below");
    }
    @Override
    public void createControl(Composite parent) {
        
            Composite page = new Composite(parent, SWT.NONE);
            setControl(page);
            page.setLayout(new FillLayout());
        
    }
    
    @Override
    public IWizard getWizard() {
        IWizard wizard = super.getWizard();
        return wizard;
    }
    @Override
    public void setVisible(boolean visible) {
        
        if (visible) {
            InterfacePage interfacePage = (InterfacePage) (getWizard().getPage("InterfacePage"));
            String interfaceId = interfacePage.getInterfaceID();
            // do something...
        }
        super.setVisible(visible);
    }
    
}

在页面之间制作进度条的最佳方法是覆盖 Wizard class 的 getNextPage 方法。 getNextPage可以returnnull停止变化

@Override
public IWizardPage getNextPage(final IWizardPage page)
{
  if (!(page instanceof YourFirstPage))  // Next page from first page?
    return super.getNextPage(page);

  // Moving from first to second page, run the progress

  try
   {
     getContainer().run .....
   }
  catch (InvocationTargetException ex)
   {
     // TODO deal with error
   }
  catch (InterruptedException ex)
   {
     // Cancelled
     return null;  // Stay on same page
   }

  return super.getNextPage(page);
}

您还需要在第一个 WizardPage 中覆盖 canFlipToNextPage 作为默认调用 getNextPage:

@Override
public boolean canFlipToNextPage()
{
  // Default calls getNextPage() which runs the progress just checking page complete is enough here
  return isPageComplete();
}