简体   繁体   English

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

[英]How to disable/enable view toolbar menu/action in Eclipse Plugin Development

I have view that extends ViewPart . 我有扩展ViewPart视图。 In this view, I want to add toolbar menu. 在此视图中,我要添加工具栏菜单。

What I know, we can add toolbar menu by using ActionContributionItem or Action , and add it to ToolBarMenu from createPartControl method in ViewPart . 我所知道的,我们可以使用ActionContributionItemAction添加工具栏菜单,并通过ToolBarMenucreatePartControl方法将其添加到ViewPart

But what I don't know is this: How can we disable/enable the toolbar menu programmatically? 但是我不知道这是什么:我们如何以编程方式禁用/启用工具栏菜单?

So basically, I want to add Play , Stop , and Pause button to toolbar view. 因此,基本上,我想在工具栏视图中添加“ 播放” ,“ 停止 ”和“ 暂停”按钮。 So at first, the Play button is on enabled mode, and the others are disabled. 因此,首先,“ 播放”按钮处于启用模式,而其他按钮则被禁用。 When I pressed Play button, it is disabled, and others will be enabled. 当我按“ 播放”按钮时,该按钮被禁用,其他按钮将被启用。

For more details, what I want to achieve is something like the following image. 有关更多详细信息,我要实现的功能如下图所示。

In the red circle are disabled button, and in the blue circle are enabled button. 在红色圆圈中,禁用按钮,在蓝色圆圈中,启用按钮。

视图

Instead of using Actions, have a look at Eclipse commands (they are the replacement for actions and function in a cleaner way): http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/guide/workbench_cmd.htm 除了使用Actions,还可以查看Eclipse命令(它们以更简洁的方式代替了Actions和Function): http : //help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/指南/workbench_cmd.htm

You will see in the documentation that you can enable and disable a command and all places where it's used will properly update their state automatically. 您会在文档中看到可以启用和禁用命令,并且所有使用该命令的地方都会自动正确更新其状态。

There is another approach which I found by stumbling upon on google. 我在google上绊倒了,发现了另一种方法。 This approach is using ISourceProvider to provide variable state. 此方法使用ISourceProvider提供变量状态。 So we can provide the state of enablement/disablement of command in that class (that implementing ISourceProvider). 因此,我们可以在该类(实现ISourceProvider的类)中提供命令的启用/禁用状态。 Here is the detail link http://eclipse-tips.com/tutorials/1-actions-vs-commands?showall=1 这是详细链接http://eclipse-tips.com/tutorials/1-actions-vs-commands?showall=1

Try this.. 尝试这个..

1: Implement your actions. 1:实施您的行动。 ex: PlayAction, StopAction. 例如: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: Register your view part(Player view part) 2:注册您的视图部分(Player视图部分)

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

            }

        }

    }

    }

Hope this would help you. 希望这对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM