0
votes

I added a main menu button for an eclipse plugin, and this code was generated to handle the action when the button is pressed:

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

}

}

I added the print statement in the execute class to test to see if anything would happen when I pressed the button. However, when I run this plugin as an eclipse application, and press the button, the button stays pressed until I click it again, and nothing else happens. How can I execute some event when I press the button? And then have the process complete so I don't have to press the button again to turn it off?

EDIT:

Here is my 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
Extend org.eclipse.core.commands.AbstractHandler rather than implementing IHandler from scratch, it will deal with everything except execute. How is the menu, command and handler defined in the plugin.xml? - greg-449
I extended AbstractHandler, and it ran but it still did not change anything. The command IDs of the menu, command, and handler, and the default handler of the command are all the same, and the class for the handler is the class I showed above. - Frank Anthony
I think we really need to see the plugin.xml - greg-449
Ok I edited the question - Frank Anthony

1 Answers

0
votes

The menuContribution you have is just defining the top level menu item which doesn't work like a normal push button. You need to define a menu to appear in the drop down list for the menu. Something like:

<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>