View | Details | Raw Unified | Return to bug 60117 | Differences between
and this patch

Collapse All | Expand All

(-)plugin.xml (-1 / +10 lines)
Lines 315-321 Link Here
315
            class="org.eclipse.ui.tests.decorators.DecoratorTableTreeView"
315
            class="org.eclipse.ui.tests.decorators.DecoratorTableTreeView"
316
            category="org.eclipse.ui.tests.decoratorCategory"
316
            category="org.eclipse.ui.tests.decoratorCategory"
317
            name="Table Tree Test"
317
            name="Table Tree Test"
318
            id="org.eclipse.ui.tests.decorator.TableTreeTest"/>      
318
            id="org.eclipse.ui.tests.decorator.TableTreeTest"/>
319
      <view
320
            class="org.eclipse.jface.tests.viewers.interactive.ConcurrentTableTestView"
321
            name="Concurrent Table Test View"
322
            id="org.eclipse.ui.tests.concurrentTableTest"/>      
323
      <view
324
            class="org.eclipse.jface.tests.viewers.interactive.TestMarkerView"
325
            name="Test Marker View"
326
            id="org.eclipse.ui.tests.testMarkerView"/>      
327
319
            
328
            
320
   </extension>
329
   </extension>
321
   <extension
330
   <extension
(-)Eclipse JFace Tests/org/eclipse/jface/tests/viewers/AllTests.java (+1 lines)
Lines 23-28 Link Here
23
23
24
    public static Test suite() {
24
    public static Test suite() {
25
        TestSuite suite = new TestSuite();
25
        TestSuite suite = new TestSuite();
26
        suite.addTest(new TestSuite(LazySortedCollectionTest.class));
26
        suite.addTest(new TestSuite(TreeViewerTest.class));
27
        suite.addTest(new TestSuite(TreeViewerTest.class));
27
        suite.addTest(new TestSuite(TableViewerTest.class));
28
        suite.addTest(new TestSuite(TableViewerTest.class));
28
        suite.addTest(new TestSuite(TableTreeViewerTest.class));
29
        suite.addTest(new TestSuite(TableTreeViewerTest.class));
(-)Eclipse (+562 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import java.util.Arrays;
14
import java.util.Iterator;
15
import java.util.TreeSet;
16
17
import junit.framework.Assert;
18
import junit.framework.Test;
19
import junit.framework.TestCase;
20
import junit.framework.TestSuite;
21
22
import org.eclipse.jface.viewers.deferred.LazySortedCollection;
23
24
/**
25
 * @since 3.1
26
 */
27
public class LazySortedCollectionTest extends TestCase {
28
    
29
    public static void main(String[] args) {
30
        junit.textui.TestRunner.run(LazySortedCollectionTest.class);
31
    }
32
    
33
    public static Test suite() {
34
        return new TestSuite(LazySortedCollectionTest.class);
35
    }
36
    
37
    private TestComparator comparator;
38
    private TestComparator comparisonComparator;
39
    
40
    // All operations will be done on both collections -- after each operation, the result
41
    // will be compared
42
    private LazySortedCollection collection;
43
    private TreeSet comparisonCollection;
44
45
    /**
46
     * Please don't add or remove from this set -- we rely on the exact insertion order
47
     * to get full coverage from the removal tests.
48
     */
49
    private String[] se = new String[] {
50
            "v00 aaaaaa",
51
            "v01 apple",
52
            "v02 booger",
53
            "v03 car",
54
            "v04 dog",
55
            "v05 elephant",
56
            "v06 fox",
57
            "v07 goose",
58
            "v08 hippie",
59
            "v09 iguana",
60
            "v10 junk",
61
            "v11 karma",
62
            "v12 lemon",
63
            "v13 mongoose",
64
            "v14 noodle",
65
            "v15 opal",
66
            "v16 pumpkin",
67
            "v17 quirks",
68
            "v18 resteraunt",
69
            "v19 soap",
70
            "v20 timmy",
71
            "v21 ugly",
72
            "v22 virus",
73
            "v23 wigwam",
74
            "v24 xerxes",
75
            "v25 yellow",
76
            "v26 zero"
77
    };
78
    
79
    /**
80
     * Please don't mess with the insertion order here or add or remove from this set -- 
81
     * we rely on the exact order in order to get full coverage for the removal tests.
82
     */
83
    private String[] elements = new String[] {
84
            se[19],
85
            se[7],
86
            se[6],
87
            se[1],
88
            se[20],
89
            se[8],
90
            se[0],
91
            se[23],
92
            se[17],
93
            se[18],
94
            se[24],
95
            se[25],
96
            se[10],
97
            se[5],
98
            se[15],
99
            se[16],
100
            se[21],
101
            se[26],
102
            se[22],
103
            se[3],
104
            se[9],
105
            se[4],
106
            se[11],
107
            se[12],
108
            se[13],
109
            se[14],
110
            se[2]  
111
        };
112
113
    
114
    public static void printArray(Object[] array) {
115
        for (int i = 0; i < array.length; i++) {
116
            Object object = array[i];
117
            
118
            System.out.println("[" + i + "] = " + array[i]);
119
        }
120
    }
121
    
122
    /* (non-Javadoc)
123
     * @see junit.framework.TestCase#setUp()
124
     */
125
    protected void setUp() throws Exception {
126
        System.out.println("--- " + getName());
127
        
128
        comparator = new TestComparator();
129
        collection = new LazySortedCollection(comparator);
130
        // Ensure a predictable tree structure
131
        collection.enableRandomization = false;
132
     
133
        comparisonComparator = new TestComparator();
134
        comparisonCollection = new TreeSet(comparisonComparator);
135
        
136
        addAll(elements);
137
        
138
        super.setUp();
139
    }
140
    
141
    /* (non-Javadoc)
142
     * @see junit.framework.TestCase#tearDown()
143
     */
144
    protected void tearDown() throws Exception {
145
        System.out.println("Comparisons required by lazy collection: " + comparator.comparisons);
146
        System.out.println("Comparisons required by reference implementation: " + comparisonComparator.comparisons);
147
        System.out.println("");
148
        
149
        super.tearDown();
150
    }
151
    
152
    /**
153
     * Computes the entries that are expected to lie in the given range. The result
154
     * is sorted.
155
     * 
156
     * @param start
157
     * @param length
158
     * @return
159
     * @since 3.1
160
     */
161
    private Object[] computeExpectedElementsInRange(int start, int length) {
162
        int counter = 0;
163
        
164
        Iterator iter = comparisonCollection.iterator();
165
        while(iter.hasNext() && counter < start) {
166
            iter.next();
167
            counter++;
168
        }
169
        
170
        Object[] result = new Object[length];
171
        for (int i = 0; i < result.length; i++) {
172
            result[i] = iter.next(); 
173
        }
174
                
175
        return result;
176
    }
177
178
    private void addAll(Object[] elements) {
179
        collection.addAll(elements);
180
        comparisonCollection.addAll(Arrays.asList(elements));
181
    }
182
    
183
    private void add(Object toAdd) {
184
        collection.add(toAdd);
185
        comparisonCollection.add(toAdd);
186
    }
187
    
188
    private void remove(Object toRemove) {
189
        collection.remove(toRemove);
190
        comparisonCollection.remove(toRemove);
191
    }
192
    
193
    private void removeRange(int start, int length) {
194
        collection.removeRange(start, length);
195
        
196
        Object[] expected = computeExpectedElementsInRange(start, length);
197
198
        // Alternative remove implementation that removes one-at-a-time
199
        for (int i = 0; i < expected.length; i++) {
200
            Object object = expected[i];
201
            
202
            comparisonCollection.remove(expected[i]);
203
        }
204
    }
205
    
206
    private void clear() {
207
        collection.clear();
208
        comparisonCollection.clear();
209
    }
210
    
211
    /**
212
     * Force the collection to sort all elements immediately.
213
     * 
214
     * @since 3.1
215
     */
216
    private void forceFullSort() {
217
        queryRange(0, elements.length, true);
218
    }
219
    
220
    private void assertContentsValid() {
221
        queryRange(0, comparisonCollection.size(), false);
222
        Assert.assertEquals(comparisonCollection.size(), collection.size());
223
        Assert.assertEquals(comparisonCollection.isEmpty(), collection.isEmpty());
224
    }
225
    
226
    private void assertIsPermutation(Object[] array1, Object[] array2) {
227
        Object[] sorted1 = new Object[array1.length];
228
        System.arraycopy(array1, 0, sorted1, 0, array1.length);
229
        Arrays.sort(sorted1, new TestComparator());
230
        
231
        Object[] sorted2 = new Object[array2.length];
232
        System.arraycopy(array2, 0, sorted2, 0, array2.length);
233
        Arrays.sort(sorted2, new TestComparator());
234
        
235
        assertArrayEquals(sorted1, sorted2);
236
    }
237
238
    /**
239
     * Queries the collection for the given range, and throws an assertation failure if the 
240
     * result was unexpected. Assumes that the "elements" array was initially added and that nothing
241
     * has been added or removed since.
242
     * 
243
     * @param start
244
     * @param length
245
     * @param sorted
246
     * @since 3.1
247
     */
248
    private Object[] queryRange(int start, int length, boolean sorted) {
249
        Object[] result = new Object[length];
250
        
251
        int returnValue = collection.getRange(result, start, sorted);
252
        
253
        Assert.assertEquals(returnValue, length);
254
        
255
        Object[] expectedResult = computeExpectedElementsInRange(start, length);
256
        
257
        if (sorted) {
258
            // If the result is supposed to be sorted, it will match the expected
259
            // result exactly
260
            assertArrayEquals(expectedResult, result);
261
        } else {
262
            // Otherwise, the result merely needs to be a permutation of the
263
            // expected result.
264
            assertIsPermutation(result, expectedResult);
265
        }
266
        
267
        collection.testInvariants();
268
        
269
        return result;
270
    }
271
    
272
    private void assertArrayEquals(Object[] array1, Object[] array2) {
273
        for (int i = 0; i < array1.length; i++) {
274
275
            Assert.assertEquals(array1[i], array2[i]);
276
        }        
277
    }
278
    
279
    public void testComparisonCount() {
280
        Assert.assertTrue("additions should not require any comparisons", comparator.comparisons == 0);
281
        
282
        queryRange(0, elements.length, false);
283
284
        Assert.assertTrue("requesting the complete set of unsorted elements should not require any comparisons", comparator.comparisons == 0);
285
    }
286
    
287
    /**
288
     * Ensure that no comparisons are required for range queries once the collection is fully sorted
289
     * 
290
     * @since 3.1
291
     */
292
    public void testSortAll() {
293
        // Test that sorting the entire array works
294
        queryRange(0, elements.length, true);
295
        
296
        int comparisons = comparator.comparisons;
297
        
298
        // Ensure that subsequent operations require no comparisons
299
        queryRange(elements.length - 10, 10, true);
300
        queryRange(0, 10, false);
301
        
302
        Assert.assertEquals("Once the lazy collection is fully sorted, it should not require further comparisons", 
303
                comparisons, comparator.comparisons);
304
    }
305
    
306
    /**
307
     * Tests LazySortedCollection.removeNode(int) when removing a leaf node
308
     */
309
    public void testRemoveLeafNode() {
310
        forceFullSort();
311
        remove(se[9]);
312
        assertContentsValid();
313
    }
314
315
    /**
316
     * Tests LazySortedCollection.removeNode(int) when removing a node with no left child
317
     */
318
    public void testRemoveNodeWithNoLeftChild() {
319
        forceFullSort();
320
        remove(se[23]);
321
        assertContentsValid();
322
    }
323
324
    /**
325
     * Tests LazySortedCollection.removeNode(int) when removing a node with no right child
326
     * 
327
     * @since 3.1
328
     */
329
    public void testRemoveNodeWithNoRightChild() {
330
        forceFullSort();
331
        remove(se[13]);
332
        assertContentsValid();
333
    }
334
    
335
    /**
336
     * Tests LazySortedCollection.remove when removing the root node
337
     * 
338
     * @since 3.1
339
     */
340
    public void testRemoveRootNode() {
341
        forceFullSort();
342
        remove(se[19]);
343
        assertContentsValid();
344
    }
345
346
    /**
347
     * Tests LazySortedCollection.remove when the removal
348
     * will require swapping with a non-leaf node. (The descendent with the closest value
349
     * in the largest subtree has at least 1 child).
350
     * 
351
     * @since 3.1
352
     */
353
    public void testRemoveWhereSwappedNodeIsntLeaf() {
354
        forceFullSort();
355
        remove(se[14]);
356
        assertContentsValid();
357
    }
358
    
359
    /**
360
     * Tests LazySortedCollection.remove when the removal
361
     * will require swapping with a non-leaf node, and both the removed node and the swapped
362
     * node contain unsorted children.
363
     * 
364
     * @since 3.1
365
     */
366
    public void testRemoveWithUnsortedSwap() {
367
        // Ensure that we've sorted nodes 13 and 14 
368
        queryRange(14, 1, true);
369
        queryRange(13, 1, true);
370
        
371
        // Add some unsorted nodes that will become children of the node with value "v13 mongoose"
372
        addAll(new String[] {"v13 n", "v13 l"});
373
        // Ensure that the new nodes are pushed down to node 14 by querying its parent (currently at position 8)
374
        queryRange(8, 1, true);
375
        
376
        // Add some unsorted nodes that will become children of the node with value "v14 noodle" 
377
        addAll(new String[] {"v14 m", "v14 o"});
378
        // Push down the unsorted nodes by querying for the parent of "v14 noodle" 
379
        // (the parent is currently at position 7) 
380
        queryRange(7, 1, true);
381
        
382
        // Now remove node with value "v14 noodle" -- this should swap it with the node "v13 mongoose", requiring
383
        // both sets of unsorted children to be dealt with
384
        
385
        remove(se[14]);
386
        assertContentsValid();
387
    }
388
    
389
    /**
390
     * Remove an element from an initially unsorted collection
391
     * 
392
     * @since 3.1
393
     */
394
    public void testRemoveFromUnsorted() {
395
        remove(se[10]);
396
        assertContentsValid();
397
    }
398
    
399
    /**
400
     * Remove the root from an initially-unsorted collection
401
     * 
402
     * @since 3.1
403
     */
404
    public void testRemoveRootFromUnsorted() {
405
        remove(se[19]);
406
        assertContentsValid();
407
    }
408
    
409
    /**
410
     * Ensure that removing an element that isn't in the collection is a no-op
411
     * 
412
     * @since 3.1
413
     */
414
    public void testRemoveUnknown() {
415
        remove("some unknown element");
416
        assertContentsValid();
417
    }
418
    
419
    /**
420
     * Tests that the swaps during removal don't mess up the internal hashmap.
421
     * Perform a removal that will require a swap, add a new item, then
422
     * remove the item that was previously swapped. If the hashmap was not updated,
423
     * then the removed item may still be pointing to its old index and the 
424
     * new item will be removed instead. 
425
     * 
426
     * @since 3.1
427
     */
428
    public void testRemovePreviouslySwappedNode() {
429
        queryRange(0, elements.length, true);
430
        remove(se[14]);
431
        // Add a new item -- should reuse the same index used by the previous item
432
        add("something new");
433
        assertContentsValid();
434
        remove(se[13]);
435
        assertContentsValid();
436
    }
437
    
438
    /**
439
     * Remove all nodes using removeRange
440
     * 
441
     * @since 3.1
442
     */
443
    public void testRemoveFullRange() {
444
        removeRange(0, se.length);
445
        assertContentsValid();
446
    }
447
    
448
    /**
449
     * Remove a range that includes the start
450
     * 
451
     * @since 3.1
452
     */
453
    public void testRemoveFromStart() {
454
        removeRange(0, se.length / 2);
455
        assertContentsValid();
456
    }
457
    
458
    /**
459
     * Remove a range that includes the last node
460
     * 
461
     * @since 3.1
462
     */
463
    public void testRemoveFromEnd() {
464
        int start = se.length / 2;
465
        removeRange(start, se.length - start);
466
        assertContentsValid();
467
    }
468
    
469
    /**
470
     * Remove a range that includes the root, but leaves nodes in both subtrees intact.
471
     * 
472
     * @since 3.1
473
     */
474
    public void testRemoveIncludingRoot() {
475
        removeRange(14, 6);
476
        assertContentsValid();
477
    }
478
    
479
    /**
480
     * Test boundary conditions: 
481
     * 
482
     * Tests moving an entire right subtree, and a left subtree including the tree itself
483
     */
484
    public void testRemoveRightSubtree() {
485
        int start = 20;
486
        removeRange(9, 5);
487
        assertContentsValid();
488
    }
489
490
    /**
491
     * Test boundary conditions: Tests moving an entire left subtree
492
     */
493
    public void testRemoveLeftSubtree() {
494
        removeRange(3, 4);
495
        assertContentsValid();
496
    }
497
498
    /**
499
     * Test boundary conditions: Tests moving an entire left subtree including the tree itself
500
     */
501
    public void testRemoveRightIncludingRoot() {
502
        removeRange(3, 5);
503
        assertContentsValid();
504
    }
505
    
506
    public void testClear() {
507
        clear();
508
        assertContentsValid();
509
    }
510
    
511
    public void testClearSorted() {
512
        forceFullSort();
513
        clear();
514
        assertContentsValid();
515
    }
516
    
517
    //    
518
//    
519
//    public static void testAdditions() {
520
//        TestComparator comparator = new TestComparator();
521
//        LazySortedCollection collection = new LazySortedCollection(comparator);
522
//
523
//        addSomeElements(collection);
524
//        
525
//        System.out.println("comparisons after add = " + comparator.comparisons);
526
//        comparator.comparisons = 0;
527
//        
528
//        // Getfirst10Elements
529
//        Object[] first10Elements = new Object[10];
530
//        collection.getFirst(first10Elements, false);
531
//        System.out.println("first 10 elements:");
532
//        printArray(first10Elements);
533
//        System.out.println("comparisons after getFirst = " + comparator.comparisons);
534
//        comparator.comparisons = 0;
535
//        
536
//        collection.print();
537
//        
538
//        // remove the 10th element
539
//        collection.remove(first10Elements[9]);
540
//        
541
//        collection.print();
542
//
543
////        
544
////        collection.getFirst(first10Elements, true);
545
////        System.out.println("first 10 elements (sorted):");
546
////        printArray(first10Elements);
547
////        System.out.println("comparisons after getFirst = " + comparator.comparisons);
548
////        comparator.comparisons = 0;
549
////        
550
////        
551
////        
552
////        // get elements 8-13
553
////        Object[] eightThrough14 = new Object[7];
554
////        collection.getRange(eightThrough14, 8, false);
555
////        System.out.println("elements 8-14:");
556
////        printArray(eightThrough14);
557
////        System.out.println("comparisons after 8-14 query = " + comparator.comparisons);
558
////        comparator.comparisons = 0;
559
//        
560
//        collection.print();
561
//    }
562
}
(-)Eclipse (+80 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import java.util.ArrayList;
14
import java.util.Collection;
15
16
import org.eclipse.jface.viewers.deferred.AbstractConcurrentContentProvider;
17
import org.eclipse.jface.viewers.deferred.IConcurrentContentProviderListener;
18
19
/**
20
 * @since 3.1
21
 */
22
public class ListContentProvider extends AbstractConcurrentContentProvider {
23
    
24
    private ArrayList data = new ArrayList();
25
    
26
    /* (non-Javadoc)
27
     * @see org.eclipse.jface.viewers.deferred.IConcurrentContentProvider#getElements(org.eclipse.core.runtime.IProgressMonitor)
28
     */
29
    public Object[] getElements() {
30
        return data.toArray();
31
    }
32
33
    public void set(Object[] newContents) {
34
        clear();
35
        addAll(newContents);
36
    }
37
    
38
    public void clear() {
39
        Object[] removed = data.toArray();
40
        data.clear();
41
        fireRemove(removed);
42
    }
43
    
44
    public void addAll(Object[] toAdd) {
45
        for (int i = 0; i < toAdd.length; i++) {
46
            Object object = toAdd[i];
47
            
48
            data.add(object);
49
        }
50
        
51
        fireAdd(toAdd);
52
    }
53
    
54
    public void addAll(Collection toAdd) {
55
        addAll(toAdd.toArray());
56
    }
57
    
58
    public void changeAll(Object[] changed) {
59
        fireUpdate(changed);
60
    }
61
    
62
    public void removeAll(Object[] toRemove) {
63
        for (int i = 0; i < toRemove.length; i++) {
64
            Object object = toRemove[i];
65
            
66
            data.add(object);
67
        }        
68
        
69
        fireRemove(toRemove);
70
    }
71
72
    /* (non-Javadoc)
73
     * @see org.eclipse.jface.viewers.deferred.IConcurrentContentProvider#requestUpdate(org.eclipse.jface.viewers.deferred.IConcurrentContentProviderListener)
74
     */
75
    public void requestUpdate(IConcurrentContentProviderListener listener) {
76
        listener.setContents(getElements());
77
        
78
    }
79
    
80
}
(-)Eclipse (+30 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import java.util.Comparator;
14
15
/**
16
 * @since 3.1
17
 */
18
public class TestComparator implements Comparator {
19
20
    public volatile int comparisons = 0;
21
    
22
    /* (non-Javadoc)
23
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
24
     */
25
    public int compare(Object arg0, Object arg1) {
26
        comparisons++;
27
28
        return (arg0.toString()).compareTo(arg1.toString());
29
    }
30
}
(-)Eclipse (+142 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import java.util.Comparator;
14
15
import org.eclipse.jface.viewers.deferred.ConcurrentTableManager;
16
import org.eclipse.jface.viewers.deferred.IConcurrentContentProvider;
17
import org.eclipse.jface.viewers.deferred.IConcurrentTable;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.DisposeEvent;
20
import org.eclipse.swt.events.DisposeListener;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Event;
23
import org.eclipse.swt.widgets.Listener;
24
import org.eclipse.swt.widgets.Table;
25
import org.eclipse.swt.widgets.TableColumn;
26
import org.eclipse.swt.widgets.TableItem;
27
28
/**
29
 * @since 3.1
30
 */
31
public class TestConcurrentTable {
32
33
    private Table tbl; 
34
    private ConcurrentTableManager manager;
35
    private IConcurrentContentProvider provider;
36
    private Comparator sortOrder;
37
    private int limit = -1;
38
    private DisposeListener dl = new DisposeListener() {
39
40
        public void widgetDisposed(DisposeEvent e) {
41
            disposed();
42
        }
43
44
    };
45
    
46
    private IConcurrentTable concurrentTable = new IConcurrentTable() {
47
48
        public void flushCache(Object element) {
49
            
50
        }
51
52
        public void update(int itemIndex, Object element) {
53
            TableItem item = tbl.getItem(itemIndex);
54
55
            item.setText(0, element.toString());            
56
        }
57
58
        public void setTotalItems(int total) {
59
            tbl.setItemCount(total);
60
            tbl.redraw();
61
            scrolled();            
62
        }
63
        
64
    };
65
    
66
    private Listener scrollListener = new Listener() {
67
68
        public void handleEvent(Event event) {
69
            scrolled();
70
        }
71
        
72
    };
73
    
74
    public TestConcurrentTable(Composite parent, Comparator sortOrder) {
75
        tbl = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.VIRTUAL);
76
        tbl.setHeaderVisible(true);
77
        TableColumn column = new TableColumn(tbl, SWT.CENTER);
78
        column.setText("test");
79
        column.setWidth(400);
80
        tbl.getVerticalBar().addListener(SWT.Selection, scrollListener);
81
        tbl.getVerticalBar().addListener(SWT.Show, scrollListener);
82
        tbl.getVerticalBar().addListener(SWT.Hide, scrollListener);
83
        tbl.addListener(SWT.Resize, scrollListener);
84
        this.sortOrder = sortOrder;
85
    }
86
    
87
    public void refresh() {
88
        manager.refresh();
89
    }
90
    
91
    public void setLimit(int newLimit) {
92
        if (manager != null) {
93
            manager.setLimit(newLimit);
94
        }
95
        this.limit = newLimit;
96
    }
97
    
98
    public int getLimit() {
99
        return limit;
100
    }
101
    
102
    /**
103
     * 
104
     * @since 3.1
105
     */
106
    protected void scrolled() {
107
        if (manager != null) {
108
            manager.setRangeOfInterest(tbl.getTopIndex(), 
109
                    (tbl.getBounds().height / tbl.getItemHeight()) + 1);
110
        }
111
    }
112
113
    public Table getControl() {
114
        return tbl;
115
    }
116
    
117
    public void setContentProvider(IConcurrentContentProvider contentProvider) {
118
        tbl.removeAll();
119
        disposeManager();
120
        
121
        manager = new ConcurrentTableManager(concurrentTable, contentProvider, sortOrder, tbl.getDisplay());
122
        manager.setLimit(limit);
123
        manager.refresh();
124
        scrolled();
125
    }
126
    
127
    protected void disposed() {
128
        disposeManager();
129
    }
130
    
131
    private void disposeManager() {
132
        if (manager != null) {
133
            manager.dispose();
134
            manager = null;
135
        }
136
    }
137
        
138
    public Table getTable() {
139
        return tbl;
140
    }
141
142
}
(-)Eclipse (+30 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import org.eclipse.swt.widgets.Display;
14
import org.eclipse.swt.widgets.Shell;
15
16
/**
17
 * @since 3.1
18
 */
19
public class VirtualTableTest {
20
21
    public static void main(String[] args) {
22
		Display display = new Display ();
23
		final Shell shell = new Shell (display);
24
				
25
		while (!shell.isDisposed()) {
26
			if (!display.readAndDispatch ()) display.sleep ();
27
		}
28
		display.dispose ();
29
    }
30
}
(-)Eclipse (+188 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers.interactive;
12
13
import java.util.ArrayList;
14
import java.util.Random;
15
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.Status;
19
import org.eclipse.jface.tests.viewers.ListContentProvider;
20
import org.eclipse.jface.tests.viewers.TestComparator;
21
import org.eclipse.jface.tests.viewers.TestConcurrentTable;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.events.SelectionAdapter;
24
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.layout.GridData;
26
import org.eclipse.swt.layout.GridLayout;
27
import org.eclipse.swt.widgets.Button;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Label;
30
import org.eclipse.ui.part.ViewPart;
31
import org.eclipse.ui.progress.WorkbenchJob;
32
33
/**
34
 * @since 3.1
35
 */
36
public class ConcurrentTableTestView extends ViewPart {
37
38
    private TestConcurrentTable table;
39
    private TestComparator comparator = new TestComparator() {
40
        
41
        /* (non-Javadoc)
42
         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
43
         */
44
        public int compare(Object arg0, Object arg1) {
45
//            try {
46
//                // Insert a bogus delay to simulate doing work
47
//                Thread.sleep(1);
48
//            } catch (InterruptedException e) {
49
//            }
50
51
//            int delay = 2; // Time to spin the CPU for (milliseconds)
52
//            
53
//            // Do some work to occupy time 
54
//            int counter = 0;
55
//            long timestamp = System.currentTimeMillis();
56
//            while (System.currentTimeMillis() < timestamp + delay) {
57
//                counter++;
58
//            }
59
            
60
            int result = super.compare(arg0, arg1);
61
            
62
            scheduleComparisonUpdate();
63
            
64
            return result;
65
        };
66
    };
67
    
68
    private WorkbenchJob updateCountRunnable = new WorkbenchJob("") {
69
        
70
        /* (non-Javadoc)
71
         * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
72
         */
73
        public IStatus runInUIThread(IProgressMonitor monitor) {
74
            updateCount.setText("Comparison count = " + comparator.comparisons);
75
            return Status.OK_STATUS;
76
        }
77
    };
78
    
79
    
80
    
81
    private Label updateCount;
82
    private ListContentProvider contentProvider = new ListContentProvider();
83
    private Random rand = new Random();
84
    
85
    /* (non-Javadoc)
86
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
87
     */
88
    public void createPartControl(Composite temp) {
89
        Composite parent = new Composite(temp, SWT.NONE);
90
        GridLayout layout = new GridLayout();
91
        layout.numColumns = 2;
92
        
93
        parent.setLayout(layout);
94
        
95
        // Create the table
96
        {
97
	        table = new TestConcurrentTable(parent, comparator);
98
	        table.setLimit(400);
99
	        GridData data = new GridData(GridData.FILL_BOTH);
100
	        table.getControl().setLayoutData(data);
101
	        table.setContentProvider(contentProvider);
102
        }
103
        
104
        // Create the buttons
105
        Composite buttonBar = new Composite(parent, SWT.NONE);
106
        buttonBar.setLayoutData(new GridData(GridData.FILL_BOTH));
107
        GridLayout buttonBarLayout = new GridLayout();
108
        buttonBarLayout.numColumns = 1;
109
        buttonBar.setLayout(buttonBarLayout);
110
        {
111
112
            updateCount = new Label(buttonBar, SWT.NONE);
113
            updateCount.setLayoutData(new GridData(GridData.FILL_BOTH));
114
115
            Button resetCountButton = new Button(buttonBar, SWT.PUSH);
116
            resetCountButton.setLayoutData(new GridData(GridData.FILL_BOTH));
117
            resetCountButton.setText("Reset comparison count");
118
	        resetCountButton.addSelectionListener(new SelectionAdapter() {
119
		        /* (non-Javadoc)
120
	             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
121
	             */
122
	            public void widgetSelected(SelectionEvent e) {
123
	                comparator.comparisons = 0;
124
	                scheduleComparisonUpdate();
125
	            } 
126
	        });
127
	        
128
	        Button testButton = new Button(buttonBar, SWT.PUSH);
129
	        testButton.setLayoutData(new GridData(GridData.FILL_BOTH));
130
	        testButton.setText("add 1000 elements");
131
	        testButton.addSelectionListener(new SelectionAdapter() {
132
		        /* (non-Javadoc)
133
	             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
134
	             */
135
	            public void widgetSelected(SelectionEvent e) {
136
	                addRandomElements(1000);
137
	            } 
138
	        });
139
	        
140
	        Button removeButton = new Button(buttonBar, SWT.PUSH);
141
	        removeButton.setLayoutData(new GridData(GridData.FILL_BOTH));
142
	        removeButton.setText("remove all");
143
	        removeButton.addSelectionListener(new SelectionAdapter() {
144
		        /* (non-Javadoc)
145
	             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
146
	             */
147
	            public void widgetSelected(SelectionEvent e) {
148
	                clear();
149
	            } 
150
	        });
151
152
        }
153
    }
154
    
155
    
156
    /**
157
     * 
158
     * @since 3.1
159
     */
160
    protected void scheduleComparisonUpdate() {
161
        updateCountRunnable.schedule(100);
162
    }
163
164
165
166
    public void addRandomElements(int amount) {
167
        
168
        ArrayList tempList = new ArrayList();
169
170
        for (int counter = 0; counter < amount; counter++) {
171
            tempList.add("" + rand.nextLong() + " " + counter );
172
        }
173
        
174
        contentProvider.addAll(tempList);
175
    }
176
    
177
    public void clear() {
178
        contentProvider.clear();
179
    }
180
    
181
    /* (non-Javadoc)
182
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
183
     */
184
    public void setFocus() {
185
        // TODO Auto-generated method stub
186
187
    }
188
}
(-)Eclipse (+105 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers.interactive;
12
13
import java.util.Random;
14
15
import org.eclipse.jface.tests.viewers.TestComparator;
16
import org.eclipse.jface.tests.viewers.TestConcurrentTable;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.layout.GridData;
21
import org.eclipse.swt.layout.GridLayout;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.ui.IViewSite;
25
import org.eclipse.ui.PartInitException;
26
import org.eclipse.ui.part.ViewPart;
27
import org.eclipse.ui.views.markers.internal.MarkerProvider;
28
29
/**
30
 * @since 3.1
31
 */
32
public class TestMarkerView extends ViewPart {
33
34
    private TestConcurrentTable table;
35
    private TestComparator comparator = new TestComparator();
36
    private MarkerProvider contentProvider;
37
    private Random rand = new Random();
38
    
39
    /* (non-Javadoc)
40
     * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite)
41
     */
42
    public void init(IViewSite site) throws PartInitException {
43
        super.init(site);
44
        
45
        contentProvider = new MarkerProvider();
46
    }
47
    /* (non-Javadoc)
48
     * @see org.eclipse.ui.IWorkbenchPart#dispose()
49
     */
50
    public void dispose() {
51
        super.dispose();
52
        
53
        contentProvider.dispose();
54
    }
55
    
56
    /* (non-Javadoc)
57
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
58
     */
59
    public void createPartControl(Composite temp) {
60
        Composite parent = new Composite(temp, SWT.NONE);
61
        GridLayout layout = new GridLayout();
62
        layout.numColumns = 2;
63
        
64
        parent.setLayout(layout);
65
        
66
        // Create the table
67
        {
68
	        table = new TestConcurrentTable(parent, comparator);
69
	        //table.setLimit(400);
70
	        GridData data = new GridData(GridData.FILL_BOTH);
71
	        table.getControl().setLayoutData(data);
72
	        table.setContentProvider(contentProvider);
73
        }
74
        
75
        // Create the buttons
76
        Composite buttonBar = new Composite(parent, SWT.NONE);
77
        buttonBar.setLayoutData(new GridData(GridData.FILL_BOTH));
78
        GridLayout buttonBarLayout = new GridLayout();
79
        buttonBarLayout.numColumns = 1;
80
        buttonBar.setLayout(buttonBarLayout);
81
        {
82
83
            Button refreshButton = new Button(buttonBar, SWT.PUSH);
84
            refreshButton.setLayoutData(new GridData(GridData.FILL_BOTH));
85
            refreshButton.setText("Refresh");
86
	        refreshButton.addSelectionListener(new SelectionAdapter() {
87
		        /* (non-Javadoc)
88
	             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
89
	             */
90
	            public void widgetSelected(SelectionEvent e) {
91
	                table.refresh();
92
	            } 
93
	        });
94
	        
95
        }
96
    }
97
        
98
    /* (non-Javadoc)
99
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
100
     */
101
    public void setFocus() {
102
        // TODO Auto-generated method stub
103
104
    }
105
}
(-)plugin.xml (-1 / +10 lines)
Lines 315-321 Link Here
315
            class="org.eclipse.ui.tests.decorators.DecoratorTableTreeView"
315
            class="org.eclipse.ui.tests.decorators.DecoratorTableTreeView"
316
            category="org.eclipse.ui.tests.decoratorCategory"
316
            category="org.eclipse.ui.tests.decoratorCategory"
317
            name="Table Tree Test"
317
            name="Table Tree Test"
318
            id="org.eclipse.ui.tests.decorator.TableTreeTest"/>      
318
            id="org.eclipse.ui.tests.decorator.TableTreeTest"/>
319
      <view
320
            class="org.eclipse.jface.tests.viewers.interactive.ConcurrentTableTestView"
321
            name="Concurrent Table Test View"
322
            id="org.eclipse.ui.tests.concurrentTableTest"/>      
323
      <view
324
            class="org.eclipse.jface.tests.viewers.interactive.TestMarkerView"
325
            name="Test Marker View"
326
            id="org.eclipse.ui.tests.testMarkerView"/>      
327
319
            
328
            
320
   </extension>
329
   </extension>
321
   <extension
330
   <extension
(-)Eclipse JFace Tests/org/eclipse/jface/tests/viewers/AllTests.java (+1 lines)
Lines 23-28 Link Here
23
23
24
    public static Test suite() {
24
    public static Test suite() {
25
        TestSuite suite = new TestSuite();
25
        TestSuite suite = new TestSuite();
26
        suite.addTest(new TestSuite(LazySortedCollectionTest.class));
26
        suite.addTest(new TestSuite(TreeViewerTest.class));
27
        suite.addTest(new TestSuite(TreeViewerTest.class));
27
        suite.addTest(new TestSuite(TableViewerTest.class));
28
        suite.addTest(new TestSuite(TableViewerTest.class));
28
        suite.addTest(new TestSuite(TableTreeViewerTest.class));
29
        suite.addTest(new TestSuite(TableTreeViewerTest.class));
(-)Eclipse (+562 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import java.util.Arrays;
14
import java.util.Iterator;
15
import java.util.TreeSet;
16
17
import junit.framework.Assert;
18
import junit.framework.Test;
19
import junit.framework.TestCase;
20
import junit.framework.TestSuite;
21
22
import org.eclipse.jface.viewers.deferred.LazySortedCollection;
23
24
/**
25
 * @since 3.1
26
 */
27
public class LazySortedCollectionTest extends TestCase {
28
    
29
    public static void main(String[] args) {
30
        junit.textui.TestRunner.run(LazySortedCollectionTest.class);
31
    }
32
    
33
    public static Test suite() {
34
        return new TestSuite(LazySortedCollectionTest.class);
35
    }
36
    
37
    private TestComparator comparator;
38
    private TestComparator comparisonComparator;
39
    
40
    // All operations will be done on both collections -- after each operation, the result
41
    // will be compared
42
    private LazySortedCollection collection;
43
    private TreeSet comparisonCollection;
44
45
    /**
46
     * Please don't add or remove from this set -- we rely on the exact insertion order
47
     * to get full coverage from the removal tests.
48
     */
49
    private String[] se = new String[] {
50
            "v00 aaaaaa",
51
            "v01 apple",
52
            "v02 booger",
53
            "v03 car",
54
            "v04 dog",
55
            "v05 elephant",
56
            "v06 fox",
57
            "v07 goose",
58
            "v08 hippie",
59
            "v09 iguana",
60
            "v10 junk",
61
            "v11 karma",
62
            "v12 lemon",
63
            "v13 mongoose",
64
            "v14 noodle",
65
            "v15 opal",
66
            "v16 pumpkin",
67
            "v17 quirks",
68
            "v18 resteraunt",
69
            "v19 soap",
70
            "v20 timmy",
71
            "v21 ugly",
72
            "v22 virus",
73
            "v23 wigwam",
74
            "v24 xerxes",
75
            "v25 yellow",
76
            "v26 zero"
77
    };
78
    
79
    /**
80
     * Please don't mess with the insertion order here or add or remove from this set -- 
81
     * we rely on the exact order in order to get full coverage for the removal tests.
82
     */
83
    private String[] elements = new String[] {
84
            se[19],
85
            se[7],
86
            se[6],
87
            se[1],
88
            se[20],
89
            se[8],
90
            se[0],
91
            se[23],
92
            se[17],
93
            se[18],
94
            se[24],
95
            se[25],
96
            se[10],
97
            se[5],
98
            se[15],
99
            se[16],
100
            se[21],
101
            se[26],
102
            se[22],
103
            se[3],
104
            se[9],
105
            se[4],
106
            se[11],
107
            se[12],
108
            se[13],
109
            se[14],
110
            se[2]  
111
        };
112
113
    
114
    public static void printArray(Object[] array) {
115
        for (int i = 0; i < array.length; i++) {
116
            Object object = array[i];
117
            
118
            System.out.println("[" + i + "] = " + array[i]);
119
        }
120
    }
121
    
122
    /* (non-Javadoc)
123
     * @see junit.framework.TestCase#setUp()
124
     */
125
    protected void setUp() throws Exception {
126
        System.out.println("--- " + getName());
127
        
128
        comparator = new TestComparator();
129
        collection = new LazySortedCollection(comparator);
130
        // Ensure a predictable tree structure
131
        collection.enableRandomization = false;
132
     
133
        comparisonComparator = new TestComparator();
134
        comparisonCollection = new TreeSet(comparisonComparator);
135
        
136
        addAll(elements);
137
        
138
        super.setUp();
139
    }
140
    
141
    /* (non-Javadoc)
142
     * @see junit.framework.TestCase#tearDown()
143
     */
144
    protected void tearDown() throws Exception {
145
        System.out.println("Comparisons required by lazy collection: " + comparator.comparisons);
146
        System.out.println("Comparisons required by reference implementation: " + comparisonComparator.comparisons);
147
        System.out.println("");
148
        
149
        super.tearDown();
150
    }
151
    
152
    /**
153
     * Computes the entries that are expected to lie in the given range. The result
154
     * is sorted.
155
     * 
156
     * @param start
157
     * @param length
158
     * @return
159
     * @since 3.1
160
     */
161
    private Object[] computeExpectedElementsInRange(int start, int length) {
162
        int counter = 0;
163
        
164
        Iterator iter = comparisonCollection.iterator();
165
        while(iter.hasNext() && counter < start) {
166
            iter.next();
167
            counter++;
168
        }
169
        
170
        Object[] result = new Object[length];
171
        for (int i = 0; i < result.length; i++) {
172
            result[i] = iter.next(); 
173
        }
174
                
175
        return result;
176
    }
177
178
    private void addAll(Object[] elements) {
179
        collection.addAll(elements);
180
        comparisonCollection.addAll(Arrays.asList(elements));
181
    }
182
    
183
    private void add(Object toAdd) {
184
        collection.add(toAdd);
185
        comparisonCollection.add(toAdd);
186
    }
187
    
188
    private void remove(Object toRemove) {
189
        collection.remove(toRemove);
190
        comparisonCollection.remove(toRemove);
191
    }
192
    
193
    private void removeRange(int start, int length) {
194
        collection.removeRange(start, length);
195
        
196
        Object[] expected = computeExpectedElementsInRange(start, length);
197
198
        // Alternative remove implementation that removes one-at-a-time
199
        for (int i = 0; i < expected.length; i++) {
200
            Object object = expected[i];
201
            
202
            comparisonCollection.remove(expected[i]);
203
        }
204
    }
205
    
206
    private void clear() {
207
        collection.clear();
208
        comparisonCollection.clear();
209
    }
210
    
211
    /**
212
     * Force the collection to sort all elements immediately.
213
     * 
214
     * @since 3.1
215
     */
216
    private void forceFullSort() {
217
        queryRange(0, elements.length, true);
218
    }
219
    
220
    private void assertContentsValid() {
221
        queryRange(0, comparisonCollection.size(), false);
222
        Assert.assertEquals(comparisonCollection.size(), collection.size());
223
        Assert.assertEquals(comparisonCollection.isEmpty(), collection.isEmpty());
224
    }
225
    
226
    private void assertIsPermutation(Object[] array1, Object[] array2) {
227
        Object[] sorted1 = new Object[array1.length];
228
        System.arraycopy(array1, 0, sorted1, 0, array1.length);
229
        Arrays.sort(sorted1, new TestComparator());
230
        
231
        Object[] sorted2 = new Object[array2.length];
232
        System.arraycopy(array2, 0, sorted2, 0, array2.length);
233
        Arrays.sort(sorted2, new TestComparator());
234
        
235
        assertArrayEquals(sorted1, sorted2);
236
    }
237
238
    /**
239
     * Queries the collection for the given range, and throws an assertation failure if the 
240
     * result was unexpected. Assumes that the "elements" array was initially added and that nothing
241
     * has been added or removed since.
242
     * 
243
     * @param start
244
     * @param length
245
     * @param sorted
246
     * @since 3.1
247
     */
248
    private Object[] queryRange(int start, int length, boolean sorted) {
249
        Object[] result = new Object[length];
250
        
251
        int returnValue = collection.getRange(result, start, sorted);
252
        
253
        Assert.assertEquals(returnValue, length);
254
        
255
        Object[] expectedResult = computeExpectedElementsInRange(start, length);
256
        
257
        if (sorted) {
258
            // If the result is supposed to be sorted, it will match the expected
259
            // result exactly
260
            assertArrayEquals(expectedResult, result);
261
        } else {
262
            // Otherwise, the result merely needs to be a permutation of the
263
            // expected result.
264
            assertIsPermutation(result, expectedResult);
265
        }
266
        
267
        collection.testInvariants();
268
        
269
        return result;
270
    }
271
    
272
    private void assertArrayEquals(Object[] array1, Object[] array2) {
273
        for (int i = 0; i < array1.length; i++) {
274
275
            Assert.assertEquals(array1[i], array2[i]);
276
        }        
277
    }
278
    
279
    public void testComparisonCount() {
280
        Assert.assertTrue("additions should not require any comparisons", comparator.comparisons == 0);
281
        
282
        queryRange(0, elements.length, false);
283
284
        Assert.assertTrue("requesting the complete set of unsorted elements should not require any comparisons", comparator.comparisons == 0);
285
    }
286
    
287
    /**
288
     * Ensure that no comparisons are required for range queries once the collection is fully sorted
289
     * 
290
     * @since 3.1
291
     */
292
    public void testSortAll() {
293
        // Test that sorting the entire array works
294
        queryRange(0, elements.length, true);
295
        
296
        int comparisons = comparator.comparisons;
297
        
298
        // Ensure that subsequent operations require no comparisons
299
        queryRange(elements.length - 10, 10, true);
300
        queryRange(0, 10, false);
301
        
302
        Assert.assertEquals("Once the lazy collection is fully sorted, it should not require further comparisons", 
303
                comparisons, comparator.comparisons);
304
    }
305
    
306
    /**
307
     * Tests LazySortedCollection.removeNode(int) when removing a leaf node
308
     */
309
    public void testRemoveLeafNode() {
310
        forceFullSort();
311
        remove(se[9]);
312
        assertContentsValid();
313
    }
314
315
    /**
316
     * Tests LazySortedCollection.removeNode(int) when removing a node with no left child
317
     */
318
    public void testRemoveNodeWithNoLeftChild() {
319
        forceFullSort();
320
        remove(se[23]);
321
        assertContentsValid();
322
    }
323
324
    /**
325
     * Tests LazySortedCollection.removeNode(int) when removing a node with no right child
326
     * 
327
     * @since 3.1
328
     */
329
    public void testRemoveNodeWithNoRightChild() {
330
        forceFullSort();
331
        remove(se[13]);
332
        assertContentsValid();
333
    }
334
    
335
    /**
336
     * Tests LazySortedCollection.remove when removing the root node
337
     * 
338
     * @since 3.1
339
     */
340
    public void testRemoveRootNode() {
341
        forceFullSort();
342
        remove(se[19]);
343
        assertContentsValid();
344
    }
345
346
    /**
347
     * Tests LazySortedCollection.remove when the removal
348
     * will require swapping with a non-leaf node. (The descendent with the closest value
349
     * in the largest subtree has at least 1 child).
350
     * 
351
     * @since 3.1
352
     */
353
    public void testRemoveWhereSwappedNodeIsntLeaf() {
354
        forceFullSort();
355
        remove(se[14]);
356
        assertContentsValid();
357
    }
358
    
359
    /**
360
     * Tests LazySortedCollection.remove when the removal
361
     * will require swapping with a non-leaf node, and both the removed node and the swapped
362
     * node contain unsorted children.
363
     * 
364
     * @since 3.1
365
     */
366
    public void testRemoveWithUnsortedSwap() {
367
        // Ensure that we've sorted nodes 13 and 14 
368
        queryRange(14, 1, true);
369
        queryRange(13, 1, true);
370
        
371
        // Add some unsorted nodes that will become children of the node with value "v13 mongoose"
372
        addAll(new String[] {"v13 n", "v13 l"});
373
        // Ensure that the new nodes are pushed down to node 14 by querying its parent (currently at position 8)
374
        queryRange(8, 1, true);
375
        
376
        // Add some unsorted nodes that will become children of the node with value "v14 noodle" 
377
        addAll(new String[] {"v14 m", "v14 o"});
378
        // Push down the unsorted nodes by querying for the parent of "v14 noodle" 
379
        // (the parent is currently at position 7) 
380
        queryRange(7, 1, true);
381
        
382
        // Now remove node with value "v14 noodle" -- this should swap it with the node "v13 mongoose", requiring
383
        // both sets of unsorted children to be dealt with
384
        
385
        remove(se[14]);
386
        assertContentsValid();
387
    }
388
    
389
    /**
390
     * Remove an element from an initially unsorted collection
391
     * 
392
     * @since 3.1
393
     */
394
    public void testRemoveFromUnsorted() {
395
        remove(se[10]);
396
        assertContentsValid();
397
    }
398
    
399
    /**
400
     * Remove the root from an initially-unsorted collection
401
     * 
402
     * @since 3.1
403
     */
404
    public void testRemoveRootFromUnsorted() {
405
        remove(se[19]);
406
        assertContentsValid();
407
    }
408
    
409
    /**
410
     * Ensure that removing an element that isn't in the collection is a no-op
411
     * 
412
     * @since 3.1
413
     */
414
    public void testRemoveUnknown() {
415
        remove("some unknown element");
416
        assertContentsValid();
417
    }
418
    
419
    /**
420
     * Tests that the swaps during removal don't mess up the internal hashmap.
421
     * Perform a removal that will require a swap, add a new item, then
422
     * remove the item that was previously swapped. If the hashmap was not updated,
423
     * then the removed item may still be pointing to its old index and the 
424
     * new item will be removed instead. 
425
     * 
426
     * @since 3.1
427
     */
428
    public void testRemovePreviouslySwappedNode() {
429
        queryRange(0, elements.length, true);
430
        remove(se[14]);
431
        // Add a new item -- should reuse the same index used by the previous item
432
        add("something new");
433
        assertContentsValid();
434
        remove(se[13]);
435
        assertContentsValid();
436
    }
437
    
438
    /**
439
     * Remove all nodes using removeRange
440
     * 
441
     * @since 3.1
442
     */
443
    public void testRemoveFullRange() {
444
        removeRange(0, se.length);
445
        assertContentsValid();
446
    }
447
    
448
    /**
449
     * Remove a range that includes the start
450
     * 
451
     * @since 3.1
452
     */
453
    public void testRemoveFromStart() {
454
        removeRange(0, se.length / 2);
455
        assertContentsValid();
456
    }
457
    
458
    /**
459
     * Remove a range that includes the last node
460
     * 
461
     * @since 3.1
462
     */
463
    public void testRemoveFromEnd() {
464
        int start = se.length / 2;
465
        removeRange(start, se.length - start);
466
        assertContentsValid();
467
    }
468
    
469
    /**
470
     * Remove a range that includes the root, but leaves nodes in both subtrees intact.
471
     * 
472
     * @since 3.1
473
     */
474
    public void testRemoveIncludingRoot() {
475
        removeRange(14, 6);
476
        assertContentsValid();
477
    }
478
    
479
    /**
480
     * Test boundary conditions: 
481
     * 
482
     * Tests moving an entire right subtree, and a left subtree including the tree itself
483
     */
484
    public void testRemoveRightSubtree() {
485
        int start = 20;
486
        removeRange(9, 5);
487
        assertContentsValid();
488
    }
489
490
    /**
491
     * Test boundary conditions: Tests moving an entire left subtree
492
     */
493
    public void testRemoveLeftSubtree() {
494
        removeRange(3, 4);
495
        assertContentsValid();
496
    }
497
498
    /**
499
     * Test boundary conditions: Tests moving an entire left subtree including the tree itself
500
     */
501
    public void testRemoveRightIncludingRoot() {
502
        removeRange(3, 5);
503
        assertContentsValid();
504
    }
505
    
506
    public void testClear() {
507
        clear();
508
        assertContentsValid();
509
    }
510
    
511
    public void testClearSorted() {
512
        forceFullSort();
513
        clear();
514
        assertContentsValid();
515
    }
516
    
517
    //    
518
//    
519
//    public static void testAdditions() {
520
//        TestComparator comparator = new TestComparator();
521
//        LazySortedCollection collection = new LazySortedCollection(comparator);
522
//
523
//        addSomeElements(collection);
524
//        
525
//        System.out.println("comparisons after add = " + comparator.comparisons);
526
//        comparator.comparisons = 0;
527
//        
528
//        // Getfirst10Elements
529
//        Object[] first10Elements = new Object[10];
530
//        collection.getFirst(first10Elements, false);
531
//        System.out.println("first 10 elements:");
532
//        printArray(first10Elements);
533
//        System.out.println("comparisons after getFirst = " + comparator.comparisons);
534
//        comparator.comparisons = 0;
535
//        
536
//        collection.print();
537
//        
538
//        // remove the 10th element
539
//        collection.remove(first10Elements[9]);
540
//        
541
//        collection.print();
542
//
543
////        
544
////        collection.getFirst(first10Elements, true);
545
////        System.out.println("first 10 elements (sorted):");
546
////        printArray(first10Elements);
547
////        System.out.println("comparisons after getFirst = " + comparator.comparisons);
548
////        comparator.comparisons = 0;
549
////        
550
////        
551
////        
552
////        // get elements 8-13
553
////        Object[] eightThrough14 = new Object[7];
554
////        collection.getRange(eightThrough14, 8, false);
555
////        System.out.println("elements 8-14:");
556
////        printArray(eightThrough14);
557
////        System.out.println("comparisons after 8-14 query = " + comparator.comparisons);
558
////        comparator.comparisons = 0;
559
//        
560
//        collection.print();
561
//    }
562
}
(-)Eclipse (+80 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import java.util.ArrayList;
14
import java.util.Collection;
15
16
import org.eclipse.jface.viewers.deferred.AbstractConcurrentContentProvider;
17
import org.eclipse.jface.viewers.deferred.IConcurrentContentProviderListener;
18
19
/**
20
 * @since 3.1
21
 */
22
public class ListContentProvider extends AbstractConcurrentContentProvider {
23
    
24
    private ArrayList data = new ArrayList();
25
    
26
    /* (non-Javadoc)
27
     * @see org.eclipse.jface.viewers.deferred.IConcurrentContentProvider#getElements(org.eclipse.core.runtime.IProgressMonitor)
28
     */
29
    public Object[] getElements() {
30
        return data.toArray();
31
    }
32
33
    public void set(Object[] newContents) {
34
        clear();
35
        addAll(newContents);
36
    }
37
    
38
    public void clear() {
39
        Object[] removed = data.toArray();
40
        data.clear();
41
        fireRemove(removed);
42
    }
43
    
44
    public void addAll(Object[] toAdd) {
45
        for (int i = 0; i < toAdd.length; i++) {
46
            Object object = toAdd[i];
47
            
48
            data.add(object);
49
        }
50
        
51
        fireAdd(toAdd);
52
    }
53
    
54
    public void addAll(Collection toAdd) {
55
        addAll(toAdd.toArray());
56
    }
57
    
58
    public void changeAll(Object[] changed) {
59
        fireUpdate(changed);
60
    }
61
    
62
    public void removeAll(Object[] toRemove) {
63
        for (int i = 0; i < toRemove.length; i++) {
64
            Object object = toRemove[i];
65
            
66
            data.add(object);
67
        }        
68
        
69
        fireRemove(toRemove);
70
    }
71
72
    /* (non-Javadoc)
73
     * @see org.eclipse.jface.viewers.deferred.IConcurrentContentProvider#requestUpdate(org.eclipse.jface.viewers.deferred.IConcurrentContentProviderListener)
74
     */
75
    public void requestUpdate(IConcurrentContentProviderListener listener) {
76
        listener.setContents(getElements());
77
        
78
    }
79
    
80
}
(-)Eclipse (+30 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import java.util.Comparator;
14
15
/**
16
 * @since 3.1
17
 */
18
public class TestComparator implements Comparator {
19
20
    public volatile int comparisons = 0;
21
    
22
    /* (non-Javadoc)
23
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
24
     */
25
    public int compare(Object arg0, Object arg1) {
26
        comparisons++;
27
28
        return (arg0.toString()).compareTo(arg1.toString());
29
    }
30
}
(-)Eclipse (+142 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import java.util.Comparator;
14
15
import org.eclipse.jface.viewers.deferred.ConcurrentTableManager;
16
import org.eclipse.jface.viewers.deferred.IConcurrentContentProvider;
17
import org.eclipse.jface.viewers.deferred.IConcurrentTable;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.DisposeEvent;
20
import org.eclipse.swt.events.DisposeListener;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Event;
23
import org.eclipse.swt.widgets.Listener;
24
import org.eclipse.swt.widgets.Table;
25
import org.eclipse.swt.widgets.TableColumn;
26
import org.eclipse.swt.widgets.TableItem;
27
28
/**
29
 * @since 3.1
30
 */
31
public class TestConcurrentTable {
32
33
    private Table tbl; 
34
    private ConcurrentTableManager manager;
35
    private IConcurrentContentProvider provider;
36
    private Comparator sortOrder;
37
    private int limit = -1;
38
    private DisposeListener dl = new DisposeListener() {
39
40
        public void widgetDisposed(DisposeEvent e) {
41
            disposed();
42
        }
43
44
    };
45
    
46
    private IConcurrentTable concurrentTable = new IConcurrentTable() {
47
48
        public void flushCache(Object element) {
49
            
50
        }
51
52
        public void update(int itemIndex, Object element) {
53
            TableItem item = tbl.getItem(itemIndex);
54
55
            item.setText(0, element.toString());            
56
        }
57
58
        public void setTotalItems(int total) {
59
            tbl.setItemCount(total);
60
            tbl.redraw();
61
            scrolled();            
62
        }
63
        
64
    };
65
    
66
    private Listener scrollListener = new Listener() {
67
68
        public void handleEvent(Event event) {
69
            scrolled();
70
        }
71
        
72
    };
73
    
74
    public TestConcurrentTable(Composite parent, Comparator sortOrder) {
75
        tbl = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.VIRTUAL);
76
        tbl.setHeaderVisible(true);
77
        TableColumn column = new TableColumn(tbl, SWT.CENTER);
78
        column.setText("test");
79
        column.setWidth(400);
80
        tbl.getVerticalBar().addListener(SWT.Selection, scrollListener);
81
        tbl.getVerticalBar().addListener(SWT.Show, scrollListener);
82
        tbl.getVerticalBar().addListener(SWT.Hide, scrollListener);
83
        tbl.addListener(SWT.Resize, scrollListener);
84
        this.sortOrder = sortOrder;
85
    }
86
    
87
    public void refresh() {
88
        manager.refresh();
89
    }
90
    
91
    public void setLimit(int newLimit) {
92
        if (manager != null) {
93
            manager.setLimit(newLimit);
94
        }
95
        this.limit = newLimit;
96
    }
97
    
98
    public int getLimit() {
99
        return limit;
100
    }
101
    
102
    /**
103
     * 
104
     * @since 3.1
105
     */
106
    protected void scrolled() {
107
        if (manager != null) {
108
            manager.setRangeOfInterest(tbl.getTopIndex(), 
109
                    (tbl.getBounds().height / tbl.getItemHeight()) + 1);
110
        }
111
    }
112
113
    public Table getControl() {
114
        return tbl;
115
    }
116
    
117
    public void setContentProvider(IConcurrentContentProvider contentProvider) {
118
        tbl.removeAll();
119
        disposeManager();
120
        
121
        manager = new ConcurrentTableManager(concurrentTable, contentProvider, sortOrder, tbl.getDisplay());
122
        manager.setLimit(limit);
123
        manager.refresh();
124
        scrolled();
125
    }
126
    
127
    protected void disposed() {
128
        disposeManager();
129
    }
130
    
131
    private void disposeManager() {
132
        if (manager != null) {
133
            manager.dispose();
134
            manager = null;
135
        }
136
    }
137
        
138
    public Table getTable() {
139
        return tbl;
140
    }
141
142
}
(-)Eclipse (+30 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers;
12
13
import org.eclipse.swt.widgets.Display;
14
import org.eclipse.swt.widgets.Shell;
15
16
/**
17
 * @since 3.1
18
 */
19
public class VirtualTableTest {
20
21
    public static void main(String[] args) {
22
		Display display = new Display ();
23
		final Shell shell = new Shell (display);
24
				
25
		while (!shell.isDisposed()) {
26
			if (!display.readAndDispatch ()) display.sleep ();
27
		}
28
		display.dispose ();
29
    }
30
}
(-)Eclipse (+188 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers.interactive;
12
13
import java.util.ArrayList;
14
import java.util.Random;
15
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.Status;
19
import org.eclipse.jface.tests.viewers.ListContentProvider;
20
import org.eclipse.jface.tests.viewers.TestComparator;
21
import org.eclipse.jface.tests.viewers.TestConcurrentTable;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.events.SelectionAdapter;
24
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.layout.GridData;
26
import org.eclipse.swt.layout.GridLayout;
27
import org.eclipse.swt.widgets.Button;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Label;
30
import org.eclipse.ui.part.ViewPart;
31
import org.eclipse.ui.progress.WorkbenchJob;
32
33
/**
34
 * @since 3.1
35
 */
36
public class ConcurrentTableTestView extends ViewPart {
37
38
    private TestConcurrentTable table;
39
    private TestComparator comparator = new TestComparator() {
40
        
41
        /* (non-Javadoc)
42
         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
43
         */
44
        public int compare(Object arg0, Object arg1) {
45
//            try {
46
//                // Insert a bogus delay to simulate doing work
47
//                Thread.sleep(1);
48
//            } catch (InterruptedException e) {
49
//            }
50
51
//            int delay = 2; // Time to spin the CPU for (milliseconds)
52
//            
53
//            // Do some work to occupy time 
54
//            int counter = 0;
55
//            long timestamp = System.currentTimeMillis();
56
//            while (System.currentTimeMillis() < timestamp + delay) {
57
//                counter++;
58
//            }
59
            
60
            int result = super.compare(arg0, arg1);
61
            
62
            scheduleComparisonUpdate();
63
            
64
            return result;
65
        };
66
    };
67
    
68
    private WorkbenchJob updateCountRunnable = new WorkbenchJob("") {
69
        
70
        /* (non-Javadoc)
71
         * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
72
         */
73
        public IStatus runInUIThread(IProgressMonitor monitor) {
74
            updateCount.setText("Comparison count = " + comparator.comparisons);
75
            return Status.OK_STATUS;
76
        }
77
    };
78
    
79
    
80
    
81
    private Label updateCount;
82
    private ListContentProvider contentProvider = new ListContentProvider();
83
    private Random rand = new Random();
84
    
85
    /* (non-Javadoc)
86
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
87
     */
88
    public void createPartControl(Composite temp) {
89
        Composite parent = new Composite(temp, SWT.NONE);
90
        GridLayout layout = new GridLayout();
91
        layout.numColumns = 2;
92
        
93
        parent.setLayout(layout);
94
        
95
        // Create the table
96
        {
97
	        table = new TestConcurrentTable(parent, comparator);
98
	        table.setLimit(400);
99
	        GridData data = new GridData(GridData.FILL_BOTH);
100
	        table.getControl().setLayoutData(data);
101
	        table.setContentProvider(contentProvider);
102
        }
103
        
104
        // Create the buttons
105
        Composite buttonBar = new Composite(parent, SWT.NONE);
106
        buttonBar.setLayoutData(new GridData(GridData.FILL_BOTH));
107
        GridLayout buttonBarLayout = new GridLayout();
108
        buttonBarLayout.numColumns = 1;
109
        buttonBar.setLayout(buttonBarLayout);
110
        {
111
112
            updateCount = new Label(buttonBar, SWT.NONE);
113
            updateCount.setLayoutData(new GridData(GridData.FILL_BOTH));
114
115
            Button resetCountButton = new Button(buttonBar, SWT.PUSH);
116
            resetCountButton.setLayoutData(new GridData(GridData.FILL_BOTH));
117
            resetCountButton.setText("Reset comparison count");
118
	        resetCountButton.addSelectionListener(new SelectionAdapter() {
119
		        /* (non-Javadoc)
120
	             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
121
	             */
122
	            public void widgetSelected(SelectionEvent e) {
123
	                comparator.comparisons = 0;
124
	                scheduleComparisonUpdate();
125
	            } 
126
	        });
127
	        
128
	        Button testButton = new Button(buttonBar, SWT.PUSH);
129
	        testButton.setLayoutData(new GridData(GridData.FILL_BOTH));
130
	        testButton.setText("add 1000 elements");
131
	        testButton.addSelectionListener(new SelectionAdapter() {
132
		        /* (non-Javadoc)
133
	             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
134
	             */
135
	            public void widgetSelected(SelectionEvent e) {
136
	                addRandomElements(1000);
137
	            } 
138
	        });
139
	        
140
	        Button removeButton = new Button(buttonBar, SWT.PUSH);
141
	        removeButton.setLayoutData(new GridData(GridData.FILL_BOTH));
142
	        removeButton.setText("remove all");
143
	        removeButton.addSelectionListener(new SelectionAdapter() {
144
		        /* (non-Javadoc)
145
	             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
146
	             */
147
	            public void widgetSelected(SelectionEvent e) {
148
	                clear();
149
	            } 
150
	        });
151
152
        }
153
    }
154
    
155
    
156
    /**
157
     * 
158
     * @since 3.1
159
     */
160
    protected void scheduleComparisonUpdate() {
161
        updateCountRunnable.schedule(100);
162
    }
163
164
165
166
    public void addRandomElements(int amount) {
167
        
168
        ArrayList tempList = new ArrayList();
169
170
        for (int counter = 0; counter < amount; counter++) {
171
            tempList.add("" + rand.nextLong() + " " + counter );
172
        }
173
        
174
        contentProvider.addAll(tempList);
175
    }
176
    
177
    public void clear() {
178
        contentProvider.clear();
179
    }
180
    
181
    /* (non-Javadoc)
182
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
183
     */
184
    public void setFocus() {
185
        // TODO Auto-generated method stub
186
187
    }
188
}
(-)Eclipse (+105 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.tests.viewers.interactive;
12
13
import java.util.Random;
14
15
import org.eclipse.jface.tests.viewers.TestComparator;
16
import org.eclipse.jface.tests.viewers.TestConcurrentTable;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.layout.GridData;
21
import org.eclipse.swt.layout.GridLayout;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.ui.IViewSite;
25
import org.eclipse.ui.PartInitException;
26
import org.eclipse.ui.part.ViewPart;
27
import org.eclipse.ui.views.markers.internal.MarkerProvider;
28
29
/**
30
 * @since 3.1
31
 */
32
public class TestMarkerView extends ViewPart {
33
34
    private TestConcurrentTable table;
35
    private TestComparator comparator = new TestComparator();
36
    private MarkerProvider contentProvider;
37
    private Random rand = new Random();
38
    
39
    /* (non-Javadoc)
40
     * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite)
41
     */
42
    public void init(IViewSite site) throws PartInitException {
43
        super.init(site);
44
        
45
        contentProvider = new MarkerProvider();
46
    }
47
    /* (non-Javadoc)
48
     * @see org.eclipse.ui.IWorkbenchPart#dispose()
49
     */
50
    public void dispose() {
51
        super.dispose();
52
        
53
        contentProvider.dispose();
54
    }
55
    
56
    /* (non-Javadoc)
57
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
58
     */
59
    public void createPartControl(Composite temp) {
60
        Composite parent = new Composite(temp, SWT.NONE);
61
        GridLayout layout = new GridLayout();
62
        layout.numColumns = 2;
63
        
64
        parent.setLayout(layout);
65
        
66
        // Create the table
67
        {
68
	        table = new TestConcurrentTable(parent, comparator);
69
	        //table.setLimit(400);
70
	        GridData data = new GridData(GridData.FILL_BOTH);
71
	        table.getControl().setLayoutData(data);
72
	        table.setContentProvider(contentProvider);
73
        }
74
        
75
        // Create the buttons
76
        Composite buttonBar = new Composite(parent, SWT.NONE);
77
        buttonBar.setLayoutData(new GridData(GridData.FILL_BOTH));
78
        GridLayout buttonBarLayout = new GridLayout();
79
        buttonBarLayout.numColumns = 1;
80
        buttonBar.setLayout(buttonBarLayout);
81
        {
82
83
            Button refreshButton = new Button(buttonBar, SWT.PUSH);
84
            refreshButton.setLayoutData(new GridData(GridData.FILL_BOTH));
85
            refreshButton.setText("Refresh");
86
	        refreshButton.addSelectionListener(new SelectionAdapter() {
87
		        /* (non-Javadoc)
88
	             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
89
	             */
90
	            public void widgetSelected(SelectionEvent e) {
91
	                table.refresh();
92
	            } 
93
	        });
94
	        
95
        }
96
    }
97
        
98
    /* (non-Javadoc)
99
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
100
     */
101
    public void setFocus() {
102
        // TODO Auto-generated method stub
103
104
    }
105
}

Return to bug 60117