summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet358.java
blob: d6abf68f31eac0dee010efa14a276caba2f3b680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others.
 * All rights reserved. This Example Content is intended to demonstrate
 * usage of Eclipse technology. It is provided to you under the terms and
 * conditions of the Eclipse Distribution License v1.0 which is available
 * at http://www.eclipse.org/org/documents/edl-v10.php
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Tree example snippet: determine which TreeItems are visible in a Tree's viewport
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;

public class Snippet358 {
	
public static void main(String [] args) {
	Display display = new Display();
	Shell shell = new Shell(display);
	shell.setLayout(new GridLayout ());
	final Tree tree = new Tree(shell, SWT.NONE);
	tree.setLayoutData(new GridData(200, 200));
	for (int i = 0; i < 9; i++) {
		TreeItem item = new TreeItem(tree, SWT.NONE);
		item.setText("root-level item " + i);
		for (int j = 0; j < 9; j++) {
			new TreeItem(item, SWT.NONE).setText("item " + i + "-" + j);
		}
	}

	Button button = new Button(shell, SWT.PUSH);
	button.setText("Print item visibilities");
	button.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event event) {
			Rectangle treeBounds = new Rectangle(0, 0, 0, 0);
			Point treeSize = tree.getSize();
			treeBounds.width = treeSize.x;
			treeBounds.height = treeSize.y;
			TreeItem[] rootItems = tree.getItems();
			for (int i = 0; i < rootItems.length; i++) {
				TreeItem rootItem = rootItems[i];
				System.out.println(rootItem.getText() + " is at least partially visible? " + treeBounds.intersects(rootItem.getBounds()));
				TreeItem[] childItems = rootItem.getItems();
				for (int j = 0; j < childItems.length; j++) {
					TreeItem childItem = childItems[j];
					System.out.println(childItem.getText() + " is at least partially visible? " + treeBounds.intersects(childItem.getBounds()));
				}
			}
		}
	});

	shell.pack();
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	display.dispose ();
}

}