视图开发-快捷键、状态栏、监听其它workbench

1快捷键,以Delete为例
viewer.getControl().addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent event) {
				if (event.character == SWT.DEL && event.stateMask == 0) {
					deleteAction.run();
				}
			}
		});



2状态栏

viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();
				IStatusLineManager statusline = getViewSite().getActionBars()
						.getStatusLineManager();

				Object obj = selection.getFirstElement();
				if (obj == null)
					return;
				if (obj instanceof AddressItem) {
					AddressItem addressItem = (AddressItem) obj;
statusline.setMessage(addressItem.getCategory().getImage(),
							addressItem.getName()
									+ " : "
									+ addressItem.getCategory()
											.getCategoryName());
				} else
					statusline.setMessage(obj.toString());
			}
		});


3监听workbench的其他选择部分,只要让AressView视线ISelectionListener,同时向page注册监听器:
public class AddressView extends ViewPart implements ISelectionListener {
public void createPartControl(Composite parent) {
//注册监听器
getSite().getPage().addSelectionListener(this);
}

@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		// TODO Auto-generated method stub

	}

	public void dispose() {
		getViewSite().getWorkbenchWindow().
		getSelectionService().removeSelectionListener(this);
	super.dispose();
	}
}

你可能感兴趣的:(java)