| 1 | 2 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
package org.eclipse.swtbot.swt.finder.widgets; |
| 14 | |
|
| 15 | |
import java.util.Arrays; |
| 16 | |
|
| 17 | |
import org.eclipse.swt.SWT; |
| 18 | |
import org.eclipse.swt.widgets.Combo; |
| 19 | |
import org.eclipse.swtbot.swt.finder.ReferenceBy; |
| 20 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
| 21 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
| 22 | |
import org.eclipse.swtbot.swt.finder.results.ArrayResult; |
| 23 | |
import org.eclipse.swtbot.swt.finder.results.IntResult; |
| 24 | |
import org.eclipse.swtbot.swt.finder.results.StringResult; |
| 25 | |
import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 26 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
| 27 | |
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
| 28 | |
import org.hamcrest.SelfDescribing; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
@SWTBotWidget(clasz = Combo.class, preferredName = "comboBox", referenceBy = { ReferenceBy.LABEL, ReferenceBy.TEXT }) |
| 35 | |
public class SWTBotCombo extends AbstractSWTBotControl<Combo> { |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public SWTBotCombo(Combo w) throws WidgetNotFoundException { |
| 45 | 0 | this(w, null); |
| 46 | 0 | } |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public SWTBotCombo(Combo w, SelfDescribing description) throws WidgetNotFoundException { |
| 57 | 9 | super(w, description); |
| 58 | 9 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public SWTBotCombo typeText(final String text) { |
| 67 | 1 | return typeText(text, SWTBotPreferences.TYPE_INTERVAL); |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public SWTBotCombo typeText(final String text, int interval) { |
| 78 | 1 | log.debug(MessageFormat.format("Inserting text:{0} into text {1}", text, this)); |
| 79 | 1 | setFocus(); |
| 80 | 1 | keyboard().typeText(text, interval); |
| 81 | 1 | return this; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public void setSelection(final String text) { |
| 90 | 2 | log.debug(MessageFormat.format("Setting selection on {0} to {1}", this, text)); |
| 91 | 2 | waitForEnabled(); |
| 92 | 2 | _setSelection(text); |
| 93 | 1 | notify(SWT.Selection); |
| 94 | 1 | log.debug(MessageFormat.format("Set selection on {0} to {1}", this, text)); |
| 95 | 1 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
private void _setSelection(final String text) { |
| 103 | 2 | final int indexOf = syncExec(new IntResult() { |
| 104 | |
public Integer run() { |
| 105 | 2 | String[] items = widget.getItems(); |
| 106 | 2 | return Arrays.asList(items).indexOf(text); |
| 107 | |
} |
| 108 | |
}); |
| 109 | 2 | if (indexOf == -1) |
| 110 | 1 | throw new RuntimeException("Item `" + text + "' not found in combo box."); |
| 111 | 1 | asyncExec(new VoidResult() { |
| 112 | |
public void run() { |
| 113 | 1 | widget.select(indexOf); |
| 114 | 1 | } |
| 115 | |
}); |
| 116 | 1 | } |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public String selection() { |
| 124 | 2 | return syncExec(new StringResult() { |
| 125 | |
public String run() { |
| 126 | 2 | return widget.getItem(widget.getSelectionIndex()); |
| 127 | |
} |
| 128 | |
}); |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
public int selectionIndex() { |
| 137 | 1 | return syncExec(new IntResult() { |
| 138 | |
public Integer run() { |
| 139 | 1 | return widget.getSelectionIndex(); |
| 140 | |
} |
| 141 | |
}); |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
public void setSelection(final int index) { |
| 150 | 2 | waitForEnabled(); |
| 151 | 2 | int itemCount = itemCount(); |
| 152 | 2 | if (index > itemCount) |
| 153 | 1 | throw new RuntimeException("The index (" + index + ") is more than the number of items (" + itemCount + ") in the combo."); |
| 154 | |
|
| 155 | 1 | asyncExec(new VoidResult() { |
| 156 | |
public void run() { |
| 157 | 1 | widget.select(index); |
| 158 | 1 | } |
| 159 | |
}); |
| 160 | 1 | notify(SWT.Selection); |
| 161 | 1 | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public int itemCount() { |
| 169 | 3 | return syncExec(new IntResult() { |
| 170 | |
public Integer run() { |
| 171 | 3 | return widget.getItemCount(); |
| 172 | |
} |
| 173 | |
}); |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public String[] items() { |
| 183 | 1 | return syncExec(new ArrayResult<String>() { |
| 184 | |
public String[] run() { |
| 185 | 1 | return widget.getItems(); |
| 186 | |
} |
| 187 | |
}); |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
public void setText(final String text) { |
| 197 | 3 | log.debug(MessageFormat.format("Setting text on {0} to {1}", this, text)); |
| 198 | 3 | waitForEnabled(); |
| 199 | |
|
| 200 | 3 | if (hasStyle(widget, SWT.READ_ONLY)) |
| 201 | 1 | throw new RuntimeException("This combo box is read-only."); |
| 202 | |
|
| 203 | 2 | asyncExec(new VoidResult() { |
| 204 | |
public void run() { |
| 205 | 2 | widget.setText(text); |
| 206 | 2 | } |
| 207 | |
}); |
| 208 | 2 | notify(SWT.Modify); |
| 209 | 2 | log.debug(MessageFormat.format("Set text on {0} to {1}", this, text)); |
| 210 | 2 | } |
| 211 | |
|
| 212 | |
} |