Eclipse开发java插件

本文介绍了如何使用Eclipse进行插件开发,包括通过plugin.xml配置文件定义菜单、操作集等元素,以及Activator类来控制插件的生命周期。

plugin.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.actionSets"> ①
         //这里的ID指定了扩展名称为org.eclipse.ui.actionSets.extension里的内容根据扩展点而不同。示例的插件中包含了actionset,menu,action等元素
      <actionSet
            label="Sample Action Set"
            visible="true"
            id="cn.sf.amateras.sample.actionSet">
         <menu
               label="Sample &Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               label="&Sample Action"
               icon="icons/sample.gif"
               class="cn.sf.amateras.sample.actions.SampleAction" ②
               //class属性指定了cn.sf.amateras.sample.actions.SampleAction类作为响应菜单或者工具栏按钮的actiontooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="cn.sf.amateras.sample.actions.SampleAction">
         </action>
      </actionSet>
   </extension>
</plugin>

Activator.java类

package cn.sf.amateras.sample;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "cn.sf.amateras.sample"; //$NON-NLS-1$

    // The shared instance
    private static Activator plugin;

    /**
     * The constructor
     */
    public Activator() {
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

    /**
     * Returns an image descriptor for the image file at the given
     * plug-in relative path
     *
     * @param path the path
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }
}

这个类是对插件的生命周期进行了管理,被称为插件类;

  • getDefault() 取得插件类的实例的方法
  • start() 插件开始时的处理
  • stop() 插件结束时的处理
  • getLog() log输出时取得ILog的方法
  • getImageRegistry() 取得管理插件内图像的ImageRegistry类
  • getPerferenceStor() 取得保存插件设定的IPerferenceStore类
  • getDialogSettings() 取得保存对话框设定的IDialogSettings类
  • getWorkbench() 取得IWorkbench的实例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值