尝试这个..
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
}
}
}
}
希望这会对你有所帮助。