问题
I have registered a handler through the org.eclipse.ui.handlers
extension point and added an enabledWhen
condition which checks the variable selection
in the Eclipse variable pool. This works perfectly fine, but now I want to replicate this behavior to my SWT buttons which are displayed in a view.
My question is as follows: How can I access the Eclipse variable pool in order to get the selection
variable to listen on selection events and subsequently call the button.setEnabled(true/false)
.
回答1:
You use the selection service ISelectionService
to listen for selection changes. In a view or editor you can get this using:
ISelectionService selectionService = getSite().getService(ISelectionService.class);
You can then use the
public void addSelectionListener(ISelectionListener listener);
method to listen to all selection changes or you can use
public void addSelectionListener(String partId, ISelectionListener listener);
to listen to selection changes in a particular part.
来源:https://stackoverflow.com/questions/41585695/how-to-programatically-access-the-eclipse-variable-pool