Question

I have added the button in the following way:

<extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               label="File">
            <command
                  commandId="org.eclipse.ui.file.exit"
                  label="Exit">
            </command>
         </menu>
      </menuContribution>
      <menuContribution
            allPopups="false"
            locationURI="toolbar:MYVIEWID">
         <command
               commandId="MYCOMMANDID"
               icon="icons/1389818330_Run.png"
               label="Run a bot"
               style="push">
         </command>
      </menuContribution>
   </extension>

In result, toolbar button appears on the view, but it is dimmed and does nothing.

Where to set the code which will execute on this button press?

Was it helpful?

Solution

You use the org.eclipse.ui.handlers extension point to define a handler for the command id.

<extension
     point="org.eclipse.ui.handlers">
  <handler
        class="org.eclipse.ui.examples.contributions.handlers.GlobalMenuHandler"
        commandId="org.eclipse.ui.examples.contributions.commands.globalCommand">
  </handler>
</extension>

You must also use the org.eclipse.ui.commands extension point to define the command id in the first place. You can specify a default handler in the command id definition:

<extension
     point="org.eclipse.ui.handlers">
  <command
        categoryId="org.eclipse.ui.examples.contributions.commands.category"
        defaultHandler="org.eclipse.ui.examples.contributions.handlers.GlobalMenuHandler"
        id="org.eclipse.ui.examples.contributions.commands.globalCommand"
        name="%contributions.commands.globalCommand.name">
  </command>
</extension>

(above examples are from the Eclipse help).

There is also this tutorial on commands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top