Eclipse插件:禁用/启用主菜单中的动作 [英] Eclipse plugin : disable/enable dynamically an action from main menubar

查看:254
本文介绍了Eclipse插件:禁用/启用主菜单中的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:解决

背景

我已经实现了一个eclipse插件(在靛蓝,3.7.2),在条形菜单中提供一个新的图标。

I have implemented a eclipse plugin (on indigo, 3.7.2) that provide a new icon in the bar menu.

这个新动作在eclipse启动后几秒钟完全初始化(一些配置文件被加载从我的maven存储库)。

This new action is fully initialized few seconds after eclipse startup (some configuration files are loaded from my maven repository).

我试图禁用此操作,直到我的活化器完成生命。

I'm trying to disable this action until my activator finish initalization.

我正在寻找

初始化之前

初始化后

我已经完成了

我在我的操作中添加了一个启用测试。只有当我的插件被激活时,我的操作才会启用。

I added an enablement test on my action. My action will be enabled only if my plugin is activated.

<extension
     point="org.eclipse.ui.actionSets">
  <actionSet
        label="Action Set"
        visible="true"
        id="com.eclipse.plugins.extarcheytpe.actionSet">
     <action
           class="com.eclipse.plugins.extarchetype.actions.ShortcutsAction"
           icon="src/main/resources/icons/generic_box_16x16.png"
           id="com.eclipse.plugins.extarcheytpe.actions.ShortcutsAction"
           label="Shortcuts List"
           style="pulldown"
           toolbarPath="Shortcuts"
           tooltip="Shortcuts">
        <enablement>
           <pluginState
                 id="com.eclipse.plugins.extarchetype"
                 value="activated">
           </pluginState>
        </enablement>
     </action>
  </actionSet>
</extension>

[...]

<extension
      point="org.eclipse.ui.startup">
   <startup
          class="com.eclipse.plugins.extarchetype.Startup">
   </startup>
</extension>

我定义了一个启动类,其中ignite()方法初始化我的插件配置:

I defined a startup class, where ignite() method initialize my plugin configuration :

public class Startup implements org.eclipse.ui.IStartup{

    @Override
    public void earlyStartup() {
            try {
                Activator.getDefault().ignite();
                Activator.getDefault().setChanged();
                Activator.getDefault().notifyObservers();
            } catch (Exception e) {
                e.printStackTrace();
            }       
    }

}

我的动作和我的激活器之间的观察设计模式(通知初始化结束)。

I implemented an observing design pattern between my action and my activator (to notify the initialization end).

public class ShortcutsAction extends Action implements
        IWorkbenchWindowPulldownDelegate, IMenuCreator, IObserver {

    private IAction action;
    private Menu menu;
    public boolean enabled = false;

    public ShortcutsAction() {
        super();
        Activator.getDefault().addObserver(this);
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        // Change action object instance at first selection
        if (action != this.action) {
            this.action = action;
        }
        // If property enabled is true, enable this action
        if (enabled) {
            action.setEnabled(true);
        } else {
            action.setEnabled(false);
        }
    }

    [...]

    @Override
    public void update(IObservable obs, Object obj) {
        this.enabled = true;
        ConsoleHandler.logInfo("Shortcut enabled");
        action.setEnabled(true);
    }
}

结论

我得到了我正在寻找的东西:

I got what I was looking for :


  1. 加载时,我的操作被禁用

  2. 由于我的插件已初始化,我的操作已启用

谢谢。

推荐答案

< objectState> < objectClass& / code>测试当前所选对象的状态和类操作处理程序类。

我想你必须使用< systemProperty> 来检查您设置的属性。或使用 selectionChanged 方法启用 IAction

I think you would have to use <systemProperty> to check a property that you set. Or use the selectionChanged method to enable the IAction.

selectionChanged 方法是 IActionDelegate 界面的一部分您正在使用间接实现IWorkbenchWindowPulldownDelegate

The selectionChanged method is part of the IActionDelegate interface that you are indirectly implementing with IWorkbenchWindowPulldownDelegate:

public void selectionChanged(IAction action, ISelection selection)

动作是实际的动作,调用 action.setEnabled(true)来启用动作。

The action parameter is the actual action, call action.setEnabled(true) to enable the action.

这篇关于Eclipse插件:禁用/启用主菜单中的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆