繁体   English   中英

Eclipse RCP:用于视图和编辑器的命令处理程序

[英]Eclipse RCP: Command handler for view and editor

我有一个命令已添加到视图(具有树形视图)的上下文菜单和自定义编辑器的上下文菜单中。

在我的处理程序中,我有什么方法可以区分调用命令的上下文菜单? 这是因为在视图的情况下,我使用类似的方法来获取所需的数据,

ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
                .getActivePage().getSelection();
        if (selection != null & selection instanceof IStructuredSelection)
        {
            IStructuredSelection strucSelection = (IStructuredSelection) selection;
.....

对于编辑器,选择当然为空。 因此,我添加了以下内容来处理编辑器部分,

IEditorPart editor = HandlerUtil.getActiveEditor(event);
IEditorInput input = editor.getEditorInput();
        IPath path = ((FileEditorInput)input).getPath();

但是发生的是,即使我从视图执行此命令,它也始终会返回一个活动的编辑器。 这是否意味着我必须根据视图或编辑器是否处于焦点状态来编写将处于活动状态的单独处理程序?

谢谢!

这是否意味着我必须根据视图或编辑器是否处于焦点状态来编写将处于活动状态的单独处理程序?

是的,它确实。 但是,您单独的处理程序可以是小的类,它们可以调用公共类来完成命令的大部分工作。 我不知道您的命令需要执行什么数据,但是您单独的处理程序可以准备该数据,并通过一个或多个构造函数将其传递给公共类。

您可以具有相同的处理程序,并根据HandlerUtil.getActiveEditor(event) (当活动部件是编辑器时,该值仅为非null )和HandlerUtil.getActiveView(event) (当该情况下的非null时)来决定行为。活动部分是视图)。

或者,您可以让处理程序实现IExecutableExtension并提供方法setInitializationData(IConfigurationElement config, String propertyName, Object data)' which is invoked when the handler is created. Here setInitializationData(IConfigurationElement config, String propertyName, Object data)' which is invoked when the handler is created. Here数据is usually given in the attribute of the handler declaration (see the Javadoc of is usually given in the class attribute of the handler declaration (see the Javadoc of is usually given in the attribute of the handler declaration (see the Javadoc of setInitializationData` attribute of the handler declaration (see the Javadoc of )...

在这种情况下,我更喜欢前一种方法,但是当我无法确定运行时的行为时,我经常使用后一种方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM