如何在Eclipse插件开发中禁用/启用视图工具栏菜单/操作

问题描述 投票:1回答:3

我认为扩展ViewPart。在这个视图中,我想添加工具栏菜单。

我所知道的,我们可以使用ActionContributionItemAction添加工具栏菜单,并将其从ToolBarMenu中的createPartControl方法添加到ViewPart

但我不知道的是:我们如何以编程方式禁用/启用工具栏菜单?

基本上,我想在工具栏视图中添加“播放”,“停止”和“暂停”按钮。因此,首先,“播放”按钮处于启用模式,其他模式处于禁用状态。当我按下“播放”按钮时,它被禁用,其他人将被启用。

有关更多详细信息,我想要实现的是类似下图。

在红色圆圈中禁用按钮,并在蓝色圆圈中启用按钮。

View

eclipse eclipse-plugin swt eclipse-rcp jface
3个回答
5
投票

不要使用Actions,而是查看Eclipse命令(它们以更清晰的方式替换动作和功能):http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/guide/workbench_cmd.htm

您将在文档中看到您可以启用和禁用命令,并且所有使用它的地方都会自动正确更新其状态。


2
投票

还有另一种方法,我发现在谷歌上磕磕绊绊。这种方法使用ISourceProvider来提供变量状态。因此,我们可以在该类(实现ISourceProvider)中提供命令的启用/禁用状态。这是详细链接http://eclipse-tips.com/tutorials/1-actions-vs-commands?showall=1


2
投票

试试这个..

1:实施您的行动。例如:PlayAction,StopAction。

Public class StartAction extends Action {

@Override
public void run() {
    //actual code run here
}

@Override
public boolean isEnabled() {
    //This is the initial value, Check for your respective criteria and return the appropriate value.
    return false;
}

@Override
public String getText() {
    return "Play";
}
}

2:注册视图部分(播放器视图部分)

Public class Playerview extends ViewPart
    {

 @Override
public void createPartControl(Composite parent) {

 //your player UI code here.


    //Listener registration. This is very important for enabling and disabling the tool bar level buttons
     addListenerObject(this);


    //Attach selection changed listener to the object where you want to perform the action based on the selection type. ex; viewer
    viewer.addselectionchanged(new SelectionChangedListener())  



      }
        }

    //selection changed

    private class SelectionChangedListener implements ISelectionChangedListener        {

    @Override
    public void selectionChanged(SelectionChangedEvent event) {
        ISelection selection = Viewer.getSelection();
        if (selection != null && selection instanceof StructuredSelection) {
            Object firstElement = ((StructuredSelection)selection).getFirstElement();

            //here you can handle the enable or disable based on your selection. that could be your viewer selection or toolbar.
            if (playaction.isEnabled()) { //once clicked on play, stop  should be enabled.
                stopaction.setEnabled(true); //Do required actions here.
                playaction.setEnabled (false);  //do

            }

        }

    }

    }

希望这会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.