summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet287.java
blob: 11f83b4745a7a69b0c455ad01d4ec78e92907b2c (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Tree example snippet: search for a string in a tree (recursively)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet287 {

static Tree tree;
public static void main(String[] args) {
	final String SEARCH_STRING = "4b";

	Display display = new Display ();
	Shell shell = new Shell (display);
	shell.setBounds (10,10,300,300);
	shell.setLayout (new GridLayout ());

	/* create the Tree */
	tree = new Tree (shell, SWT.FULL_SELECTION);
	tree.setLinesVisible (true);
	tree.setLayoutData (new GridData (GridData.FILL_BOTH));
	for (int i = 0; i < 3; i++) {
		new TreeColumn (tree, SWT.NONE).setWidth (90);
	}
	int index = 0;
	for (int i = 0; i < 3; i++) {
		TreeItem item = createItem (null, index++);
		for (int j = 0; j < i; j++) {
			item = createItem (item, index++);
		}
	}

	Button button = new Button (shell, SWT.PUSH);
	button.setText ("Find '" + SEARCH_STRING + "'");
	button.addListener (SWT.Selection, new Listener () {
		public void handleEvent (Event event) {
			int itemCount = tree.getItemCount ();
			for (int i = 0; i < itemCount; i++) {
				TreeItem item = tree.getItem (i);
				boolean success = find (item, SEARCH_STRING);
				if (success) {
					System.out.println ("Found it");
					return;
				}
			}
			System.out.println ("Did not find it");
		}
	});
	shell.open ();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}

/* for creating sample Tree */
static TreeItem createItem (TreeItem parent, int itemIndex) {
	TreeItem newItem = null;
	if (parent == null) {	/* root level item */
		newItem = new TreeItem (tree, SWT.NONE);
	} else {
		newItem = new TreeItem (parent, SWT.NONE);
	}
	String indexString = String.valueOf (itemIndex); 
	newItem.setText(new String[] {
		indexString + 'a', indexString + 'b', indexString + 'c'});
	return newItem;
}

/* recursive find */
public static boolean find (TreeItem item, String searchString) {
	/* check this item */
	for (int i = 0; i < tree.getColumnCount (); i++) {
		String contents = item.getText (i);
		if ((contents.toUpperCase ().indexOf (searchString.toUpperCase ())) != -1) {
			tree.setSelection (item);
			return true;
		}
	}

	if (!item.getExpanded ()) return false; /* don't check child items */

	/* check child items */
	int childCount = item.getItemCount ();
	for (int i = 0; i < childCount; i++) {
		TreeItem child = item.getItem (i);
		boolean success = find (child, searchString);
		if (success) return true;
	}

	return false;
}
}