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[]{});
}
}