| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MenuFinder |
|
| 1.5;1.5 | ||||
| MenuFinder$1 |
|
| 1.5;1.5 | ||||
| MenuFinder$2 |
|
| 1.5;1.5 | ||||
| MenuFinder$3 |
|
| 1.5;1.5 |
| 1 | 440 | /******************************************************************************* |
| 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.swt.finder.finders; | |
| 12 | ||
| 13 | import java.util.ArrayList; | |
| 14 | import java.util.LinkedHashSet; | |
| 15 | import java.util.List; | |
| 16 | ||
| 17 | import org.apache.log4j.Logger; | |
| 18 | import org.eclipse.swt.SWT; | |
| 19 | import org.eclipse.swt.widgets.Display; | |
| 20 | import org.eclipse.swt.widgets.Event; | |
| 21 | import org.eclipse.swt.widgets.Menu; | |
| 22 | import org.eclipse.swt.widgets.MenuItem; | |
| 23 | import org.eclipse.swt.widgets.Shell; | |
| 24 | import org.eclipse.swtbot.swt.finder.results.ArrayResult; | |
| 25 | import org.eclipse.swtbot.swt.finder.results.ListResult; | |
| 26 | import org.eclipse.swtbot.swt.finder.results.WidgetResult; | |
| 27 | import org.eclipse.swtbot.swt.finder.utils.SWTUtils; | |
| 28 | import org.hamcrest.Matcher; | |
| 29 | ||
| 30 | /** | |
| 31 | * Finds menus matching a particular matcher. | |
| 32 | * | |
| 33 | * @see UIThreadRunnable | |
| 34 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
| 35 | * @version $Id$ | |
| 36 | */ | |
| 37 | 1 | public class MenuFinder { |
| 38 | ||
| 39 | /** | |
| 40 | * The logging instance for this class. | |
| 41 | */ | |
| 42 | 1 | private static final Logger log = Logger.getLogger(MenuFinder.class); |
| 43 | ||
| 44 | /** The display */ | |
| 45 | protected final Display display; | |
| 46 | ||
| 47 | /** | |
| 48 | * Creates a MenuFinder. | |
| 49 | */ | |
| 50 | 3409 | public MenuFinder() { |
| 51 | 3409 | display = SWTUtils.display(); |
| 52 | 3409 | } |
| 53 | ||
| 54 | /** | |
| 55 | * Finds a menu matching the given item in all available shells. If recursive is set, it will attempt to find the | |
| 56 | * controls recursively in each of the menus it that is found. | |
| 57 | * | |
| 58 | * @param matcher the matcher that can match menus and menu items. | |
| 59 | * @return all menus in all shells that match the matcher. | |
| 60 | */ | |
| 61 | public List<MenuItem> findMenus(Matcher<MenuItem> matcher) { | |
| 62 | 22 | return findMenus(getShells(), matcher, true); |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Finds all the menus using the given matcher in the set of shells provided. If recursive is set, it will attempt | |
| 67 | * to find the controls recursively in each of the menus it that is found. | |
| 68 | * | |
| 69 | * @param shells the shells to probe for menus. | |
| 70 | * @param matcher the matcher that can match menus and menu items. | |
| 71 | * @param recursive if set to true, will find sub-menus as well. | |
| 72 | * @return all menus in the specified shells that match the matcher. | |
| 73 | */ | |
| 74 | public List<MenuItem> findMenus(Shell[] shells, Matcher<MenuItem> matcher, boolean recursive) { | |
| 75 | 22 | LinkedHashSet<MenuItem> result = new LinkedHashSet<MenuItem>(); |
| 76 | 212 | for (Shell shell : shells) |
| 77 | 190 | result.addAll(findMenus(shell, matcher, recursive)); |
| 78 | 22 | return new ArrayList<MenuItem>(result); |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Finds the menus in the given shell using the given matcher. If recursive is set, it will attempt to find the | |
| 83 | * controls recursively in each of the menus it that is found. | |
| 84 | * | |
| 85 | * @param shell the shell to probe for menus. | |
| 86 | * @param matcher the matcher that can match menus and menu items. | |
| 87 | * @param recursive if set to true, will find sub-menus as well. | |
| 88 | * @return all menus in the specified shell that match the matcher. | |
| 89 | */ | |
| 90 | public List<MenuItem> findMenus(final Shell shell, Matcher<MenuItem> matcher, boolean recursive) { | |
| 91 | 216 | LinkedHashSet<MenuItem> result = new LinkedHashSet<MenuItem>(); |
| 92 | 216 | result.addAll(findMenus(menuBar(shell), matcher, recursive)); |
| 93 | 216 | return new ArrayList<MenuItem>(result); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Gets the menu bar in the given shell. | |
| 98 | * | |
| 99 | * @param shell the shell. | |
| 100 | * @return the menu in the shell. | |
| 101 | * @see Shell#getMenuBar() | |
| 102 | */ | |
| 103 | protected Menu menuBar(final Shell shell) { | |
| 104 | 47 | return UIThreadRunnable.syncExec(display, new WidgetResult<Menu>() { |
| 105 | public Menu run() { | |
| 106 | 47 | return shell.getMenuBar(); |
| 107 | } | |
| 108 | }); | |
| 109 | ||
| 110 | } | |
| 111 | ||
| 112 | /** | |
| 113 | * Finds all the menus in the given menu bar matching the given matcher. If recursive is set, it will attempt to | |
| 114 | * find the controls recursively in each of the menus it that is found. | |
| 115 | * | |
| 116 | * @param bar the menu bar | |
| 117 | * @param matcher the matcher that can match menus and menu items. | |
| 118 | * @param recursive if set to true, will find sub-menus as well. | |
| 119 | * @return all menus in the specified menubar that match the matcher. | |
| 120 | */ | |
| 121 | public List<MenuItem> findMenus(final Menu bar, final Matcher<MenuItem> matcher, final boolean recursive) { | |
| 122 | 220 | return UIThreadRunnable.syncExec(display, new ListResult<MenuItem>() { |
| 123 | public List<MenuItem> run() { | |
| 124 | 220 | return findMenusInternal(bar, matcher, recursive); |
| 125 | } | |
| 126 | }); | |
| 127 | } | |
| 128 | ||
| 129 | /** | |
| 130 | * Gets all of the shells in the current display. | |
| 131 | * | |
| 132 | * @return all shells in the display. | |
| 133 | * @see Display#getShells() | |
| 134 | */ | |
| 135 | protected Shell[] getShells() { | |
| 136 | 22 | return UIThreadRunnable.syncExec(display, new ArrayResult<Shell>() { |
| 137 | public Shell[] run() { | |
| 138 | 22 | return display.getShells(); |
| 139 | } | |
| 140 | }); | |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * @param bar | |
| 145 | * @param matcher | |
| 146 | * @param recursive | |
| 147 | * @return | |
| 148 | */ | |
| 149 | 220 | private List<MenuItem> findMenusInternal(final Menu bar, final Matcher<MenuItem> matcher, final boolean recursive) { |
| 150 | 2249 | LinkedHashSet<MenuItem> result = new LinkedHashSet<MenuItem>(); |
| 151 | 2249 | if (bar != null) { |
| 152 | 456 | bar.notifyListeners(SWT.Show, new Event()); |
| 153 | 456 | MenuItem[] items = bar.getItems(); |
| 154 | 2894 | for (MenuItem menuItem : items) { |
| 155 | 2438 | if (isSeparator(menuItem)) { |
| 156 | 409 | continue; |
| 157 | } | |
| 158 | 2029 | if (matcher.matches(menuItem)) |
| 159 | 485 | result.add(menuItem); |
| 160 | 2029 | if (recursive) |
| 161 | 2029 | result.addAll(findMenusInternal(menuItem.getMenu(), matcher, recursive)); |
| 162 | } | |
| 163 | 456 | bar.notifyListeners(SWT.Hide, new Event()); |
| 164 | } | |
| 165 | 2249 | return new ArrayList<MenuItem>(result); |
| 166 | } | |
| 167 | ||
| 168 | private boolean isSeparator(MenuItem menuItem) { | |
| 169 | // FIXME see https://bugs.eclipse.org/bugs/show_bug.cgi?id=208188 | |
| 170 | // FIXED > 20071101 https://bugs.eclipse.org/bugs/show_bug.cgi?id=208188#c2 | |
| 171 | 2438 | return (menuItem.getStyle() & SWT.SEPARATOR) != 0; |
| 172 | } | |
| 173 | ||
| 174 | } |