문제
두 번째 페이지 콘텐츠가 첫 페이지의 선택에 따라 다르면 마법사가 필요합니다. 첫 번째 페이지는 사용자에게 작성하려는 필터의 "종류"를 묻고 두 번째 페이지는 사용자에게 선택한 "종류"의 필터 인스턴스 하나를 만들도록 요청합니다.
Jface의 Wizards Pages 내용 (CreateControl (...) 메소드)는 마법사가 열릴 때 생성됩니다.
이로 인해 마법사가 열리기 전에 두 번째 페이지 컨텐츠를 만들어야하지만 두 번째 페이지의 내용이 첫 페이지 선택에 따라 달라질 수 없습니다.
현재 찾은 클리너 솔루션은 마법사가 열리기 전에 (초) 페이지를 작성하고 (콘텐츠와 함께) 첫 페이지 구현에서 getNextPage () 메소드를 무시합니다.
이 솔루션의 주요 단점은 두 번째 페이지가 많이있을 때 비용이 많이들 수 있다는 것입니다.
그 솔루션에 대해 어떻게 생각하십니까? 마법사 페이지를 어떻게 관리합니까? 내가 놓친 더 깨끗한 솔루션이 있습니까?
해결책
다른 여러 페이지라면 접근 방식이 옳습니다.
- 완전히 다른 것
- 이전 페이지의 이전 선택에 따라 다릅니다.
그럼 당신은 할 수 있습니다 다음 페이지를 동적으로 추가하십시오 (또한 여기에 설명되어 있습니다)
그러나 동적 콘텐츠가있는 다음 페이지 만 있으면 해당 콘텐츠를 onEnterPage()
방법
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 Corner 기사에서 볼 수 있듯이 Jface 마법사 생성:
마법사 페이지의 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;
}
우리 이 초기화를 수행하는 메소드를 정의하십시오 PlanePage
, onEnterPage()
그리고 우리는이 방법으로 이동할 때이 방법을 호출합니다 PlanePage
, 그것은 그 안에 있습니다 getNextPage()
첫 번째 페이지에 대한 방법.
다른 팁
첫 페이지에서 선택을 기반으로 새 마법사를 시작하려면 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);
}
}
또 다른 대안은 @override입니다 setvisible. 당시 페이지 값을 업데이트하거나 추가 위젯을 추가 할 수 있습니다.
다른 해결책이 있습니다.
페이지가 1 페이지의 결과에 의존하는 경우 변수를 작성하여 첫 페이지로 전달합니다. 마법사 페이지가 사용자로부터 옵션이있는 경우 페이지가 닫히기 전에 마지막으로 변수를 필요한 값으로 설정하는 것입니다. .
그런 다음이 변수를 마법사로 전달한 다음 다음 마법사 페이지로 전달하십시오. 그런 다음 간단한 if 문장을 수행하면 두 가지 방법을 함께 선택하십시오.
대부분의 코드에서는 사용자 옵션에는 작은 차이가 있으므로 코드를 복제하는 데 방해가되지 않음을 기억하십시오.