对话框设置

org.eclipse.jface.dialogs 包提供了实用程序类 DialogSettings,用来存储和检索输入的值。可以使用此类来保存和检索与键名相关联的基本数据类型和字符串值。设置是使用 XML 文件来装入和保存的。

AbstractUIPlugin 为存储在插件目录的 XML 文件中的插件宽幅对话框设置提供了支持。如果在插件目录中找不到对话框设置文件, 则将会创建空的 DialogSettings。如果关闭插件,添加到该插件中的任何设置都将保存在 XML 文件中, 下一次启动该插件时,就会检索这些设置。

可以在插件代码中的任何位置访问对话框设置。下列代码片段说明您可以如何获取自述文件工具的对话框设置。

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

值是使用 Get 和 Put 方法来存储和检索的。Get 方法是在正访问的基本类型之后命名的。可以存储和检索布尔、长型、双精度、浮点、整型、数组和字符串值。以下代码片段说明可以如何使用对话框设置来初始化对话框中的控件值。

   protected Control createDialogArea(Composite parent) {
      IDialogSettings settings = ReadmePlugin.getDefault().getDialogSettings();
      checkbox = new Button(parent,SWT.CHECK);
      checkbox.setText("Generate sample section titles");
      // initialize the checkbox according to the dialog settings
      checkbox.setSelection(settings.getBoolean("GenSections"));
   }

按下“确定”按钮时,稍后就可以存储设置值。

   protected void okPressed() {
      IDialogSettings settings = ReadmePlugin.getDefault().getDialogSettings();
      // store the value of the generate sections checkbox
      settings.put("GenSections", checkbox.getSelection());
      super.okPressed();
   }

Copyright IBM Corporation and others 2000, 2003.