Using Eclipse and SWT I am currently trying to get a CommandContributionItem (CCI) as a Button into a ViewPart with two text fields. When I push the button my ParameterizedCommand should be called using the current text values of the text fields as parameters.
I was able to pass the initial values of the text fields to the CCI like that:
public void createPartControl(Composite parent) {
parent.setLayout(new GridLayout(1, false));
text = new Text(parent, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
text_1 = new Text(parent, SWT.BORDER);
text_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Map<String, String> params = new HashMap<String, String>();
params.put("myString", text.getText());
params.put("mySecondString", text_1.getText());
CommandContributionItemParameter p = new CommandContributionItemParameter(getSite(),
"commandSyso","com.voo.example.commandparameter.simple.sysoCommand", CommandContributionItem.STYLE_PUSH);
p.label = "My Label";
p.parameters = params;
CommandContributionItem item = new CommandContributionItem(p);
item.fill(parent);
}
But it is a static one-time pass. Is there a way to update this dynamically every time the CCI is called?
CommandContributionItemparameters are static in nature. You can’t modify them, only create a new instance ofCommandContributionItem.When working with commands, implementation of
IHandlershould look for the current selection using theExecutionEvent.getApplicationContext(). If it is anIEvaluationContext, the selection can be retrieved usingorg.eclipse.ui.handlers.HandlerUtilBut in your example, you would need some way to supply the values of your 2 text fields to the framework, either by implementing an
ISelectionProvider, anISourceProvider(where you could provide each text field under a new name), or by having your handler check for yourIViewPartand then access the information through accessors.