(转)Eclipse插件开发之基础篇(2)下

 

4. 解读示例插件的代码

  让我们回到插件工程来,看一看插件工程生成的代码。首先看一下plugin.xml的代码。

代码2-1 plugin.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.actionSets"> ①
      <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" ②
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="cn.sf.amateras.sample.actions.SampleAction">
         </action>
      </actionSet>
   </extension>
</plugin>
  为了在菜单栏增加一个项目,文件中使用了extension元素。①的id指定了扩展点的名称org.eclipse.ui.actionSets。extension里的内容根据扩展点而不同。示例插件中包含了actionSet、menu、action等元素。

  ②中的class属性指定了cn.sf.amateras.sample.actions.SampleAction类作为响应菜单或者工具栏按钮的action类。

代码2-2 SampleAction.java

package cn.sf.amateras.sample.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.jface.dialogs.MessageDialog;

/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class SampleAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	/**
	 * The constructor.
	 */
	public SampleAction() {
	}

	/**
	 * The action has been activated. The argument of the
	 * method represents the 'real' action sitting
	 * in the workbench UI.
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {   //......①
		MessageDialog.openInformation(
			window.getShell(),
			"Sample",
			"Hello, Eclipse world");
	}

	/**
	 * Selection in the workbench has been changed. We 
	 * can change the state of the 'real' action here
	 * if we want, but this can only happen after 
	 * the delegate has been created.
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/**
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
	}

	/**
	 * We will cache window object in order to
	 * be able to provide parent shell for the message dialog.
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}
 ①run()方法内记录了action执行时的处理。利用MessageDialog.openInformation方法打开了一个对话框。

  咱们再看看生成的另外一个类--Activator类。这个类对插件的生命周期进行了管理,被称为插件类。

代码2-3 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);
	}
}
 示例中的插件类继承自AbstractUIPlugin(①)。如果没有UI的插件继承AbstractUIPlugin的父类Plugin就可以了。

  插件类的方法如下表所示:

getDefault()取得插件类的实例的方法。插件类是单例的,所以这个方法作为一个静态方法提供。
start()插件开始时的处理。
stop()插件停止时的处理。
getLog()log输出时取得ILog用的方法。
getImageRegistry()取得管理插件内图像的ImageRegistry类。
getPerferenceStore()取得保存插件设定的IPerferenceStore类。
getDialogSettings()取得保存对话框设定的IDialogSettings类。
getWorkbench()取得IWorkbench的实例。

 

  以上就是根据[Hello World]模板生成的插件的代码讲解,在PDE中为我们准备了很多的模板,大家可以都试试看。

转载自:http://www.cnblogs.com/liuzhuo  后面章节不再转载,读者请查看原出处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值