10
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2004, 2007 IBM Corporation 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 * IBM Corporation - initial API and implementation
|
|
10 * Chris Longfield <clongfield@internap.com> - Fix for Bug 70856
|
|
11 * Tom Schindl - fix for bug 157309
|
|
12 * Brad Reynolds - bug 141435
|
|
13 * Port to the D programming language:
|
|
14 * Frank Benoit <benoit@tionex.de>
|
|
15 *******************************************************************************/
|
|
16
|
|
17 module dwtx.jface.viewers.AbstractListViewer;
|
|
18
|
|
19 import dwtx.jface.viewers.StructuredViewer;
|
|
20 import dwtx.jface.viewers.ILabelProvider;
|
|
21 import dwtx.jface.viewers.IBaseLabelProvider;
|
|
22 import dwtx.jface.viewers.ViewerComparator;
|
|
23 import dwtx.jface.viewers.IElementComparer;
|
|
24
|
|
25 import tango.util.collection.ArraySeq;
|
|
26 import tango.util.collection.model.Seq;
|
|
27 import tango.util.collection.model.SeqView;
|
|
28
|
|
29 import dwt.widgets.Control;
|
|
30 import dwt.widgets.Widget;
|
|
31 import dwtx.core.runtime.Assert;
|
|
32
|
|
33 import dwt.dwthelper.utils;
|
|
34 import dwt.dwthelper.Runnable;
|
|
35
|
|
36 /**
|
|
37 * Abstract base class for viewers that contain lists of items (such as a combo or list).
|
|
38 * Most of the viewer implementation is in this base class, except for the minimal code that
|
|
39 * actually communicates with the underlying widget.
|
|
40 *
|
|
41 * @see dwtx.jface.viewers.ListViewer
|
|
42 * @see dwtx.jface.viewers.ComboViewer
|
|
43 *
|
|
44 * @since 3.0
|
|
45 */
|
|
46 public abstract class AbstractListViewer : StructuredViewer {
|
|
47
|
|
48 /**
|
|
49 * A list of viewer elements (element type: <code>Object</code>).
|
|
50 */
|
|
51 private Seq!(Object) listMap;
|
|
52
|
|
53 /**
|
|
54 * Adds the given string to the underlying widget at the given index
|
|
55 *
|
|
56 * @param string the string to add
|
|
57 * @param index position to insert the string into
|
|
58 */
|
|
59 protected abstract void listAdd(String string, int index);
|
|
60
|
|
61 /**
|
|
62 * Sets the text of the item at the given index in the underlying widget.
|
|
63 *
|
|
64 * @param index index to modify
|
|
65 * @param string new text
|
|
66 */
|
|
67 protected abstract void listSetItem(int index, String string);
|
|
68
|
|
69 /**
|
|
70 * Returns the zero-relative indices of the items which are currently
|
|
71 * selected in the underlying widget. The array is empty if no items are selected.
|
|
72 * <p>
|
|
73 * Note: This is not the actual structure used by the receiver
|
|
74 * to maintain its selection, so modifying the array will
|
|
75 * not affect the receiver.
|
|
76 * </p>
|
|
77 * @return the array of indices of the selected items
|
|
78 */
|
|
79 protected abstract int[] listGetSelectionIndices();
|
|
80
|
|
81 /**
|
|
82 * Returns the number of items contained in the underlying widget.
|
|
83 *
|
|
84 * @return the number of items
|
|
85 */
|
|
86 protected abstract int listGetItemCount();
|
|
87
|
|
88 /**
|
|
89 * Sets the underlying widget's items to be the given array of items.
|
|
90 *
|
|
91 * @param labels the array of label text
|
|
92 */
|
|
93 protected abstract void listSetItems(String[] labels);
|
|
94
|
|
95 /**
|
|
96 * Removes all of the items from the underlying widget.
|
|
97 */
|
|
98 protected abstract void listRemoveAll();
|
|
99
|
|
100 /**
|
|
101 * Removes the item from the underlying widget at the given
|
|
102 * zero-relative index.
|
|
103 *
|
|
104 * @param index the index for the item
|
|
105 */
|
|
106 protected abstract void listRemove(int index);
|
|
107
|
|
108 /**
|
|
109 * Selects the items at the given zero-relative indices in the underlying widget.
|
|
110 * The current selection is cleared before the new items are selected.
|
|
111 * <p>
|
|
112 * Indices that are out of range and duplicate indices are ignored.
|
|
113 * If the receiver is single-select and multiple indices are specified,
|
|
114 * then all indices are ignored.
|
|
115 *
|
|
116 * @param ixs the indices of the items to select
|
|
117 */
|
|
118 protected abstract void listSetSelection(int[] ixs);
|
|
119
|
|
120 /**
|
|
121 * Shows the selection. If the selection is already showing in the receiver,
|
|
122 * this method simply returns. Otherwise, the items are scrolled until
|
|
123 * the selection is visible.
|
|
124 */
|
|
125 protected abstract void listShowSelection();
|
|
126
|
|
127 /**
|
|
128 * Deselects all selected items in the underlying widget.
|
|
129 */
|
|
130 protected abstract void listDeselectAll();
|
|
131
|
|
132 public this(){
|
|
133 listMap = new ArraySeq!(Object);
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Adds the given elements to this list viewer.
|
|
138 * If this viewer does not have a sorter, the elements are added at the end
|
|
139 * in the order given; otherwise the elements are inserted at appropriate positions.
|
|
140 * <p>
|
|
141 * This method should be called (by the content provider) when elements
|
|
142 * have been added to the model, in order to cause the viewer to accurately
|
|
143 * reflect the model. This method only affects the viewer, not the model.
|
|
144 * </p>
|
|
145 *
|
|
146 * @param elements the elements to add
|
|
147 */
|
|
148 public void add(Object[] elements) {
|
|
149 assertElementsNotNull(elements);
|
|
150 Object[] filtered = filter(elements);
|
|
151 ILabelProvider labelProvider = cast(ILabelProvider) getLabelProvider();
|
|
152 for (int i = 0; i < filtered.length; i++) {
|
|
153 Object element = filtered[i];
|
|
154 int ix = indexForElement(element);
|
|
155 insertItem(labelProvider, element, ix);
|
|
156 }
|
|
157 }
|
|
158
|
|
159 private void insertItem(ILabelProvider labelProvider, Object element, int index) {
|
|
160 listAdd(getLabelProviderText(labelProvider, element), index);
|
|
161 listMap.addAt(index, element);
|
|
162 mapElement(element, getControl()); // must map it, since findItem only looks in map, if enabled
|
|
163 }
|
|
164
|
|
165 /**
|
|
166 * Inserts the given element into this list viewer at the given position.
|
|
167 * If this viewer has a sorter, the position is ignored and the element is
|
|
168 * inserted at the correct position in the sort order.
|
|
169 * <p>
|
|
170 * This method should be called (by the content provider) when elements have
|
|
171 * been added to the model, in order to cause the viewer to accurately
|
|
172 * reflect the model. This method only affects the viewer, not the model.
|
|
173 * </p>
|
|
174 *
|
|
175 * @param element
|
|
176 * the element
|
|
177 * @param position
|
|
178 * a 0-based position relative to the model, or -1 to indicate
|
|
179 * the last position
|
|
180 * @since 3.3
|
|
181 */
|
|
182 public void insert(Object element, int position) {
|
|
183 if (getComparator() !is null || hasFilters()) {
|
|
184 add(element);
|
|
185 return;
|
|
186 }
|
|
187
|
|
188 insertItem(cast(ILabelProvider) getLabelProvider(), element, position);
|
|
189 }
|
|
190
|
|
191
|
|
192 /**
|
|
193 * Return the text for the element from the labelProvider.
|
|
194 * If it is null then return the empty String.
|
|
195 * @param labelProvider ILabelProvider
|
|
196 * @param element
|
|
197 * @return String. Return the emptyString if the labelProvider
|
|
198 * returns null for the text.
|
|
199 *
|
|
200 * @since 3.1
|
|
201 */
|
|
202 private String getLabelProviderText(ILabelProvider labelProvider, Object element){
|
|
203 String text = labelProvider.getText(element);
|
|
204 if(text is null) {
|
|
205 return "";//$NON-NLS-1$
|
|
206 }
|
|
207 return text;
|
|
208 }
|
|
209
|
|
210 /**
|
|
211 * Adds the given element to this list viewer.
|
|
212 * If this viewer does not have a sorter, the element is added at the end;
|
|
213 * otherwise the element is inserted at the appropriate position.
|
|
214 * <p>
|
|
215 * This method should be called (by the content provider) when a single element
|
|
216 * has been added to the model, in order to cause the viewer to accurately
|
|
217 * reflect the model. This method only affects the viewer, not the model.
|
|
218 * Note that there is another method for efficiently processing the simultaneous
|
|
219 * addition of multiple elements.
|
|
220 * </p>
|
|
221 *
|
|
222 * @param element the element
|
|
223 */
|
|
224 public void add(Object element) {
|
|
225 add([ element ]);
|
|
226 }
|
|
227
|
|
228 /* (non-Javadoc)
|
|
229 * Method declared on StructuredViewer.
|
|
230 * Since DWT.List doesn't use items we always return the List itself.
|
|
231 */
|
|
232 protected Widget doFindInputItem(Object element) {
|
|
233 if (element !is null && opEquals(element, getRoot())) {
|
|
234 return getControl();
|
|
235 }
|
|
236 return null;
|
|
237 }
|
|
238
|
|
239 /* (non-Javadoc)
|
|
240 * Method declared on StructuredViewer.
|
|
241 * Since DWT.List doesn't use items we always return the List itself.
|
|
242 */
|
|
243 protected Widget doFindItem(Object element) {
|
|
244 if (element !is null) {
|
|
245 if (listMapContains(element)) {
|
|
246 return getControl();
|
|
247 }
|
|
248 }
|
|
249 return null;
|
|
250 }
|
|
251
|
|
252 /* (non-Javadoc)
|
|
253 * Method declared on StructuredViewer.
|
|
254 */
|
|
255 protected void doUpdateItem(Widget data, Object element, bool fullMap) {
|
|
256 if (element !is null) {
|
|
257 int ix = getElementIndex(element);
|
|
258 if (ix >= 0) {
|
|
259 ILabelProvider labelProvider = cast(ILabelProvider) getLabelProvider();
|
|
260 listSetItem(ix, getLabelProviderText(labelProvider,element));
|
|
261 }
|
|
262 }
|
|
263 }
|
|
264
|
|
265 /* (non-Javadoc)
|
|
266 * Method declared on Viewer.
|
|
267 */
|
|
268 public abstract Control getControl();
|
|
269
|
|
270 /**
|
|
271 * Returns the element with the given index from this list viewer.
|
|
272 * Returns <code>null</code> if the index is out of range.
|
|
273 *
|
|
274 * @param index the zero-based index
|
|
275 * @return the element at the given index, or <code>null</code> if the
|
|
276 * index is out of range
|
|
277 */
|
|
278 public Object getElementAt(int index) {
|
|
279 if (index >= 0 && index < listMap.size()) {
|
|
280 return listMap.get(index);
|
|
281 }
|
|
282 return null;
|
|
283 }
|
|
284
|
|
285 /**
|
|
286 * The list viewer implementation of this <code>Viewer</code> framework
|
|
287 * method returns the label provider, which in the case of list
|
|
288 * viewers will be an instance of <code>ILabelProvider</code>.
|
|
289 */
|
|
290 public IBaseLabelProvider getLabelProvider() {
|
|
291 return super.getLabelProvider();
|
|
292 }
|
|
293
|
|
294 /* (non-Javadoc)
|
|
295 * Method declared on Viewer.
|
|
296 */
|
|
297 /* (non-Javadoc)
|
|
298 * Method declared on StructuredViewer.
|
|
299 */
|
|
300 protected SeqView!(Object) getSelectionFromWidget() {
|
|
301 int[] ixs = listGetSelectionIndices();
|
|
302 ArraySeq!(Object) list = new ArraySeq!(Object);
|
|
303 list.capacity(ixs.length);
|
|
304 for (int i = 0; i < ixs.length; i++) {
|
|
305 Object e = getElementAt(ixs[i]);
|
|
306 if (e !is null) {
|
|
307 list.append(e);
|
|
308 }
|
|
309 }
|
|
310 return list;
|
|
311 }
|
|
312
|
|
313 /**
|
|
314 * @param element the element to insert
|
|
315 * @return the index where the item should be inserted.
|
|
316 */
|
|
317 protected int indexForElement(Object element) {
|
|
318 ViewerComparator comparator = getComparator();
|
|
319 if (comparator is null) {
|
|
320 return listGetItemCount();
|
|
321 }
|
|
322 int count = listGetItemCount();
|
|
323 int min = 0, max = count - 1;
|
|
324 while (min <= max) {
|
|
325 int mid = (min + max) / 2;
|
|
326 Object data = listMap.get(mid);
|
|
327 int compare = comparator.compare(this, data, element);
|
|
328 if (compare is 0) {
|
|
329 // find first item > element
|
|
330 while (compare is 0) {
|
|
331 ++mid;
|
|
332 if (mid >= count) {
|
|
333 break;
|
|
334 }
|
|
335 data = listMap.get(mid);
|
|
336 compare = comparator.compare(this, data, element);
|
|
337 }
|
|
338 return mid;
|
|
339 }
|
|
340 if (compare < 0) {
|
|
341 min = mid + 1;
|
|
342 } else {
|
|
343 max = mid - 1;
|
|
344 }
|
|
345 }
|
|
346 return min;
|
|
347 }
|
|
348
|
|
349 /* (non-Javadoc)
|
|
350 * Method declared on Viewer.
|
|
351 */
|
|
352 protected void inputChanged(Object input, Object oldInput) {
|
|
353 listMap.clear();
|
|
354 Object[] children = getSortedChildren(getRoot());
|
|
355 int size = children.length;
|
|
356
|
|
357 listRemoveAll();
|
|
358 String[] labels = new String[size];
|
|
359 for (int i = 0; i < size; i++) {
|
|
360 Object el = children[i];
|
|
361 labels[i] = getLabelProviderText(cast(ILabelProvider) getLabelProvider(),el);
|
|
362 listMap.append(el);
|
|
363 mapElement(el, getControl()); // must map it, since findItem only looks in map, if enabled
|
|
364 }
|
|
365 listSetItems(labels);
|
|
366 }
|
|
367
|
|
368 /* (non-Javadoc)
|
|
369 * Method declared on StructuredViewer.
|
|
370 */
|
|
371 protected void internalRefresh(Object element) {
|
|
372 Control list = getControl();
|
|
373 if (element is null || opEquals(element, getRoot())) {
|
|
374 // the parent
|
|
375 if (listMap !is null) {
|
|
376 listMap.clear();
|
|
377 }
|
|
378 unmapAllElements();
|
|
379 auto selection = getSelectionFromWidget();
|
|
380
|
|
381 int topIndex = -1;
|
|
382 if (selection is null || selection.drained()) {
|
|
383 topIndex = listGetTopIndex();
|
|
384 }
|
|
385
|
|
386 list.setRedraw(false);
|
|
387 listRemoveAll();
|
|
388
|
|
389 Object[] children = getSortedChildren(getRoot());
|
|
390 String[] items = new String[children.length];
|
|
391
|
|
392 ILabelProvider labelProvider = cast(ILabelProvider) getLabelProvider();
|
|
393
|
|
394 for (int i = 0; i < items.length; i++) {
|
|
395 Object el = children[i];
|
|
396 items[i] = getLabelProviderText(labelProvider, el);
|
|
397 listMap.append(el);
|
|
398 mapElement(el, list); // must map it, since findItem only looks in map, if enabled
|
|
399 }
|
|
400
|
|
401 listSetItems(items);
|
|
402 list.setRedraw(true);
|
|
403
|
|
404 if (topIndex is -1) {
|
|
405 setSelectionToWidget(selection, false);
|
|
406 } else {
|
|
407 listSetTopIndex(Math.min(topIndex, children.length));
|
|
408 }
|
|
409 } else {
|
|
410 doUpdateItem(list, element, true);
|
|
411 }
|
|
412 }
|
|
413
|
|
414 /**
|
|
415 * Returns the index of the item currently at the top of the viewable area.
|
|
416 * <p>
|
|
417 * Default implementation returns -1.
|
|
418 * </p>
|
|
419 * @return index, -1 for none
|
|
420 * @since 3.3
|
|
421 */
|
|
422 protected int listGetTopIndex(){
|
|
423 return -1;
|
|
424 }
|
|
425
|
|
426 /**
|
|
427 * Sets the index of the item to be at the top of the viewable area.
|
|
428 * <p>
|
|
429 * Default implementation does nothing.
|
|
430 * </p>
|
|
431 * @param index the given index. -1 for none. index will always refer to a valid index.
|
|
432 * @since 3.3
|
|
433 */
|
|
434 protected void listSetTopIndex(int index) {
|
|
435 }
|
|
436
|
|
437 /**
|
|
438 * Removes the given elements from this list viewer.
|
|
439 *
|
|
440 * @param elements the elements to remove
|
|
441 */
|
|
442 private void internalRemove(Object[] elements) {
|
|
443 Object input = getInput();
|
|
444 for (int i = 0; i < elements.length; ++i) {
|
|
445 if ( opEquals(elements[i], input)) {
|
|
446 setInput(null);
|
|
447 return;
|
|
448 }
|
|
449 int ix = getElementIndex(elements[i]);
|
|
450 if (ix >= 0) {
|
|
451 listRemove(ix);
|
|
452 listMap.removeAt(ix);
|
|
453 unmapElement(elements[i], getControl());
|
|
454 }
|
|
455 }
|
|
456 }
|
|
457
|
|
458 /**
|
|
459 * Removes the given elements from this list viewer.
|
|
460 * The selection is updated if required.
|
|
461 * <p>
|
|
462 * This method should be called (by the content provider) when elements
|
|
463 * have been removed from the model, in order to cause the viewer to accurately
|
|
464 * reflect the model. This method only affects the viewer, not the model.
|
|
465 * </p>
|
|
466 *
|
|
467 * @param elements the elements to remove
|
|
468 */
|
|
469 public void remove(Object[] elements) {
|
|
470 assertElementsNotNull(elements);
|
|
471 if (elements.length is 0) {
|
|
472 return;
|
|
473 }
|
|
474 preservingSelection(new class Runnable {
|
|
475 Object[] elements_;
|
|
476 this(){
|
|
477 elements_= elements;
|
|
478 }
|
|
479 public void run() {
|
|
480 internalRemove(elements_);
|
|
481 }
|
|
482 });
|
|
483 }
|
|
484
|
|
485 /**
|
|
486 * Removes the given element from this list viewer.
|
|
487 * The selection is updated if necessary.
|
|
488 * <p>
|
|
489 * This method should be called (by the content provider) when a single element
|
|
490 * has been removed from the model, in order to cause the viewer to accurately
|
|
491 * reflect the model. This method only affects the viewer, not the model.
|
|
492 * Note that there is another method for efficiently processing the simultaneous
|
|
493 * removal of multiple elements.
|
|
494 * </p>
|
|
495 *
|
|
496 * @param element the element
|
|
497 */
|
|
498 public void remove(Object element) {
|
|
499 remove([ element ]);
|
|
500 }
|
|
501
|
|
502 /**
|
|
503 * The list viewer implementation of this <code>Viewer</code> framework
|
|
504 * method ensures that the given label provider is an instance of
|
|
505 * <code>ILabelProvider</code>.
|
|
506 *
|
|
507 * <b>The optional interfaces {@link IColorProvider} and
|
|
508 * {@link IFontProvider} have no effect for this type of viewer</b>
|
|
509 */
|
|
510 public void setLabelProvider(IBaseLabelProvider labelProvider) {
|
|
511 Assert.isTrue( null !is cast(ILabelProvider)labelProvider );
|
|
512 super.setLabelProvider(labelProvider);
|
|
513 }
|
|
514
|
|
515 /* (non-Javadoc)
|
|
516 * Method declared on StructuredViewer.
|
|
517 */
|
|
518 protected void setSelectionToWidget(SeqView!(Object) in_, bool reveal) {
|
|
519 if (in_ is null || in_.size() is 0) { // clear selection
|
|
520 listDeselectAll();
|
|
521 } else {
|
|
522 int n = in_.size();
|
|
523 int[] ixs = new int[n];
|
|
524 int count = 0;
|
|
525 for (int i = 0; i < n; ++i) {
|
|
526 Object el = in_.get(i);
|
|
527 int ix = getElementIndex(el);
|
|
528 if (ix >= 0) {
|
|
529 ixs[count++] = ix;
|
|
530 }
|
|
531 }
|
|
532 if (count < n) {
|
|
533 System.arraycopy(ixs, 0, ixs = new int[count], 0, count);
|
|
534 }
|
|
535 listSetSelection(ixs);
|
|
536 if (reveal) {
|
|
537 listShowSelection();
|
|
538 }
|
|
539 }
|
|
540 }
|
|
541
|
|
542 /**
|
|
543 * Returns the index of the given element in listMap, or -1 if the element cannot be found.
|
|
544 * As of 3.3, uses the element comparer if available.
|
|
545 *
|
|
546 * @param element
|
|
547 * @return the index
|
|
548 */
|
|
549 int getElementIndex(Object element) {
|
|
550 IElementComparer comparer = getComparer();
|
|
551 if (comparer is null) {
|
|
552 int idx = 0;
|
|
553 foreach( e; listMap ){
|
|
554 if( e == element ){
|
|
555 return idx;
|
|
556 }
|
|
557 idx++;
|
|
558 }
|
|
559 return -1;
|
|
560 }
|
|
561 int size = listMap.size();
|
|
562 for (int i = 0; i < size; i++) {
|
|
563 if (comparer.opEquals(element, listMap.get(i)))
|
|
564 return i;
|
|
565 }
|
|
566 return -1;
|
|
567 }
|
|
568
|
|
569 /**
|
|
570 * @param element
|
|
571 * @return true if listMap contains the given element
|
|
572 *
|
|
573 * @since 3.3
|
|
574 */
|
|
575 private bool listMapContains(Object element) {
|
|
576 return getElementIndex(element) !is -1;
|
|
577 }
|
|
578
|
|
579 }
|