FileDocCategorySizeDatePackage
SampleView.javaAPI DocExample4221Wed Dec 10 14:20:50 GMT 2003org.eclipsebook.ch12.Ch1202.views

SampleView

public class SampleView extends ViewPart

Fields Summary
private TableViewer
viewer
private Action
action1
private Action
action2
private Action
doubleClickAction
Constructors Summary
public SampleView()

	
Methods Summary
private voidcontributeToActionBars()

		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	
public voidcreatePartControl(org.eclipse.swt.widgets.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(ResourcesPlugin.getWorkspace());
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	
private voidfillContextMenu(IMenuManager manager)

		manager.add(action1);
		manager.add(action2);
		manager.add(new Separator("Additions"));
	
private voidfillLocalPullDown(IMenuManager manager)

		manager.add(action1);
		manager.add(new Separator());
		manager.add(action2);
	
private voidfillLocalToolBar(IToolBarManager manager)

		manager.add(action1);
		manager.add(action2);
	
private voidhookContextMenu()

		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				SampleView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	
private voidhookDoubleClickAction()

		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				doubleClickAction.run();
			}
		});
	
private voidmakeActions()

		action1 = new Action() {
			public void run() {
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				showMessage("You selected  " + obj.toString());
			}
		};
		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_TASK_TSK));
		doubleClickAction = new Action() {
			public void run() {
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				showMessage("You double-clicked " + obj.toString());
			}
		};
	
public voidsetFocus()

		viewer.getControl().setFocus();
	
private voidshowMessage(java.lang.String message)

		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"Sample View",
			message);