Ron Santis
Ron Santis

Reputation: 45

In SWT Wizard Page how to write a event only when Next button is clicked

I am trying to implement an import wizard which have three different pages, How do I ensure that a piece of code for validation is only when the next button is clicked.

  1. As a first step is for selection in import wizard.
  2. On the second step, Server URL and other information necessary to establish the connections are to be keyed in.
  3. Once the details are keyed in and Next button is pressed it a series of actions take place

    • Check if the connection details are valid.
    • If the connection is valid then import the project details. if the connection details are wrong then show error dialogue.
    • Proceed to the next page and display the project contents.

I have been using

    public IWizardPage getNextPage(IWizardPage page) { 

to get to the next page, but on using this I can see the call is being made multiple times to the next page.

To Implement the validation i.e the Next button is disabled until the user enters all the necessary details until then the next button is disabled. As they are entering validations getNextPage(IWizardPage page) method gets called it keeps throwing error dialogue.

How do I ensure that nextPage is called only when the button is clicked.

code Snippet:

Code for checking validation: and if yes it's enabling next button.

    textIFServiceURL.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            keyReleasedImpl();                
        }           
    });

    textIFServiceURL.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent arg0) {
             modifyTextImpl();
        }           
    });

    txtUserName.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            keyReleasedImpl();  
        }
    });

    txtUserName.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent arg0) {
            modifyTextImpl();
        }
    });


    txtPassword.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            keyReleasedImpl();  
        }
    });

    txtPassword.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent arg0) {
            modifyTextImpl();
        }
    });


}




private void modifyTextImpl() {
    // TODO Auto-generated method stub
    if ((txtUserName.getText().isEmpty() || textIFServiceURL.getText().isEmpty() || txtPassword.getText().isEmpty())) {
        consumerImportWizardPage.setPageComplete(false);
    }
}

private void keyReleasedImpl() {
    // TODO Auto-generated method stub
    if (!(txtUserName.getText().isEmpty() || textIFServiceURL.getText().isEmpty() || txtPassword.getText().isEmpty())) {                    
        consumerImportWizardPage.setPageComplete(true);
    }
}

Updated Answer:

    <?xml version="1.0" encoding="UTF-8"?>
    <?eclipse version="3.4"?>
    <plugin>

       <extension
             point="org.eclipse.ui.importWizards">
          <category
                name="Sample File Import"
                id="com.myplugin.importWizards.sampleCategory">
          </category>
          <wizard
                name="Import File"
                icon="icons/sample.gif"
                category="com.myplugin.importWizards.sampleCategory"
                class="com.myplugin.importWizards.ImportWizard"
                id="com.myplugin.importWizards.ImportWizard">
             <description>
                Import a file from the local file system into the workspace.
             </description>
          </wizard>
       </extension>

    </plugin>

ImportWizard

    package com.myplugin.importWizards;

    public class ImportWizard extends Wizard implements IImportWizard {

        ConsumerImportWizardPage mainPage;
        PageThree three;

        public ImportWizard() {
            super();
            mainPage = new ConsumerImportWizardPage();
            three = new PageThree();
        }

        @Override
        public String getWindowTitle() {
            return "Import Integration Project";
        }

        @Override
        public void addPages() {
            addPage(mainPage);
            addPage(three);
        }


        @Override
        public IWizardPage getNextPage(IWizardPage page) {
            // TODO Auto-generated method stub
            System.out.println("WE are in get NextPage");
            return super.getNextPage(page);
        }

        @Override
        public void init(IWorkbench arg0, IStructuredSelection arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean performFinish() {
            // TODO Auto-generated method stub
            return false;
        }


    }

ConsumerImportWizardPage

    package com.myplugin.importWizards;

    public class ConsumerImportWizardPage extends WizardPage  {

        private final static String  TITLE = "Import an existing Project";
        private ImportPropertiesView twsPropertiesView;

        public ConsumerImportWizardPage() {
            super(TITLE);
            setTitle(TITLE);

        }

        @Override
        public void createControl(Composite parent) {
            // TODO Auto-generated method stub

            Composite composite = new Composite(parent, SWT.NONE);
            GridLayout layout = new GridLayout(3, false);
            layout.horizontalSpacing = 5;
            layout.verticalSpacing = 15;
            composite.setLayout(layout);
            setControl(composite);
            setPropertiesView(composite);   
            setPageComplete(false);

        }

         private void setPropertiesView(Composite twsPropertiesGroup) {
                twsPropertiesView = new ImportPropertiesView(twsPropertiesGroup,this);        
            }

         @Override
        public void setPageComplete(boolean complete) {
            // TODO Auto-generated method stub
            super.setPageComplete(complete);
        }

         @Override
         public void setVisible(final boolean visible)
         {
           if (visible)
            {
             System.out.println("Cool we are in the ");
            }

           super.setVisible(visible);
         } 
    }

ImportPropertiesView

    package com.myplugin.importWizards;



    public class ImportPropertiesView {

        private Link configureWorkspaceSettingsLink;
        private Button projectSpecificSettingsButton;
        private ServerDetailsImportView serverDetailsView;
        private ConsumerImportWizardPage consumerImportWizardPage;

        public ImportPropertiesView(Composite parent,ConsumerImportWizardPage consumerImportWizardPage) {
            this.consumerImportWizardPage=consumerImportWizardPage;
            parent.setLayout(new GridLayout(2, false));
            addServerDetailsView(parent);

        }

        private void addServerDetailsView(Composite parent) {
            serverDetailsView = new ServerDetailsImportView(parent,consumerImportWizardPage);
            GridData gridData = new GridData();
            gridData.horizontalAlignment = GridData.FILL;
            gridData.grabExcessHorizontalSpace = true;
            gridData.horizontalSpan = 2;
        }


        /**
         * get the landscape url.
         *
         * @return
         */
        public String getComponentServiceURL() {
            return serverDetailsView.getComponentServiceURL();
        }



        public String getPassword() {
            return serverDetailsView.getPassword();
        }



        public String getUserName() {
            return serverDetailsView.getUserName();
        }

        public boolean getPageCompleteStatus() {
            return serverDetailsView.getPageCompleteStatus();
        }




        /**
         * set the given landscape url to {@link ServerDetailsView}
         *
         * @param componentServiceURL
         */
        public void setComponentServiceURL(String componentServiceURL) {
            serverDetailsView.setComponentServiceURL(componentServiceURL);
        }


        public void setPassword(String password) {
            serverDetailsView.setPassword(password);
        }


        public void setUserName(String userName) {
            serverDetailsView.setUserName(userName);
        }
    }

ServerDetailsImportView

    package com.myplugin.importWizards;


    public class ServerDetailsImportView {
        private final Label lblUserName;
        private final Text txtUserName;
        private final Label lblPassword;
        private final Text txtPassword;
        private final Group userCredentials;
        private boolean pageCompleteStatus=false;
        /** connection choice group */

        /** web connection group */
        private final Group webConnection;
        /** landscape url text box */
        private final Text textIFServiceURL;
        /** landscape url label */
        private final Label labelIFServiceURL;
        private ConsumerImportWizardPage consumerImportWizardPage;


        public ServerDetailsImportView(Composite parent, ConsumerImportWizardPage consumerImportWizardPage) {
            // connection choice group creation
            this.consumerImportWizardPage=consumerImportWizardPage;
            GridData gridData = new GridData();
            gridData.horizontalAlignment = GridData.FILL;
            gridData.grabExcessHorizontalSpace = true;
            gridData.horizontalSpan = 2;

            // web connection group creation
            webConnection = new Group(parent, SWT.SHADOW_IN);
            webConnection.setText("Connection Details");
            webConnection.setLayout(new GridLayout(2, false));
            webConnection.setLayoutData(gridData);

            gridData = new GridData();
            gridData.horizontalAlignment = GridData.FILL;
            gridData.grabExcessHorizontalSpace = true;

            labelIFServiceURL = new Label(webConnection, SWT.RIGHT);
            labelIFServiceURL.setText("Server IP : ");

            textIFServiceURL = new Text(webConnection, SWT.SINGLE | SWT.BORDER);
            textIFServiceURL.setLayoutData(gridData);

            gridData = new GridData();
            gridData.horizontalAlignment = GridData.FILL;
            gridData.grabExcessHorizontalSpace = true;
            gridData.horizontalSpan = 2;

            userCredentials = new Group(parent, SWT.SHADOW_IN);
            userCredentials.setText("User Credentials");
            userCredentials.setLayout(new GridLayout(2, false));
            userCredentials.setLayoutData(gridData);

            gridData = new GridData();
            gridData.horizontalAlignment = GridData.FILL;
            gridData.grabExcessHorizontalSpace = true;

            lblUserName = new Label(userCredentials, SWT.LEFT);
            lblUserName.setText("User Name:");

            txtUserName = new Text(userCredentials, SWT.SINGLE | SWT.BORDER);
            txtUserName.setLayoutData(gridData);

            lblPassword = new Label(userCredentials, SWT.LEFT);
            lblPassword.setText("Password:");

            txtPassword = new Text(userCredentials, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
            txtPassword.setEchoChar('*');
            txtPassword.setLayoutData(gridData);       


            textIFServiceURL.addKeyListener(new KeyListener() {

                @Override
                public void keyPressed(KeyEvent e) {
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    keyReleasedImpl();                
                }           
            });

            textIFServiceURL.addModifyListener(new ModifyListener() {

                @Override
                public void modifyText(ModifyEvent arg0) {
                     modifyTextImpl();
                }           
            });

            txtUserName.addKeyListener(new KeyListener() {

                @Override
                public void keyPressed(KeyEvent e) {
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    keyReleasedImpl();  
                }
            });

            txtUserName.addModifyListener(new ModifyListener() {

                @Override
                public void modifyText(ModifyEvent arg0) {
                    modifyTextImpl();
                }
            });


            txtPassword.addKeyListener(new KeyListener() {

                @Override
                public void keyPressed(KeyEvent e) {
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    keyReleasedImpl();  
                }
            });

            txtPassword.addModifyListener(new ModifyListener() {

                @Override
                public void modifyText(ModifyEvent arg0) {
                    modifyTextImpl();
                }
            });


        }




        private void modifyTextImpl() {
            // TODO Auto-generated method stub
            if ((txtUserName.getText().isEmpty() || textIFServiceURL.getText().isEmpty() || txtPassword.getText().isEmpty())) {
                consumerImportWizardPage.setPageComplete(false);
            }
        }

        private void keyReleasedImpl() {
            // TODO Auto-generated method stub
            if (!(txtUserName.getText().isEmpty() || textIFServiceURL.getText().isEmpty() || txtPassword.getText().isEmpty())) {                    
                consumerImportWizardPage.setPageComplete(true);
            }
        }


        /**
         * get the entered value from {@link #textIFServiceURL}
         *
         * @return
         */
        public String getComponentServiceURL() {
            return textIFServiceURL.getText();
        }

        public String getPassword() {
            return txtPassword.getText();
        }

        public boolean getPageCompleteStatus() {
            return this.pageCompleteStatus;
        }


        public String getUserName() {
            return txtUserName.getText();
        }



        /**
         * set the given value to the text box {@link #textIFServiceURL}
         *
         * @param componentServiceURL
         */
        public void setComponentServiceURL(String componentServiceURL) {
            textIFServiceURL.setText(componentServiceURL);
        }



        public void setPassword(String password) {
            txtPassword.setText(password);
        }


        public void setUserName(String userName) {
            txtUserName.setText(userName);
        }


    }

Upvotes: 0

Views: 934

Answers (1)

greg-449
greg-449

Reputation: 111142

Normally getNextPage should only return the next page as it may be called multiple times by the wizard updateButtons method when setPageComplete is called.

You can stop the multiple calls happening by overriding the wizard page canFlipToNextPage method:

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

Upvotes: 1

Related Questions