Search code examples
javaeclipseeclipse-cdteclipse-pde

How to programatically access the Eclipse variable pool?


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).


Solution

  • 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.