如何以编程方式访问Eclipse变量池?

问题描述:

我已经通过 org.eclipse.ui.handlers 扩展点注册了一个处理程序,并添加了一个 enabledWhen 条件,该条件检查变量 selection 在Eclipse变量池中.效果很好,但是现在我想将此行为复制到视图中显示的SWT按钮上.

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.

我的问题如下:如何访问Eclipse变量池以获取 selection 变量以侦听选择事件并随后调用 button.setEnabled(true/false).

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

您可以使用选择服务 ISelectionService 来侦听选择更改.在视图或编辑器中,您可以使用以下方法获取该信息:

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

然后您可以使用

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.