Eclipse插件:菜单按钮显示为灰色

时间:2013-10-06 15:00:33

标签: java eclipse plugins

我正在尝试基于org.eclipse.ui.commands创建一个简单的eclipse按钮,org.eclipse.ui.menus和org.eclipse.ui.handlers

但在调试时,我创建的按钮显示为灰色: http://i.stack.imgur.com/rNpbz.png

这是我的plugin.xml和我的LoginHandler.java

的plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         point="org.eclipse.ui.views">
      <category
            name="Server Browsing"
            id="com.abc.serverapi">
      </category>
      <view
            name="Server Browser"
            icon="icons/sample.gif"
            category="com.abc.serverapi"
            class="com.abc.serverapi.views.ServerBrowser"
            id="com.abc.serverapi.views.serverbrowser">
      </view>
   </extension>
   <extension
         point="org.eclipse.ui.perspectiveExtensions">
      <perspectiveExtension
            targetID="org.eclipse.jdt.ui.JavaPerspective">
         <view
               ratio="0.5"
               relative="org.eclipse.ui.views.ProblemView"
               relationship="right"
               id="com.abc.serverapi.views.serverbrowser">
         </view>
      </perspectiveExtension>
   </extension>
   <extension
         point="org.eclipse.help.contexts">
      <contexts
            file="contexts.xml">
      </contexts>
   </extension>
   <extension
         point="org.eclipse.ui.commands">
      <command
            id="com.abc.serverapi.command.login"
            name="Open Server Login">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="com.abc.serverapi.handler.LoginHandler"
            commandId="com.abc.serverapi.command.login">
      </handler>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            allPopups="false"
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               id="com.abc.serverapi.menu.login"
               label="Server Login Menu">
            <command
                  commandId="com.abc.serverapi.command.login"
                  icon="icons/sample.gif"
                  id="com.abc.serverapi.menu.openLoginDialog"
                  mnemonic="O"
                  style="push">
            </command>
         </menu>
      </menuContribution>
   </extension>

</plugin>

这是我的命令处理程序:

package com.abc.serverapi.handler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

public class LoginHandler implements IHandler {

    @Override
    public void addHandlerListener(IHandlerListener handlerListener) {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        MessageDialog.openInformation(
                window.getShell(),
                "Test",
                "Hello, Eclipse world");
        return null;
    }

    @Override
    public boolean isEnabled() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isHandled() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void removeHandlerListener(IHandlerListener handlerListener) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:4)

处理程序中的isEnabled()方法返回false - 因此处理程序未启用!

您可以扩展org.eclipse.core.commands.AbstractHandler而不是实施所有IHandler