Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SWTBotCombo |
|
| 1.3;1.3 | ||||
SWTBotCombo$1 |
|
| 1.3;1.3 | ||||
SWTBotCombo$2 |
|
| 1.3;1.3 | ||||
SWTBotCombo$3 |
|
| 1.3;1.3 | ||||
SWTBotCombo$4 |
|
| 1.3;1.3 | ||||
SWTBotCombo$5 |
|
| 1.3;1.3 | ||||
SWTBotCombo$6 |
|
| 1.3;1.3 | ||||
SWTBotCombo$7 |
|
| 1.3;1.3 | ||||
SWTBotCombo$8 |
|
| 1.3;1.3 |
1 | 2 | /******************************************************************************* |
2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Ketan Padegaonkar - initial API and implementation | |
10 | * Cédric Chabanois - http://swtbot.org/bugzilla/show_bug.cgi?id=17 | |
11 | * Stefan Seelmann - http://swtbot.org/bugzilla/show_bug.cgi?id=26 | |
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 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
32 | * @version $Id$ | |
33 | */ | |
34 | @SWTBotWidget(clasz = Combo.class, preferredName = "comboBox", referenceBy = { ReferenceBy.LABEL, ReferenceBy.TEXT }) | |
35 | public class SWTBotCombo extends AbstractSWTBotControl<Combo> { | |
36 | ||
37 | /** | |
38 | * Constructs an instance of this with the given combo box. | |
39 | * | |
40 | * @param w the widget. | |
41 | * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. | |
42 | * @since 1.0 | |
43 | */ | |
44 | public SWTBotCombo(Combo w) throws WidgetNotFoundException { | |
45 | 0 | this(w, null); |
46 | 0 | } |
47 | ||
48 | /** | |
49 | * Constructs an instance of this with the given combo box. | |
50 | * | |
51 | * @param w the widget. | |
52 | * @param description the description of the widget, this will be reported by {@link #toString()} | |
53 | * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. | |
54 | * @since 1.0 | |
55 | */ | |
56 | public SWTBotCombo(Combo w, SelfDescribing description) throws WidgetNotFoundException { | |
57 | 9 | super(w, description); |
58 | 9 | } |
59 | ||
60 | /** | |
61 | * Types the string in the combo box. | |
62 | * | |
63 | * @param text the text to be typed. | |
64 | * @return the same instance. | |
65 | */ | |
66 | public SWTBotCombo typeText(final String text) { | |
67 | 1 | return typeText(text, SWTBotPreferences.TYPE_INTERVAL); |
68 | } | |
69 | ||
70 | /** | |
71 | * Types the string in the combo box. | |
72 | * | |
73 | * @param text the text to be typed. | |
74 | * @param interval the interval between consecutive key strokes. | |
75 | * @return the same instance. | |
76 | */ | |
77 | public SWTBotCombo typeText(final String text, int interval) { | |
78 | 1 | log.debug(MessageFormat.format("Inserting text:{0} into text {1}", text, this)); //$NON-NLS-1$ |
79 | 1 | setFocus(); |
80 | 1 | keyboard().typeText(text, interval); |
81 | 1 | return this; |
82 | } | |
83 | ||
84 | /** | |
85 | * Set the selection to the specified text. | |
86 | * | |
87 | * @param text the text to set into the combo. | |
88 | */ | |
89 | public void setSelection(final String text) { | |
90 | 2 | log.debug(MessageFormat.format("Setting selection on {0} to {1}", this, text)); //$NON-NLS-1$ |
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)); //$NON-NLS-1$ |
95 | 1 | } |
96 | ||
97 | /** | |
98 | * Sets the selection to the given text. | |
99 | * | |
100 | * @param text The text to select. | |
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."); //$NON-NLS-1$ //$NON-NLS-2$ |
111 | 1 | asyncExec(new VoidResult() { |
112 | public void run() { | |
113 | 1 | widget.select(indexOf); |
114 | 1 | } |
115 | }); | |
116 | 1 | } |
117 | ||
118 | /** | |
119 | * Attempts to select the current item. | |
120 | * | |
121 | * @return the current selection in the combo box. | |
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 | * Sets the selection to the given index. | |
133 | * | |
134 | * @return the zero based index of the current selection. | |
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 | * Sets the selection to the specified index. | |
146 | * | |
147 | * @param index the zero based index. | |
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."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
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 | * Gets the item count in the combo box. | |
165 | * | |
166 | * @return the number of items in the combo box. | |
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 | * Returns an array of <code>String</code>s which are the items in the receiver's list. | |
178 | * | |
179 | * @return the items in the receiver's list | |
180 | * @since 1.0 | |
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 | * Sets the text of the combo box. | |
192 | * | |
193 | * @param text the text to set. | |
194 | * @since 1.0 | |
195 | */ | |
196 | public void setText(final String text) { | |
197 | 3 | log.debug(MessageFormat.format("Setting text on {0} to {1}", this, text)); //$NON-NLS-1$ |
198 | 3 | waitForEnabled(); |
199 | ||
200 | 3 | if (hasStyle(widget, SWT.READ_ONLY)) |
201 | 1 | throw new RuntimeException("This combo box is read-only."); //$NON-NLS-1$ |
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)); //$NON-NLS-1$ |
210 | 2 | } |
211 | ||
212 | } |