在Handler中获取用户的选择

在程序设计中,需要在一个视图的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;
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值