添加菜单按钮Eclipse插件

时间:2014-02-18 18:48:50

标签: eclipse eclipse-plugin

我为eclipse插件添加了一个主菜单按钮,生成此代码以处理按下按钮时的操作:

package de.vogella.plugin.experiment;

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.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;


public class SomethingHandler 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 {
    System.out.println("Hello");
    // TODO Auto-generated method stub
    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

}

}

我在execute类中添加了print语句来测试,看看按下按钮时是否会发生任何事情。但是,当我将此插件作为eclipse应用程序运行时,按下按钮,按钮一直按下,直到我再次单击它,没有其他任何事情发生。按下按钮后如何执行某些事件?然后让过程完成,这样我就不必再按下按钮关闭它了吗?

编辑:

这是我的plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
>
   <extension
         point="org.eclipse.ui.commands">
      <command
            defaultHandler="de.vogella.plugin.experiment.SomethingHandler"
            id="de.vogella.plugin.experiment.SomethingHandler"
            name="name">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="SomethingHandler"
            commandId="de.vogella.plugin.experiment.SomethingHandler">
      </handler>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            allPopups="false"
            locationURI="menu:org.eclipse.ui.main.menu">
         <command
               commandId="de.vogella.plugin.experiment.SomethingHandler"
               label="DoSomething"
               style="push">
         </command>
      </menuContribution>
   </extension>
</plugin>

1 个答案:

答案 0 :(得分:0)

你所拥有的menuContribution只是定义顶级菜单项,它不像普通的按钮那样工作。您需要定义一个菜单以显示在菜单的下拉列表中。类似的东西:

<extension
     point="org.eclipse.ui.menus">
  <menuContribution
        allPopups="false"
        locationURI="menu:org.eclipse.ui.main.menu">
    <menu 
          id="menu.id" 
          label="Top Level Menu">
     <command
           commandId="de.vogella.plugin.experiment.SomethingHandler"
           label="DoSomething"
           style="push">
     </command>
   </menu>         
  </menuContribution>
</extension>