簡體   English   中英

如何在Eclipse插件開發中禁用/啟用視圖工具欄菜單/操作

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

我有擴展ViewPart視圖。 在此視圖中,我要添加工具欄菜單。

我所知道的,我們可以使用ActionContributionItemAction添加工具欄菜單,並通過ToolBarMenucreatePartControl方法將其添加到ViewPart

但是我不知道這是什么:我們如何以編程方式禁用/啟用工具欄菜單?

因此,基本上,我想在工具欄視圖中添加“ 播放” ,“ 停止 ”和“ 暫停”按鈕。 因此,首先,“ 播放”按鈕處於啟用模式,而其他按鈕則被禁用。 當我按“ 播放”按鈕時,該按鈕被禁用,其他按鈕將被啟用。

有關更多詳細信息,我要實現的功能如下圖所示。

在紅色圓圈中,禁用按鈕,在藍色圓圈中,啟用按鈕。

視圖

除了使用Actions,還可以查看Eclipse命令(它們以更簡潔的方式代替了Actions和Function): http : //help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/指南/workbench_cmd.htm

您會在文檔中看到可以啟用和禁用命令,並且所有使用該命令的地方都會自動正確更新其狀態。

我在google上絆倒了,發現了另一種方法。 此方法使用ISourceProvider提供變量狀態。 因此,我們可以在該類(實現ISourceProvider的類)中提供命令的啟用/禁用狀態。 這是詳細鏈接http://eclipse-tips.com/tutorials/1-actions-vs-commands?showall=1

嘗試這個..

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:注冊您的視圖部分(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

            }

        }

    }

    }

希望這對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM