| 1 | 4 | |
| 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.List; |
| 15 | |
import org.eclipse.swtbot.swt.finder.ReferenceBy; |
| 16 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
| 17 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
| 18 | |
import org.eclipse.swtbot.swt.finder.results.ArrayResult; |
| 19 | |
import org.eclipse.swtbot.swt.finder.results.IntResult; |
| 20 | |
import org.eclipse.swtbot.swt.finder.results.StringResult; |
| 21 | |
import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 22 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
| 23 | |
import org.eclipse.swtbot.swt.finder.utils.StringUtils; |
| 24 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
| 25 | |
import org.hamcrest.SelfDescribing; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
@SWTBotWidget(clasz = List.class, preferredName = "list", referenceBy = { ReferenceBy.LABEL }) |
| 33 | |
public class SWTBotList extends AbstractSWTBotControl<List> { |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public SWTBotList(List list) throws WidgetNotFoundException { |
| 42 | 0 | this(list, null); |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public SWTBotList(List list, SelfDescribing description) throws WidgetNotFoundException { |
| 53 | 14 | super(list, description); |
| 54 | 14 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public void select(final String item) { |
| 62 | 2 | log.debug(MessageFormat.format("Set selection {0} to text {1}", this, item)); |
| 63 | 2 | waitForEnabled(); |
| 64 | 2 | final int indexOf = indexOf(item); |
| 65 | 2 | Assert.isTrue(indexOf != -1, "Item `" + item + "' not found in list."); |
| 66 | 1 | asyncExec(new VoidResult() { |
| 67 | |
public void run() { |
| 68 | 1 | widget.setSelection(indexOf); |
| 69 | 1 | } |
| 70 | |
}); |
| 71 | 1 | notifySelect(); |
| 72 | 1 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public void select(final int index) { |
| 80 | 5 | log.debug(MessageFormat.format("Set selection {0} to index {1}", this, index)); |
| 81 | 5 | waitForEnabled(); |
| 82 | 5 | int itemCount = itemCount(); |
| 83 | 10 | Assert.isTrue(index <= itemCount, java.text.MessageFormat.format( |
| 84 | 5 | "The index ({0}) is more than the number of items ({1}) in the list.", index, itemCount)); |
| 85 | 4 | asyncExec(new VoidResult() { |
| 86 | |
public void run() { |
| 87 | 4 | widget.setSelection(index); |
| 88 | 4 | } |
| 89 | |
}); |
| 90 | 4 | notifySelect(); |
| 91 | 4 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public int itemCount() { |
| 99 | 5 | return syncExec(new IntResult() { |
| 100 | |
public Integer run() { |
| 101 | 5 | return widget.getItemCount(); |
| 102 | |
} |
| 103 | |
}); |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public int selectionCount() { |
| 112 | 6 | return syncExec(new IntResult() { |
| 113 | |
public Integer run() { |
| 114 | 6 | return widget.getSelectionCount(); |
| 115 | |
} |
| 116 | |
}); |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public String[] selection() { |
| 125 | 8 | return syncExec(new ArrayResult<String>() { |
| 126 | |
public String[] run() { |
| 127 | 8 | return widget.getSelection(); |
| 128 | |
} |
| 129 | |
}); |
| 130 | |
} |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
public void select(final int... indices) { |
| 138 | 1 | log.debug(MessageFormat.format("Set selection {0} to indices {1}]", this, StringUtils.join(indices, ", "))); |
| 139 | 1 | waitForEnabled(); |
| 140 | 1 | asyncExec(new VoidResult() { |
| 141 | |
public void run() { |
| 142 | 1 | widget.setSelection(indices); |
| 143 | 1 | } |
| 144 | |
|
| 145 | |
}); |
| 146 | 1 | notifySelect(); |
| 147 | 1 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public void select(final String... items) { |
| 155 | 1 | log.debug(MessageFormat.format("Set selection {0} to items [{1}]", this, StringUtils.join(items, ", "))); |
| 156 | 1 | waitForEnabled(); |
| 157 | 1 | asyncExec(new VoidResult() { |
| 158 | |
public void run() { |
| 159 | 1 | widget.deselectAll(); |
| 160 | 4 | for (String item : items) { |
| 161 | 3 | int index = widget.indexOf(item); |
| 162 | 3 | if (index != -1) |
| 163 | 3 | widget.select(index); |
| 164 | |
} |
| 165 | 1 | } |
| 166 | |
}); |
| 167 | 1 | notifySelect(); |
| 168 | 1 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
protected void notifySelect() { |
| 174 | 9 | notify(SWT.MouseDown); |
| 175 | 9 | notify(SWT.Selection); |
| 176 | 9 | notify(SWT.MouseUp); |
| 177 | 9 | } |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public void unselect() { |
| 183 | 2 | asyncExec(new VoidResult() { |
| 184 | |
public void run() { |
| 185 | 2 | widget.deselectAll(); |
| 186 | 2 | } |
| 187 | |
}); |
| 188 | 2 | notifySelect(); |
| 189 | 2 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
public int indexOf(final String item) { |
| 198 | 3 | return syncExec(new IntResult() { |
| 199 | |
public Integer run() { |
| 200 | 3 | return widget.indexOf(item); |
| 201 | |
} |
| 202 | |
}); |
| 203 | |
} |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public String itemAt(final int index) { |
| 212 | 1 | return syncExec(new StringResult() { |
| 213 | |
public String run() { |
| 214 | 1 | return widget.getItem(index); |
| 215 | |
} |
| 216 | |
}); |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public String[] getItems() { |
| 225 | 0 | return syncExec(new ArrayResult<String>() { |
| 226 | |
public String[] run() { |
| 227 | 0 | return widget.getItems(); |
| 228 | |
} |
| 229 | |
}); |
| 230 | |
} |
| 231 | |
} |