FileDocCategorySizeDatePackage
SampleView.javaAPI DocExample8153Thu Mar 25 09:17:52 GMT 2004org.cookbook.ch13.ViewPlugIn.views

SampleView

public class SampleView extends ViewPart
This sample class demonstrates how to plug-in a new workbench view. The view shows data obtained from the model. The sample creates a dummy model on the fly, but a real implementation would connect to the model available either in this or another plug-in (e.g. the workspace). The view is connected to the model using a content provider.

The view uses a label provider to define how model objects should be presented in the view. Each view can present the same model objects using different labels and icons, if needed. Alternatively, a single label provider can be shared between views in order to ensure that objects of the same type are presented in the same way everywhere.

Fields Summary
private TreeViewer
viewer
private DrillDownAdapter
drillDownAdapter
private Action
action1
private Action
action2
private Action
doubleClickAction
Constructors Summary
public SampleView()
The constructor.

	
Methods Summary
private voidcontributeToActionBars()

		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	
public voidcreatePartControl(org.eclipse.swt.widgets.Composite parent)
This is a callback that will allow us to create the viewer and initialize it.

		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		drillDownAdapter = new DrillDownAdapter(viewer);
		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());
		drillDownAdapter.addNavigationActions(manager);
		// Other plug-ins can contribute there actions here
		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);
		manager.add(new Separator());
		drillDownAdapter.addNavigationActions(manager);
	
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() {
				showMessage("You selected Action 1");
			}
		};
		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("You selected Action 2");
			}
		};
		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()
Passing the focus request to the viewer's control.

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

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