| 1 | 6 | |
| 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.swtbot.swt.finder.ReferenceBy; |
| 16 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
| 17 | |
import org.eclipse.swtbot.swt.finder.Style; |
| 18 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
| 19 | |
import org.eclipse.swtbot.swt.finder.results.BoolResult; |
| 20 | |
import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 21 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
| 22 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
| 23 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
| 24 | |
import org.hamcrest.SelfDescribing; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
@SWTBotWidget(clasz = Button.class, style = @Style(name = "SWT.CHECK", value = SWT.CHECK), preferredName = "checkBox", referenceBy = { ReferenceBy.LABEL, ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP }) |
| 36 | |
public class SWTBotCheckBox extends AbstractSWTBotControl<Button> { |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public SWTBotCheckBox(Button w) throws WidgetNotFoundException { |
| 46 | 0 | this(w, null); |
| 47 | 0 | } |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public SWTBotCheckBox(Button w, SelfDescribing description) throws WidgetNotFoundException { |
| 58 | 728 | super(w, description); |
| 59 | 728 | Assert.isTrue(SWTUtils.hasStyle(w, SWT.CHECK), "Expecting a checkbox."); |
| 60 | 728 | } |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public SWTBotCheckBox click() { |
| 66 | 6 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
| 67 | 6 | toggle(); |
| 68 | 6 | log.debug(MessageFormat.format("Clicked on {0}", this)); |
| 69 | 6 | return this; |
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public void deselect() { |
| 76 | 240 | log.debug(MessageFormat.format("Deselecting {0}", this)); |
| 77 | 240 | waitForEnabled(); |
| 78 | 240 | if (!isChecked()) { |
| 79 | 80 | log.debug(MessageFormat.format("Widget {0} already deselected, not deselecting again.", this)); |
| 80 | 80 | return; |
| 81 | |
} |
| 82 | 160 | asyncExec(new VoidResult() { |
| 83 | |
public void run() { |
| 84 | 160 | log.debug(MessageFormat.format("Deselecting {0}", this)); |
| 85 | 160 | widget.setSelection(false); |
| 86 | 160 | } |
| 87 | |
}); |
| 88 | 160 | notifyListeners(); |
| 89 | 160 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public void select() { |
| 95 | 478 | log.debug(MessageFormat.format("Selecting {0}", this)); |
| 96 | 478 | waitForEnabled(); |
| 97 | 478 | if (isChecked()) { |
| 98 | 306 | log.debug(MessageFormat.format("Widget {0} already selected, not selecting again.", this)); |
| 99 | 306 | return; |
| 100 | |
} |
| 101 | 172 | asyncExec(new VoidResult() { |
| 102 | |
public void run() { |
| 103 | 172 | log.debug(MessageFormat.format("Selecting {0}", this)); |
| 104 | 172 | widget.setSelection(true); |
| 105 | 172 | } |
| 106 | |
}); |
| 107 | 172 | notifyListeners(); |
| 108 | 172 | } |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
protected void toggle() { |
| 114 | 6 | waitForEnabled(); |
| 115 | 6 | asyncExec(new VoidResult() { |
| 116 | |
public void run() { |
| 117 | 12 | log.debug(MessageFormat.format("Toggling state on {0}. Setting state to {1}", widget, (!widget.getSelection() ? "selected" |
| 118 | 3 | : "unselected"))); |
| 119 | 6 | widget.setSelection(!widget.getSelection()); |
| 120 | 6 | } |
| 121 | |
}); |
| 122 | 6 | notifyListeners(); |
| 123 | 6 | } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
protected void notifyListeners() { |
| 129 | 338 | notify(SWT.MouseEnter); |
| 130 | 338 | notify(SWT.MouseMove); |
| 131 | 338 | notify(SWT.Activate); |
| 132 | 338 | notify(SWT.FocusIn); |
| 133 | 338 | notify(SWT.MouseDown); |
| 134 | 338 | notify(SWT.MouseUp); |
| 135 | 338 | notify(SWT.Selection); |
| 136 | 338 | notify(SWT.MouseHover); |
| 137 | 338 | notify(SWT.MouseMove); |
| 138 | 338 | notify(SWT.MouseExit); |
| 139 | 338 | notify(SWT.Deactivate); |
| 140 | 338 | notify(SWT.FocusOut); |
| 141 | 338 | } |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public boolean isChecked() { |
| 149 | 720 | return syncExec(new BoolResult() { |
| 150 | |
public Boolean run() { |
| 151 | 720 | return widget.getSelection(); |
| 152 | |
} |
| 153 | |
}); |
| 154 | |
} |
| 155 | |
|
| 156 | |
} |