1 | 4 | |
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 | 1 | public class ViewMenuFinder { |
37 | |
|
38 | |
|
39 | |
|
40 | 1 | private static final Logger log = Logger.getLogger(ViewMenuFinder.class); |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 18 | public ViewMenuFinder() { |
46 | |
|
47 | 18 | } |
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 | 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 | |
|
74 | |
|
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 | |
|
84 | 0 | MenuManager menuManager = (MenuManager) item; |
85 | |
|
86 | 0 | l.addAll(getMenuItemsInternal(menuManager.getItems(), matcher, recursive)); |
87 | 12 | } else if (item instanceof ActionContributionItem) { |
88 | |
|
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 | |
} |