Coverage Report - org.eclipse.swtbot.eclipse.finder.widgets.SWTBotViewMenu
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotViewMenu
69%
25/36
37%
3/8
2.125
SWTBotViewMenu$1
0%
0/6
N/A
2.125
SWTBotViewMenu$2
100%
4/4
N/A
2.125
 
 1  1
 /*******************************************************************************
 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.Assert;
 16  
 import org.eclipse.core.runtime.AssertionFailedException;
 17  
 import org.eclipse.jface.action.ActionContributionItem;
 18  
 import org.eclipse.jface.action.IAction;
 19  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 20  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 21  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 22  
 import org.eclipse.ui.PlatformUI;
 23  
 import org.eclipse.ui.handlers.IHandlerService;
 24  
 
 25  
 /**
 26  
  * A SWTBotViewMenu represents a menu item within a view's menu.
 27  
  *
 28  
  * @author @author Stephen Paulin <paulin [at] spextreme [dot] com>
 29  
  * @version $Id$
 30  
  * @since 1.2
 31  
  */
 32  
 public class SWTBotViewMenu {
 33  4
         private IAction                                        action                        = null;
 34  3
         private ActionContributionItem        actionItem                = null;
 35  3
         private String                                        text                        = null;
 36  
         /**
 37  
          * Holds the results of the click action.
 38  
          */
 39  3
         protected Object                                menuClickResult        = null;
 40  
         /**
 41  
          * Holds command if setup.
 42  
          */
 43  3
         protected Command                                cmdItem                        = null;
 44  
         /**
 45  
          * Holds the id of the command if one exists.
 46  
          */
 47  3
         protected String                                commandID                = null;
 48  
 
 49  
         /**
 50  
          * Constructs a SWTBot View Menu item.
 51  
          *
 52  
          * @param commandItem The command contribution item.
 53  
          * @throws WidgetNotFoundException Thrown if both values are <code>null</code>.
 54  
          * @throws AssertionFailedException If the contribution item is <code>null</code>.
 55  
          */
 56  2
         public SWTBotViewMenu(Command commandItem) throws WidgetNotFoundException {
 57  2
                 cmdItem = commandItem;
 58  
 
 59  2
                 commandID = cmdItem.getId();
 60  
                 try {
 61  2
                         text = cmdItem.getName();
 62  0
                 } catch (NotDefinedException e) {
 63  0
                         text = ""; //$NON-NLS-1$
 64  
                 }
 65  2
         }
 66  
 
 67  
         /**
 68  
          * Constructs a SWTBot View Menu item.
 69  
          *
 70  
          * @param contributionItem The action contribution item.
 71  
          * @throws WidgetNotFoundException Thrown if both values are <code>null</code>.
 72  
          * @throws AssertionFailedException If the contribution item is <code>null</code>.
 73  
          */
 74  1
         public SWTBotViewMenu(ActionContributionItem contributionItem) throws WidgetNotFoundException {
 75  1
                 Assert.isNotNull(contributionItem);
 76  1
                 Assert.isNotNull(contributionItem.getAction());
 77  
 
 78  1
                 actionItem = contributionItem;
 79  1
                 action = actionItem.getAction();
 80  1
                 commandID = actionItem.getId();
 81  1
                 text = actionItem.getAction().getText();
 82  
 
 83  1
                 if (commandID == null)
 84  1
                         commandID = actionItem.getAction().getActionDefinitionId();
 85  1
         }
 86  
 
 87  
         /**
 88  
          * Simulates the click action of the menu.
 89  
          *
 90  
          * @throws WidgetNotFoundException Thrown if the action or command id are not valid.
 91  
          */
 92  
         public void click() throws WidgetNotFoundException {
 93  1
                 if (commandID != null) {
 94  0
                         menuClickResult = null;
 95  0
                         final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
 96  
 
 97  0
                         UIThreadRunnable.asyncExec(new VoidResult() {
 98  
                                 public void run() {
 99  
                                         try {
 100  0
                                                 menuClickResult = handlerService.executeCommand(commandID, null);
 101  0
                                         } catch (Exception e) {
 102  0
                                                 throw new RuntimeException("Failed to execute the command - " + commandID, e); //$NON-NLS-1$
 103  
                                         }
 104  0
                                 }
 105  
                         });
 106  1
                 } else if (action != null)
 107  1
                         UIThreadRunnable.asyncExec(new VoidResult() {
 108  
                                 public void run() {
 109  1
                                         action.run();
 110  1
                                 }
 111  
                         });
 112  
                 else
 113  0
                         throw new WidgetNotFoundException("There is no action or contribution id to execute."); //$NON-NLS-1$
 114  1
         }
 115  
 
 116  
         /**
 117  
          * After a click completes, this may be use to access the results returned by the command. If a click had not
 118  
          * previously been done then this value will be <code>null</code>.
 119  
          *
 120  
          * @return The object data from the click or <code>null</code> if a click never occurred.
 121  
          */
 122  
         public Object getClickResult() {
 123  0
                 return menuClickResult;
 124  
         }
 125  
 
 126  
         /**
 127  
          * GEts the text label for the menu item.
 128  
          *
 129  
          * @return The text label.
 130  
          * @throws WidgetNotFoundException Thrown if the action is <code>null</code>.
 131  
          */
 132  
         public String getText() throws WidgetNotFoundException {
 133  0
                 return text;
 134  
         }
 135  
 
 136  
         /**
 137  
          * Gets if the menu item is checked (has a check mark next to it).
 138  
          *
 139  
          * @return <code>true</code> if checked. Otherwise <code>false</code>.
 140  
          */
 141  
         public boolean isChecked() {
 142  0
                 if (action != null)
 143  0
                         return action.isChecked();
 144  
 
 145  
                 // FIXME This needs to find if a contribution item (Command) has been checked...
 146  0
                 return false;
 147  
         }
 148  
 }