1 /*******************************************************************************
2  * Copyright (c) 2019 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 import java.util.stream.IntStream;
19 
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.RowData;
22 import org.eclipse.swt.layout.RowLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Table;
28 import org.eclipse.swt.widgets.TableColumn;
29 import org.eclipse.swt.widgets.TableItem;
30 
31 public class Bug544282_TablePerformanceMeasure {
32 
33 	static final int COUNT = 100000;
34 	static final int START = 10;
35 	static final int END = COUNT - 100;
36 
getIndices()37 	public static int [] getIndices() {
38 		int [] res = IntStream.range(0, COUNT - 1).filter(i -> i % 2 == 0).toArray();
39 		return res;
40 	}
41 
main(String[] args)42 	public static void main(String[] args) {
43 		Random random = new Random();
44 
45 		int [] indices = getIndices();
46 
47 		final int[][] data = new int[COUNT + 1][];
48 		for (int i = 0; i < data.length; i++) {
49 			data[i] = new int[] {i, random.nextInt()};
50 		}
51 
52 		Display display = new Display ();
53 		final Shell shell = new Shell (display);
54 		shell.setLayout (new RowLayout (SWT.VERTICAL));
55 		final Table table = new Table (shell, SWT.VIRTUAL | SWT.MULTI);
56 		table.setHeaderVisible(true);
57 		table.setLinesVisible(true);
58 		table.setItemCount(COUNT);
59 		TableColumn eventColumn = new TableColumn(table, SWT.NONE);
60 		eventColumn.setText("Event");
61 		eventColumn.setWidth(200);
62 		TableColumn dataColumn = new TableColumn(table, SWT.NONE);
63 		dataColumn.setText("Data");
64 		dataColumn.setWidth(200);
65 		table.addListener (SWT.SetData, event -> {
66 			TableItem item = (TableItem) event.item;
67 			int index = table.indexOf (item);
68 			int[] datum = data[index + 1];
69 			item.setText(new String[] {Integer.toString(datum[0]),
70 					Integer.toString(datum[1]) });
71 		});
72 		table.setLayoutData (new RowData (400, 300));
73 		Button button = new Button (shell, SWT.PUSH);
74 		button.setText ("Add Items");
75 		button.addListener (SWT.Selection, event -> {
76 			table.setItemCount (COUNT);
77 			shell.layout ();
78 		});
79 		Button removeButton;
80 		removeButton = new Button(shell, SWT.PUSH);
81 		removeButton.setText("Remove All");
82 		final Label label = new Label(shell, SWT.NONE);
83 		label.setLayoutData(new RowData (400, 30));
84 		removeButton.addListener(SWT.Selection, e -> {
85 			long t1 = System.currentTimeMillis ();
86 			table.remove(0, COUNT - 1);
87 			long t2 = System.currentTimeMillis ();
88 			label.setText ("Items: " + COUNT + ", Time to remove: " + (t2 - t1) + " (ms)");
89 		});
90 
91 		Button removeSome = new Button(shell, SWT.PUSH);
92 		removeSome.setText("Remove Some");
93 		removeSome.addListener(SWT.Selection, e -> {
94 			long t1 = System.currentTimeMillis();
95 			table.remove(indices);
96 			long t2 = System.currentTimeMillis();
97 			label.setText("Items: " + indices.length + ", Time to remove: " + (t2 - t1) + " (ms)");
98 		});
99 
100 		Button removeMiddle = new Button(shell, SWT.PUSH);
101 		removeMiddle.setText("Remove Start-End");
102 		removeMiddle.addListener(SWT.Selection, e -> {
103 			long t1 = System.currentTimeMillis();
104 			table.remove(START, END);
105 			long t2 = System.currentTimeMillis();
106 			label.setText("Items: " + (END - START) + ", Time to remove: " + (t2 - t1) + " (ms)");
107 		});
108 
109 		shell.pack ();
110 		shell.open ();
111 		while (!shell.isDisposed ()) {
112 			if (!display.readAndDispatch ()) display.sleep ();
113 		}
114 		display.dispose ();
115 	}
116 }