如何自己定义RetargetAction

本文详细介绍了如何在Eclipse平台自定义RetargetAction,包括通过编写代码和扩展配置两种方式来实现,提供了具体的示例代码和操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值