zoukankan      html  css  js  c++  java
  • 关于Eclipse插件开发-----加入首选项(preferencePages)

    选择主菜单"窗口---->首选项"命令打开"首选项"窗口.此窗口是Eclipse设置项的集中营,

    修改plugin.xml文件,设置首选项的扩展点:

    plug.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <?eclipse version="3.4"?>
    <plugin>
     <extension point="org.eclipse.ui.perspectives">
           <perspective
            name="myplugin 透视图"
            icon="icons/selectall.gif"
            class="cn.com.kxh.myplugin.SamplePerspective"
            id="cn.com.kxh.myplugin.SamplePerspective">
        </perspective>
     </extension>
     <extension point="org.eclipse.ui.views">
         <view
                 name="视图1"
                 icon="icons/prev.gif"
                 category="com.glkxh.myplugin.view"
                 class="cn.com.kxh.myplugin.View1"
                 id="cn.com.kxh.myplugin.View1">
         </view>
         <view
                 name="视图2"
                 icon="icons/project.gif"
                 category="com.glkxh.myplugin.view"
                 class="cn.com.kxh.myplugin.View2"
                 id="cn.com.kxh.myplugin.View2">
         </view>
     </extension>
     <extension point="org.eclipse.ui.editors">
      <editor 
            name="中国Editor"
            icon="icons/project.gif"
            class="cn.com.kxh.myplugin.ChinaEditor"
            id="cn.com.kxh.myplugin.ChinaEditor">
      </editor>
      <editor 
            name="美国Editor"
            icon="icons/prev.gif"
            class="cn.com.kxh.myplugin.UsaEditor"
            id="cn.com.kxh.myplugin.UsaEditor">
      </editor>
      <editor 
            name="法国Editor"
            icon="icons/remove.gif"
            class="cn.com.kxh.myplugin.FranceEditor"
            id="cn.com.kxh.myplugin.FranceEditor">
       </editor>
      </extension>
      <extension point="org.eclipse.ui.preferencePages">
          <page
              name="myplugin插件设置"
              class="cn.com.kxh.myplugin.RootPreferencePage"
              id="cn.com.kxh.myplugin.RootPreferencePage">
          </page>
          <page
              name="DB数据库"
              category="cn.com.kxh.myplugin.RootPreferencePage"
              class="cn.com.kxh.myplugin.DBPreferencePage"
              id="cn.com.kxh.myplugin.DBPreferencePage">
          </page>      
      </extension>
    </plugin>

    代码说明:

    1.org.eclipse.ui.preferencePages 是首选项(Preference)的扩展点

    2.name是首选项的树节点显示的名称.

    3.class是首选项的树节点所对应的类(还没编写,下一步将完成此类)

    4.id是首选项的树节点标识.建议设置成和class一样的名称.

    5.category是父节点的id标识,当然,父节点要存在才行.

    建立首选项对应的类

    在上面的plugin.xml文件中已经定义的两个类.

    cn.com.kxh.myplugin.RootPreferencePage和cn.com.kxh.myplugin.DBPreferencePage

    首选项的类必须继承PreferencePage抽象类并实现IWorkbenchPreferencepage接口.该接口只有一个init方法,抽象类中则有一些"首选项"窗口固有按钮的处理方法需要被实现.

    RootPreferencePage.java

     1 public class RootPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
     2 
     3     public void init(IWorkbench workbench) {}
     4 
     5     protected Control createContents(Composite parent) {
     6         Composite topComp = new Composite(parent, SWT.NONE);
     7         topComp.setLayout(new RowLayout());
     8         new Label(topComp, SWT.NONE).setText("欢迎使用myplugin插件");
     9         return topComp;
    10     }
    11 }

    DBPreferencePage.java

      1 public class DBPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, ModifyListener {
      2     // 为文本框定义三个键值
      3     public static final String URL_KEY = "$URL_KEY";
      4     public static final String USERNAME_KEY = "$USERNAME_KEY";
      5     public static final String PASSWORD_KEY = "$PASSWORD_KEY";
      6     // 为文本框值定义三个默认值
      7     public static final String URL_DEFAULT = "jdbc:db2://127.0.0.1/mydb";
      8     public static final String USERNAME_DEFAULT = "kongxiaohan";
      9     public static final String PASSWORD_DEFAULT = "kxhkxhkxhkxh";
     10     // 定义三个文本框
     11     private Text urlText, usernameText, passwordText;
     12     // 定义一个IPreferenceStore对象
     13     private IPreferenceStore ps;
     14 
     15     // 接口IWorkbenchPreferencePage的方法,它负责初始化。在此方法中设置一个
     16     // PreferenceStore对象,由此对象提供文本框值的读入/写出方法
     17     public void init(IWorkbench workbench) {
     18         setPreferenceStore(Activator.getDefault().getPreferenceStore());
     19     }
     20 
     21     // 父类的界面创建方法
     22     protected Control createContents(Composite parent) {
     23         Composite topComp = new Composite(parent, SWT.NONE);
     24         topComp.setLayout(new GridLayout(2, false));
     25 
     26         // 创建三个文本框及其标签
     27         new Label(topComp, SWT.NONE).setText("URL:");
     28         urlText = new Text(topComp, SWT.BORDER);
     29         urlText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
     30 
     31         new Label(topComp, SWT.NONE).setText("用户名:");
     32         usernameText = new Text(topComp, SWT.BORDER);
     33         usernameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
     34         
     35         new Label(topComp, SWT.NONE).setText("密码:");
     36         passwordText = new Text(topComp, SWT.BORDER | SWT.PASSWORD);
     37         passwordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
     38         
     39         // 取出以前保存的值,并设置到文本框中。如果取出值为空值或空字串,则填入默认值。
     40         ps = getPreferenceStore();// 取得一个IPreferenceStore对象
     41         String url = ps.getString(URL_KEY);
     42         if (url == null || url.trim().equals(""))
     43             urlText.setText(URL_DEFAULT);
     44         else
     45             urlText.setText(url);
     46 
     47         String username = ps.getString(USERNAME_KEY);
     48         if (username == null || username.trim().equals(""))
     49             usernameText.setText(USERNAME_DEFAULT);
     50         else
     51             usernameText.setText(username);
     52 
     53         String password = ps.getString(PASSWORD_KEY);
     54         if (password == null || password.trim().equals(""))
     55             passwordText.setText(PASSWORD_DEFAULT);
     56         else
     57             passwordText.setText(password);
     58 
     59         // 添加事件监听器。this代表本类,因为本类实现了ModifyListener接口成了监听器
     60         usernameText.addModifyListener(this);
     61         passwordText.addModifyListener(this);
     62         urlText.addModifyListener(this);
     63         return topComp;
     64     }
     65 
     66     // 实现自ModifyListener接口的方法,当三个文本框中发生修改时将执行此方法。
     67     // 方法中对输入值进行了验证并将“确定”、“应用”两按钮使能
     68     public void modifyText(ModifyEvent e) {
     69         String errorStr = null;// 将原错误信息清空
     70         if (urlText.getText().trim().length() == 0) {
     71             errorStr = "URL不能为空!";
     72         } else if (usernameText.getText().trim().length() == 0) {
     73             errorStr = "用户名不能为空!";
     74         } else if (passwordText.getText().trim().length() == 0) {
     75             errorStr = "密码不能为空!";
     76         }
     77         setErrorMessage(errorStr);// errorStr=null时复原为正常的提示文字
     78         setValid(errorStr == null);// “确定”按钮
     79         getApplyButton().setEnabled(errorStr == null);// “应用”按钮
     80     }
     81 
     82     // 父类方法。单击“复原默认值”按钮时将执行此方法,取出默认值设置到文本框中
     83     protected void performDefaults() {
     84         urlText.setText(URL_DEFAULT);
     85         usernameText.setText(USERNAME_DEFAULT);
     86         passwordText.setText(PASSWORD_DEFAULT);
     87     }
     88 
     89     // 父类方法。单击“应用”按钮时执行此方法,将文本框值保存并弹出成功的提示信息
     90     protected void performApply() {
     91         doSave(); // 自定义方法,保存设置
     92         MessageDialog.openInformation(getShell(), "信息", "成功保存修改!");
     93     }
     94 
     95     // 父类方法。单击“确定”按钮时执行此方法,将文本框值保存并弹出成功的提示信息
     96     public boolean performOk() {
     97         doSave();
     98         MessageDialog.openInformation(getShell(), "信息", "修改在下次启动生效");
     99         return true; // true表示成功退出
    100     }
    101 
    102     // 自定义方法。保存文本框的值
    103     private void doSave() {
    104         ps.setValue(URL_KEY, urlText.getText());
    105         ps.setValue(USERNAME_KEY, usernameText.getText());
    106         ps.setValue(PASSWORD_KEY, passwordText.getText());
    107     }
    108 }

    运行结果:

    将其中的密码删除之后得到下面的提示效果.

    这个例子中的核心是IPreferenceStroe对象的使用,用它的getString方法来取值,setValue方法来存值.其次和以前的事件代码写法有所不同的是:本类实现了ModifyListener接口,也成为了一个监听器,这样在各文本框的加入监听器的代码就会简洁很多,不过其事件代码必须保证3个文本框可以共用才行.

    此外还用的其他的程序文件.

    Activator.java

     1 /**
     2  * The activator class controls the plug-in life cycle
     3  */
     4 public class Activator extends AbstractUIPlugin {
     5 
     6     // The plug-in ID
     7     public static final String PLUGIN_ID = "cn.com.kxh.myplugin"; //$NON-NLS-1$
     8 
     9     // The shared instance
    10     private static Activator plugin;
    11     
    12     /**
    13      * The constructor
    14      */
    15     public Activator() {
    16         plugin = this;
    17     }
    18 
    19     /**
    20      * Returns the shared instance
    21      *
    22      * @return the shared instance
    23      */
    24     public static Activator getDefault() {
    25         return plugin;
    26     }
    27     
    28     /*
    29      * (non-Javadoc)
    30      * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
    31      */
    32     @Override
    33     public void start(BundleContext context) throws Exception {
    34         super.start(context);
    35     }
    36 
    37     /*
    38      * (non-Javadoc)
    39      * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
    40      */
    41     @Override
    42     public void stop(BundleContext context) throws Exception {
    43         plugin = null;
    44         super.stop(context);
    45     }
    46 
    47     public static ImageDescriptor getImageDescriptor(String path) {
    48         return imageDescriptorFromPlugin(PLUGIN_ID, path);
    49     }
    50 
    51 }

    Messages.java

     1 public class Messages {
     2     private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle ("cn.com.kxh.myplugin.messages");
     3 
     4     public static String getString(String key) {
     5         try {
     6             return RESOURCE_BUNDLE.getString(key);
     7         } catch (MissingResourceException e) {
     8             return '!' + key + '!'; 
     9         }
    10     }
    11 }

    messages.properties

    LanguageDialog.ok=OK
    LanguageDialog.remove=Remove

     另外,我在调试程序的时候有一个地方老是报空指针NPE的错误.

    最后查到其实是Activator.java这个类要在MANIFEST.MF这个文件中注册正确才行.

    MANIFEST.MF

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: Myplugin插件
    Bundle-SymbolicName: cn.com.kxh.myplugin;singleton:=true
    Bundle-Version: 1.0.1
    Bundle-Activator: cn.com.kxh.myplugin.Activator
    Require-Bundle: org.eclipse.ui,
     org.eclipse.core.runtime
    Bundle-RequiredExecutionEnvironment: JavaSE-1.6
    Bundle-ActivationPolicy: lazy
    Bundle-Vendor: Eclipse从入门到精通
  • 相关阅读:
    SQL Server 2005 镜像构建说明(转载)
    表变量 vs. 临时表
    SQL Server日志
    MDX常见计算方法(百分比/分配/平均值/基于时间的计算)
    MDX中一些边界问题的处理
    MDX中的Where vs. Subselect/Subcube
    MDX优化Set操作
    SSIS处理导入数据时, 存在的更新, 不存在的插入(转载)
    MDX实现同比和环比
    iPhone4S出现应用无法打开时的解决方案
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4175772.html
Copyright © 2011-2022 走看看