Coverage Report - org.eclipse.swtbot.eclipse.finder.finders.ViewMenuFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
ViewMenuFinder
78%
15/19
70%
7/10
2.5
ViewMenuFinder$1
100%
7/7
N/A
2.5
 
 1  4
 /*******************************************************************************
 2  
  * Copyright (c) 2008 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.swtbot.eclipse.finder.widgets.SWTBotViewMenu;
 21  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 22  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 23  
 import org.eclipse.swtbot.swt.finder.results.ListResult;
 24  
 import org.eclipse.ui.IViewReference;
 25  
 import org.eclipse.ui.internal.ViewPane;
 26  
 import org.eclipse.ui.internal.WorkbenchPartReference;
 27  
 import org.hamcrest.Matcher;
 28  
 
 29  
 /**
 30  
  * Finds the menu items within a view.
 31  
  *
 32  
  * @author @author Stephen Paulin <paulin [at] spextreme [dot] com>
 33  
  * @version $Id$
 34  
  * @since 1.2
 35  
  */
 36  1
 public class ViewMenuFinder {
 37  
         /**
 38  
          * The logging instance for this class.
 39  
          */
 40  1
         private static final Logger        log        = Logger.getLogger(ViewMenuFinder.class);
 41  
 
 42  
         /**
 43  
          * Creates a MenuFinder.
 44  
          */
 45  18
         public ViewMenuFinder() {
 46  
                 // Do nothing.
 47  18
         }
 48  
 
 49  
         /**
 50  
          * Gets a list of all menus within the view.
 51  
          *
 52  
          * @param view the view to probe for menus.
 53  
          * @param matcher the matcher that can match menus and menu items.
 54  
          * @param recursive if set to <code>true</code>, will find sub-menus as well.
 55  
          * @return The list of menus (IContributionItems) that match the matcher.
 56  
          * @since 2.0
 57  
          */
 58  
         public List<SWTBotViewMenu> findMenus(final IViewReference view, final Matcher<?> matcher, final boolean recursive) {
 59  2
                 return UIThreadRunnable.syncExec(new ListResult<SWTBotViewMenu>() {
 60  
 
 61  
                         public List<SWTBotViewMenu> run() {
 62  2
                                 ViewPane viewPane = (ViewPane) ((WorkbenchPartReference) view).getPane();
 63  2
                                 MenuManager mgr = viewPane.getMenuManager();
 64  2
                                 List<SWTBotViewMenu> l = new ArrayList<SWTBotViewMenu>();
 65  
 
 66  2
                                 l.addAll(getMenuItemsInternal(mgr.getItems(), matcher, recursive));
 67  
 
 68  2
                                 return l;
 69  
                         }
 70  
                 });
 71  
         }
 72  
 
 73  
         // This is expected to be called from within the UI thread. If not it will throw
 74  
         // exceptions based on invalid thread access.
 75  2
         private List<SWTBotViewMenu> getMenuItemsInternal(IContributionItem[] items, Matcher<?> matcher, boolean recursive) {
 76  2
                 List<SWTBotViewMenu> l = new ArrayList<SWTBotViewMenu>();
 77  
 
 78  14
                 for (int i = 0; i < items.length; i++) {
 79  12
                         IContributionItem item = items[i];
 80  
 
 81  
                         try {
 82  12
                                 if ((item instanceof MenuManager) && recursive) {
 83  
                                         // Sub menus
 84  0
                                         MenuManager menuManager = (MenuManager) item;
 85  
 
 86  0
                                         l.addAll(getMenuItemsInternal(menuManager.getItems(), matcher, recursive));
 87  12
                                 } else if (item instanceof ActionContributionItem) {
 88  
                                         // Menus
 89  8
                                         ActionContributionItem actionContribution = (ActionContributionItem) item;
 90  
 
 91  8
                                         if (matcher.matches(actionContribution.getAction()))
 92  1
                                                 l.add(new SWTBotViewMenu(actionContribution));
 93  
                                 }
 94  0
                         } catch (WidgetNotFoundException e) {
 95  0
                                 log.warn(e);
 96  
                         }
 97  
                 }
 98  
 
 99  2
                 return l;
 100  
         }
 101  
 }