如何使用SourceProvider启用和禁用菜单项(eclipse rcp Indigo 3.7) [英] How to enable and disable menu items upon condition with using SourceProvider (eclipse rcp Indigo 3.7)

查看:110
本文介绍了如何使用SourceProvider启用和禁用菜单项(eclipse rcp Indigo 3.7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个eclipse rcp(版本Indigo 3.7)应用程序(eclipse插件项目)。我读了Lars Vogel的教程Eclipse Commands Advanced(修订版0.2-2.1,11.04.2009-24.09.2011)。

I have an eclipse rcp (version Indigo 3.7) application (eclipse plug-in project). And I read the tutorial "Eclipse Commands Advanced" (Revision 0.2-2.1, 11.04.2009-24.09.2011) by Lars Vogel.

首先,我创建了菜单扩展名菜单和一些命令的菜单贡献。

First I created the menus extension with a menuContribution with menu and with some commands.

第二我用一些命令创建了命令扩展名。

Second I created the commands extension with some commands.

第三我定义了处理程序扩展(具有enabledWhen表达式的处理程序)。

Third I defined the handlers extension (handlers with an enabledWhen expression).

第四,我用变量编写了服务扩展。

And Fourth I wrote the services extension with a variable.

我的plugin.xml如下所示:

My plugin.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
    <extension id="application" point="org.eclipse.core.runtime.applications">
        ...
    </extension>
    <extension point="org.eclipse.ui.perspectives">
        ...
    </extension>
    <extension id="product" point="org.eclipse.core.runtime.products">
        ...
    </extension>
    <extension point="org.eclipse.ui.views">
        ...
    </extension>
    <extension point="org.eclipse.ui.menus">
        <menuContribution allPopups="false" locationURI="menu:org.eclipse.ui.main.menu">
            <menu id="fileMenuItem" label="File" mnemonic="F">
                <command commandId="de.falk.rcp37.drawfigures.commands.new" id="newFileMenuItem" label="New" mnemonic="N" style="push">
                </command>
                <command commandId="de.falk.rcp37.drawfigures.commands.open" id="openFileMenuItem" label="Open..." mnemonic="O" style="push">
                </command>
                <separator name="de.falk.rcp37.drawfigures.menu.file.separator1" visible="true">
                </separator>
                <command commandId="de.falk.rcp37.drawfigures.commands.close" id="closeFileMenuItem" label="Close" mnemonic="C" style="push">
                </command>
                <separator name="de.falk.rcp37.drawfigures.menu.file.separator2" visible="true">
                </separator>
                <command commandId="de.falk.rcp37.drawfigures.commands.save" id="saveFileMenuItem" label="Save" mnemonic="S" style="push">
                </command>
                <command commandId="de.falk.rcp37.drawfigures.commands.saveas" id="saveAsFileMenuItem" label="Save as..." mnemonic="a" style="push">
                </command>
                <separator name="de.falk.rcp37.drawfigures.menu.file.separator3" visible="true">
                </separator>
                <command commandId="de.falk.rcp37.drawfigures.commands.exit" id="exitFileMenuItem" label="Exit" mnemonic="x" style="push">
                </command>
                <command commandId="de.falk.rcp37.drawfigures.commands.change" id="changeFileMenuItem" label="Change" mnemonic="h" style="push">
                </command>
            </menu>
        </menuContribution>
    </extension>
    <extension point="org.eclipse.ui.commands">
        <command id="de.falk.rcp37.drawfigures.commands.new" name="newFileCommand"></command>
        <command id="de.falk.rcp37.drawfigures.commands.open" name="openFileCommand"></command>
        <command id="de.falk.rcp37.drawfigures.commands.close" name="closeFileCommand"></command>
        <command id="de.falk.rcp37.drawfigures.commands.save" name="saveFileCommand"></command>
        <command id="de.falk.rcp37.drawfigures.commands.saveas" name="saveAsFileCommand"></command>
        <command id="de.falk.rcp37.drawfigures.commands.exit" name="exitFileCommand"></command>
        <command id="de.falk.rcp37.drawfigures.commands.change" name="changeFileCommand"></command>
    </extension>
    <extension point="org.eclipse.ui.handlers">
        <handler class="de.falk.rcp37.drawfigures.handler.NewHandler" commandId="de.falk.rcp37.drawfigures.commands.new"></handler>
        <handler class="de.falk.rcp37.drawfigures.handler.OpenHandler" commandId="de.falk.rcp37.drawfigures.commands.open"></handler>
        <handler class="de.falk.rcp37.drawfigures.handler.CloseHandler" commandId="de.falk.rcp37.drawfigures.commands.close">
            <enabledWhen>
                <with variable="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.closestate">
                    <equals value="true"></equals>
                </with>
            </enabledWhen>
        </handler>
        <handler class="de.falk.rcp37.drawfigures.handler.SaveHandler" commandId="de.falk.rcp37.drawfigures.commands.save">
            <enabledWhen>
                <with variable="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.savestate">
                    <equals value="true"></equals>
                </with>
            </enabledWhen>
        </handler>
        <handler class="de.falk.rcp37.drawfigures.handler.SaveAsHandler" commandId="de.falk.rcp37.drawfigures.commands.saveas">
            <enabledWhen>
                <with variable="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.saveasstate">
                    <equals value="true"></equals>
                </with>
            </enabledWhen>
        </handler>
        <handler class="de.falk.rcp37.drawfigures.handler.ExitHandler" commandId="de.falk.rcp37.drawfigures.commands.exit"></handler>
        <handler class="de.falk.rcp37.drawfigures.handler.ChangeHandler" commandId="de.falk.rcp37.drawfigures.commands.change"></handler>
    </extension>
    <extension point="org.eclipse.ui.services">
        <sourceProvider provider="de.falk.rcp37.drawfigures.miscellaneous.MenuItemsStateSourceProvider">
            <variable name="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.closestate" priorityLevel="workbench"></variable>
            <variable name="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.savestate" priorityLevel="workbench"></variable>
            <variable name="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.saveasstate" priorityLevel="workbench"></variable>
        </sourceProvider>
    </extension>
</plugin>

此外我实现了MenuItemsStateSourceProvider:

Additionally I implemented the MenuItemsStateSourceProvider:

package de.falk.rcp37.drawfigures.miscellaneous;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.ui.AbstractSourceProvider;

public class MenuItemsStateSourceProvider extends AbstractSourceProvider {
    public static final String ID = "de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider";
    public static final String DRAWING_CLOSE_STATE = "de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.closestate";
    public static final String DRAWING_SAVE_STATE = "de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.savestate";
    public static final String DRAWING_SAVEAS_STATE = "de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.saveasstate";

    @Override
    public void dispose() {

    }

    @SuppressWarnings("rawtypes")
    @Override
    public Map getCurrentState() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put(ID, ID);
        hashMap.put(DRAWING_CLOSE_STATE, "false");
        hashMap.put(DRAWING_SAVE_STATE, "false");
        hashMap.put(DRAWING_SAVEAS_STATE, "false");
        return hashMap;
}

    @Override
    public String[] getProvidedSourceNames() {
        return new String[] { ID, DRAWING_CLOSE_STATE, DRAWING_SAVE_STATE, DRAWING_SAVEAS_STATE };
    }

public void setDrawingNewed(boolean drawingNewed) {
    if (ModelDefinitions.currentDrawing.getNewed() != drawingNewed) {
            ModelDefinitions.currentDrawing.setNewed(drawingNewed);
            fireSourceChanged(ISources.WORKBENCH, DRAWING_SAVEAS_STATE, ModelDefinitions.currentDrawing.getSaveAsStateString());
    }
}
public void setDrawingOpend(boolean drawingOpend) {
        if (ModelDefinitions.currentDrawing.getOpend() != drawingOpend) {
            ModelDefinitions.currentDrawing.setOpend(drawingOpend);
    fireSourceChanged(ISources.WORKBENCH, DRAWING_CLOSE_STATE, ModelDefinitions.currentDrawing.getCloseStateString());
            fireSourceChanged(ISources.WORKBENCH, DRAWING_SAVE_STATE, ModelDefinitions.currentDrawing.getSaveStateString());
            fireSourceChanged(ISources.WORKBENCH, DRAWING_SAVEAS_STATE, ModelDefinitions.currentDrawing.getSaveAsStateString());
        }
}
public void setDrawingChanged(boolean drawingChanged) {
        if (ModelDefinitions.currentDrawing.getChanged() != drawingChanged) {
            ModelDefinitions.currentDrawing.setChanged(drawingChanged);
            fireSourceChanged(ISources.WORKBENCH, DRAWING_SAVE_STATE, ModelDefinitions.currentDrawing.getSaveStateString());
        }
}
}

和不同的处理程序:

public class ChangeHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

        // doing some CHANGE operations...

    // Get the source provider service
    ISourceProviderService sourceProviderService = (ISourceProviderService) HandlerUtil.getActiveWorkbenchWindow(event).getService(ISourceProviderService.class);
    // Now get my service
    MenuItemsStateSourceProvider menuItemsStateSourceProvider = (MenuItemsStateSourceProvider)sourceProviderService.getSourceProvider(MenuItemsStateSourceProvider.ID);
    menuItemsStateSourceProvider.setDrawingChanged(true);

    return null;
    }
}

或:

public class NewHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

        // doing some NEW operations...

    // Get the source provider service
    ISourceProviderService sourceProviderService = (ISourceProviderService) HandlerUtil.getActiveWorkbenchWindow(event).getService(ISourceProviderService.class);
    // Now get my service
    MenuItemsStateSourceProvider menuItemsStateSourceProvider = (MenuItemsStateSourceProvider)sourceProviderService.getSourceProvider(MenuItemsStateSourceProvider.ID);
    menuItemsStateSourceProvider.setDrawingNewed(true);
    menuItemsStateSourceProvider.setDrawingOpend(false);
    menuItemsStateSourceProvider.setDrawingChanged(false);

    return null;
}
}

依此类推(适用于所有处理程序)...

and so on (for all handler)...

最后在Drawing类中:

and finally in the Drawing class:

public String getCloseStateString() {
    return (opend) ? "true" : "false";
}
public String getSaveStateString() {
    return (opend && changed) ? "true" : "false";
}
public String getSaveAsStateString() {
    return (opend || newed) ? "true" : "false";
}

public void setNewed(boolean newed) {
    this.newed = newed;
}
public void setOpend(boolean opend) {
    this.opend = opend;
}
public void setChanged(boolean changed) {
    this.changed = changed;
}

但没有任何作用!任何人都可以帮忙??

But nothing works! Can anyone help??

谢谢Rumo

推荐答案

ISourceProvider.getCurrentState 期望 Map< String,Object> 作为返回值(请参阅Javadoc)。对启用的处理程序的检查使用 Boolean 值,而不是 String

ISourceProvider.getCurrentState expects a Map<String, Object> as a return value (see Javadoc). The check for enabled handlers uses a Boolean value, not a String.

如果您将 MenuItemsStateSourceProvider 中的 getCurrentState 方法更改为

If you change your getCurrentState method in MenuItemsStateSourceProvider to

@SuppressWarnings("rawtypes")
@Override
public Map getCurrentState() {
  HashMap<String, Object> hashMap = new HashMap<String, Object>();
  hashMap.put(ID, ID);
  hashMap.put(DRAWING_CLOSE_STATE, Boolean.TRUE);
  hashMap.put(DRAWING_SAVE_STATE, Boolean.FALSE);
  hashMap.put(DRAWING_SAVEAS_STATE, Boolean.FALSE);
  return hashMap;
}

关闭菜单将启用条目,其他人将被禁用。
所有州的值必须是布尔值

the Close menu entry will be enabled and the others disabled. All the state values must be Boolean.

这篇关于如何使用SourceProvider启用和禁用菜单项(eclipse rcp Indigo 3.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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