Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EventContextMenuFinder |
|
| 1.625;1.625 | ||||
EventContextMenuFinder$1 |
|
| 1.625;1.625 | ||||
EventContextMenuFinder$2 |
|
| 1.625;1.625 | ||||
EventContextMenuFinder$ShowHideListener |
|
| 1.625;1.625 |
1 | 0 | /******************************************************************************* |
2 | * Copyright (c) 2008 Cedric Chabanois 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 | * Cedric Chabanois - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.swtbot.swt.finder.finders; | |
12 | ||
13 | ||
14 | import org.eclipse.swt.SWT; | |
15 | import org.eclipse.swt.widgets.Display; | |
16 | import org.eclipse.swt.widgets.Event; | |
17 | import org.eclipse.swt.widgets.Listener; | |
18 | import org.eclipse.swt.widgets.Menu; | |
19 | import org.eclipse.swt.widgets.Shell; | |
20 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
21 | import org.eclipse.swtbot.swt.finder.utils.SWTUtils; | |
22 | import org.eclipse.swtbot.swt.finder.utils.internal.Assert; | |
23 | ||
24 | /** | |
25 | * Context menu finder that uses events to get the current context menu. It must be used instead of ContextMenuFinder | |
26 | * when the context menu is not associated with a Widget. It must be registered before the context menu appears. | |
27 | * <p> | |
28 | * Here is a sample usage: | |
29 | * </p> | |
30 | * | |
31 | * <pre> | |
32 | * EventContextMenuFinder eventContextMenuFinder = new EventContextMenuFinder(); | |
33 | * try { | |
34 | * eventContextMenuFinder.register(); | |
35 | * button.click(); // a popup menu appears below the button | |
36 | * SWTBotMenu menu = new SWTBotMenu(new Finder(new ControlFinder(), eventContextMenuFinder), "Menu Text"); | |
37 | * menu.click(); | |
38 | * } finally { | |
39 | * eventContextMenuFinder.unregister(); | |
40 | * } | |
41 | * </pre> | |
42 | * | |
43 | * This is not convenient to use but the need for this is not so frequent. In case you have better idea on the | |
44 | * implementation or usage please add a comment to <a href="http://swtbot.org/bugzilla/show_bug.cgi?id=19">this bug</a>. | |
45 | * | |
46 | * @author Cedric Chabanois <cchabanois [at] no-log [dot] org> | |
47 | * @version $Id$ | |
48 | * @since 1.0 | |
49 | */ | |
50 | public class EventContextMenuFinder extends MenuFinder { | |
51 | /** | |
52 | * The current context menu. | |
53 | */ | |
54 | 0 | private Menu currentContextMenu = null; |
55 | /** | |
56 | * The listener to be used. | |
57 | */ | |
58 | 0 | private final ShowHideListener showHideListener; |
59 | /** | |
60 | * The display to use. | |
61 | */ | |
62 | 0 | private final Display display; |
63 | ||
64 | /** | |
65 | * Creates an event based context menu finder. | |
66 | * | |
67 | * @param display the display | |
68 | * @throws NullPointerException Thrown if the display is <code>null</code>. | |
69 | */ | |
70 | 0 | public EventContextMenuFinder(Display display) { |
71 | 0 | Assert.isNotNull(display, "The display can not be null"); //$NON-NLS-1$ |
72 | 0 | this.display = display; |
73 | 0 | showHideListener = new ShowHideListener(); |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * Creates an event based context menu finder. | |
78 | */ | |
79 | public EventContextMenuFinder() { | |
80 | 0 | this(SWTUtils.display()); |
81 | 0 | } |
82 | ||
83 | /** | |
84 | * Registers this finder so that it may start 'looking for' controls. It does so by listening for {@link SWT#Show} | |
85 | * and {@link SWT#Hide} events on menus. | |
86 | */ | |
87 | public void register() { | |
88 | 0 | UIThreadRunnable.syncExec(display, new VoidResult() { |
89 | public void run() { | |
90 | 0 | display.addFilter(SWT.Show, showHideListener); |
91 | 0 | display.addFilter(SWT.Hide, showHideListener); |
92 | 0 | } |
93 | }); | |
94 | 0 | } |
95 | ||
96 | /** | |
97 | * Unregisters this finder so that it may stop 'looking for' controls. It does so by listening for {@link SWT#Show} | |
98 | * and {@link SWT#Hide} events on menus. | |
99 | */ | |
100 | public void unregister() { | |
101 | 0 | UIThreadRunnable.syncExec(display, new VoidResult() { |
102 | public void run() { | |
103 | 0 | display.removeFilter(SWT.Show, showHideListener); |
104 | 0 | display.removeFilter(SWT.Hide, showHideListener); |
105 | 0 | } |
106 | }); | |
107 | 0 | } |
108 | ||
109 | /** | |
110 | * Gets the menu bar that has been found. This may be <code>null</code> if one had not been found yet. | |
111 | * | |
112 | * @see org.eclipse.swtbot.swt.finder.finders.MenuFinder#menuBar(org.eclipse.swt.widgets.Shell) | |
113 | * @param shell This is not used. | |
114 | * @return The menu or <code>null</code> if not yet found. | |
115 | */ | |
116 | @Override | |
117 | protected Menu menuBar(final Shell shell) { | |
118 | 0 | return currentContextMenu; |
119 | } | |
120 | ||
121 | /** | |
122 | * A private class to listen for the show/hide events. | |
123 | */ | |
124 | 0 | private class ShowHideListener implements Listener { |
125 | /** | |
126 | * Handles the event by checking if it is the proper event. If it is a show, then the current context menu is | |
127 | * set. Otherwise it will be set to <code>null</code> if it is a hide event. | |
128 | * | |
129 | * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) | |
130 | * @param event the event to check. | |
131 | */ | |
132 | public void handleEvent(Event event) { | |
133 | 0 | if (!(event.widget instanceof Menu)) |
134 | 0 | return; |
135 | 0 | Menu menu = (Menu) event.widget; |
136 | 0 | if (SWTUtils.hasStyle(menu, SWT.POP_UP)) { |
137 | 0 | if (event.type == SWT.Show) |
138 | 0 | currentContextMenu = menu; |
139 | 0 | if (event.type == SWT.Hide) |
140 | 0 | currentContextMenu = null; |
141 | } | |
142 | 0 | } |
143 | } | |
144 | ||
145 | } |