Coverage Report - org.eclipse.swtbot.eclipse.finder.finders.ViewMenuFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
ViewMenuFinder
83%
25/30
77%
14/18
2.571
ViewMenuFinder$1
100%
7/7
N/A
2.571
ViewMenuFinder$CommandItemWithTextMatcherWrapper
100%
5/5
100%
2/2
2.571
 
 1  8
 /*******************************************************************************
 2  
  * Copyright (c) 2008, 2011 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  * 
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.eclipse.finder.finders;
 12  
 
 13  
 import java.util.ArrayList;
 14  
 import java.util.List;
 15  
 
 16  
 import org.apache.log4j.Logger;
 17  
 import org.eclipse.jface.action.ActionContributionItem;
 18  
 import org.eclipse.jface.action.IContributionItem;
 19  
 import org.eclipse.jface.action.MenuManager;
 20  
 import org.eclipse.jface.action.SubContributionItem;
 21  
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotViewMenu;
 22  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 23  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 24  
 import org.eclipse.swtbot.swt.finder.results.ListResult;
 25  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 26  
 import org.eclipse.ui.IViewReference;
 27  
 import org.eclipse.ui.internal.ViewPane;
 28  
 import org.eclipse.ui.internal.WorkbenchPartReference;
 29  
 import org.eclipse.ui.menus.CommandContributionItem;
 30  
 import org.hamcrest.Matcher;
 31  
 
 32  
 /**
 33  
  * Finds the menu items within a view.
 34  
  *
 35  
  * @author @author Stephen Paulin <paulin [at] spextreme [dot] com>
 36  
  * @version $Id$
 37  
  * @since 1.2
 38  
  */
 39  1
 public class ViewMenuFinder {
 40  
         /**
 41  
          * The logging instance for this class.
 42  
          */
 43  1
         private static final Logger        log        = Logger.getLogger(ViewMenuFinder.class);
 44  
 
 45  
         /**
 46  
          * Creates a MenuFinder.
 47  
          */
 48  18
         public ViewMenuFinder() {
 49  
                 // Do nothing.
 50  18
         }
 51  
 
 52  
         /**
 53  
          * Gets a list of all menus within the view.
 54  
          *
 55  
          * @param view the view to probe for menus.
 56  
          * @param matcher the matcher that can match menus and menu items.
 57  
          * @param recursive if set to <code>true</code>, will find sub-menus as well.
 58  
          * @return The list of menus (IContributionItems) that match the matcher.
 59  
          * @since 2.0
 60  
          */
 61  
         public List<SWTBotViewMenu> findMenus(final IViewReference view, final Matcher<?> matcher, final boolean recursive) {
 62  4
                 return UIThreadRunnable.syncExec(new ListResult<SWTBotViewMenu>() {
 63  
 
 64  
                         public List<SWTBotViewMenu> run() {
 65  4
                                 ViewPane viewPane = (ViewPane) ((WorkbenchPartReference) view).getPane();
 66  4
                                 MenuManager mgr = viewPane.getMenuManager();
 67  4
                                 List<SWTBotViewMenu> l = new ArrayList<SWTBotViewMenu>();
 68  
 
 69  4
                                 l.addAll(getMenuItemsInternal(mgr.getItems(), matcher, recursive));
 70  
 
 71  4
                                 return l;
 72  
                         }
 73  
                 });
 74  
         }
 75  
 
 76  
         // This is expected to be called from within the UI thread. If not it will throw
 77  
         // exceptions based on invalid thread access.
 78  4
         private List<SWTBotViewMenu> getMenuItemsInternal(IContributionItem[] items, Matcher<?> matcher, boolean recursive) {
 79  4
                 List<SWTBotViewMenu> l = new ArrayList<SWTBotViewMenu>();
 80  
 
 81  36
                 for (int i = 0; i < items.length; i++) {
 82  32
                         IContributionItem item = items[i];
 83  
 
 84  
                         try {
 85  32
                                 if ((item instanceof MenuManager) && recursive) {
 86  
                                         // Sub menus
 87  0
                                         MenuManager menuManager = (MenuManager) item;
 88  
 
 89  0
                                         l.addAll(getMenuItemsInternal(menuManager.getItems(), matcher, recursive));
 90  
                                 } else {
 91  32
                                         SWTBotViewMenu menu = getMenuItem(item, matcher);
 92  32
                                         if (menu != null)
 93  9
                                                 l.add(menu);
 94  
                                 }
 95  0
                         } catch (WidgetNotFoundException e) {
 96  0
                                 log.warn(e);
 97  
                         }
 98  
                 }
 99  
 
 100  4
                 return l;
 101  
         }
 102  
         
 103  
         private SWTBotViewMenu  getMenuItem(IContributionItem item, Matcher<?> matcher) {
 104  32
                 SWTBotViewMenu menu = null;
 105  32
                 if (item instanceof ActionContributionItem) {
 106  16
                         ActionContributionItem actionContribution = (ActionContributionItem) item;
 107  16
                         if (matcher.matches(actionContribution.getAction()))
 108  5
                                 menu = new SWTBotViewMenu(actionContribution);
 109  16
                 } else if (item instanceof SubContributionItem ) {
 110  0
                             menu = getMenuItem(((SubContributionItem) item).getInnerItem(), matcher);
 111  16
                 } else if (item instanceof CommandContributionItem) {
 112  12
                         CommandContributionItem cmdContribution = (CommandContributionItem) item;
 113  12
                         if (matcher.matches(new CommandItemWithTextMatcherWrapper(cmdContribution)))
 114  4
                                 menu = new SWTBotViewMenu(cmdContribution.getCommand());
 115  
                 }
 116  32
                 return menu;
 117  
         }
 118  
         
 119  
         /* This class should be public at it will be accessed outside of its parent class*/
 120  
         public static class CommandItemWithTextMatcherWrapper {
 121  
                 
 122  
                 private CommandContributionItem wrappedCommandItem;
 123  
                 
 124  12
                 public CommandItemWithTextMatcherWrapper(CommandContributionItem item) {
 125  12
                         this.wrappedCommandItem = item;
 126  12
                 }
 127  
                 
 128  
                 /*
 129  
                  * This method will be called reflectively by a matcher.
 130  
                  */
 131  
                 @SuppressWarnings("unused")
 132  
                 public String getText() throws Exception {
 133  
                                 /* label attribute of command contribution item is not available */
 134  9
                                 String label = (String) SWTUtils.getAttribute(wrappedCommandItem, "label"); ////$NON-NLS-1$
 135  9
                                 return label != null ? label:""; ////$NON-NLS-1$
 136  
                 }
 137  
         }
 138  
 }