| /******************************************************************************* |
| * Copyright (c) 2005, 2007 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.ui.tests.api.workbenchpart; |
| |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.action.IMenuListener; |
| import org.eclipse.jface.action.IMenuManager; |
| import org.eclipse.jface.action.IToolBarManager; |
| import org.eclipse.jface.action.MenuManager; |
| import org.eclipse.jface.action.Separator; |
| import org.eclipse.jface.dialogs.MessageDialog; |
| import org.eclipse.jface.viewers.DoubleClickEvent; |
| import org.eclipse.jface.viewers.IDoubleClickListener; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.jface.viewers.IStructuredContentProvider; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.ITableLabelProvider; |
| import org.eclipse.jface.viewers.LabelProvider; |
| import org.eclipse.jface.viewers.TableViewer; |
| import org.eclipse.jface.viewers.Viewer; |
| import org.eclipse.jface.viewers.ViewerSorter; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.graphics.Image; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Menu; |
| import org.eclipse.ui.IActionBars; |
| import org.eclipse.ui.ISharedImages; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.contexts.IContextActivation; |
| import org.eclipse.ui.contexts.IContextService; |
| import org.eclipse.ui.menus.IMenuService; |
| import org.eclipse.ui.part.ViewPart; |
| |
| /** |
| * This view acts as a test harness for the new 3.3 menu contribution story. It |
| * read the additions and uses the mechanism to construct its various menus and |
| * toolbars. |
| * |
| * Right now it's under development but will act as a primary testing point for |
| * the development. |
| * |
| * Currently reads the additions when the control is created and only handles |
| * the popup menu contributions... |
| */ |
| |
| public class MenuContributionHarness extends ViewPart { |
| public final static String VIEW_ID = "org.eclipse.ui.tests.api.MenuTestHarness"; |
| public final static String CONTEXT_TEST1_ID = "org.eclipse.ui.menus.contexts.test1"; |
| |
| IMenuService menuSvc; |
| |
| private TableViewer viewer; |
| private Action action1; |
| private Action action2; |
| private Action doubleClickAction; |
| private Action action3; |
| |
| /* |
| * The content provider class is responsible for providing objects to the |
| * view. It can wrap existing objects in adapters or simply return objects |
| * as-is. These objects may be sensitive to the current input of the view, |
| * or ignore it and always show the same content (like Task List, for |
| * example). |
| */ |
| |
| class ViewContentProvider implements IStructuredContentProvider { |
| public void inputChanged(Viewer v, Object oldInput, Object newInput) { |
| } |
| |
| public void dispose() { |
| } |
| |
| public Object[] getElements(Object parent) { |
| return new String[] { "One", "Two", "Three" }; |
| } |
| } |
| |
| 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 { |
| } |
| |
| /** |
| * The constructor. |
| */ |
| public MenuContributionHarness() { |
| } |
| |
| /** |
| * This is a callback that will allow us to create the viewer and initialize |
| * it. |
| */ |
| public void createPartControl(Composite parent) { |
| // Access the menu service |
| menuSvc = (IMenuService) getSite().getService(IMenuService.class); |
| |
| 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(getViewSite()); |
| makeActions(); |
| hookContextMenu(); |
| hookDoubleClickAction(); |
| contributeToActionBars(); |
| } |
| |
| private void hookContextMenu() { |
| MenuManager menuMgr = new MenuManager("#PopupMenu"); |
| |
| menuMgr.setRemoveAllWhenShown(true); |
| menuMgr.addMenuListener(new IMenuListener() { |
| public void menuAboutToShow(IMenuManager manager) { |
| MenuContributionHarness.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) { |
| // Add some local actions |
| manager.add(action1); |
| manager.add(new Separator()); |
| manager.add(action2); |
| } |
| |
| private void fillLocalToolBar(IToolBarManager manager) { |
| manager.add(action1); |
| manager.add(action2); |
| manager.add(action3); |
| } |
| |
| 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)); |
| |
| final IContextService contextService = (IContextService) getSite() |
| .getService(IContextService.class); |
| action3 = new Action() { |
| IContextActivation currentActivation = null; |
| |
| public void run() { |
| if (currentActivation == null) { |
| currentActivation = contextService |
| .activateContext(MenuContributionHarness.CONTEXT_TEST1_ID); |
| setImageDescriptor(PlatformUI.getWorkbench() |
| .getSharedImages().getImageDescriptor( |
| ISharedImages.IMG_TOOL_UNDO)); |
| } else { |
| contextService.deactivateContext(currentActivation); |
| currentActivation = null; |
| setImageDescriptor(PlatformUI.getWorkbench() |
| .getSharedImages().getImageDescriptor( |
| ISharedImages.IMG_TOOL_REDO)); |
| } |
| } |
| }; |
| action3.setText("Toggle test1"); |
| action3.setToolTipText("Toggle test1"); |
| action3.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() |
| .getImageDescriptor(ISharedImages.IMG_TOOL_REDO)); |
| |
| 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(), |
| "Sample View", message); |
| } |
| |
| /** |
| * Passing the focus request to the viewer's control. |
| */ |
| public void setFocus() { |
| viewer.getControl().setFocus(); |
| } |
| |
| int callCount = 0; |
| |
| public void updateCount() { |
| callCount++; |
| } |
| |
| public int getCount() { |
| return callCount; |
| } |
| } |