Coverage Report - org.eclipse.swtbot.eclipse.finder.widgets.SWTBotCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotCommand
64%
9/14
50%
1/2
2.167
SWTBotCommand$1
66%
4/6
N/A
2.167
 
 1  2
 /*******************************************************************************
 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.widgets;
 12  
 
 13  
 import org.eclipse.core.commands.Command;
 14  
 import org.eclipse.core.commands.common.NotDefinedException;
 15  
 import org.eclipse.core.runtime.AssertionFailedException;
 16  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 17  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 18  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 19  
 import org.eclipse.ui.PlatformUI;
 20  
 import org.eclipse.ui.handlers.IHandlerService;
 21  
 
 22  
 /**
 23  
  * A SWTBotViewMenu represents a menu item within a view's menu.
 24  
  *
 25  
  * @author @author Stephen Paulin <paulin [at] spextreme [dot] com>
 26  
  * @version $Id$
 27  
  * @since 1.2
 28  
  */
 29  
 public class SWTBotCommand extends SWTBotViewMenu {
 30  4
         private IHandlerService        handlerService        = null;
 31  
 
 32  
         /**
 33  
          * Constructs a SWTBot command item.
 34  
          *
 35  
          * @param command The command item.
 36  
          * @throws WidgetNotFoundException Thrown if both values are <code>null</code>.
 37  
          * @throws AssertionFailedException If the contribution item is <code>null</code>.
 38  
          */
 39  
         public SWTBotCommand(Command command) throws WidgetNotFoundException {
 40  2
                 super(command);
 41  
 
 42  2
                 handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
 43  2
         }
 44  
 
 45  
         /**
 46  
          * Simulates the click action of the menu.
 47  
          *
 48  
          * @throws WidgetNotFoundException Thrown if the action or command id are not valid.
 49  
          */
 50  
         public void click() throws WidgetNotFoundException {
 51  2
                 if (commandID != null) {
 52  2
                         menuClickResult = null;
 53  
 
 54  2
                         UIThreadRunnable.asyncExec(new VoidResult() {
 55  
                                 public void run() {
 56  
                                         try {
 57  2
                                                 menuClickResult = handlerService.executeCommand(commandID, null);
 58  0
                                         } catch (Exception e) {
 59  0
                                                 throw new RuntimeException("Failed to execute the command - " + commandID, e); //$NON-NLS-1$
 60  
                                         }
 61  2
                                 }
 62  
                         });
 63  
                 } else
 64  0
                         throw new WidgetNotFoundException("The command to could not be execute due to the lack of an ID."); //$NON-NLS-1$
 65  2
         }
 66  
 
 67  
         /**
 68  
          * After a click completes, this may be use to access the results returned by the command. If a click had not
 69  
          * previously been done then this value will be <code>null</code>.
 70  
          *
 71  
          * @return The object data from the click or <code>null</code> if a click never occurred.
 72  
          */
 73  
         public Object getClickResult() {
 74  0
                 return menuClickResult;
 75  
         }
 76  
 
 77  
         /**
 78  
          * Gets the text name for this item.
 79  
          *
 80  
          * @return The text name of this item.
 81  
          * @throws WidgetNotFoundException Thrown if the command name has not been defined.
 82  
          */
 83  
         public String getText() throws WidgetNotFoundException {
 84  
                 try {
 85  1
                         return cmdItem.getName();
 86  0
                 } catch (NotDefinedException e) {
 87  0
                         throw new WidgetNotFoundException(e.getMessage());
 88  
                 }
 89  
         }
 90  
 
 91  
         /**
 92  
          * Gets if the command is enabled.
 93  
          *
 94  
          * @return <code>true</code> if enabled. Otherwise <code>false</code>.
 95  
          */
 96  
         public boolean isEnabled() {
 97  0
                 return cmdItem.isEnabled();
 98  
         }
 99  
 }