1 /*******************************************************************************
2  * Copyright (c) 2018 Red Hat and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Red Hat - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.tests.gtk.snippets;
15 
16 
17 import java.util.Random;
18 
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.layout.RowData;
21 import org.eclipse.swt.layout.RowLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Table;
27 import org.eclipse.swt.widgets.TableColumn;
28 import org.eclipse.swt.widgets.TableItem;
29 
30 public class Bug499850_VirtualTableHang {
31 
32 	static final int COUNT = 100000;
33 
main(String[] args)34 	public static void main(String[] args) {
35 		Random random = new Random();
36 		final int[][] data = new int[COUNT][];
37 		for (int i = 0; i < data.length; i++) {
38 			data[i] = new int[] {i, random.nextInt()};
39 		}
40 
41 		Display display = new Display ();
42 		final Shell shell = new Shell (display);
43 		shell.setLayout (new RowLayout (SWT.VERTICAL));
44 		final Table table = new Table (shell, SWT.VIRTUAL | SWT.MULTI);
45 		table.setHeaderVisible(true);
46 		table.setLinesVisible(true);
47 		table.setItemCount(COUNT);
48 		TableColumn eventColumn = new TableColumn(table, SWT.NONE);
49 		eventColumn.setText("Event");
50 		eventColumn.setWidth(200);
51 		TableColumn dataColumn = new TableColumn(table, SWT.NONE);
52 		dataColumn.setText("Data");
53 		dataColumn.setWidth(200);
54 		table.addListener (SWT.SetData, event -> {
55 			TableItem item = (TableItem) event.item;
56 			int index = table.indexOf (item);
57 			int[] datum = data[index + 1];
58 			item.setText(new String[] {Integer.toString(datum[0]),
59 					Integer.toString(datum[1]) });
60 		});
61 		table.setLayoutData (new RowData (400, 300));
62 		Button button = new Button (shell, SWT.PUSH);
63 		button.setText ("Add Items");
64 		button.addListener (SWT.Selection, event -> {
65 			table.setItemCount (COUNT);
66 			shell.layout ();
67 		});
68 		Button removeButton;
69 		removeButton = new Button(shell, SWT.PUSH);
70 		removeButton.setText("Remove All");
71 		final Label label = new Label(shell, SWT.NONE);
72 		label.setLayoutData(new RowData (400, 30));
73 		removeButton.addListener(SWT.Selection, e -> {
74 			long t1 = System.currentTimeMillis ();
75 			table.removeAll();
76 			long t2 = System.currentTimeMillis ();
77 			label.setText ("Items: " + COUNT + ", Time to remove: " + (t2 - t1) + " (ms)");
78 		});
79 		shell.pack ();
80 		shell.open ();
81 		while (!shell.isDisposed ()) {
82 			if (!display.readAndDispatch ()) display.sleep ();
83 		}
84 		display.dispose ();
85 	}
86 }