gpt4 book ai didi

Eclipse:为特定 View 转换命令中的操作

转载 作者:行者123 更新时间:2023-12-02 03:53:00 25 4
gpt4 key购买 nike

我最初的 RCP 是在 3.x 中启动的,目前我在具有兼容层的 Juno 上运行它。我正在考虑进行软迁移,所以我开始慢慢改变我的做法。我正在做的其中一件事就是将我的行动变成命令。

我目前有一个 View (类似于目录浏览器),它向 View 的工具栏和弹出菜单添加操作。这些操作调用 View 中的特定方法,例如向上一个目录。

通过操作很容易做到这一点,因为我只是在 View 类本身中创建我的操作并以编程方式将它们添加到工具栏

IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
mgr.add(upDirectory);
mgr.add(refresh);
mgr.add(changeRoot);

并且从 createPartControl()

调用 Action 的创建
upDirectory = new Action("Go up one directory") {
public void run() {
goUpOneDirectory();
}
};

其中 goUpOneDirectory() 是 View 中的一个方法

如果我想将其转换为命令,我希望能够在我的处理程序中访问 View 的此方法。所以我尝试了以下,

private void createHandlers()
{
IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
IHandler upDirHandler = new AbstractHandler() {
public Object execute(ExecutionEvent event)
throws ExecutionException {
goUpOneDirectory();
return null;
}
};
handlerService.activateHandler("updir.id", upDirHandler);
}

createHandlerscreatePartControl中调用,命令通过plugin.xml添加到 View 的工具栏。问题是,当我的 View 失去焦点时,它会禁用工具栏中这些命令的按钮。

我希望它们始终保持启用状态。我该怎么做?

我知道 isEnabled() 一直返回 true,所以我不确定为什么会这样。 activateHanlder 在 createPartControl 中被调用一次,所以我觉得它应该一直保持事件状态。

编辑:好的,我刚看到 this ,

IHandlerService from the workbench part site is the part handler service. Any handlers activated through the part handlers service will only be active when that part is active. Any listeners added to the part handler service will be removed when the part is disposed, and any active handlers will be deactivated (but not disposed).

那么我怎样才能得到这个,

IHandlerService from the workbench is the global handler service. it provides no special activation scoping or lifecycle.

最佳答案

对不起,我应该再等一会儿才问的,我想通了!

我改变了,

IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class); 

IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);

它奏效了。如果它能帮助其他人,我会留下问题。

关于Eclipse:为特定 View 转换命令中的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13708167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com