保存JFace对话框(Java)中的选择

时间:2015-02-27 09:16:50

标签: java eclipse-plugin dialog eclipse-rcp

我有一个包含多个复选框的对话框。当用户单击按钮时,对话框将打开,默认情况下会选中所有复选框。用户可以取消选中一些复选框。用户第二次单击按钮我想要使用用户在第一次单击时执行的先前设置加载对话框。有没有什么方法可以加载对话框的最后输入值而不是加载对话框的默认设置?

我的代码

按钮

Button btnSelectChapters = new Button(composite, SWT.NONE);
    btnSelectChapters.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            SelectChaptersDialog chaptersdialog = new SelectChaptersDialog(getShell());
            int result = chaptersdialog.open();
            if (result == 1)
                return;
            else{
                selectedChapters = chaptersdialog.getSelectedChapters();
            }
        }
    });
    btnSelectChapters.setBounds(150, 0, 90, 25);
    btnSelectChapters.setText("Select Chapters");

对话

  @Override
protected Control createDialogArea(Composite parent) {
    setTitleImage(ResourceManager.getPluginImage("Featuremodel.aspecgeneration", "icons/pdf.png"));
    setTitle("Chapters Selection for Document Generation");
    setMessage("Select chapters you want to include in the document");
    Composite area = (Composite) super.createDialogArea(parent);
    area.setLayout(new GridLayout(1, false));

    Composite composite_2 = new Composite(area, SWT.NONE);
    composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    composite_2.setLayout(new GridLayout(1, false));

    Composite compositeChapters = new Composite(composite_2, SWT.NONE);
    compositeChapters.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
    compositeChapters.setLayout(new GridLayout(3, false));

    Group grpChapters = new Group(compositeChapters, SWT.NONE);
    grpChapters.setLayout(new GridLayout(2, false));
    grpChapters.setText("Chapters");
    grpChapters.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

    btnPandA = new Button(grpChapters, SWT.CHECK);
    btnPandA.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnPandA.setText("Preface/Appendix Chapters");
    btnPandA.setSelection(prefs.get("PA"));
    new Label(compositeChapters, SWT.NONE);
    btnPandA.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if(btnPandA.getSelection()){
                btnPandAR.setEnabled(true);     
            }
            else{
                btnPandAR.setSelection(false);
                btnPandAR.setEnabled(false);        
            }
        }
        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });

    btnPandAR = new Button(grpChapters, SWT.CHECK);
    btnPandAR.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnPandAR.setText("Recursive Preface/Appendix Chapters");
    btnPandAR.setEnabled(false);
    btnPandAR.setSelection(true);
    new Label(compositeChapters, SWT.NONE);

    btnCD = new Button(grpChapters, SWT.CHECK);
    btnCD.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnCD.setText("Component Description Chapter");
    btnCD.setSelection(true);
    btnCD.setEnabled(false);
    new Label(compositeChapters, SWT.NONE);

    btnCV = new Button(grpChapters, SWT.CHECK);
    btnCV.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnCV.setText("Context View Chapter");
    btnCV.setSelection(true);
    btnCV.setEnabled(false);
    new Label(compositeChapters, SWT.NONE);

    btnAV = new Button(grpChapters, SWT.CHECK);
    btnAV.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnAV.setText("Architecture View Chapter");
    btnAV.setSelection(true);
    btnAV.setEnabled(false);
    new Label(compositeChapters, SWT.NONE);

    btnIF = new Button(grpChapters, SWT.CHECK);
    btnIF.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnIF.setText("Interface Chapter");
    btnIF.setSelection(true);
    btnIF.setEnabled(false);
    new Label(compositeChapters, SWT.NONE);

    btnDV = new Button(grpChapters, SWT.CHECK);
    btnDV.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnDV.setText("Deployment View Chapter");
    btnDV.setSelection(true);
    new Label(compositeChapters, SWT.NONE);

    btnDNV = new Button(grpChapters, SWT.CHECK);
    btnDNV.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnDNV.setText("Dynamic View Chapter");
    btnDNV.setSelection(true);
    new Label(compositeChapters, SWT.NONE);

    btnFandG = new Button(grpChapters, SWT.CHECK);
    btnFandG.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnFandG.setText("Framework And Guidelines Chapter");
    btnFandG.setSelection(true);
    new Label(compositeChapters, SWT.NONE);

    btnPandD = new Button(grpChapters, SWT.CHECK);
    btnPandD.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnPandD.setText("Patterns And Definitions Chapter");
    btnPandD.setSelection(true);
    new Label(compositeChapters, SWT.NONE);

    btnSubSys = new Button(grpChapters, SWT.CHECK);
    btnSubSys.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    btnSubSys.setText("Subsystems Chapter");
    btnSubSys.setSelection(true);
    new Label(compositeChapters, SWT.NONE);

    return area;
}

由于

1 个答案:

答案 0 :(得分:1)

使用IDialogSettings

IDialogSettings settings = Activator.getDefault().getDialogSettings();

// Access an array of values stored in the settings

String [] valuesArray = settings.getArray("key for value");

// Save array of strings in the settings
settings.put("key for value", valuesArray);

Activator是您的插件激活器。 IDialogSettings还有访问单个字符串,整数,长整数,浮点数和双精度数的方法。如果需要,您还可以添加子部分。

因此,例如,当您创建一个复选框按钮时,您可以使用保存的设置:

IDialogSettings settings = Activator.getDefault().getDialogSettings();

Button checkBox = new Button(parent SWT.CHECK);

checkBox.setSelection(settings.getBoolean("checkbox setting key"));

如果尚未保存设置,请注意默认值为false。如果您需要默认值为' true'你可以使用:

boolean selection = true;
if (settings.get("checkbox setting key") != null)
  selection = settings.getBoolean("checkbox setting key");

checkbox.setSelection(selection);

要保存您需要调用putBooolean方法的选择。一个地方是okPressed方法:

@Override
protected void okPressed()
{
  IDialogSettings settings = Activator.getDefault().getDialogSettings();

  settings.putBoolean("checkbox setting key", checkbox.getSelection());

  super.okPressed();
}