简体   繁体   English

Eclipse插件:基于当前选择启用操作

[英]Eclipse Plugin: Enablement of an Action based on the current selection

I am using the org.eclipse.ui.popupMenus extension point for adding a sub-menu whose Action that is bounded to the following class: 我正在使用org.eclipse.ui.popupMenus扩展点添加一个子菜单,该子菜单的Action绑定到以下类:

  public class MyAction implements IObjectActionDelegate {

     private Logic logic = Logic.getInstance(); // Singleton

     public void setActivePart(IAction a, IWorkbenchPart targetPart) {
        // Nothing here
     }

     public void run(IAction a) {      
        // Do something...
     }

     public void selectionChanged(IAction a, ISelection s) {
        a.setEnabled(logic.isEnabled(s));
     }
  }

This action is working correctly in most cases (including the call a.setEnabled() in selectionChanged() ). 这一行动是在大多数情况下正常工作(包括呼叫a.setEnabled()selectionChanged() My problem at the very first time my action is being invoked. 我的问题在我的动作被第一次调用时就出现了。 The selectionChanged method is called only after the menu item has been displayed (and not when the user has made the selection) which means that the call to a.setEnabled() will have no affect. selectionChanged菜单项已经显示只有 (而不是当用户做出的选择),这意味着调用方法被调用a.setEnabled()不会有任何影响。

Any ideas on how to make my action receive selectionChanged() notifications even before the fist time it is being invoked? 关于如何使我的操作在调用之前就收到selectionChanged()通知的任何想法?

Eclipse uses so-called lazy plugin activation, so it first derives as much as possible from plugin.xml, and the behaviour you are observing is well-documented in the API. Eclipse使用了所谓的惰性插件激活,因此它首先尽可能地从plugin.xml派生,并且您观察到的行为在API中有详细记录。 See this related question. 请参阅此相关问题。

The words first time and after make me wonder about a synchronization problem. 第一次之后的单词使我想知道同步问题。 If the initialization of Logic.getInstance() is deferred, you might look at the Initialization On Demand Holder Idiom , also discussed in item 71 of Effective Java . 如果推迟了Logic.getInstance()的初始化,则可以查看“ 按需初始化持有人惯用语” ,这也在“ 有效Java”的项目71中进行了讨论。

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

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