在程序设计中,需要在一个视图的Jface中选择一项,然后通过命令打开一个新的视图。需要将选择项作为参数传递到命令的handler,以决定需要打开哪一个视图。
需要将Jface想公共的管理平台注册
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new NodeNameContentProvider());
viewer.setLabelProvider(new NodeNameLabelProvider());
// Expand the tree
viewer.setAutoExpandLevel(2);
// Provide the input to the ContentProvider
viewer.setInput(new NodeNameMockModel());
getSite().setSelectionProvider(viewer);
hookDoubleClickAction();
}
当选中某项时执行相应的命令
viewer.addDoubleClickListener(new IDoubleClickListener() {
@SuppressWarnings("restriction")
public void doubleClick(DoubleClickEvent event) {
IHandlerService handlerService = (IHandlerService) getSite()
.getService(IHandlerService.class);
try {
handlerService.executeCommand(
"com.commands.tShow",
null);
}
} catch (Exception e) {
throw new RuntimeException("command not found");
}
在handler中获取具体的选中项。
NodeName node = null;
IWorkbenchWindow window = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
.getActivePage().getSelection();
if (selection != null & selection instanceof IStructuredSelection) {
IStructuredSelection strucSelection = (IStructuredSelection) selection;
for (Iterator<Object> iterator = strucSelection.iterator(); iterator
.hasNext();) {
Object element = (NodeName) iterator.next();
if (element instanceof NodeName) {
node = (NodeName) element;
Debug.println("hello hello" + node.getSummary());
}
}
}
try {
window.getActivePage().showView(
"com.view.viewture");
} catch (Exception e) {
System.out.println("false");
}
// shou message windows
MessageDialog.openInformation(
HandlerUtil.getActiveWorkbenchWindow(event).getShell(), "Info",
node.getSummary());
return null;
}