我有一个扩展ViewPart的视图。在此视图中,我要添加工具栏菜单。
据我所知,我们可以使用ActionContributionItem或Action添加工具栏菜单,并将其添加到ViewPart中的createPartControl方法中的ToolBarMenu中。
但我不知道的是:我们如何以编程方式禁用/启用工具栏菜单?
因此,基本上,我想添加播放,停止,暂停按钮到工具栏视图。所以一开始,Play按钮处于启用模式,其他按钮被禁用。当我按下“播放”按钮时,它将被禁用,其他的将被启用。
关于更多的细节,我想要实现的是类似于下面的图像。
在红色圆圈中有禁用按钮,在蓝色圆圈中启用按钮。

发布于 2012-04-30 05:15:58
与其使用操作,不如看一下Eclipse命令(它们是操作和函数的替代品):http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/guide/workbench_cmd.htm
您将在文档中看到可以启用和禁用命令,所有使用命令的地方都将自动更新其状态。
发布于 2012-05-01 04:25:55
还有一种方法是我偶然发现谷歌的。这种方法是使用ISourceProvider提供可变状态。因此,我们可以在该类中提供启用/禁用命令的状态(即实现ISourceProvider)。下面是详细链接http://eclipse-tips.com/tutorials/1-actions-vs-commands?showall=1
发布于 2012-11-09 13:01:10
试试这个..。
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
}
}
}
}希望这能帮到你。
https://stackoverflow.com/questions/10378087
复制相似问题