| 1 | 0 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 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 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 0 | public class ViewMenuFinder { |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 0 | private static final Logger log = Logger.getLogger(ViewMenuFinder.class); |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public ViewMenuFinder() { |
| 46 | |
|
| 47 | 0 | } |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public List<SWTBotViewMenu> findMenus(final IViewReference view, final Matcher<?> matcher, final boolean recursive) { |
| 59 | 0 | return UIThreadRunnable.syncExec(new ListResult<SWTBotViewMenu>() { |
| 60 | |
|
| 61 | |
public List<SWTBotViewMenu> run() { |
| 62 | 0 | ViewPane viewPane = (ViewPane) ((WorkbenchPartReference) view).getPane(); |
| 63 | 0 | MenuManager mgr = viewPane.getMenuManager(); |
| 64 | 0 | List<SWTBotViewMenu> l = new ArrayList<SWTBotViewMenu>(); |
| 65 | |
|
| 66 | 0 | l.addAll(getMenuItemsInternal(mgr.getItems(), matcher, recursive)); |
| 67 | |
|
| 68 | 0 | return l; |
| 69 | |
} |
| 70 | |
}); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 0 | private List<SWTBotViewMenu> getMenuItemsInternal(IContributionItem[] items, Matcher<?> matcher, boolean recursive) { |
| 76 | 0 | List<SWTBotViewMenu> l = new ArrayList<SWTBotViewMenu>(); |
| 77 | |
|
| 78 | 0 | for (int i = 0; i < items.length; i++) { |
| 79 | 0 | IContributionItem item = items[i]; |
| 80 | |
|
| 81 | |
try { |
| 82 | 0 | if ((item instanceof MenuManager) && recursive) { |
| 83 | |
|
| 84 | 0 | MenuManager menuManager = (MenuManager) item; |
| 85 | |
|
| 86 | 0 | l.addAll(getMenuItemsInternal(menuManager.getItems(), matcher, recursive)); |
| 87 | 0 | } else if (item instanceof ActionContributionItem) { |
| 88 | |
|
| 89 | 0 | ActionContributionItem actionContribution = (ActionContributionItem) item; |
| 90 | |
|
| 91 | 0 | if (matcher.matches(actionContribution.getAction())) |
| 92 | 0 | l.add(new SWTBotViewMenu(actionContribution)); |
| 93 | |
} |
| 94 | 0 | } catch (WidgetNotFoundException e) { |
| 95 | 0 | log.warn(e); |
| 96 | |
} |
| 97 | |
} |
| 98 | |
|
| 99 | 0 | return l; |
| 100 | |
} |
| 101 | |
} |