1 /*******************************************************************************
2 * Copyright (c) 2008, 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: search for a string in a tree (recursively)
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.layout.*;
24 import org.eclipse.swt.widgets.*;
25
26 public class Snippet287 {
27
28 static Tree tree;
main(String[] args)29 public static void main(String[] args) {
30 final String SEARCH_STRING = "4b";
31
32 Display display = new Display ();
33 Shell shell = new Shell (display);
34 shell.setText("Snippet 287");
35 shell.setBounds (10,10,300,300);
36 shell.setLayout (new GridLayout ());
37
38 /* create the Tree */
39 tree = new Tree (shell, SWT.FULL_SELECTION);
40 tree.setLinesVisible (true);
41 tree.setLayoutData (new GridData (GridData.FILL_BOTH));
42 for (int i = 0; i < 3; i++) {
43 new TreeColumn (tree, SWT.NONE).setWidth (90);
44 }
45 int index = 0;
46 for (int i = 0; i < 3; i++) {
47 TreeItem item = createItem (null, index++);
48 for (int j = 0; j < i; j++) {
49 item = createItem (item, index++);
50 }
51 }
52
53 Button button = new Button (shell, SWT.PUSH);
54 button.setText ("Find '" + SEARCH_STRING + "'");
55 button.addListener (SWT.Selection, event -> {
56 int itemCount = tree.getItemCount ();
57 for (int i = 0; i < itemCount; i++) {
58 TreeItem item = tree.getItem (i);
59 boolean success = find (item, SEARCH_STRING);
60 if (success) {
61 System.out.println ("Found it");
62 return;
63 }
64 }
65 System.out.println ("Did not find it");
66 });
67 shell.open ();
68 while (!shell.isDisposed ()) {
69 if (!display.readAndDispatch ()) display.sleep ();
70 }
71 display.dispose ();
72 }
73
74 /* for creating sample Tree */
createItem(TreeItem parent, int itemIndex)75 static TreeItem createItem (TreeItem parent, int itemIndex) {
76 TreeItem newItem = null;
77 if (parent == null) { /* root level item */
78 newItem = new TreeItem (tree, SWT.NONE);
79 } else {
80 newItem = new TreeItem (parent, SWT.NONE);
81 }
82 String indexString = String.valueOf (itemIndex);
83 newItem.setText(new String[] {
84 indexString + 'a', indexString + 'b', indexString + 'c'});
85 return newItem;
86 }
87
88 /* recursive find */
find(TreeItem item, String searchString)89 public static boolean find (TreeItem item, String searchString) {
90 /* check this item */
91 for (int i = 0; i < tree.getColumnCount (); i++) {
92 String contents = item.getText (i);
93 if ((contents.toUpperCase ().indexOf (searchString.toUpperCase ())) != -1) {
94 tree.setSelection (item);
95 return true;
96 }
97 }
98
99 if (!item.getExpanded ()) return false; /* don't check child items */
100
101 /* check child items */
102 int childCount = item.getItemCount ();
103 for (int i = 0; i < childCount; i++) {
104 TreeItem child = item.getItem (i);
105 boolean success = find (child, searchString);
106 if (success) return true;
107 }
108
109 return false;
110 }
111 }
112