ViewPart的基本操作

public class AddressView extends ViewPart {
	private TableViewer viewer;
	private Action action1;
	private Action action2;
	private Action doubleClickAction;

	class ViewContentProvider implements 

IStructuredContentProvider {
		public void inputChanged(Viewer v, Object 

oldInput, Object newInput) {
		}
		public void dispose() {
		}
		public Object[] getElements(Object parent) {
			return (String[])parent;
		}
	}
	class ViewLabelProvider extends LabelProvider implements 

ITableLabelProvider {
		public String getColumnText(Object obj, int 

index) {
			return getText(obj);
		}
		public Image getColumnImage(Object obj, int 

index) {
			return getImage(obj);
		}
		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().
					getSharedImages

().getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}
	class NameSorter extends ViewerSorter {
	}

	public AddressView() {
	}

	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | 

SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new 

ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider

());
		viewer.setSorter(new NameSorter());
		viewer.setInput(new String[] { "One", "Two", 

"Three" });
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager

("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager 

manager) {
				

AddressView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu

(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars

();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(action1);
		manager.add(new Separator());
		manager.add(action2);
	}

	private void fillContextMenu(IMenuManager manager) {
		manager.add(action1);
		manager.add(action2);
		manager.add(new Separator

(IWorkbenchActionConstants.MB_ADDITIONS));
	}
	
	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(action1);
		manager.add(action2);
	}

	private void makeActions() {
		action1 = new Action() {
			public void run() {
				showMessage("Action 1 

executed");
			}
		};
		action1.setText("Action 1");
		action1.setToolTipText("Action 1 tooltip");
		action1.setImageDescriptor

(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor

(ISharedImages.IMG_OBJS_INFO_TSK));
		
		action2 = new Action() {
			public void run() {
				showMessage("Action 2 

executed");
			}
		};
		action2.setText("Action 2");
		action2.setToolTipText("Action 2 tooltip");
		action2.setImageDescriptor

(PlatformUI.getWorkbench().getSharedImages().
				getImageDescriptor

(ISharedImages.IMG_OBJS_INFO_TSK));
		doubleClickAction = new Action() {
			public void run() {
				ISelection selection = 

viewer.getSelection();
				Object obj = 

((IStructuredSelection)selection).getFirstElement();
				showMessage("Double-click 

detected on "+obj.toString());
			}
		};
	}

	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new 

IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent 

event) {
				doubleClickAction.run();
			}
		});
	}
	private void showMessage(String message) {
		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"Test01",
			message);
	}

	public void setFocus() {
		viewer.getControl().setFocus();
	}
}
 

你可能感兴趣的:(view)