1 | 120 | |
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.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 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
@SWTBotWidget(clasz = Button.class, style = @Style(name = "SWT.RADIO", value = SWT.RADIO), preferredName = "radio", referenceBy = { ReferenceBy.LABEL, ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP }) |
37 | |
public class SWTBotRadio extends AbstractSWTBotControl<Button> { |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public SWTBotRadio(Button w) throws WidgetNotFoundException { |
46 | 0 | this(w, null); |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
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."); |
59 | 64 | } |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public SWTBotRadio click() { |
65 | 54 | if (isSelected()) { |
66 | 37 | log.debug(MessageFormat.format("Widget {0} is already selected, not clicking again.", this)); |
67 | 37 | return this; |
68 | |
} |
69 | 17 | waitForEnabled(); |
70 | 17 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
71 | 17 | asyncExec(new VoidResult() { |
72 | |
public void run() { |
73 | 17 | deselectOtherRadioButtons(); |
74 | 17 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
75 | 17 | widget.setSelection(true); |
76 | 17 | } |
77 | |
|
78 | |
|
79 | |
|
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)); |
104 | 17 | return this; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public boolean isSelected() { |
113 | 60 | return syncExec(new BoolResult() { |
114 | |
public Boolean run() { |
115 | 60 | return widget.getSelection(); |
116 | |
} |
117 | |
}); |
118 | |
} |
119 | |
} |