1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation 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  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.snippets;
15 
16 /*
17  * Tree example snippet: compute the number of visible rows in a tree
18  *
19  * For a list of all SWT example snippets see
20  * http://www.eclipse.org/swt/snippets/
21  */
22 import org.eclipse.swt.*;
23 import org.eclipse.swt.graphics.*;
24 import org.eclipse.swt.layout.*;
25 import org.eclipse.swt.widgets.*;
26 
27 public class Snippet254 {
28 
nextItem(Tree tree, TreeItem item)29 	static TreeItem nextItem(Tree tree, TreeItem item) {
30 		if (item == null) return null;
31 		if (item.getExpanded() && item.getItemCount() > 0) return item.getItem(0);
32 		TreeItem childItem = item;
33 		TreeItem parentItem = childItem.getParentItem();
34 		int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
35 		int count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount();
36 		while (true) {
37 			if (index + 1 < count) return parentItem == null ? tree.getItem(index + 1) : parentItem.getItem(index + 1);
38 			if (parentItem == null) return null;
39 			childItem = parentItem;
40 			parentItem = childItem.getParentItem();
41 			index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
42 			count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount();
43 		}
44 	}
45 
main(String[] args)46 	public static void main(String[] args) {
47 		final Display display = new Display();
48 		final Shell shell = new Shell(display);
49 		shell.setText("Snippet 254");
50 		RowLayout layout = new RowLayout (SWT.VERTICAL);
51 		layout.fill = true;
52 		layout.wrap = false;
53 		shell.setLayout (layout);
54 		final Tree tree = new Tree (shell, SWT.NONE);
55 		for (int i=0; i<32; i++) {
56 			TreeItem item0 = new TreeItem (tree, SWT.NONE);
57 			item0.setText ("Item " + i + " is quite long");
58 			for (int j=0; j<3; j++) {
59 				TreeItem item1 = new TreeItem (item0, SWT.NONE);
60 				item1.setText ("Item " + i + " " + j + " is quite long");
61 				for (int k=0; k<3; k++) {
62 					TreeItem item2 = new TreeItem (item1, SWT.NONE);
63 					item2.setText ("Item " + i + " " + j + " " + k + " is quite long");
64 					for (int l=0; l<3; l++) {
65 						TreeItem item3 = new TreeItem (item2, SWT.NONE);
66 						item3.setText ("Item " + i + " " + j + " " + k + " " + l + " is quite long");
67 					}
68 				}
69 			}
70 		}
71 		tree.setLayoutData(new RowData (200, 200));
72 		final Button button = new Button (shell, SWT.PUSH);
73 		button.setText ("Visible Items []");
74 		button.addListener (SWT.Selection, e -> {
75 			int visibleCount = 0;
76 			Rectangle rect = tree.getClientArea ();
77 			TreeItem item = tree.getTopItem ();
78 			while (item != null) {
79 				visibleCount++;
80 				Rectangle itemRect = item.getBounds();
81 				if (itemRect.y + itemRect.height > rect.y + rect.height) {
82 					break;
83 				}
84 				item = nextItem (tree, item);
85 			}
86 			button.setText ("Visible Items [" + visibleCount + "]");
87 		});
88 		shell.pack();
89 		shell.open();
90 		while (!shell.isDisposed()) {
91 			if (!display.readAndDispatch()) display.sleep();
92 		}
93 		display.dispose();
94 	}
95 }
96