12
返回列表 发新帖
楼主: Sky-Tiger

Standard Widget Toolkit trees: Creating, sorting, and searching

[复制链接]
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
11#
 楼主| 发表于 2009-5-16 12:35 | 只看该作者
Searching trees

Another feature you can add to an application is a search feature, which is especially useful if users must deal with large sets of data.

The code in listing 6 shows how you can implement a listener to search the top-level elements of the tree.


Listing 6. Search tree items listener
1:import org.eclipse.jface.dialogs.InputDialog;
2:import org.eclipse.swt.SWT;
3:import org.eclipse.swt.events.KeyEvent;
4:import org.eclipse.swt.events.KeyListener;
5:import org.eclipse.swt.widgets.MessageBox;
6:import org.eclipse.swt.widgets.Tree;
7:import org.eclipse.swt.widgets.TreeItem;
8:
9ublic class SearchListener implements KeyListener {
10:    private Tree tree = null;
11:
12:    @Override
13:    public void keyPressed(KeyEvent e) {
14:        tree = (Tree) e.widget;
15:
16:        String searchString = showSearchPopup();
17:        if (searchString == null) {
18:            return;
19:        }
20:
21:        if (!findString(searchString, tree.getItems())) {
22:            MessageBox messageBox = new MessageBox(tree.getShell(), SWT.OK
23:                    | SWT.ICON_ERROR);
24:            messageBox.setMessage("Could not find: '" + searchString + "'";
25:            messageBox.setText("Search Error";
26:            messageBox.open();
27:        }
28:
29:    }
30:
31:    @Override
32:    public void keyReleased(KeyEvent e) {
33:
34:    }
35:
36:    private String showSearchPopup() {
37:        InputDialog d = new InputDialog(this.tree.getParent().getShell(),
38:                "Search", "Search text", "", null);
39:        d.open();
40:        return d.getValue();
41:    }
42:
43:    private boolean findString(String searchString, TreeItem[] treeItems) {
44:        for (TreeItem treeItem : treeItems) {
45:            for (int i = 0; i < tree.getColumnCount(); i++) {
46:                String text = treeItem.getText(i);
47:                if ((text.toUpperCase().contains(searchString.toUpperCase()))) {
48:                    tree.setSelection(treeItem);
49:                    return true;
50:                }
51:            }
52:        }
53:
54:        return false;
55:    }
56:}



This listener can be added to the tree as a KeyListener, so that when a user types something while one of the tree’s items is selected, an input window displays as shown in figure 7.


Figure 7. Search window

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
12#
 楼主| 发表于 2009-5-16 12:35 | 只看该作者
The algorithm is simple; it iterates through the top-level items of the tree and compares their text contents with the search string (lines 43 to 52 in listing 6). When the search string is found in one of the tree items, that tree item is selected (line 48). If that string is not found, the Search Error message, Could not find: 'TreeItemX', displays (lines 21 to 27).

Now, the only modification to be done is to add the SearchListener to the tree’s collection of key listeners, as shown in line 23 of listing 7.


Listing 7. Basic tree search
1:import java.util.Random;
2:
3:import org.eclipse.swt.SWT;
4:import org.eclipse.swt.layout.FillLayout;
5:import org.eclipse.swt.widgets.Display;
6:import org.eclipse.swt.widgets.Shell;
7:import org.eclipse.swt.widgets.Tree;
8:import org.eclipse.swt.widgets.TreeColumn;
9:import org.eclipse.swt.widgets.TreeItem;
10:
11ublic class Searching {
12:    public static void main(String[] args) {
13:        Display display = new Display();
14:
15:        Shell shell = new Shell(display);
16:        shell.setText("Searching";
17:        shell.setLayout(new FillLayout());
18:        shell.setSize(400, 300);
19:
20:        Tree tree = new Tree(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL
21:                | SWT.FULL_SELECTION);
22:        tree.setHeaderVisible(true);
23:        tree.addKeyListener(new SearchListener());
24:
25:        TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
26:        column1.setText("TreeColumn0";
27:        column1.setWidth(200);
28:        column1.setAlignment(SWT.LEFT);
29:        column1.addSelectionListener(new SortTreeListener());
30:
31:        TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
32:        column2.setText("TreeColumn1";
33:        column2.setWidth(200);
34:        column2.setAlignment(SWT.CENTER);
35:        column2.addSelectionListener(new SortTreeListener());
36:
37:        Random generator = new Random();
38:
39:        for (int i = 0; i < 5; i++) {
40:            TreeItem treeItem = new TreeItem(tree, 0);
41:            treeItem.setText(new String[] { "TreeItem" + i,
42:                    Integer.toString(generator.nextInt()) });
43:            for (int j = 0; j < 5; j++) {
44:                TreeItem subTreeItem = new TreeItem(treeItem, SWT.NONE);
45:                subTreeItem.setText(new String[] { "SubTreeItem" + j,
46:                        Integer.toString(generator.nextInt()) });
47:                for (int k = 0; k < 5; k++) {
48:                    TreeItem subSubTreeItem = new TreeItem(subTreeItem,
49:                            SWT.NONE);
50:                    subSubTreeItem.setText(new String[] { "SubSubTreeItem" + k,
51:                            Integer.toString(generator.nextInt()) });
52:                }
53:            }
54:        }
55:
56:        shell.open();
57:        while (!shell.isDisposed()) {
58:            if (!display.readAndDispatch())
59:                display.sleep();
60:        }
61:        display.dispose();
62:    }
63:}



Finally, the search algorithm can be easily modified to do a recursive search of all the tree’s items.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
13#
 楼主| 发表于 2009-5-16 12:35 | 只看该作者
Conclusion

SWT trees are an extremely effective way of organizing and presenting data to the user. Using the information and the code examples from this article, you can enhance and make the most of your composite or SWT-based applications.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
14#
发表于 2009-5-29 23:42 | 只看该作者
什么东西?

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表