Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6fdad583087d7b907a6b76a705fa2f2206dff95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation bug 154329
 *                                               - fixes in bug 170381, 198665, 200731
 *******************************************************************************/

package org.eclipse.jface.viewers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Widget;

import org.eclipse.core.runtime.Assert;

/**
 * This is a widget independent class implementors of
 * {@link org.eclipse.swt.widgets.Table} like widgets can use to provide a
 * viewer on top of their widget implementations.
 *
 * @since 3.3
 */
public abstract class AbstractTableViewer extends ColumnViewer {

	private class VirtualManager {

		/**
		 * The currently invisible elements as provided by the content provider
		 * or by addition. This will not be populated by an
		 * ILazyStructuredContentProvider as an ILazyStructuredContentProvider
		 * is only queried on the virtual callback.
		 */
		private Object[] cachedElements = new Object[0];

		/**
		 * Create a new instance of the receiver.
		 *
		 */
		public VirtualManager() {
			addTableListener();
		}

		/**
		 * Add the listener for SetData on the table
		 */
		private void addTableListener() {
			getControl().addListener(SWT.SetData, new Listener() {
				/*
				 * (non-Javadoc)
				 *
				 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
				 */
				public void handleEvent(Event event) {
					Item item = (Item) event.item;
					final int index = doIndexOf(item);
					
					if (index == -1) {
						// Should not happen, but the spec for doIndexOf allows returning -1.
						// See bug 241117.
						return;
					}
					
					Object element = resolveElement(index);
					if (element == null) {
						// Didn't find it so make a request
						// Keep looking if it is not in the cache.
						IContentProvider contentProvider = getContentProvider();
						// If we are building lazily then request lookup now
						if (contentProvider instanceof ILazyContentProvider) {
							((ILazyContentProvider) contentProvider)
									.updateElement(index);
							return;
						}
					}

					associate(element, item);
					updateItem(item, element);
				}

			});
		}

		/**
		 * Get the element at index.Resolve it lazily if this is available.
		 *
		 * @param index
		 * @return Object or <code>null</code> if it could not be found
		 */
		protected Object resolveElement(int index) {

			Object element = null;
			if (index < cachedElements.length) {
				element = cachedElements[index];
			}

			return element;
		}

		/**
		 * A non visible item has been added.
		 *
		 * @param element
		 * @param index
		 */
		public void notVisibleAdded(Object element, int index) {

			int requiredCount = doGetItemCount() + 1;

			Object[] newCache = new Object[requiredCount];
			System.arraycopy(cachedElements, 0, newCache, 0, index);
			if (index < cachedElements.length) {
				System.arraycopy(cachedElements, index, newCache, index + 1,
						cachedElements.length - index);
			}
			newCache[index] = element;
			cachedElements = newCache;

			doSetItemCount(requiredCount);
		}

		/**
		 * The elements with the given indices need to be removed from the
		 * cache.
		 *
		 * @param indices
		 */
		public void removeIndices(int[] indices) {
			if (indices.length == 1) {
				removeIndicesFromTo(indices[0], indices[0]);
			}
			int requiredCount = doGetItemCount() - indices.length;

			Arrays.sort(indices);
			Object[] newCache = new Object[requiredCount];
			int indexInNewCache = 0;
			int nextToSkip = 0;
			for (int i = 0; i < cachedElements.length; i++) {
				if (nextToSkip < indices.length && i == indices[nextToSkip]) {
					nextToSkip++;
				} else {
					newCache[indexInNewCache++] = cachedElements[i];
				}
			}
			cachedElements = newCache;
		}

		/**
		 * The elements between the given indices (inclusive) need to be removed
		 * from the cache.
		 *
		 * @param from
		 * @param to
		 */
		public void removeIndicesFromTo(int from, int to) {
			int indexAfterTo = to + 1;
			Object[] newCache = new Object[cachedElements.length
					- (indexAfterTo - from)];
			System.arraycopy(cachedElements, 0, newCache, 0, from);
			if (indexAfterTo < cachedElements.length) {
				System.arraycopy(cachedElements, indexAfterTo, newCache, from,
						cachedElements.length - indexAfterTo);
			}
		}

		/**
		 * @param element
		 * @return the index of the element in the cache, or null
		 */
		public int find(Object element) {
			return Arrays.asList(cachedElements).indexOf(element);
		}

		/**
		 * @param count
		 */
		public void adjustCacheSize(int count) {
			if (count == cachedElements.length) {
				return;
			} else if (count < cachedElements.length) {
				Object[] newCache = new Object[count];
				System.arraycopy(cachedElements, 0, newCache, 0, count);
				cachedElements = newCache;
			} else {
				Object[] newCache = new Object[count];
				System.arraycopy(cachedElements, 0, newCache, 0,
						cachedElements.length);
				cachedElements = newCache;
			}
		}

	}

	private VirtualManager virtualManager;

	/**
	 * Create the new viewer for table like widgets
	 */
	public AbstractTableViewer() {
		super();
	}

	@Override
	protected void hookControl(Control control) {
		super.hookControl(control);
		initializeVirtualManager(getControl().getStyle());
	}
	
	@Override
	protected void handleDispose(DisposeEvent event) {
		super.handleDispose(event);
		virtualManager = null;
	}

	/**
	 * Initialize the virtual manager to manage the virtual state if the table
	 * is VIRTUAL. If not use the default no-op version.
	 *
	 * @param style
	 */
	private void initializeVirtualManager(int style) {
		if ((style & SWT.VIRTUAL) == 0) {
			return;
		}

		virtualManager = new VirtualManager();
	}

	/**
	 * Adds the given elements to this table viewer. If this viewer does not
	 * have a sorter, the elements are added at the end in the order given;
	 * otherwise the elements are inserted at appropriate positions.
	 * <p>
	 * This method should be called (by the content provider) when elements have
	 * been added to the model, in order to cause the viewer to accurately
	 * reflect the model. This method only affects the viewer, not the model.
	 * </p>
	 *
	 * @param elements
	 *            the elements to add
	 */
	public void add(Object[] elements) {
		assertElementsNotNull(elements);
		if (checkBusy())
			return;
		Object[] filtered = filter(elements);

		for (int i = 0; i < filtered.length; i++) {
			Object element = filtered[i];
			int index = indexForElement(element);
			createItem(element, index);
		}
	}

	/**
	 * Create a new TableItem at index if required.
	 *
	 * @param element
	 * @param index
	 *
	 * @since 3.1
	 */
	private void createItem(Object element, int index) {
		if (virtualManager == null) {
			updateItem(internalCreateNewRowPart(SWT.NONE, index).getItem(),
					element);
		} else {
			virtualManager.notVisibleAdded(element, index);

		}
	}

	/**
	 * Create a new row.  Callers can only use the returned object locally and before
	 * making the next call on the viewer since it may be re-used for subsequent method
	 * calls.
	 *
	 * @param style
	 *            the style for the new row
	 * @param rowIndex
	 *            the index of the row or -1 if the row is appended at the end
	 * @return the newly created row
	 */
	protected abstract ViewerRow internalCreateNewRowPart(int style,
			int rowIndex);

	/**
	 * Adds the given element to this table viewer. If this viewer does not have
	 * a sorter, the element is added at the end; otherwise the element is
	 * inserted at the appropriate position.
	 * <p>
	 * This method should be called (by the content provider) when a single
	 * element has been added to the model, in order to cause the viewer to
	 * accurately reflect the model. This method only affects the viewer, not
	 * the model. Note that there is another method for efficiently processing
	 * the simultaneous addition of multiple elements.
	 * </p>
	 *
	 * @param element
	 *            the element to add
	 */
	public void add(Object element) {
		add(new Object[] { element });
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#doFindInputItem(java.lang.Object)
	 */
	@Override
	protected Widget doFindInputItem(Object element) {
		if (equals(element, getRoot())) {
			return getControl();
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#doFindItem(java.lang.Object)
	 */
	@Override
	protected Widget doFindItem(Object element) {

		Item[] children = doGetItems();
		for (int i = 0; i < children.length; i++) {
			Item item = children[i];
			Object data = item.getData();
			if (data != null && equals(data, element)) {
				return item;
			}
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#doUpdateItem(org.eclipse.swt.widgets.Widget,
	 *      java.lang.Object, boolean)
	 */
	@Override
	protected void doUpdateItem(Widget widget, Object element, boolean fullMap) {
		boolean oldBusy = isBusy();
		setBusy(true);
		try {
			if (widget instanceof Item) {
				final Item item = (Item) widget;

				// remember element we are showing
				if (fullMap) {
					associate(element, item);
				} else {
					Object data = item.getData();
					if (data != null) {
						unmapElement(data, item);
					}
					item.setData(element);
					mapElement(element, item);
				}

				int columnCount = doGetColumnCount();
				if (columnCount == 0)
					columnCount = 1;// If there are no columns do the first one

				ViewerRow viewerRowFromItem = getViewerRowFromItem(item);

				boolean isVirtual = (getControl().getStyle() & SWT.VIRTUAL) != 0;

				// If the control is virtual, we cannot use the cached viewer row object. See bug 188663.
				if (isVirtual) {
					viewerRowFromItem = (ViewerRow) viewerRowFromItem.clone();
				}

				// Also enter loop if no columns added. See 1G9WWGZ: JFUIF:WINNT -
				// TableViewer with 0 columns does not work
				for (int column = 0; column < columnCount || column == 0; column++) {
					ViewerColumn columnViewer = getViewerColumn(column);
					ViewerCell cellToUpdate = updateCell(viewerRowFromItem,
							column, element);

					// If the control is virtual, we cannot use the cached cell object. See bug 188663.
					if (isVirtual) {
						cellToUpdate = new ViewerCell(cellToUpdate.getViewerRow(), cellToUpdate.getColumnIndex(), element);
					}

					columnViewer.refresh(cellToUpdate);

					// clear cell (see bug 201280)
					updateCell(null, 0, null);

					// As it is possible for user code to run the event
					// loop check here.
					if (item.isDisposed()) {
						unmapElement(element, item);
						return;
					}

				}

			}
		} finally {
			setBusy(oldBusy);
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.ColumnViewer#getColumnViewerOwner(int)
	 */
	@Override
	protected Widget getColumnViewerOwner(int columnIndex) {
		int columnCount = doGetColumnCount();

		if (columnIndex < 0
				|| (columnIndex > 0 && columnIndex >= columnCount)) {
			return null;
		}

		if (columnCount == 0)// Hang it off the table if it
			return getControl();

		return doGetColumn(columnIndex);
	}

	/**
	 * Returns the element with the given index from this table viewer. Returns
	 * <code>null</code> if the index is out of range.
	 * <p>
	 * This method is internal to the framework.
	 * </p>
	 *
	 * @param index
	 *            the zero-based index
	 * @return the element at the given index, or <code>null</code> if the
	 *         index is out of range
	 */
	public Object getElementAt(int index) {
		if (index >= 0 && index < doGetItemCount()) {
			Item i = doGetItem(index);
			if (i != null) {
				return i.getData();
			}
		}
		return null;
	}

	/**
	 * The table viewer implementation of this <code>Viewer</code> framework
	 * method returns the label provider, which in the case of table viewers
	 * will be an instance of either <code>ITableLabelProvider</code> or
	 * <code>ILabelProvider</code>. If it is an
	 * <code>ITableLabelProvider</code>, then it provides a separate label
	 * text and image for each column. If it is an <code>ILabelProvider</code>,
	 * then it provides only the label text and image for the first column, and
	 * any remaining columns are blank.
	 */
	@Override
	public IBaseLabelProvider getLabelProvider() {
		return super.getLabelProvider();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#getSelectionFromWidget()
	 */
	@Override
	protected List getSelectionFromWidget() {
		if (virtualManager != null) {
			return getVirtualSelection();
		}
		Widget[] items = doGetSelection();
		ArrayList list = new ArrayList(items.length);
		for (int i = 0; i < items.length; i++) {
			Widget item = items[i];
			Object e = item.getData();
			if (e != null) {
				list.add(e);
			}
		}
		return list;
	}

	/**
	 * Get the virtual selection. Avoid calling SWT whenever possible to prevent
	 * extra widget creation.
	 *
	 * @return List of Object
	 */

	private List getVirtualSelection() {

		List result = new ArrayList();
		int[] selectionIndices = doGetSelectionIndices();
		if (getContentProvider() instanceof ILazyContentProvider) {
			ILazyContentProvider lazy = (ILazyContentProvider) getContentProvider();
			for (int i = 0; i < selectionIndices.length; i++) {
				int selectionIndex = selectionIndices[i];
				lazy.updateElement(selectionIndex);// Start the update
				// check for the case where the content provider changed the number of items
				if (selectionIndex < doGetItemCount()) {
					Object element = doGetItem(selectionIndex).getData();
					// Only add the element if it got updated.
					// If this is done deferred the selection will
					// be incomplete until selection is finished.
					if (element != null) {
						result.add(element);
					}
				}
			}
		} else {
			for (int i = 0; i < selectionIndices.length; i++) {
				Object element = null;
				// See if it is cached
				int selectionIndex = selectionIndices[i];
				if (selectionIndex < virtualManager.cachedElements.length) {
					element = virtualManager.cachedElements[selectionIndex];
				}
				if (element == null) {
					// Not cached so try the item's data
					Item item = doGetItem(selectionIndex);
					element = item.getData();
				}
				if (element != null) {
					result.add(element);
				}
			}

		}
		return result;
	}

	/**
	 * @param element
	 *            the element to insert
	 * @return the index where the item should be inserted.
	 */
	protected int indexForElement(Object element) {
		ViewerComparator comparator = getComparator();
		if (comparator == null) {
			return doGetItemCount();
		}
		int count = doGetItemCount();
		int min = 0, max = count - 1;
		while (min <= max) {
			int mid = (min + max) / 2;
			Object data = doGetItem(mid).getData();
			int compare = comparator.compare(this, data, element);
			if (compare == 0) {
				// find first item > element
				while (compare == 0) {
					++mid;
					if (mid >= count) {
						break;
					}
					data = doGetItem(mid).getData();
					compare = comparator.compare(this, data, element);
				}
				return mid;
			}
			if (compare < 0) {
				min = mid + 1;
			} else {
				max = mid - 1;
			}
		}
		return min;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object,
	 *      java.lang.Object)
	 */
	@Override
	protected void inputChanged(Object input, Object oldInput) {
		getControl().setRedraw(false);
		try {
			preservingSelection(new Runnable() {
				public void run() {
					internalRefresh(getRoot());
				}
			});
		} finally {
			getControl().setRedraw(true);
		}
	}

	/**
	 * Inserts the given element into this table viewer at the given position.
	 * If this viewer has a sorter, the position is ignored and the element is
	 * inserted at the correct position in the sort order.
	 * <p>
	 * This method should be called (by the content provider) when elements have
	 * been added to the model, in order to cause the viewer to accurately
	 * reflect the model. This method only affects the viewer, not the model.
	 * </p>
	 *
	 * @param element
	 *            the element
	 * @param position
	 *            a 0-based position relative to the model, or -1 to indicate
	 *            the last position
	 */
	public void insert(Object element, int position) {
		applyEditorValue();
		if (getComparator() != null || hasFilters()) {
			add(element);
			return;
		}
		if (position == -1) {
			position = doGetItemCount();
		}
		if (checkBusy())
			return;
		createItem(element, position);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#internalRefresh(java.lang.Object)
	 */
	@Override
	protected void internalRefresh(Object element) {
		internalRefresh(element, true);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#internalRefresh(java.lang.Object,
	 *      boolean)
	 */
	@Override
	protected void internalRefresh(Object element, boolean updateLabels) {
		applyEditorValue();
		if (element == null || equals(element, getRoot())) {
			if (virtualManager == null) {
				internalRefreshAll(updateLabels);
			} else {
				internalVirtualRefreshAll();
			}
		} else {
			Widget w = findItem(element);
			if (w != null) {
				updateItem(w, element);
			}
		}
	}

	/**
	 * Refresh all with virtual elements.
	 *
	 * @since 3.1
	 */
	private void internalVirtualRefreshAll() {

		Object root = getRoot();
		IContentProvider contentProvider = getContentProvider();

		// Invalidate for lazy
		if (!(contentProvider instanceof ILazyContentProvider)
				&& (contentProvider instanceof IStructuredContentProvider)) {
			// Don't cache if the root is null but cache if it is not lazy.
			if (root != null) {
				virtualManager.cachedElements = getSortedChildren(root);
				doSetItemCount(virtualManager.cachedElements.length);
			}
		}
		doClearAll();
	}

	/**
	 * Refresh all of the elements of the table. update the labels if
	 * updatLabels is true;
	 *
	 * @param updateLabels
	 *
	 * @since 3.1
	 */
	private void internalRefreshAll(boolean updateLabels) {
		// the parent

		// in the code below, it is important to do all disassociates
		// before any associates, since a later disassociate can undo an
		// earlier associate
		// e.g. if (a, b) is replaced by (b, a), the disassociate of b to
		// item 1 could undo
		// the associate of b to item 0.

		Object[] children = getSortedChildren(getRoot());
		Item[] items = doGetItems();
		int min = Math.min(children.length, items.length);
		for (int i = 0; i < min; ++i) {

			Item item = items[i];

			// if the element is unchanged, update its label if appropriate
			if (equals(children[i], item.getData())) {
				if (updateLabels) {
					updateItem(item, children[i]);
				} else {
					// associate the new element, even if equal to the old
					// one,
					// to remove stale references (see bug 31314)
					associate(children[i], item);
				}
			} else {
				// updateItem does an associate(...), which can mess up
				// the associations if the order of elements has changed.
				// E.g. (a, b) -> (b, a) first replaces a->0 with b->0, then
				// replaces b->1 with a->1, but this actually removes b->0.
				// So, if the object associated with this item has changed,
				// just disassociate it for now, and update it below.
				// we also need to reset the item (set its text,images etc. to
				// default values) because the label decorators rely on this
				disassociate(item);
				doClear(i);
			}
		}
		// dispose of all items beyond the end of the current elements
		if (min < items.length) {
			for (int i = items.length; --i >= min;) {

				disassociate(items[i]);
			}
			if (virtualManager != null) {
				virtualManager.removeIndicesFromTo(min, items.length - 1);
			}
			doRemove(min, items.length - 1);
		}
		// Workaround for 1GDGN4Q: ITPUI:WIN2000 - TableViewer icons get
		// scrunched
		if (doGetItemCount() == 0) {
			doRemoveAll();
		}
		// Update items which were disassociated above
		for (int i = 0; i < min; ++i) {

			Item item = items[i];
			if (item.getData() == null) {
				updateItem(item, children[i]);
			}
		}
		// add any remaining elements
		for (int i = min; i < children.length; ++i) {
			createItem(children[i], i);
		}
	}

	/**
	 * Removes the given elements from this table viewer.
	 *
	 * @param elements
	 *            the elements to remove
	 */
	private void internalRemove(final Object[] elements) {
		Object input = getInput();
		for (int i = 0; i < elements.length; ++i) {
			if (equals(elements[i], input)) {
				boolean oldBusy = isBusy();
				setBusy(false);
				try {
					setInput(null);
				} finally {
					setBusy(oldBusy);
				}
				return;
			}
		}
		// use remove(int[]) rather than repeated TableItem.dispose() calls
		// to allow SWT to optimize multiple removals
		int[] indices = new int[elements.length];
		int count = 0;
		for (int i = 0; i < elements.length; ++i) {
			Widget w = findItem(elements[i]);
			if (w == null && virtualManager != null) {
				int index = virtualManager.find(elements[i]);
				if (index != -1) {
					indices[count++] = index;
				}
			} else if (w instanceof Item) {
				Item item = (Item) w;
				disassociate(item);
				indices[count++] = doIndexOf(item);
			}
		}
		if (count < indices.length) {
			System.arraycopy(indices, 0, indices = new int[count], 0, count);
		}
		if (virtualManager != null) {
			virtualManager.removeIndices(indices);
		}
		doRemove(indices);

		// Workaround for 1GDGN4Q: ITPUI:WIN2000 - TableViewer icons get
		// scrunched
		if (doGetItemCount() == 0) {
			doRemoveAll();
		}
	}

	/**
	 * Removes the given elements from this table viewer. The selection is
	 * updated if required.
	 * <p>
	 * This method should be called (by the content provider) when elements have
	 * been removed from the model, in order to cause the viewer to accurately
	 * reflect the model. This method only affects the viewer, not the model.
	 * </p>
	 *
	 * @param elements
	 *            the elements to remove
	 */
	public void remove(final Object[] elements) {
		assertElementsNotNull(elements);
		if (checkBusy())
			return;
		if (elements.length == 0) {
			return;
		}
		preservingSelection(new Runnable() {
			public void run() {
				internalRemove(elements);
			}
		});
	}

	/**
	 * Removes the given element from this table viewer. The selection is
	 * updated if necessary.
	 * <p>
	 * This method should be called (by the content provider) when a single
	 * element has been removed from the model, in order to cause the viewer to
	 * accurately reflect the model. This method only affects the viewer, not
	 * the model. Note that there is another method for efficiently processing
	 * the simultaneous removal of multiple elements.
	 * </p>
	 * <strong>NOTE:</strong> removing an object from a virtual table will
	 * decrement the itemCount.
	 *
	 * @param element
	 *            the element
	 */
	public void remove(Object element) {
		remove(new Object[] { element });
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#reveal(java.lang.Object)
	 */
	@Override
	public void reveal(Object element) {
		Assert.isNotNull(element);
		Widget w = findItem(element);
		if (w instanceof Item) {
			doShowItem((Item) w);
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List,
	 *      boolean)
	 */
	@Override
	protected void setSelectionToWidget(List list, boolean reveal) {
		if (list == null) {
			doDeselectAll();
			return;
		}

		if (virtualManager != null) {
			virtualSetSelectionToWidget(list, reveal);
			return;
		}

		// This is vital to use doSetSelection because on SWT-Table on Win32 this will also
		// move the focus to this row (See bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=198665)
		if (reveal) {
			int size = list.size();
			Item[] items = new Item[size];
			int count = 0;
			for (int i = 0; i < size; ++i) {
				Object o = list.get(i);
				Widget w = findItem(o);
				if (w instanceof Item) {
					Item item = (Item) w;
					items[count++] = item;
				}
			}
			if (count < size) {
				System.arraycopy(items, 0, items = new Item[count], 0, count);
			}
			doSetSelection(items);
		} else {
			doDeselectAll(); // Clear the selection
			if( ! list.isEmpty() ) {
				int[] indices = new int[list.size()];

				Iterator it = list.iterator();
				Item[] items = doGetItems();
				Object modelElement;

				int count = 0;
				while( it.hasNext() ) {
					modelElement = it.next();
					boolean found = false;
					for (int i = 0; i < items.length && !found; i++) {
						if (equals(modelElement, items[i].getData())) {
							indices[count++] = i;
							found = true;
						}
					}
				}

				if (count < indices.length) {
					System.arraycopy(indices, 0, indices = new int[count], 0, count);
				}

				doSelect(indices);
			}
		}
	}

	/**
	 * Set the selection on a virtual table
	 *
	 * @param list
	 *            The elements to set
	 * @param reveal
	 *            Whether or not reveal the first item.
	 */
	private void virtualSetSelectionToWidget(List list, boolean reveal) {
		int size = list.size();
		int[] indices = new int[list.size()];

		Item firstItem = null;
		int count = 0;
		HashSet virtualElements = new HashSet();
		for (int i = 0; i < size; ++i) {
			Object o = list.get(i);
			Widget w = findItem(o);
			if (w instanceof Item) {
				Item item = (Item) w;
				indices[count++] = doIndexOf(item);
				if (firstItem == null) {
					firstItem = item;
				}
			} else {
				virtualElements.add(o);
			}
		}

		if (getContentProvider() instanceof ILazyContentProvider) {
			ILazyContentProvider provider = (ILazyContentProvider) getContentProvider();

			// Now go through it again until all is done or we are no longer
			// virtual
			// This may create all items so it is not a good
			// idea in general.
			// Use #setSelection (int [] indices,boolean reveal) instead
			for (int i = 0; virtualElements.size() > 0 && i < doGetItemCount(); i++) {
				provider.updateElement(i);
				Item item = doGetItem(i);
				if (virtualElements.contains(item.getData())) {
					indices[count++] = i;
					virtualElements.remove(item.getData());
					if (firstItem == null) {
						firstItem = item;
					}
				}
			}
		} else {

			if (count != list.size()) {// As this is expensive skip it if all
				// have been found
				// If it is not lazy we can use the cache
				for (int i = 0; i < virtualManager.cachedElements.length; i++) {
					Object element = virtualManager.cachedElements[i];
					if (virtualElements.contains(element)) {
						Item item = doGetItem(i);
						item.getText();// Be sure to fire the update
						indices[count++] = i;
						virtualElements.remove(element);
						if (firstItem == null) {
							firstItem = item;
						}
					}
				}
			}
		}

		if (count < size) {
			System.arraycopy(indices, 0, indices = new int[count], 0, count);
		}

		if (reveal) {
			doSetSelection(indices);
		} else {
			doDeselectAll();
			doSelect(indices);
		}

	}

	/**
	 * Set the item count of the receiver.
	 *
	 * @param count
	 *            the new table size.
	 *
	 * @since 3.1
	 */
	public void setItemCount(int count) {
		if (checkBusy())
			return;
		int oldCount = doGetItemCount();
		if (count < oldCount) {
			// need to disassociate elements that are being disposed
			for (int i = count; i < oldCount; i++) {
				Item item = doGetItem(i);
				if (item.getData() != null) {
					disassociate(item);
				}
			}
		}
		doSetItemCount(count);
		if (virtualManager != null) {
			virtualManager.adjustCacheSize(count);
		}
		getControl().redraw();
	}

	/**
	 * Replace the element at the given index with the given element. This
	 * method will not call the content provider to verify. <strong>Note that
	 * this method will materialize a TableItem the given index.</strong>.
	 * 
	 * @param element
	 * @param index
	 * @see ILazyContentProvider
	 * 
	 * @since 3.1
	 */
	public void replace(Object element, int index) {
		if (checkBusy())
			return;
		Item item = doGetItem(index);
		refreshItem(item, element);
	}

	/**
	 * Clear the table item at the specified index
	 *
	 * @param index
	 *            the index of the table item to be cleared
	 *
	 * @since 3.1
	 */
	public void clear(int index) {
		Item item = doGetItem(index);
		if (item.getData() != null) {
			disassociate(item);
		}
		doClear(index);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#getRawChildren(java.lang.Object)
	 */
	@Override
	protected Object[] getRawChildren(Object parent) {

		Assert.isTrue(!(getContentProvider() instanceof ILazyContentProvider),
				"Cannot get raw children with an ILazyContentProvider");//$NON-NLS-1$
		return super.getRawChildren(parent);

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.viewers.StructuredViewer#assertContentProviderType(org.eclipse.jface.viewers.IContentProvider)
	 */
	@Override
	protected void assertContentProviderType(IContentProvider provider) {
		Assert.isTrue(provider instanceof IStructuredContentProvider
				|| provider instanceof ILazyContentProvider);
	}

	/**
	 * Searches the receiver's list starting at the first item (index 0) until
	 * an item is found that is equal to the argument, and returns the index of
	 * that item. If no item is found, returns -1.
	 *
	 * @param item
	 *            the search item
	 * @return the index of the item
	 *
	 * @since 3.3
	 */
	protected abstract int doIndexOf(Item item);

	/**
	 * Returns the number of items contained in the receiver.
	 *
	 * @return the number of items
	 *
	 * @since 3.3
	 */
	protected abstract int doGetItemCount();

	/**
	 * Sets the number of items contained in the receiver.
	 *
	 * @param count
	 *            the number of items
	 *
	 * @since 3.3
	 */
	protected abstract void doSetItemCount(int count);

	/**
	 * Returns a (possibly empty) array of TableItems which are the items in the
	 * receiver.
	 *
	 * @return the items in the receiver
	 *
	 * @since 3.3
	 */
	protected abstract Item[] doGetItems();

	/**
	 * Returns the column at the given, zero-relative index in the receiver.
	 * Throws an exception if the index is out of range. Columns are returned in
	 * the order that they were created. If no TableColumns were created by the
	 * programmer, this method will throw ERROR_INVALID_RANGE despite the fact
	 * that a single column of data may be visible in the table. This occurs
	 * when the programmer uses the table like a list, adding items but never
	 * creating a column.
	 *
	 * @param index
	 *            the index of the column to return
	 * @return the column at the given index
	 * @exception IllegalArgumentException -
	 *                if the index is not between 0 and the number of elements
	 *                in the list minus 1 (inclusive)
	 *
	 * @since 3.3
	 */
	protected abstract Widget doGetColumn(int index);

	/**
	 * Returns the item at the given, zero-relative index in the receiver.
	 * Throws an exception if the index is out of range.
	 *
	 * @param index
	 *            the index of the item to return
	 * @return the item at the given index
	 * @exception IllegalArgumentException -
	 *                if the index is not between 0 and the number of elements
	 *                in the list minus 1 (inclusive)
	 *
	 * @since 3.3
	 */
	protected abstract Item doGetItem(int index);

	/**
	 * Returns an array of {@link Item} that are currently selected in the
	 * receiver. The order of the items is unspecified. An empty array indicates
	 * that no items are selected.
	 *
	 * @return an array representing the selection
	 *
	 * @since 3.3
	 */
	protected abstract Item[] doGetSelection();

	/**
	 * Returns the zero-relative indices of the items which are currently
	 * selected in the receiver. The order of the indices is unspecified. The
	 * array is empty if no items are selected.
	 *
	 * @return an array representing the selection
	 *
	 * @since 3.3
	 */
	protected abstract int[] doGetSelectionIndices();

	/**
	 * Clears all the items in the receiver. The text, icon and other attributes
	 * of the items are set to their default values. If the table was created
	 * with the <code>SWT.VIRTUAL</code> style, these attributes are requested
	 * again as needed.
	 *
	 * @since 3.3
	 */
	protected abstract void doClearAll();

	/**
	 * Resets the given item in the receiver. The text, icon and other attributes
	 * of the item are set to their default values.
	 *
	 * @param item the item to reset
	 *
	 * @since 3.3
	 */
	protected abstract void doResetItem(Item item);

	/**
	 * Removes the items from the receiver which are between the given
	 * zero-relative start and end indices (inclusive).
	 *
	 * @param start
	 *            the start of the range
	 * @param end
	 *            the end of the range
	 *
	 * @exception IllegalArgumentException -
	 *                if either the start or end are not between 0 and the
	 *                number of elements in the list minus 1 (inclusive)
	 *
	 * @since 3.3
	 */
	protected abstract void doRemove(int start, int end);

	/**
	 * Removes all of the items from the receiver.
	 *
	 * @since 3.3
	 */
	protected abstract void doRemoveAll();

	/**
	 * Removes the items from the receiver's list at the given zero-relative
	 * indices.
	 *
	 * @param indices
	 *            the array of indices of the items
	 *
	 * @exception IllegalArgumentException -
	 *                if the array is null, or if any of the indices is not
	 *                between 0 and the number of elements in the list minus 1
	 *                (inclusive)
	 *
	 * @since 3.3
	 */
	protected abstract void doRemove(int[] indices);

	/**
	 * Shows the item. If the item is already showing in the receiver, this
	 * method simply returns. Otherwise, the items are scrolled until the item
	 * is visible.
	 *
	 * @param item
	 *            the item to be shown
	 *
	 * @exception IllegalArgumentException -
	 *                if the item is null
	 *
	 * @since 3.3
	 */
	protected abstract void doShowItem(Item item);

	/**
	 * Deselects all selected items in the receiver.
	 *
	 * @since 3.3
	 */
	protected abstract void doDeselectAll();

	/**
	 * Sets the receiver's selection to be the given array of items. The current selection is
	 * cleared before the new items are selected, and if necessary the receiver is scrolled to make
	 * the new selection visible.
	 * <p>
	 * Items that are not in the receiver are ignored. If the receiver is single-select and multiple
	 * items are specified, then all items are ignored.
	 * </p>
	 * 
	 * @param items the array of items
	 * 
	 * @exception IllegalArgumentException - if the array of items is null
	 * 
	 * @since 3.3
	 */
	protected abstract void doSetSelection(Item[] items);

	/**
	 * Shows the selection. If the selection is already showing in the receiver,
	 * this method simply returns. Otherwise, the items are scrolled until the
	 * selection is visible.
	 *
	 * @since 3.3
	 */
	protected abstract void doShowSelection();

	/**
	 * Selects the items at the given zero-relative indices in the receiver. The current selection
	 * is cleared before the new items are selected, and if necessary the receiver is scrolled to
	 * make the new selection visible.
	 * <p>
	 * Indices that are out of range and duplicate indices are ignored. If the receiver is
	 * single-select and multiple indices are specified, then all indices are ignored.
	 * </p>
	 * 
	 * @param indices the indices of the items to select
	 * 
	 * @exception IllegalArgumentException - if the array of indices is null
	 * 
	 * @since 3.3
	 */
	protected abstract void doSetSelection(int[] indices);

	/**
	 * Clears the item at the given zero-relative index in the receiver. The
	 * text, icon and other attributes of the item are set to the default value.
	 * If the table was created with the <code>SWT.VIRTUAL</code> style, these
	 * attributes are requested again as needed.
	 *
	 * @param index
	 *            the index of the item to clear
	 *
	 * @exception IllegalArgumentException -
	 *                if the index is not between 0 and the number of elements
	 *                in the list minus 1 (inclusive)
	 *
	 * @see SWT#VIRTUAL
	 * @see SWT#SetData
	 *
	 * @since 3.3
	 */
	protected abstract void doClear(int index);



	/**
	 * Selects the items at the given zero-relative indices in the receiver.
	 * The current selection is not cleared before the new items are selected.
	 * <p>
	 * If the item at a given index is not selected, it is selected.
	 * If the item at a given index was already selected, it remains selected.
	 * Indices that are out of range and duplicate indices are ignored.
	 * If the receiver is single-select and multiple indices are specified,
	 * then all indices are ignored.
	 * </p>
	 *
	 * @param indices the array of indices for the items to select
	 *
	 * @exception IllegalArgumentException - if the array of indices is null
	 *
	 */
	protected abstract void doSelect(int[] indices);

}

Back to the top