Friday, January 11, 2008

Dynamic Labels Redux

This is an update to a previous blog posting about contributing menu items with dynamic labels to popups. The constructor

public CommandContributionItem(IServiceLocator serviceLocator, String id, String commandId, Map parameters, ImageDescriptor icon, ImageDescriptor disabledIcon, ImageDescriptor hoverIcon, String label, String mnemonic, String tooltip, int style)

has been deprecated in Ganymede (Eclipse 3.4) in favor of the less unwieldy

public CommandContributionItem(CommandContributionItemParameter contributionParameters)

CommandContributionItemParameter has both a complete and a minimal constructor (for injecting just the required parameters), and its fields are public and non-final, so they can be set as needed.

The equivalent revised example class looks something like this:


public class MyCompoundContributionItem extends CompoundContributionItem {
private static int counter = 0;

@Override
protected IContributionItem[] getContributionItems() {
final CommandContributionItemParameter contributionParameter =
new CommandContributionItemParameter(
PlatformUI.getWorkbench().getActiveWorkbenchWindow(),
"my.project.myCommandContributionItem", "my.project.myCommand",
SWT.NONE);
contributionParameter.label = "Dynamic Menu Item " + counter++;
return new IContributionItem[] {
new CommandContributionItem(contributionParameter) };
}
}

2 comments:

M@ said...

Is it possible to create dynamic menus inside of dynamic menus? maybe programatically?

Tim Myer said...

Hi m@,
I apologize for the delay in responding.
I do not have an answer offhand for this, but I would imagine it is definitely possible both programmatically and through extension points using menu contributions and visibleWhen expressions; although, without actually sitting down and trying, I cannot say for sure.
You might start by looking here if you have not already figured out how to do this:
http://www.ibm.com/developerworks/library/os-eclipse-3.3menu/index.html#N102EE
http://wiki.eclipse.org/index.php/Menu_Contributions#Declarative_menus_-_some_constraints
I am interested in hearing what solution you come up with!
Thanks.
-----Tim-----