|
1 /******************************************************************************* |
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others. |
|
3 * Portion Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
4 * All rights reserved. This program and the accompanying materials |
|
5 * are made available under the terms of the Eclipse Public License v1.0 |
|
6 * which accompanies this distribution, and is available at |
|
7 * http://www.eclipse.org/legal/epl-v10.html |
|
8 * |
|
9 * Contributors: |
|
10 * IBM Corporation - initial API and implementation |
|
11 * Nokia Corporation - Qt implementation |
|
12 *******************************************************************************/ |
|
13 package org.eclipse.swt.widgets; |
|
14 |
|
15 import org.eclipse.swt.SWT; |
|
16 import org.eclipse.swt.events.SelectionListener; |
|
17 import org.eclipse.swt.graphics.Point; |
|
18 import org.eclipse.swt.internal.qt.ListUtils; |
|
19 import org.eclipse.swt.internal.qt.OS; |
|
20 import org.eclipse.swt.internal.qt.WidgetState; |
|
21 |
|
22 /** |
|
23 * Instances of this class represent a selectable user interface object that |
|
24 * displays a list of strings and issues notification when a string is selected. |
|
25 * A list may be single or multi select. |
|
26 * <p> |
|
27 * <dl> |
|
28 * <dt><b>Styles:</b></dt> |
|
29 * <dd>SINGLE, MULTI</dd> |
|
30 * <dt><b>Events:</b></dt> |
|
31 * <dd>Selection, DefaultSelection</dd> |
|
32 * </dl> |
|
33 * <p> |
|
34 * Note: Only one of SINGLE and MULTI may be specified. |
|
35 * </p> |
|
36 * <p> |
|
37 * IMPORTANT: This class is <em>not</em> intended to be subclassed. |
|
38 * </p> |
|
39 */ |
|
40 public class List extends Scrollable { |
|
41 private boolean ignoreSelectionEvent; |
|
42 |
|
43 /** |
|
44 * Constructs a new instance of this class given its parent and a style |
|
45 * value describing its behavior and appearance. |
|
46 * <p> |
|
47 * The style value is either one of the style constants defined in class |
|
48 * <code>SWT</code> which is applicable to instances of this class, or must |
|
49 * be built by <em>bitwise OR</em>'ing together (that is, using the |
|
50 * <code>int</code> "|" operator) two or more of those <code>SWT</code> |
|
51 * style constants. The class description lists the style constants that are |
|
52 * applicable to the class. Style bits are also inherited from superclasses. |
|
53 * </p> |
|
54 * |
|
55 * @param parent |
|
56 * a composite control which will be the parent of the new |
|
57 * instance (cannot be null) |
|
58 * @param style |
|
59 * the style of control to construct |
|
60 * |
|
61 * @exception IllegalArgumentException |
|
62 * <ul> |
|
63 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
|
64 * </ul> |
|
65 * @exception SWTException |
|
66 * <ul> |
|
67 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
68 * thread that created the parent</li> |
|
69 * <li>ERROR_INVALID_SUBCLASS - if this class is not an |
|
70 * allowed subclass</li> |
|
71 * </ul> |
|
72 * |
|
73 * @see SWT#SINGLE |
|
74 * @see SWT#MULTI |
|
75 * @see Widget#checkSubclass |
|
76 * @see Widget#getStyle |
|
77 */ |
|
78 public List(Composite parent, int style) { |
|
79 super(parent, checkStyle(style)); |
|
80 } |
|
81 |
|
82 static int checkStyle(int style) { |
|
83 return checkBits(style, SWT.SINGLE, SWT.MULTI, 0, 0, 0, 0); |
|
84 } |
|
85 |
|
86 void createHandle_pp(int index) { |
|
87 frameHandle = topHandle = scrollAreaHandle = OS.QListWidget_new(); |
|
88 handle = OS.QAbstractScrollArea_viewPort(scrollAreaHandle); |
|
89 if ((style & SWT.MULTI) != 0) { |
|
90 OS.QAbstractItemView_setSelectionMode(scrollAreaHandle, |
|
91 OS.QT_ABSTRACTITEMVIEW_SELECTIONMODE_MULTI); |
|
92 } |
|
93 state |= WidgetState.HANDLE; |
|
94 } |
|
95 |
|
96 /** |
|
97 * Adds the argument to the end of the receiver's list. |
|
98 * |
|
99 * @param string |
|
100 * the new item |
|
101 * |
|
102 * @exception IllegalArgumentException |
|
103 * <ul> |
|
104 * <li>ERROR_NULL_ARGUMENT - if the string is null</li> |
|
105 * </ul> |
|
106 * @exception SWTException |
|
107 * <ul> |
|
108 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
109 * disposed</li> |
|
110 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
111 * thread that created the receiver</li> |
|
112 * </ul> |
|
113 * |
|
114 * @see #add(String,int) |
|
115 */ |
|
116 public void add(String string) { |
|
117 checkWidget(); |
|
118 ListUtils.add(topHandle, string, null); |
|
119 } |
|
120 |
|
121 /** |
|
122 * Adds the argument to the receiver's list at the given zero-relative |
|
123 * index. |
|
124 * <p> |
|
125 * Note: To add an item at the end of the list, use the result of calling |
|
126 * <code>getItemCount()</code> as the index or use <code>add(String)</code>. |
|
127 * </p> |
|
128 * |
|
129 * @param string |
|
130 * the new item |
|
131 * @param index |
|
132 * the index for the item |
|
133 * |
|
134 * @exception IllegalArgumentException |
|
135 * <ul> |
|
136 * <li>ERROR_NULL_ARGUMENT - if the string is null</li> |
|
137 * <li>ERROR_INVALID_RANGE - if the index is not between 0 |
|
138 * and the number of elements in the list (inclusive)</li> |
|
139 * </ul> |
|
140 * @exception SWTException |
|
141 * <ul> |
|
142 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
143 * disposed</li> |
|
144 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
145 * thread that created the receiver</li> |
|
146 * </ul> |
|
147 * |
|
148 * @see #add(String) |
|
149 */ |
|
150 public void add(String string, int index) { |
|
151 checkWidget(); |
|
152 ListUtils.add(topHandle, string, null, index); |
|
153 } |
|
154 |
|
155 /** |
|
156 * Adds the listener to the collection of listeners who will be notified |
|
157 * when the user changes the receiver's selection, by sending it one of the |
|
158 * messages defined in the <code>SelectionListener</code> interface. |
|
159 * <p> |
|
160 * <code>widgetSelected</code> is called when the selection changes. |
|
161 * <code>widgetDefaultSelected</code> is typically called when an item is |
|
162 * double-clicked. |
|
163 * </p> |
|
164 * |
|
165 * @param listener |
|
166 * the listener which should be notified when the user changes |
|
167 * the receiver's selection |
|
168 * |
|
169 * @exception IllegalArgumentException |
|
170 * <ul> |
|
171 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
|
172 * </ul> |
|
173 * @exception SWTException |
|
174 * <ul> |
|
175 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
176 * disposed</li> |
|
177 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
178 * thread that created the receiver</li> |
|
179 * </ul> |
|
180 * |
|
181 * @see SelectionListener |
|
182 * @see #removeSelectionListener |
|
183 * @see SelectionEvent |
|
184 */ |
|
185 public void addSelectionListener(SelectionListener listener) { |
|
186 checkWidget(); |
|
187 if (listener == null) |
|
188 error(SWT.ERROR_NULL_ARGUMENT); |
|
189 TypedListener typedListener = new TypedListener(listener); |
|
190 addListener(SWT.Selection, typedListener); |
|
191 addListener(SWT.DefaultSelection, typedListener); |
|
192 } |
|
193 |
|
194 void hookEvents_pp() { |
|
195 super.hookEvents_pp(); |
|
196 |
|
197 int selectionSignalProxy = OS.SignalHandler_new(topHandle, |
|
198 display, OS.QSIGNAL_LIST_ITEMSELECTION_CHANGED); |
|
199 |
|
200 OS.QObject_connectOrThrow(topHandle, |
|
201 "itemSelectionChanged()", selectionSignalProxy, |
|
202 "widgetSignal()", OS.QT_AUTOCONNECTION); |
|
203 |
|
204 int itemActivatedSignalProxy = OS.SignalHandler_new(topHandle, |
|
205 display, OS.QSIGNAL_LIST_ITEM_ACTIVATED); |
|
206 |
|
207 OS.QObject_connectOrThrow(topHandle, |
|
208 "itemActivated(QListWidgetItem* )", itemActivatedSignalProxy, |
|
209 "widgetSignal(QListWidgetItem* )", OS.QT_AUTOCONNECTION); |
|
210 } |
|
211 |
|
212 void qt_signal_list_itemSelectionChanged_pp() { |
|
213 if (!ignoreSelectionEvent) { |
|
214 sendEvent(SWT.Selection); |
|
215 } |
|
216 } |
|
217 |
|
218 void qt_signal_list_itemActivated_pp(int selectionHandle) { |
|
219 if (!ignoreSelectionEvent) { |
|
220 sendEvent(SWT.DefaultSelection); |
|
221 } |
|
222 } |
|
223 |
|
224 /** |
|
225 * Deselects the item at the given zero-relative index in the receiver. If |
|
226 * the item at the index was already deselected, it remains deselected. |
|
227 * Indices that are out of range are ignored. |
|
228 * |
|
229 * @param index |
|
230 * the index of the item to deselect |
|
231 * |
|
232 * @exception SWTException |
|
233 * <ul> |
|
234 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
235 * disposed</li> |
|
236 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
237 * thread that created the receiver</li> |
|
238 * </ul> |
|
239 */ |
|
240 public void deselect(int index) { |
|
241 checkWidget(); |
|
242 ignoreSelectionEvent = true; |
|
243 ListUtils.deselect(topHandle, index); |
|
244 ignoreSelectionEvent = false; |
|
245 } |
|
246 |
|
247 /** |
|
248 * Deselects the items at the given zero-relative indices in the receiver. |
|
249 * If the item at the given zero-relative index in the receiver is selected, |
|
250 * it is deselected. If the item at the index was not selected, it remains |
|
251 * deselected. The range of the indices is inclusive. Indices that are out |
|
252 * of range are ignored. |
|
253 * |
|
254 * @param start |
|
255 * the start index of the items to deselect |
|
256 * @param end |
|
257 * the end index of the items to deselect |
|
258 * |
|
259 * @exception SWTException |
|
260 * <ul> |
|
261 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
262 * disposed</li> |
|
263 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
264 * thread that created the receiver</li> |
|
265 * </ul> |
|
266 */ |
|
267 public void deselect(int start, int end) { |
|
268 checkWidget(); |
|
269 ignoreSelectionEvent = true; |
|
270 ListUtils.deselect(topHandle, start, end); |
|
271 ignoreSelectionEvent = false; |
|
272 } |
|
273 |
|
274 /** |
|
275 * Deselects the items at the given zero-relative indices in the receiver. |
|
276 * If the item at the given zero-relative index in the receiver is selected, |
|
277 * it is deselected. If the item at the index was not selected, it remains |
|
278 * deselected. Indices that are out of range and duplicate indices are |
|
279 * ignored. |
|
280 * |
|
281 * @param indices |
|
282 * the array of indices for the items to deselect |
|
283 * |
|
284 * @exception IllegalArgumentException |
|
285 * <ul> |
|
286 * <li>ERROR_NULL_ARGUMENT - if the set of indices is null</li> |
|
287 * </ul> |
|
288 * @exception SWTException |
|
289 * <ul> |
|
290 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
291 * disposed</li> |
|
292 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
293 * thread that created the receiver</li> |
|
294 * </ul> |
|
295 */ |
|
296 public void deselect(int[] indices) { |
|
297 checkWidget(); |
|
298 ignoreSelectionEvent = true; |
|
299 ListUtils.deselect(topHandle, indices); |
|
300 ignoreSelectionEvent = false; |
|
301 } |
|
302 |
|
303 /** |
|
304 * Deselects all selected items in the receiver. |
|
305 * |
|
306 * @exception SWTException |
|
307 * <ul> |
|
308 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
309 * disposed</li> |
|
310 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
311 * thread that created the receiver</li> |
|
312 * </ul> |
|
313 */ |
|
314 public void deselectAll() { |
|
315 checkWidget(); |
|
316 ignoreSelectionEvent = true; |
|
317 ListUtils.deselectAll(topHandle); |
|
318 ignoreSelectionEvent = false; |
|
319 } |
|
320 |
|
321 /** |
|
322 * Returns the zero-relative index of the item which currently has the focus |
|
323 * in the receiver, or -1 if no item has focus. |
|
324 * |
|
325 * @return the index of the selected item |
|
326 * |
|
327 * @exception SWTException |
|
328 * <ul> |
|
329 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
330 * disposed</li> |
|
331 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
332 * thread that created the receiver</li> |
|
333 * </ul> |
|
334 */ |
|
335 public int getFocusIndex() { |
|
336 checkWidget(); |
|
337 return OS.QListWidget_currentRow(topHandle); |
|
338 |
|
339 } |
|
340 |
|
341 /** |
|
342 * Returns the item at the given, zero-relative index in the receiver. |
|
343 * Throws an exception if the index is out of range. |
|
344 * |
|
345 * @param index |
|
346 * the index of the item to return |
|
347 * @return the item at the given index |
|
348 * |
|
349 * @exception IllegalArgumentException |
|
350 * <ul> |
|
351 * <li>ERROR_INVALID_RANGE - if the index is not between 0 |
|
352 * and the number of elements in the list minus 1 (inclusive) |
|
353 * </li> |
|
354 * </ul> |
|
355 * @exception SWTException |
|
356 * <ul> |
|
357 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
358 * disposed</li> |
|
359 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
360 * thread that created the receiver</li> |
|
361 * </ul> |
|
362 */ |
|
363 public String getItem(int index) { |
|
364 checkWidget(); |
|
365 return ListUtils.getItem(topHandle, index); |
|
366 } |
|
367 |
|
368 /** |
|
369 * Returns the number of items contained in the receiver. |
|
370 * |
|
371 * @return the number of items |
|
372 * |
|
373 * @exception SWTException |
|
374 * <ul> |
|
375 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
376 * disposed</li> |
|
377 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
378 * thread that created the receiver</li> |
|
379 * </ul> |
|
380 */ |
|
381 public int getItemCount() { |
|
382 checkWidget(); |
|
383 return OS.QListWidget_count(topHandle); |
|
384 } |
|
385 |
|
386 /** |
|
387 * Returns the height of the area which would be used to display |
|
388 * <em>one</em> of the items in the list. |
|
389 * |
|
390 * @return the height of one item |
|
391 * |
|
392 * @exception SWTException |
|
393 * <ul> |
|
394 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
395 * disposed</li> |
|
396 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
397 * thread that created the receiver</li> |
|
398 * </ul> |
|
399 */ |
|
400 public int getItemHeight() { |
|
401 checkWidget(); |
|
402 return ListUtils.getItemHeight(topHandle); |
|
403 } |
|
404 |
|
405 /** |
|
406 * Returns a (possibly empty) array of <code>String</code>s which are the |
|
407 * items in the receiver. |
|
408 * <p> |
|
409 * Note: This is not the actual structure used by the receiver to maintain |
|
410 * its list of items, so modifying the array will not affect the receiver. |
|
411 * </p> |
|
412 * |
|
413 * @return the items in the receiver's list |
|
414 * |
|
415 * @exception SWTException |
|
416 * <ul> |
|
417 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
418 * disposed</li> |
|
419 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
420 * thread that created the receiver</li> |
|
421 * </ul> |
|
422 */ |
|
423 public String[] getItems() { |
|
424 checkWidget(); |
|
425 return ListUtils.getItems(topHandle); |
|
426 } |
|
427 |
|
428 /** |
|
429 * Returns an array of <code>String</code>s that are currently selected in |
|
430 * the receiver. The order of the items is unspecified. An empty array |
|
431 * indicates that no items are selected. |
|
432 * <p> |
|
433 * Note: This is not the actual structure used by the receiver to maintain |
|
434 * its selection, so modifying the array will not affect the receiver. |
|
435 * </p> |
|
436 * |
|
437 * @return an array representing the selection |
|
438 * |
|
439 * @exception SWTException |
|
440 * <ul> |
|
441 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
442 * disposed</li> |
|
443 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
444 * thread that created the receiver</li> |
|
445 * </ul> |
|
446 */ |
|
447 public String[] getSelection() { |
|
448 checkWidget(); |
|
449 return ListUtils.getSelection(topHandle); |
|
450 } |
|
451 |
|
452 /** |
|
453 * Returns the number of selected items contained in the receiver. |
|
454 * |
|
455 * @return the number of selected items |
|
456 * |
|
457 * @exception SWTException |
|
458 * <ul> |
|
459 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
460 * disposed</li> |
|
461 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
462 * thread that created the receiver</li> |
|
463 * </ul> |
|
464 */ |
|
465 public int getSelectionCount() { |
|
466 checkWidget(); |
|
467 return ListUtils.getSelectionCount(topHandle); |
|
468 } |
|
469 |
|
470 /** |
|
471 * Returns the zero-relative index of the item which is currently selected |
|
472 * in the receiver, or -1 if no item is selected. |
|
473 * |
|
474 * @return the index of the selected item or -1 |
|
475 * |
|
476 * @exception SWTException |
|
477 * <ul> |
|
478 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
479 * disposed</li> |
|
480 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
481 * thread that created the receiver</li> |
|
482 * </ul> |
|
483 */ |
|
484 public int getSelectionIndex() { |
|
485 int[] indices = getSelectionIndices(); |
|
486 if (indices.length < 1) |
|
487 return -1; |
|
488 return indices[0]; |
|
489 |
|
490 } |
|
491 |
|
492 /** |
|
493 * Returns the zero-relative indices of the items which are currently |
|
494 * selected in the receiver. The order of the indices is unspecified. The |
|
495 * array is empty if no items are selected. |
|
496 * <p> |
|
497 * Note: This is not the actual structure used by the receiver to maintain |
|
498 * its selection, so modifying the array will not affect the receiver. |
|
499 * </p> |
|
500 * |
|
501 * @return the array of indices of the selected items |
|
502 * |
|
503 * @exception SWTException |
|
504 * <ul> |
|
505 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
506 * disposed</li> |
|
507 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
508 * thread that created the receiver</li> |
|
509 * </ul> |
|
510 */ |
|
511 public int[] getSelectionIndices() { |
|
512 checkWidget(); |
|
513 return ListUtils.getSelectionIndices(topHandle); |
|
514 } |
|
515 |
|
516 /** |
|
517 * Returns the zero-relative index of the item which is currently at the top |
|
518 * of the receiver. This index can change when items are scrolled or new |
|
519 * items are added or removed. |
|
520 * |
|
521 * @return the index of the top item |
|
522 * |
|
523 * @exception SWTException |
|
524 * <ul> |
|
525 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
526 * disposed</li> |
|
527 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
528 * thread that created the receiver</li> |
|
529 * </ul> |
|
530 */ |
|
531 public int getTopIndex() { |
|
532 checkWidget(); |
|
533 if (getItemCount() < 1) |
|
534 return 0; |
|
535 return OS.QAbstractItemView_swt_indexAt(topHandle, 1, 1); |
|
536 } |
|
537 |
|
538 /** |
|
539 * Gets the index of an item. |
|
540 * <p> |
|
541 * The list is searched starting at 0 until an item is found that is equal |
|
542 * to the search item. If no item is found, -1 is returned. Indexing is zero |
|
543 * based. |
|
544 * |
|
545 * @param string |
|
546 * the search item |
|
547 * @return the index of the item |
|
548 * |
|
549 * @exception IllegalArgumentException |
|
550 * <ul> |
|
551 * <li>ERROR_NULL_ARGUMENT - if the string is null</li> |
|
552 * </ul> |
|
553 * @exception SWTException |
|
554 * <ul> |
|
555 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
556 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
557 * called from the thread that created the receiver</li> |
|
558 * </ul> |
|
559 */ |
|
560 public int indexOf(String string) { |
|
561 return indexOf(string, 0); |
|
562 } |
|
563 |
|
564 /** |
|
565 * Searches the receiver's list starting at the given, zero-relative index |
|
566 * until an item is found that is equal to the argument, and returns the |
|
567 * index of that item. If no item is found or the starting index is out of |
|
568 * range, returns -1. |
|
569 * |
|
570 * @param string |
|
571 * the search item |
|
572 * @param start |
|
573 * the zero-relative index at which to start the search |
|
574 * @return the index of the item |
|
575 * |
|
576 * @exception IllegalArgumentException |
|
577 * <ul> |
|
578 * <li>ERROR_NULL_ARGUMENT - if the string is null</li> |
|
579 * </ul> |
|
580 * @exception SWTException |
|
581 * <ul> |
|
582 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
583 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
584 * called from the thread that created the receiver</li> |
|
585 * </ul> |
|
586 */ |
|
587 public int indexOf(String string, int start) { |
|
588 checkWidget(); |
|
589 return ListUtils.indexOf(topHandle, string, start); |
|
590 } |
|
591 |
|
592 /** |
|
593 * Returns <code>true</code> if the item is selected, and <code>false</code> |
|
594 * otherwise. Indices out of range are ignored. |
|
595 * |
|
596 * @param index |
|
597 * the index of the item |
|
598 * @return the selection state of the item at the index |
|
599 * |
|
600 * @exception SWTException |
|
601 * <ul> |
|
602 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
603 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
604 * called from the thread that created the receiver</li> |
|
605 * </ul> |
|
606 */ |
|
607 public boolean isSelected(int index) { |
|
608 checkWidget(); |
|
609 return ListUtils.isSelected(topHandle, index); |
|
610 } |
|
611 |
|
612 /** |
|
613 * Removes the item from the receiver at the given zero-relative index. |
|
614 * |
|
615 * @param index |
|
616 * the index for the item |
|
617 * |
|
618 * @exception IllegalArgumentException |
|
619 * <ul> |
|
620 * <li>ERROR_INVALID_RANGE - if the index is not between 0 |
|
621 * and the number of elements in the list minus 1 (inclusive) |
|
622 * </li> |
|
623 * </ul> |
|
624 * @exception SWTException |
|
625 * <ul> |
|
626 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
627 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
628 * called from the thread that created the receiver</li> |
|
629 * </ul> |
|
630 */ |
|
631 public void remove(int index) { |
|
632 checkWidget(); |
|
633 ListUtils.remove(topHandle, index); |
|
634 } |
|
635 |
|
636 /** |
|
637 * Removes the items from the receiver which are between the given |
|
638 * zero-relative start and end indices (inclusive). |
|
639 * |
|
640 * @param start |
|
641 * the start of the range |
|
642 * @param end |
|
643 * the end of the range |
|
644 * |
|
645 * @exception IllegalArgumentException |
|
646 * <ul> |
|
647 * <li>ERROR_INVALID_RANGE - if either the start or end are |
|
648 * not between 0 and the number of elements in the list minus |
|
649 * 1 (inclusive)</li> |
|
650 * </ul> |
|
651 * @exception SWTException |
|
652 * <ul> |
|
653 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
654 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
655 * called from the thread that created the receiver</li> |
|
656 * </ul> |
|
657 */ |
|
658 public void remove(int start, int end) { |
|
659 checkWidget(); |
|
660 ListUtils.remove(topHandle, start, end); |
|
661 } |
|
662 |
|
663 /** |
|
664 * Searches the receiver's list starting at the first item until an item is |
|
665 * found that is equal to the argument, and removes that item from the list. |
|
666 * |
|
667 * @param string |
|
668 * the item to remove |
|
669 * |
|
670 * @exception IllegalArgumentException |
|
671 * <ul> |
|
672 * <li>ERROR_NULL_ARGUMENT - if the string is null</li> <li> |
|
673 * ERROR_INVALID_ARGUMENT - if the string is not found in the |
|
674 * list</li> |
|
675 * </ul> |
|
676 * @exception SWTException |
|
677 * <ul> |
|
678 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
679 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
680 * called from the thread that created the receiver</li> |
|
681 * </ul> |
|
682 */ |
|
683 public void remove(String string) { |
|
684 checkWidget(); |
|
685 ListUtils.remove(topHandle, string); |
|
686 } |
|
687 |
|
688 /** |
|
689 * Removes the items from the receiver at the given zero-relative indices. |
|
690 * |
|
691 * @param indices |
|
692 * the array of indices of the items |
|
693 * |
|
694 * @exception IllegalArgumentException |
|
695 * <ul> |
|
696 * <li>ERROR_INVALID_RANGE - if the index is not between 0 |
|
697 * and the number of elements in the list minus 1 (inclusive) |
|
698 * </li> <li>ERROR_NULL_ARGUMENT - if the indices array is |
|
699 * null</li> |
|
700 * </ul> |
|
701 * @exception SWTException |
|
702 * <ul> |
|
703 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
704 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
705 * called from the thread that created the receiver</li> |
|
706 * </ul> |
|
707 */ |
|
708 public void remove(int[] indices) { |
|
709 checkWidget(); |
|
710 ListUtils.remove(topHandle, indices); |
|
711 } |
|
712 |
|
713 /** |
|
714 * Removes all of the items from the receiver. |
|
715 * <p> |
|
716 * |
|
717 * @exception SWTException |
|
718 * <ul> |
|
719 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
720 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
721 * called from the thread that created the receiver</li> |
|
722 * </ul> |
|
723 */ |
|
724 public void removeAll() { |
|
725 checkWidget(); |
|
726 ListUtils.removeAll(topHandle); |
|
727 } |
|
728 |
|
729 /** |
|
730 * Removes the listener from the collection of listeners who will be |
|
731 * notified when the user changes the receiver's selection. |
|
732 * |
|
733 * @param listener |
|
734 * the listener which should no longer be notified |
|
735 * |
|
736 * @exception IllegalArgumentException |
|
737 * <ul> |
|
738 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
|
739 * </ul> |
|
740 * @exception SWTException |
|
741 * <ul> |
|
742 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
743 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
744 * called from the thread that created the receiver</li> |
|
745 * </ul> |
|
746 * |
|
747 * @see SelectionListener |
|
748 * @see #addSelectionListener |
|
749 */ |
|
750 public void removeSelectionListener(SelectionListener listener) { |
|
751 checkWidget(); |
|
752 if (listener == null) |
|
753 error(SWT.ERROR_NULL_ARGUMENT); |
|
754 if (eventTable == null) |
|
755 return; |
|
756 eventTable.unhook(SWT.Selection, listener); |
|
757 eventTable.unhook(SWT.DefaultSelection, listener); |
|
758 } |
|
759 |
|
760 /** |
|
761 * Selects the item at the given zero-relative index in the receiver's list. |
|
762 * If the item at the index was already selected, it remains selected. |
|
763 * Indices that are out of range are ignored. |
|
764 * |
|
765 * @param index |
|
766 * the index of the item to select |
|
767 * |
|
768 * @exception SWTException |
|
769 * <ul> |
|
770 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
771 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
772 * called from the thread that created the receiver</li> |
|
773 * </ul> |
|
774 */ |
|
775 public void select(int index) { |
|
776 checkWidget(); |
|
777 ignoreSelectionEvent = true; |
|
778 ListUtils.select(topHandle, index, (SWT.MULTI & getStyle()) != 0); |
|
779 ignoreSelectionEvent = false; |
|
780 } |
|
781 |
|
782 /** |
|
783 * Selects the items in the range specified by the given zero-relative |
|
784 * indices in the receiver. The range of indices is inclusive. The current |
|
785 * selection is not cleared before the new items are selected. |
|
786 * <p> |
|
787 * If an item in the given range is not selected, it is selected. If an item |
|
788 * in the given range was already selected, it remains selected. Indices |
|
789 * that are out of range are ignored and no items will be selected if start |
|
790 * is greater than end. If the receiver is single-select and there is more |
|
791 * than one item in the given range, then all indices are ignored. |
|
792 * |
|
793 * @param start |
|
794 * the start of the range |
|
795 * @param end |
|
796 * the end of the range |
|
797 * |
|
798 * @exception SWTException |
|
799 * <ul> |
|
800 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
801 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
802 * called from the thread that created the receiver</li> |
|
803 * </ul> |
|
804 * |
|
805 * @see List#setSelection(int,int) |
|
806 */ |
|
807 public void select(int start, int end) { |
|
808 checkWidget(); |
|
809 ignoreSelectionEvent = true; |
|
810 ListUtils.select(topHandle, start, end, (SWT.MULTI & getStyle()) != 0); |
|
811 ignoreSelectionEvent = false; |
|
812 } |
|
813 |
|
814 /** |
|
815 * Selects the items at the given zero-relative indices in the receiver. The |
|
816 * current selection is not cleared before the new items are selected. |
|
817 * <p> |
|
818 * If the item at a given index is not selected, it is selected. If the item |
|
819 * at a given index was already selected, it remains selected. Indices that |
|
820 * are out of range and duplicate indices are ignored. If the receiver is |
|
821 * single-select and multiple indices are specified, then all indices are |
|
822 * ignored. |
|
823 * |
|
824 * @param indices |
|
825 * the array of indices for the items to select |
|
826 * |
|
827 * @exception IllegalArgumentException |
|
828 * <ul> |
|
829 * <li>ERROR_NULL_ARGUMENT - if the array of indices is null |
|
830 * </li> |
|
831 * </ul> |
|
832 * @exception SWTException |
|
833 * <ul> |
|
834 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
835 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
836 * called from the thread that created the receiver</li> |
|
837 * </ul> |
|
838 * |
|
839 * @see List#setSelection(int[]) |
|
840 */ |
|
841 public void select(int[] indices) { |
|
842 checkWidget(); |
|
843 ignoreSelectionEvent = true; |
|
844 ListUtils.select(topHandle, indices, (SWT.MULTI & getStyle()) != 0); |
|
845 ignoreSelectionEvent = false; |
|
846 } |
|
847 |
|
848 /** |
|
849 * Selects all of the items in the receiver. |
|
850 * <p> |
|
851 * If the receiver is single-select, do nothing. |
|
852 * |
|
853 * @exception SWTException |
|
854 * <ul> |
|
855 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
856 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
857 * called from the thread that created the receiver</li> |
|
858 * </ul> |
|
859 */ |
|
860 public void selectAll() { |
|
861 checkWidget(); |
|
862 ignoreSelectionEvent = true; |
|
863 ListUtils.selectAll(topHandle, (SWT.MULTI & getStyle()) != 0); |
|
864 ignoreSelectionEvent = false; |
|
865 } |
|
866 |
|
867 /** |
|
868 * Sets the text of the item in the receiver's list at the given |
|
869 * zero-relative index to the string argument. |
|
870 * |
|
871 * @param index |
|
872 * the index for the item |
|
873 * @param string |
|
874 * the new text for the item |
|
875 * |
|
876 * @exception IllegalArgumentException |
|
877 * <ul> |
|
878 * <li>ERROR_INVALID_RANGE - if the index is not between 0 |
|
879 * and the number of elements in the list minus 1 (inclusive) |
|
880 * </li> <li>ERROR_NULL_ARGUMENT - if the string is null</li> |
|
881 * </ul> |
|
882 * @exception SWTException |
|
883 * <ul> |
|
884 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
885 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
886 * called from the thread that created the receiver</li> |
|
887 * </ul> |
|
888 */ |
|
889 public void setItem(int index, String string) { |
|
890 checkWidget(); |
|
891 if (string == null) |
|
892 SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
893 |
|
894 if (index < 0 || index >= OS.QListWidget_count(topHandle) ) { |
|
895 SWT.error(SWT.ERROR_INVALID_RANGE); |
|
896 } |
|
897 |
|
898 boolean isSelected = isSelected(index); |
|
899 OS.QListWidget_swt_setText(topHandle, index, string); |
|
900 if (isSelected) { |
|
901 select(index); |
|
902 } |
|
903 } |
|
904 |
|
905 /** |
|
906 * Sets the receiver's items to be the given array of items. |
|
907 * |
|
908 * @param items |
|
909 * the array of items |
|
910 * |
|
911 * @exception IllegalArgumentException |
|
912 * <ul> |
|
913 * <li>ERROR_NULL_ARGUMENT - if the items array is null</li> |
|
914 * <li>ERROR_INVALID_ARGUMENT - if an item in the items array |
|
915 * is null</li> |
|
916 * </ul> |
|
917 * @exception SWTException |
|
918 * <ul> |
|
919 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
920 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
921 * called from the thread that created the receiver</li> |
|
922 * </ul> |
|
923 */ |
|
924 public void setItems(String[] items) { |
|
925 checkWidget(); |
|
926 if (items == null) |
|
927 SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
928 |
|
929 removeAll(); |
|
930 int count = items.length; |
|
931 if (count < 1) |
|
932 return; |
|
933 |
|
934 OS.QListWidget_addItems(topHandle, items, null); |
|
935 } |
|
936 |
|
937 /** |
|
938 * Selects the item at the given zero-relative index in the receiver. If the |
|
939 * item at the index was already selected, it remains selected. The current |
|
940 * selection is first cleared, then the new item is selected. Indices that |
|
941 * are out of range are ignored. |
|
942 * |
|
943 * @param index |
|
944 * the index of the item to select |
|
945 * |
|
946 * @exception SWTException |
|
947 * <ul> |
|
948 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
949 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
950 * called from the thread that created the receiver</li> |
|
951 * </ul> |
|
952 * @see List#deselectAll() |
|
953 * @see List#select(int) |
|
954 */ |
|
955 public void setSelection(int index) { |
|
956 checkWidget(); |
|
957 ignoreSelectionEvent = true; |
|
958 ListUtils.setSelection(topHandle, index); |
|
959 ignoreSelectionEvent = false; |
|
960 } |
|
961 |
|
962 /** |
|
963 * Selects the items in the range specified by the given zero-relative |
|
964 * indices in the receiver. The range of indices is inclusive. The current |
|
965 * selection is cleared before the new items are selected. |
|
966 * <p> |
|
967 * Indices that are out of range are ignored and no items will be selected |
|
968 * if start is greater than end. If the receiver is single-select and there |
|
969 * is more than one item in the given range, then all indices are ignored. |
|
970 * |
|
971 * @param start |
|
972 * the start index of the items to select |
|
973 * @param end |
|
974 * the end index of the items to select |
|
975 * |
|
976 * @exception SWTException |
|
977 * <ul> |
|
978 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
979 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
980 * called from the thread that created the receiver</li> |
|
981 * </ul> |
|
982 * |
|
983 * @see List#deselectAll() |
|
984 * @see List#select(int,int) |
|
985 */ |
|
986 public void setSelection(int start, int end) { |
|
987 checkWidget(); |
|
988 ignoreSelectionEvent = true; |
|
989 ListUtils.setSelection(topHandle, start, end, (SWT.MULTI & getStyle()) != 0); |
|
990 ignoreSelectionEvent = false; |
|
991 } |
|
992 |
|
993 /** |
|
994 * Selects the items at the given zero-relative indices in the receiver. The |
|
995 * current selection is cleared before the new items are selected. |
|
996 * <p> |
|
997 * Indices that are out of range and duplicate indices are ignored. If the |
|
998 * receiver is single-select and multiple indices are specified, then all |
|
999 * indices are ignored. |
|
1000 * |
|
1001 * @param indices |
|
1002 * the indices of the items to select |
|
1003 * |
|
1004 * @exception IllegalArgumentException |
|
1005 * <ul> |
|
1006 * <li>ERROR_NULL_ARGUMENT - if the array of indices is null |
|
1007 * </li> |
|
1008 * </ul> |
|
1009 * @exception SWTException |
|
1010 * <ul> |
|
1011 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
1012 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
1013 * called from the thread that created the receiver</li> |
|
1014 * </ul> |
|
1015 * |
|
1016 * @see List#deselectAll() |
|
1017 * @see List#select(int[]) |
|
1018 */ |
|
1019 public void setSelection(int[] indices) { |
|
1020 checkWidget(); |
|
1021 ignoreSelectionEvent = true; |
|
1022 ListUtils.setSelection(topHandle, indices, (SWT.MULTI & getStyle()) != 0); |
|
1023 ignoreSelectionEvent = false; |
|
1024 } |
|
1025 |
|
1026 /** |
|
1027 * Sets the receiver's selection to be the given array of items. The current |
|
1028 * selection is cleared before the new items are selected. |
|
1029 * <p> |
|
1030 * Items that are not in the receiver are ignored. If the receiver is |
|
1031 * single-select and multiple items are specified, then all items are |
|
1032 * ignored. |
|
1033 * |
|
1034 * @param items |
|
1035 * the array of items |
|
1036 * |
|
1037 * @exception IllegalArgumentException |
|
1038 * <ul> |
|
1039 * <li>ERROR_NULL_ARGUMENT - if the array of items is null |
|
1040 * </li> |
|
1041 * </ul> |
|
1042 * @exception SWTException |
|
1043 * <ul> |
|
1044 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
1045 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
1046 * called from the thread that created the receiver</li> |
|
1047 * </ul> |
|
1048 * |
|
1049 * @see List#deselectAll() |
|
1050 * @see List#select(int[]) |
|
1051 * @see List#setSelection(int[]) |
|
1052 */ |
|
1053 public void setSelection(String[] items) { |
|
1054 checkWidget(); |
|
1055 ignoreSelectionEvent = true; |
|
1056 ListUtils.setSelection(topHandle, items, (SWT.MULTI & getStyle()) != 0); |
|
1057 ignoreSelectionEvent = false; |
|
1058 } |
|
1059 |
|
1060 /** |
|
1061 * Sets the zero-relative index of the item which is currently at the top of |
|
1062 * the receiver. This index can change when items are scrolled or new items |
|
1063 * are added and removed. |
|
1064 * |
|
1065 * @param index |
|
1066 * the index of the top item |
|
1067 * |
|
1068 * @exception SWTException |
|
1069 * <ul> |
|
1070 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
1071 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
1072 * called from the thread that created the receiver</li> |
|
1073 * </ul> |
|
1074 */ |
|
1075 public void setTopIndex(int index) { |
|
1076 checkWidget(); |
|
1077 if (index < 0 || index > getItemCount() - 1) |
|
1078 return; |
|
1079 OS.QListWidget_scrollToItem(topHandle, index, |
|
1080 OS.QT_ABSTRACTITEMVIEW_SCROLLHINT_POSITIONATTOP); |
|
1081 } |
|
1082 |
|
1083 /** |
|
1084 * Shows the selection. If the selection is already showing in the receiver, |
|
1085 * this method simply returns. Otherwise, the items are scrolled until the |
|
1086 * selection is visible. |
|
1087 * |
|
1088 * @exception SWTException |
|
1089 * <ul> |
|
1090 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been |
|
1091 * disposed</li> <li>ERROR_THREAD_INVALID_ACCESS - if not |
|
1092 * called from the thread that created the receiver</li> |
|
1093 * </ul> |
|
1094 */ |
|
1095 public void showSelection() { |
|
1096 checkWidget(); |
|
1097 ListUtils.showSelection(topHandle); |
|
1098 } |
|
1099 |
|
1100 /* |
|
1101 * @see Scrollable.getPrefferedClientAreaSize |
|
1102 */ |
|
1103 Point getPreferredClientAreaSize_pp() { |
|
1104 int x = OS.QAbstractItemView_sizeHintForColumn(topHandle, 0); |
|
1105 int y = OS.QAbstractItemView_sizeHintForIndex( |
|
1106 topHandle, 0, 0).y*getItemCount(); |
|
1107 return new Point(x, y); |
|
1108 } |
|
1109 |
|
1110 /* |
|
1111 * @see Control.setTraversalFlags |
|
1112 */ |
|
1113 void setTraversalFlags_pp(int type, int key, int modifier, |
|
1114 int character) { |
|
1115 int handle = topHandle; |
|
1116 traverseDoit = false; |
|
1117 traverseCancel = false; |
|
1118 if (type == SWT.TRAVERSE_RETURN) { |
|
1119 return; |
|
1120 } |
|
1121 |
|
1122 switch (key) { |
|
1123 case OS.QT_KEY_UP: |
|
1124 case OS.QT_KEY_LEFT: |
|
1125 if (ListUtils.getFocusIndex(handle) == 0) { |
|
1126 traverseDoit = true; |
|
1127 } |
|
1128 break; |
|
1129 case OS.QT_KEY_DOWN: |
|
1130 case OS.QT_KEY_RIGHT: |
|
1131 if (ListUtils.getFocusIndex(handle) == ListUtils.getItemCount(handle) - 1) { |
|
1132 traverseDoit = true; |
|
1133 } |
|
1134 break; |
|
1135 default: |
|
1136 super.setTraversalFlags_pp(type, key, modifier, character); |
|
1137 break; |
|
1138 } |
|
1139 } |
|
1140 |
|
1141 void setHBarPolicy(boolean status) { |
|
1142 if (status) |
|
1143 super.setHBarPolicy(status); |
|
1144 // Qt bug: Cannot turn H Bar completely OFF because getTopIndex() would fail |
|
1145 } |
|
1146 } |