如何自己定义RetargetAction

在Eclipse平台,已经内置了很多RetargetAction,比如save,save as,cut,copy,paste,delete等,同时我们也可以定义自己的Retarget Action,赋予它们自己的语义,通常有2种方式.

1.写代码,下面代码来自gef源码:

 eg1:

package org.eclipse.gef.ui.actions;

import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RetargetAction;
import org.eclipse.gef.internal.GEFMessages;

/**
 * @author Eric Bordeau
 * RetargetAction只定义了id,图片等信息,这些信息里面最重要的是id
 */
public class CopyRetargetAction extends RetargetAction {

public CopyRetargetAction() {
	super(ActionFactory.COPY.getId(), GEFMessages.CopyAction_Label);
	ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
	setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
	setDisabledImageDescriptor(sharedImages.getImageDescriptor(
			ISharedImages.IMG_TOOL_COPY_DISABLED));
}								
}

eg2:

   public SettingsAction(IWorkbenchWindow window)
    {
        super("settings", "Settings...");//前者为id,后者为显示文本
        window.getPartService().addPartListener(this);//这行代码也比较重要
        setActionDefinitionId("org.eclipse.ui.edit.settings"); //主要为快捷键设置作准备
    }
}



 

2.通过扩展配置的方式
在org.eclipse.ui.actionSets扩展点中作如下定义(将retarget属性设置为true) 
eg:
<action
               definitionId="org.eclipse.ui.navigate.goToResource"
               label="%GoToResourceAction.label"
               icon="$nl$/icons/full/elcl16/gotoobj_tsk.gif"
               menubarPath="navigate/goTo/"
               retarget="true"//是Retarget Action
               id="goToResource">
</action>

 

阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页