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类作为响应菜单或者工具栏按钮的action类
tooltip="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的实例
本文介绍了如何使用Eclipse进行插件开发,包括通过plugin.xml配置文件定义菜单、操作集等元素,以及Activator类来控制插件的生命周期。
1009

被折叠的 条评论
为什么被折叠?



