| 1 | 46 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 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.results.WidgetResult; |
| 23 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
| 24 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
| 25 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
| 26 | |
import org.hamcrest.SelfDescribing; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
@SWTBotWidget(clasz = Button.class, style = @Style(name = "SWT.RADIO", value = SWT.RADIO), preferredName = "radio", referenceBy = { ReferenceBy.LABEL, ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP }) |
| 38 | |
public class SWTBotRadio extends AbstractSWTBotControl<Button> { |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public SWTBotRadio(Button w) throws WidgetNotFoundException { |
| 47 | 22 | this(w, null); |
| 48 | 22 | } |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public SWTBotRadio(Button w, SelfDescribing description) throws WidgetNotFoundException { |
| 58 | 96 | super(w, description); |
| 59 | 96 | Assert.isTrue(SWTUtils.hasStyle(w, SWT.RADIO), "Expecting a radio."); |
| 60 | 96 | } |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public SWTBotRadio click() { |
| 66 | 64 | if (isSelected()) { |
| 67 | 41 | log.debug(MessageFormat.format("Widget {0} is already selected, not clicking again.", this)); |
| 68 | 41 | return this; |
| 69 | |
} |
| 70 | 23 | waitForEnabled(); |
| 71 | |
|
| 72 | 23 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
| 73 | |
|
| 74 | 23 | final SWTBotRadio otherSelectedButton = otherSelectedButton(); |
| 75 | |
|
| 76 | 23 | if (otherSelectedButton != null) { |
| 77 | 22 | otherSelectedButton.notify(SWT.Deactivate); |
| 78 | 22 | asyncExec(new VoidResult() { |
| 79 | |
public void run() { |
| 80 | 22 | otherSelectedButton.widget.setSelection(false); |
| 81 | 22 | } |
| 82 | |
}); |
| 83 | |
} |
| 84 | |
|
| 85 | 23 | notify(SWT.Activate); |
| 86 | 23 | notify(SWT.MouseDown, createMouseEvent(0, 0, 1, 0, 1)); |
| 87 | 23 | notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1)); |
| 88 | 23 | asyncExec(new VoidResult() { |
| 89 | |
public void run() { |
| 90 | 23 | widget.setSelection(true); |
| 91 | 23 | } |
| 92 | |
}); |
| 93 | 23 | notify(SWT.Selection); |
| 94 | 23 | if (otherSelectedButton != null) { |
| 95 | 22 | otherSelectedButton.notify(SWT.Selection); |
| 96 | |
} |
| 97 | 23 | log.debug(MessageFormat.format("Clicked on {0}", this)); |
| 98 | 23 | return this; |
| 99 | |
} |
| 100 | |
|
| 101 | |
private SWTBotRadio otherSelectedButton() { |
| 102 | 23 | Button button = syncExec(new WidgetResult<Button>() { |
| 103 | |
public Button run() { |
| 104 | 23 | if (hasStyle(widget.getParent(), SWT.NO_RADIO_GROUP)) |
| 105 | 0 | return null; |
| 106 | 23 | Widget[] siblings = SWTUtils.siblings(widget); |
| 107 | 45 | for (Widget widget : siblings) { |
| 108 | 44 | if ((widget instanceof Button) && hasStyle(widget, SWT.RADIO)) |
| 109 | 44 | if (((Button) widget).getSelection()) |
| 110 | 22 | return (Button) widget; |
| 111 | |
} |
| 112 | 1 | return null; |
| 113 | |
} |
| 114 | |
}); |
| 115 | |
|
| 116 | 23 | if (button != null) |
| 117 | 22 | return new SWTBotRadio(button); |
| 118 | 1 | return null; |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public boolean isSelected() { |
| 127 | 70 | return syncExec(new BoolResult() { |
| 128 | |
public Boolean run() { |
| 129 | 70 | return widget.getSelection(); |
| 130 | |
} |
| 131 | |
}); |
| 132 | |
} |
| 133 | |
} |