Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Detect view in which a dynamic pulldown menu was opened
Detect view in which a dynamic pulldown menu was opened [message #1783189] Thu, 08 March 2018 22:41
Marian Schedenig is currently offline Marian SchedenigFriend
Messages: 18
Registered: July 2012
Junior Member
Note: I've tried asking this at Stack Overflow, but with no replies (https://stackoverflow.com/questions/48968816/)

I'm contributing a button with a dynamic pulldown menu to the Eclipse console view's toolbar. Since there can be more than one console view, I want to detect in which console the menu was opened when one of its entries is clicked, and also when I decide which entries are enabled (or perhaps even available).

The reason for this is that the associated actions should affect the console in question. I'm able to access the active console if I know the view (via various listeners), but I haven't been able to find how to get either the corresponding view or console when the menu is opened. Tracking the active console view doesn't help, because accessing the menu of an inactive view doesn't active the view until after the click has been handled.

Here's an example of how I create the menus:

plugin.xml:

<plugin>
   <extension
         point="org.eclipse.ui.commands">
      <command
            defaultHandler="name.schedenig.eclipse.consoleactiontest.commands.console.TestHandler"
            id="name.schedenig.eclipse.consoleactiontest.commands.console.test"
            name="Test">
         <commandParameter
               id="name.schedenig.eclipse.consoleactiontest.commands.console.test.params.index"
               name="Index"
               optional="true">
         </commandParameter>
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            allPopups="true"
            locationURI="toolbar:org.eclipse.ui.console.ConsoleView">
         <command
               commandId="name.schedenig.eclipse.consoleactiontest.commands.console.test"
               id="name.schedenig.eclipse.consoleactiontest.commands.console.test.dynamic"
               label="Test"
               style="pulldown">
         </command>
      </menuContribution>
      <menuContribution
            allPopups="true"
            locationURI="menu:name.schedenig.eclipse.consoleactiontest.commands.console.test.dynamic">
         <dynamic
               class="name.schedenig.eclipse.consoleactiontest.commands.console.DynamicChildrenHandler"
               id="name.schedenig.eclipse.consoleactiontest.commands.console.test.dynamic.children">
         </dynamic>
      </menuContribution>
   </extension>

</plugin>


TestHandler:

package name.schedenig.eclipse.consoleactiontest.commands.console;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

public class TestHandler extends AbstractHandler
{
  public static final String PARAM_INDEX = "name.schedenig.eclipse.consoleactiontest.commands.console.test.params.index";

  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException
  {
    String index = event.getParameter(PARAM_INDEX);

    if(index == null)
    {
      System.out.println("Test Handler");
    }
    else
    {
      System.out.println("Test Handler #" + (Integer.valueOf(index) + 1));
    }

    return null;
  }
}


DynamicChildrenHandler:

package name.schedenig.eclipse.consoleactiontest.commands.console;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.IWorkbenchContribution;
import org.eclipse.ui.services.IServiceLocator;

public class DynamicChildrenHandler extends CompoundContributionItem implements IWorkbenchContribution
{
  private IServiceLocator serviceLocator;

  @Override
  public void initialize(IServiceLocator serviceLocator)
  {
    this.serviceLocator = serviceLocator;
  }

  @Override
  protected IContributionItem[] getContributionItems()
  {
    List<IContributionItem> items = new ArrayList<>();
    items.add(new Separator());

    for(int i = 0; i < 3; i++)
    {
      String commandId = "name.schedenig.eclipse.consoleactiontest.commands.console.test";
      String id = commandId + "." + i;

      Map<String, String> parameters = new HashMap<>();
      parameters.put(TestHandler.PARAM_INDEX, String.valueOf(i));

      ImageDescriptor icon = null;
      ImageDescriptor disabledIcon = null;
      ImageDescriptor hoverIcon = null;
      String label = "Test " + (i + 1);
      String mnemonic = null;
      String tooltip = null;
      int style = CommandContributionItem.STYLE_CHECK;
      String helpContextId = null;
      boolean enabled = true;

      CommandContributionItemParameter styleParams = new CommandContributionItemParameter(
          serviceLocator, 
          id, 
          commandId, 
          parameters, 
          icon, 
          disabledIcon, 
          hoverIcon, 
          label, 
          mnemonic, 
          tooltip, 
          style, 
          helpContextId, 
          enabled);

      items.add(new CommandContributionItem(styleParams));
    }

    return items.toArray(new IContributionItem[]{});
  }
}

Previous Topic:How can use native library in my Eclipse Plug-in project
Next Topic:p2 director install fails after successful compilation
Goto Forum:
  


Current Time: Wed Apr 24 13:33:31 GMT 2024

Powered by FUDForum. Page generated in 0.03073 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top