1 | 8 | |
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.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 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 1 | public class ViewMenuFinder { |
40 | |
|
41 | |
|
42 | |
|
43 | 1 | private static final Logger log = Logger.getLogger(ViewMenuFinder.class); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 18 | public ViewMenuFinder() { |
49 | |
|
50 | 18 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
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 | |
|
77 | |
|
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 | |
|
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 | |
|
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 | |
|
130 | |
|
131 | |
@SuppressWarnings("unused") |
132 | |
public String getText() throws Exception { |
133 | |
|
134 | 9 | String label = (String) SWTUtils.getAttribute(wrappedCommandItem, "label"); |
135 | 9 | return label != null ? label:""; |
136 | |
} |
137 | |
} |
138 | |
} |