Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotRadio
88%
24/27
100%
2/2
2
SWTBotRadio$1
92%
12/13
75%
6/8
2
SWTBotRadio$2
100%
3/3
N/A
2
 
 1  120
 /*******************************************************************************
 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.swt.finder.widgets;
 12  
 
 13  
 import org.eclipse.swt.SWT;
 14  
 import org.eclipse.swt.widgets.Button;
 15  
 import org.eclipse.swt.widgets.Widget;
 16  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 17  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 18  
 import org.eclipse.swtbot.swt.finder.Style;
 19  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 20  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 21  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 22  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 23  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 24  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 25  
 import org.hamcrest.SelfDescribing;
 26  
 
 27  
 /**
 28  
  * Represents a radio {@link Button} of type {@link SWT#RADIO}.
 29  
  *
 30  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 31  
  * @version $Id$
 32  
  * @see SWTBotButton
 33  
  * @see SWTBotCheckBox
 34  
  * @see SWTBotToggleButton
 35  
  */
 36  
 @SWTBotWidget(clasz = Button.class, style = @Style(name = "SWT.RADIO", value = SWT.RADIO), preferredName = "radio", referenceBy = { ReferenceBy.LABEL, ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP })//$NON-NLS-1$
 37  
 public class SWTBotRadio extends AbstractSWTBotControl<Button> {
 38  
 
 39  
         /**
 40  
          * Constructs an instance of this with the given widget.
 41  
          *
 42  
          * @param w the widget.
 43  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 44  
          */
 45  
         public SWTBotRadio(Button w) throws WidgetNotFoundException {
 46  0
                 this(w, null);
 47  0
         }
 48  
 
 49  
         /**
 50  
          * Constructs an instance of this with the given widget.
 51  
          *
 52  
          * @param w the widget.
 53  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 54  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 55  
          */
 56  
         public SWTBotRadio(Button w, SelfDescribing description) throws WidgetNotFoundException {
 57  64
                 super(w, description);
 58  64
                 Assert.isTrue(SWTUtils.hasStyle(w, SWT.RADIO), "Expecting a radio."); //$NON-NLS-1$
 59  64
         }
 60  
 
 61  
         /**
 62  
          * Selects the radio button.
 63  
          */
 64  
         public SWTBotRadio click() {
 65  54
                 if (isSelected()) {
 66  37
                         log.debug(MessageFormat.format("Widget {0} is already selected, not clicking again.", this)); //$NON-NLS-1$
 67  37
                         return this;
 68  
                 }
 69  17
                 waitForEnabled();
 70  17
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 71  17
                 asyncExec(new VoidResult() {
 72  
                         public void run() {
 73  17
                                 deselectOtherRadioButtons();
 74  17
                                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 75  17
                                 widget.setSelection(true);
 76  17
                         }
 77  
 
 78  
                         /**
 79  
                          * @see "http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet224.java?view=co"
 80  
                          */
 81  
                         private void deselectOtherRadioButtons() {
 82  17
                                 if (hasStyle(widget.getParent(), SWT.NO_RADIO_GROUP))
 83  0
                                         return;
 84  17
                                 Widget[] siblings = SWTUtils.siblings(widget);
 85  119
                                 for (Widget widget : siblings) {
 86  102
                                         if ((widget instanceof Button) && hasStyle(widget, SWT.RADIO))
 87  62
                                                 ((Button) widget).setSelection(false);
 88  
                                 }
 89  17
                         }
 90  
                 });
 91  17
                 notify(SWT.MouseEnter);
 92  17
                 notify(SWT.MouseMove);
 93  17
                 notify(SWT.Activate);
 94  17
                 notify(SWT.FocusIn);
 95  17
                 notify(SWT.MouseDown);
 96  17
                 notify(SWT.MouseUp);
 97  17
                 notify(SWT.Selection);
 98  17
                 notify(SWT.MouseHover);
 99  17
                 notify(SWT.MouseMove);
 100  17
                 notify(SWT.MouseExit);
 101  17
                 notify(SWT.Deactivate);
 102  17
                 notify(SWT.FocusOut);
 103  17
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 104  17
                 return this;
 105  
         }
 106  
 
 107  
         /**
 108  
          * Checks if the item is selected.
 109  
          *
 110  
          * @return <code>true</code> if the radio button is selected. Otherwise <code>false</code>.
 111  
          */
 112  
         public boolean isSelected() {
 113  60
                 return syncExec(new BoolResult() {
 114  
                         public Boolean run() {
 115  60
                                 return widget.getSelection();
 116  
                         }
 117  
                 });
 118  
         }
 119  
 }