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

Save the selections in JFace Dialog (Java)

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

我的代码

按钮

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

谢谢

为此使用 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();
}