Eclipse JFace's Wizards

前端 未结 4 1655
梦如初夏
梦如初夏 2020-12-15 23:52

I need a wizard which second page content depends on the first page\'s selection. The first page asks the user the \"kind\" of filter he wants to create and the second one a

相关标签:
4条回答
  • 2020-12-16 00:08

    If you want to start a new wizard based on your selection on the first page, you can use the JFace base class org.eclipse.jface.wizard.WizardSelectionPage.

    The example below shows a list of available wizards defined by an extension point. When you press Next, the selected wizard is started.

    public class ModelSetupWizardSelectionPage extends WizardSelectionPage {
    
    private ComboViewer providerViewer;
    private IConfigurationElement selectedProvider;
    
    public ModelSetupWizardSelectionPage(String pageName) {
        super(pageName);
    }
    
    private class WizardNode implements IWizardNode {
        private IWizard wizard = null;
        private IConfigurationElement configurationElement;
    
        public WizardNode(IConfigurationElement c) {
            this.configurationElement = c;
        }
    
        @Override
        public void dispose() {
    
        }
    
        @Override
        public Point getExtent() {
            return new Point(-1, -1);
        }
    
        @Override
        public IWizard getWizard() {
            if (wizard == null) {
                try {
                    wizard = (IWizard) configurationElement
                            .createExecutableExtension("wizardClass");
                } catch (CoreException e) {
    
                }
            }
            return wizard;
        }
    
        @Override
        public boolean isContentCreated() {
            // TODO Auto-generated method stub
            return wizard != null;
        }
    
    }
    
    @Override
    public void createControl(Composite parent) {
        setTitle("Select model provider");
        Composite main = new Composite(parent, SWT.NONE);
        GridLayout gd = new GridLayout(2, false);
        main.setLayout(gd);
        new Label(main, SWT.NONE).setText("Model provider");
        Combo providerList = new Combo(main, SWT.NONE);
        providerViewer = new ComboViewer(providerList);
        providerViewer.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                if (element instanceof IConfigurationElement) {
                    IConfigurationElement c = (IConfigurationElement) element;
                    String result = c.getAttribute("name");
                    if (result == null || result.length() == 0) {
                        result = c.getAttribute("class");
                    }
                    return result;
                }
                return super.getText(element);
            }
    
        });
        providerViewer
                .addSelectionChangedListener(new ISelectionChangedListener() {
                    @Override
                    public void selectionChanged(SelectionChangedEvent event) {
                        ISelection selection = event.getSelection();
                        if (!selection.isEmpty()
                                && selection instanceof IStructuredSelection) {
                            Object o = ((IStructuredSelection) selection)
                                    .getFirstElement();
                            if (o instanceof IConfigurationElement) {
                                selectedProvider = (IConfigurationElement) o;
                                setMessage(selectedProvider.getAttribute("description"));
                                setSelectedNode(new WizardNode(selectedProvider));
                            }
                        }
    
                    }
                });
        providerViewer.setContentProvider(new ArrayContentProvider());
        List<IConfigurationElement> providers = new ArrayList<IConfigurationElement>();
        IExtensionRegistry registry = Platform.getExtensionRegistry();
        IExtensionPoint extensionPoint = registry
                .getExtensionPoint(<your extension point namespace>,<extension point name>);
        if (extensionPoint != null) {
            IExtension extensions[] = extensionPoint.getExtensions();
            for (IExtension extension : extensions) {
                IConfigurationElement configurationElements[] = extension
                        .getConfigurationElements();
                for (IConfigurationElement c : configurationElements) {
                    providers.add(c);
                }
            }
        }
        providerViewer.setInput(providers);
        setControl(main);
    
    }
    

    The corresponding wizard class looks like this:

     public class ModelSetupWizard extends Wizard {
    
    private ModelSetupWizardSelectionPage wizardSelectionPage;
    
    public ModelSetupWizard() {
        setForcePreviousAndNextButtons(true);
    }
    
    @Override
    public boolean performFinish() {
                // Do what you have to do to finish the wizard
        return true;
    }
    
    @Override
    public void addPages() {
        wizardSelectionPage = new ModelSetupWizardSelectionPage("Select a wizard");
        addPage(wizardSelectionPage);
    
    }
    }
    
    0 讨论(0)
  • 2020-12-16 00:11

    I have a different solution.

    If page depends on the result of page 1, create a variable and pass it into to first page, when that wizard page has the option from the user, then the last thing before the page is closed is to set the variable to the required value.

    Then pass this variable to wizard, then pass it to the next wizard page. Then do a simple if statement and that way you get both choices together.

    Remember that in most code there is only a small difference in the user options, so remember not to get bogged down in duplicating your code.

    0 讨论(0)
  • 2020-12-16 00:26

    The approach is right if you are several other pages which are

    • completely different one with another
    • depends on the previous choices made in a previous page

    Then you can add the next page dynamically (also as described here)

    But if you have just a next page with a dynamic content, you should be able to create that content in the onEnterPage() method

    public void createControl(Composite parent)
    {
        //
        // create the composite to hold the widgets
        //
        this.composite = new Composite(parent, SWT.NONE);
    
        //
        // create the desired layout for this wizard page
        //
        GridLayout layout = new GridLayout();
        layout.numColumns = 4;
        this.composite.setLayout(layout);
    
        // set the composite as the control for this page
        setControl(this.composite);
    }
    
    void onEnterPage()
    {
        final MacroModel model = ((MacroWizard) getWizard()).model;
        String selectedKey = model.selectedKey;
        String[] attrs = (String[]) model.macroMap.get(selectedKey);
    
        for (int i = 0; i < attrs.length; i++)
        {
            String attr = attrs[i];
            Label label = new Label(this.composite, SWT.NONE);
            label.setText(attr + ":");
    
            new Text(this.composite, SWT.NONE);
        }
        pack();
    }
    

    As shown in the eclipse corner article Creating JFace Wizards:

    We can change the order of the wizard pages by overwriting the getNextPage method of any wizard page.Before leaving the page, we save in the model the values chosen by the user. In our example, depending on the choice of travel the user will next see either the page with flights or the page for travelling by car.

    public IWizardPage getNextPage(){
       saveDataToModel();       
       if (planeButton.getSelection()) {
           PlanePage page = ((HolidayWizard)getWizard()).planePage;
         page.onEnterPage();
           return page;
       }
       // Returns the next page depending on the selected button
       if (carButton.getSelection()) { 
        return ((HolidayWizard)getWizard()).carPage;
       }
       return null;
    }
    

    We define a method to do this initialization for the PlanePage, onEnterPage() and we invoke this method when moving to the PlanePage, that is in the getNextPage() method for the first page.

    0 讨论(0)
  • 2020-12-16 00:27

    Another alternative is to @Override setVisible. You can update page values or add additional widgets at that time.

    0 讨论(0)
提交回复
热议问题