1 | 66 | |
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 | 31 | this(w, null); |
48 | 31 | } |
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 | 55 | if (isSelected()) { |
67 | 22 | log.debug(MessageFormat.format("Widget {0} is already selected, not clicking again.", this)); |
68 | 22 | return this; |
69 | |
} |
70 | 33 | waitForEnabled(); |
71 | |
|
72 | 33 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
73 | |
|
74 | 33 | final SWTBotRadio otherSelectedButton = otherSelectedButton(); |
75 | |
|
76 | 33 | if (otherSelectedButton != null) { |
77 | 31 | otherSelectedButton.notify(SWT.Deactivate); |
78 | 31 | asyncExec(new VoidResult() { |
79 | |
public void run() { |
80 | 31 | otherSelectedButton.widget.setSelection(false); |
81 | 31 | } |
82 | |
}); |
83 | |
} |
84 | |
|
85 | 33 | notify(SWT.Activate); |
86 | 33 | notify(SWT.MouseDown, createMouseEvent(0, 0, 1, 0, 1)); |
87 | 33 | notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1)); |
88 | 33 | asyncExec(new VoidResult() { |
89 | |
public void run() { |
90 | 33 | widget.setSelection(true); |
91 | 33 | } |
92 | |
}); |
93 | 33 | notify(SWT.Selection); |
94 | 33 | if (otherSelectedButton != null) { |
95 | 31 | otherSelectedButton.notify(SWT.Selection); |
96 | |
} |
97 | 33 | log.debug(MessageFormat.format("Clicked on {0}", this)); |
98 | 33 | return this; |
99 | |
} |
100 | |
|
101 | |
private SWTBotRadio otherSelectedButton() { |
102 | 33 | Button button = syncExec(new WidgetResult<Button>() { |
103 | |
public Button run() { |
104 | 33 | if (hasStyle(widget.getParent(), SWT.NO_RADIO_GROUP)) |
105 | 0 | return null; |
106 | 33 | Widget[] siblings = SWTUtils.siblings(widget); |
107 | 48 | for (Widget widget : siblings) { |
108 | 46 | if ((widget instanceof Button) && hasStyle(widget, SWT.RADIO)) |
109 | 46 | if (((Button) widget).getSelection()) |
110 | 31 | return (Button) widget; |
111 | |
} |
112 | 2 | return null; |
113 | |
} |
114 | |
}); |
115 | |
|
116 | 33 | if (button != null) |
117 | 31 | return new SWTBotRadio(button); |
118 | 2 | return null; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public boolean isSelected() { |
127 | 61 | return syncExec(new BoolResult() { |
128 | |
public Boolean run() { |
129 | 61 | return widget.getSelection(); |
130 | |
} |
131 | |
}); |
132 | |
} |
133 | |
} |