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

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

谢谢

最佳答案

为此使用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 还具有访问单个字符串、整数、长整型、浮点型和 double 型的方法。如果需要,您还可以添加子部分。

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

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

要保存选择,您需要调用 putBoolean 方法。执行此操作的一个地方是 okPressed 方法:

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

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

  super.okPressed();
}

关于java - 保存 JFace 对话框 (Java) 中的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28761331/

相关文章:

eclipse-plugin - 如何从 Eclipse 插件访问 JDT "static"图标?

带有按钮onClick事件的android自定义对话框

javascript - 如何使用 html/jQuery UI 正确显示弹出对话框

java - 一个 Activity 上的第二个按钮关闭 android 应用程序

Java:无法找到方法的符号,即使该方法是稍后在类中声明的。剩下的代码就是寻找一个类。

java - JRE 版本足以运行 jar

node.js - 使用 Electron Dialog 模块和 .showSaveDialog 时在另存为窗口中填充文件名

java - paint(Graphics g) 阻止组件被绘制

eclipse - 在服务中注册 ISourceProvider(Eclipse 插件开发,RCP)

eclipse - 无法删除 Eclipse 工具栏中的新元素