Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SWTBotCCombo |
|
| 1.4;1.4 | ||||
SWTBotCCombo$1 |
|
| 1.4;1.4 | ||||
SWTBotCCombo$2 |
|
| 1.4;1.4 | ||||
SWTBotCCombo$3 |
|
| 1.4;1.4 | ||||
SWTBotCCombo$4 |
|
| 1.4;1.4 | ||||
SWTBotCCombo$5 |
|
| 1.4;1.4 | ||||
SWTBotCCombo$6 |
|
| 1.4;1.4 | ||||
SWTBotCCombo$7 |
|
| 1.4;1.4 | ||||
SWTBotCCombo$8 |
|
| 1.4;1.4 |
1 | 2 | /******************************************************************************* |
2 | * Copyright (c) 2008, 2010 Cedric Chabanois 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 | * Cedric Chabanois - 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 | * Ketan Padegaonkar - ongoing bugfixes | |
13 | *******************************************************************************/ | |
14 | package org.eclipse.swtbot.swt.finder.widgets; | |
15 | ||
16 | import java.util.Arrays; | |
17 | ||
18 | import org.eclipse.swt.SWT; | |
19 | import org.eclipse.swt.custom.CCombo; | |
20 | import org.eclipse.swtbot.swt.finder.ReferenceBy; | |
21 | import org.eclipse.swtbot.swt.finder.SWTBotWidget; | |
22 | import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; | |
23 | import org.eclipse.swtbot.swt.finder.results.ArrayResult; | |
24 | import org.eclipse.swtbot.swt.finder.results.IntResult; | |
25 | import org.eclipse.swtbot.swt.finder.results.StringResult; | |
26 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
27 | import org.eclipse.swtbot.swt.finder.utils.MessageFormat; | |
28 | import org.hamcrest.SelfDescribing; | |
29 | ||
30 | /** | |
31 | * This represents a {@link CCombo} widget. | |
32 | * | |
33 | * @author Cedric Chabanois <cchabanois [at] no-log [dot] org> | |
34 | * @version $Id$ | |
35 | * @since 1.0 | |
36 | */ | |
37 | @SWTBotWidget(clasz = CCombo.class, preferredName = "ccomboBox", referenceBy = { ReferenceBy.TEXT, ReferenceBy.LABEL }) | |
38 | public class SWTBotCCombo extends AbstractSWTBotControl<CCombo> { | |
39 | ||
40 | /** | |
41 | * Constructs an instance of this with the given widget. | |
42 | * | |
43 | * @param w the widget. | |
44 | * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. | |
45 | */ | |
46 | public SWTBotCCombo(CCombo w) throws WidgetNotFoundException { | |
47 | 0 | this(w, null); |
48 | 0 | } |
49 | ||
50 | /** | |
51 | * Constructs an instance of this with the given widget. | |
52 | * | |
53 | * @param w the widget. | |
54 | * @param description the description of the widget, this will be reported by {@link #toString()} | |
55 | * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. | |
56 | */ | |
57 | public SWTBotCCombo(CCombo w, SelfDescribing description) throws WidgetNotFoundException { | |
58 | 9 | super(w, description); |
59 | 9 | } |
60 | ||
61 | /** | |
62 | * Sets the text in the cCombo box. | |
63 | * | |
64 | * @param text the text to set. | |
65 | */ | |
66 | public void setText(final String text) { | |
67 | 3 | log.debug(MessageFormat.format("Setting text on widget {0} to {1}", this, text)); //$NON-NLS-1$ |
68 | 3 | waitForEnabled(); |
69 | 3 | if (hasStyle(widget, SWT.READ_ONLY)) |
70 | 1 | throw new RuntimeException("This combo box is read-only."); //$NON-NLS-1$ |
71 | 2 | asyncExec(new VoidResult() { |
72 | public void run() { | |
73 | 2 | widget.setText(text); |
74 | 2 | } |
75 | }); | |
76 | 2 | notify(SWT.Modify); |
77 | 2 | } |
78 | ||
79 | /** | |
80 | * Returns the maximum number of characters that the receiver's text field is capable of holding. If this has not | |
81 | * been changed by <code>setTextLimit()</code>, it will be the constant <code>Combo.LIMIT</code>. | |
82 | * | |
83 | * @return the text limit | |
84 | */ | |
85 | public int textLimit() { | |
86 | 0 | return syncExec(new IntResult() { |
87 | public Integer run() { | |
88 | 0 | return widget.getTextLimit(); |
89 | } | |
90 | }); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Set the selection to the specified text. | |
95 | * | |
96 | * @param text the text to set into the combo. | |
97 | */ | |
98 | public void setSelection(final String text) { | |
99 | 2 | log.debug(MessageFormat.format("Setting selection on {0} to {1}", widget, text)); //$NON-NLS-1$ |
100 | 2 | _setSelection(text); |
101 | 1 | log.debug(MessageFormat.format("Set selection on {0} to {1}", widget, text)); //$NON-NLS-1$ |
102 | 1 | } |
103 | ||
104 | /** | |
105 | * Sets the selection to the given text. | |
106 | * | |
107 | * @param text The text to use. | |
108 | */ | |
109 | private void _setSelection(final String text) { | |
110 | 2 | waitForEnabled(); |
111 | 2 | final int indexOf = syncExec(new IntResult() { |
112 | public Integer run() { | |
113 | 2 | String[] items = widget.getItems(); |
114 | 2 | return Arrays.asList(items).indexOf(text); |
115 | } | |
116 | }); | |
117 | 2 | if (indexOf == -1) |
118 | 1 | throw new RuntimeException("Item `" + text + "' not found in combo box."); //$NON-NLS-1$ //$NON-NLS-2$ |
119 | ||
120 | 1 | select(indexOf); |
121 | 1 | } |
122 | ||
123 | private void select(final int indexOf) { | |
124 | 2 | asyncExec(new VoidResult() { |
125 | public void run() { | |
126 | 2 | widget.select(indexOf); |
127 | 2 | } |
128 | }); | |
129 | 2 | notify(SWT.Selection); |
130 | 2 | } |
131 | ||
132 | /** | |
133 | * Gets the current selection in the combo. | |
134 | * | |
135 | * @return the current selection in the combo box or null if no item is selected. | |
136 | */ | |
137 | public String selection() { | |
138 | 4 | return syncExec(new StringResult() { |
139 | public String run() { | |
140 | 4 | int selectionIndex = widget.getSelectionIndex(); |
141 | 4 | if (selectionIndex == -1) |
142 | 1 | return null; |
143 | 3 | return widget.getItem(selectionIndex); |
144 | } | |
145 | }); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Gets the current selection index. | |
150 | * | |
151 | * @return the zero based index of the current selection. | |
152 | */ | |
153 | public int selectionIndex() { | |
154 | 3 | return syncExec(new IntResult() { |
155 | public Integer run() { | |
156 | 3 | return widget.getSelectionIndex(); |
157 | } | |
158 | }); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Sets the selection to the specified index. | |
163 | * | |
164 | * @param index the zero based index. | |
165 | */ | |
166 | public void setSelection(final int index) { | |
167 | 2 | waitForEnabled(); |
168 | 2 | int itemCount = itemCount(); |
169 | 2 | if (index > itemCount) |
170 | 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$ |
171 | ||
172 | 1 | select(index); |
173 | 1 | } |
174 | ||
175 | /** | |
176 | * Gets the number of items in the combo box. | |
177 | * | |
178 | * @return the number of items in the combo box. | |
179 | */ | |
180 | public int itemCount() { | |
181 | 3 | return syncExec(new IntResult() { |
182 | public Integer run() { | |
183 | 3 | return widget.getItemCount(); |
184 | } | |
185 | }); | |
186 | } | |
187 | ||
188 | /** | |
189 | * Returns an array of <code>String</code>s which are the items in the receiver's list. | |
190 | * | |
191 | * @return the items in the receiver's list | |
192 | */ | |
193 | public String[] items() { | |
194 | 1 | return syncExec(new ArrayResult<String>() { |
195 | public String[] run() { | |
196 | 1 | return widget.getItems(); |
197 | } | |
198 | }); | |
199 | } | |
200 | ||
201 | } |