Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CommandFinder |
|
| 1.6;1.6 | ||||
CommandFinder$1 |
|
| 1.6;1.6 |
1 | 6 | /******************************************************************************* |
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.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.core.commands.Command; | |
18 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotCommand; | |
19 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
20 | import org.eclipse.swtbot.swt.finder.results.ListResult; | |
21 | import org.eclipse.ui.PlatformUI; | |
22 | import org.eclipse.ui.commands.ICommandService; | |
23 | import org.hamcrest.Matcher; | |
24 | ||
25 | /** | |
26 | * Finds all the contribution items within the application. | |
27 | * | |
28 | * @author @author Stephen Paulin <paulin [at] spextreme [dot] com> | |
29 | * @version $Id$ | |
30 | * @since 1.2 | |
31 | */ | |
32 | 1 | public class CommandFinder { |
33 | /** | |
34 | * The logging instance for this class. | |
35 | */ | |
36 | 1 | private static final Logger log = Logger.getLogger(CommandFinder.class); |
37 | ||
38 | /** | |
39 | * Creates a CommandFinder. | |
40 | */ | |
41 | 3 | public CommandFinder() { |
42 | // Do nothing. | |
43 | 3 | } |
44 | ||
45 | /** | |
46 | * Finds a command matching the given item. | |
47 | * | |
48 | * @param matcher the matcher that can match commands. | |
49 | * @return all command that match the matcher. | |
50 | */ | |
51 | public List<SWTBotCommand> findCommand(Matcher<?> matcher) { | |
52 | 3 | return findCommand(getCommandService(), matcher); |
53 | } | |
54 | ||
55 | /** | |
56 | * Gets a list of all command matching the matcher. | |
57 | * | |
58 | * @param service the {@link ICommandService} instance to use. | |
59 | * @param matcher the matcher that can match the command item. | |
60 | * @return The list of {@link Command}s that match the matcher. | |
61 | */ | |
62 | public List<SWTBotCommand> findCommand(final ICommandService service, final Matcher<?> matcher) { | |
63 | 3 | return UIThreadRunnable.syncExec(new ListResult<SWTBotCommand>() { |
64 | ||
65 | public List<SWTBotCommand> run() { | |
66 | 3 | List<SWTBotCommand> l = new ArrayList<SWTBotCommand>(); |
67 | 3 | Command[] commands = service.getDefinedCommands(); |
68 | ||
69 | 2139 | for (int i = 0; i < commands.length; i++) |
70 | try { | |
71 | 2136 | String name = commands[i].getName(); |
72 | 2136 | if (matcher.matches(name)) |
73 | 2 | l.add(new SWTBotCommand(commands[i])); |
74 | 0 | } catch (Exception e) { |
75 | 0 | log.error("Failed with an exception on the command: " + commands[i].toString(), e); //$NON-NLS-1$ |
76 | } | |
77 | ||
78 | 3 | return l; |
79 | } | |
80 | }); | |
81 | } | |
82 | ||
83 | /** | |
84 | * Gets the command service. | |
85 | * | |
86 | * @return The {@link ICommandService}. | |
87 | */ | |
88 | protected ICommandService getCommandService() { | |
89 | 3 | return (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); |
90 | } | |
91 | } |