RCP如何将视图的右键进行隐藏显示
1、相关代码MyAction
package myviewrcp2;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
public class MyAction extends Action implements ISelectionListener,IWorkbenchAction{
private IWorkbenchWindow window;
public final static String ID = "myviewrcp2.MyAction";
public MyAction(IWorkbenchWindow window) {
super("Test Action");
setId(ID);
setText("MyAction");
setToolTipText("My Action");
this.window = window;
//注册选择服务监听器
window.getSelectionService().addSelectionListener(this);
}
public void run() {}
//IWorkbenchAction 接口中的方法,释放后取消注册
@Override
public void dispose() {
// TODO Auto-generated method stub
window.getSelectionService().removeSelectionListener(this);
}
//接口 ISelectionListener 中的方法
@Override
public void selectionChanged(IWorkbenchPart part, ISelection incoming) {
// TODO Auto-generated method stub
if(incoming instanceof IStructuredSelection) {
//获得事件发生的源所携带的对象
IStructuredSelection selection = (IStructuredSelection) incoming;
String s = (String) selection.getFirstElement();
if(s == null || "".equals(s)) {
return;
}
//如果选择的是Three,则设置为可用,否则设置为不可用
if(s.equals("Three3")) {
setEnabled(true);
}else {
setEnabled(false);
}
}
}
}
2、SampleView.java

3、效果:


浙公网安备 33010602011771号