2009-06-05 48 views
13

我需要一個嚮導,第二頁內容取決於第一頁的選擇。第一頁向用戶詢問他想要創建的「種類」過濾器,第二個頁面要求用戶創建所選「種類」的過濾器實例。當嚮導打開時,JFace的嚮導頁面內容(createControl(...)方法)全部創建,而不是在顯示給定頁面時創建(這允許JFace知道嚮導大小,我猜?)。Eclipse JFace的奇才

因此,我必須在嚮導打開之前創建我的第二頁內容,但由於第二頁的內容取決於第一頁選擇,因此我無法創建。

現在,我發現的更清潔的解決方案包括在嚮導打開(包含其內容)之前創建所有(秒)頁面並覆蓋第一頁實現中的getNextPage()方法。

該解決方案的主要缺點是,當需要創建許多第二頁時,它可能會很昂貴。

您對該解決方案有何看法?你如何管理你的嚮導的頁面?我錯過了更清潔的解決方案嗎?

回答

9

方法是正確的,如果你是這是

  • 完全不同的另一種
  • 依賴於前一頁面

然後你可以add the next page dynamically之前所做的選擇,其他幾頁(也作爲described here

但是,如果你只有一個動態內容的下一頁,你應該是一個BLE創建於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(); 
} 

的內容作爲Eclipse的角落文章Creating JFace Wizards所示:

我們可以覆蓋任何嚮導page.Before的getNextPage方法離開更改向導頁面順序頁面,我們在模型中保存用戶選擇的值。在我們的示例中,根據旅行的選擇,用戶接下來會看到帶有航班的頁面或開車旅行的頁面。

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; 
} 

我們定義做這個初始化爲PlanePageonEnterPage()的方法,我們調用此方法移動到PlanePage的時候,也就是在第一頁的getNextPage()方法。

+0

這是一個非常好的模式,感謝您的偉大答案! – rooftop 2012-07-05 18:45:49

5

如果您想根據您在第一頁上的選擇啓動一個新嚮導,則可以使用JFace基類org.eclipse.jface.wizard.WizardSelectionPage

下面的示例顯示了由擴展點定義的可用嚮導列表。 當您按下一步時,將啓動所選嚮導。

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); 

} 

相應的嚮導類是這樣的:

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

我重新格式化了您的代碼,如果您不喜歡它 - 請回滾。 – MByD 2011-04-21 12:47:14

0

我有不同的解決方案。

如果頁面取決於頁面1的結果,則創建一個變量並將其傳遞到第一頁,當該嚮導頁面具有用戶選項時,頁面關閉之前的最後一件事是設置變量達到所需的值。

然後將此變量傳遞給嚮導,然後將其傳遞給下一個嚮導頁面。然後做一個簡單的if語句,這樣你就可以將兩種選擇結合在一起。

請記住,在大多數代碼中,用戶選項只有很小的差異,所以請記住不要在複製代碼時陷入困境。

1

另一種選擇是@Override setVisible。您可以在此時更新頁面值或添加其他小部件。