Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2088362
  • 博文数量: 1647
  • 博客积分: 80000
  • 博客等级: 元帅
  • 技术积分: 9980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 15:15
文章分类

全部博文(1647)

文章存档

2011年(1)

2008年(1646)

我的朋友

分类:

2008-10-28 17:46:41

 

package tree;

    import java.util.ArrayList;

    import java.util.HashMap;

    import java.util.List;

    import java.util.Map;

    import org.eclipse.jface.viewers.CheckboxTreeViewer;

    import org.eclipse.swt.SWT;

    import org.eclipse.swt.widgets.Display;

    import org.eclipse.swt.widgets.Item;

    import org.eclipse.swt.widgets.Shell;

    import org.eclipse.swt.widgets.Tree;

    import org.eclipse.swt.widgets.TreeItem;

    import org.eclipse.swt.widgets.Widget;

    public class TE {

        private Tree tree;

        protected Shell shell;

        /**

         * Launch the application

         * @param args

         */

        public static void main(String[] args) {

            try {

                TE window = new TE();

                window.open();

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        /**

         * Open the window

         */

        public void open() {

            final Display display = Display.getDefault();

            createContents();

            shell.open();

            shell.layout();

            while (!shell.isDisposed()) {

                if (!display.readAndDispatch())

                    display.sleep();

            }

        }

        /**

         * Create contents of the window

         */

        protected void createContents() {

            shell = new Shell();

            shell.setSize(500, 375);

            shell.setText("SWT Application");

            final CheckboxTreeViewer checkboxTreeViewer = new CheckboxTreeViewer(shell, SWT.BORDER);

            tree = checkboxTreeViewer.getTree();

            tree.setBounds(23, 48, 459, 260);

            checkboxTreeViewer.setContentProvider(new Content());

            checkboxTreeViewer.setLabelProvider(new Label());

            checkboxTreeViewer.setInput(getInputContent());

    /*      Parent []p = new Parent[1];

            p[0] = new Parent();

            p[0].setName("小一");

            List ls = new ArrayList();

            Child c1 = new Child();

            c1.setName("小一一");

            ls.add(c1);

            p[0].setChild(ls);*/

            checkboxTreeViewer.expandAll();

            String []obj = new String[2];

            obj[0] = "小一";

            obj[1] = "小二二";

            setChecked(obj, tree);

            //

        }

        private void setChecked(String [] obj, Tree tree) {

            Map map = new HashMap();

            for(int i = 0; i < obj.length; i++) {

                map.put(obj[i], obj);

            }

            internalSetChecked(map, tree);

        }

        private Item[] getChildren(Widget o) {

            if (o instanceof TreeItem) {

                return ((TreeItem) o).getItems();

            }

            if (o instanceof Tree) {

                return ((Tree) o).getItems();

            }

            return null;

        }

        private void internalSetChecked(Map checkedElements,

                Widget widget) {

            Item[] items = getChildren(widget);

            for (int i = 0; i < items.length; i++) {

                TreeItem item = (TreeItem) items[i];

                Object data = item.getData();

                if (data != null) {

                    String temp = ((IName)data).getName();

                    boolean checked = checkedElements.containsKey(temp);

                    if (checked != item.getChecked()) {

                        item.setChecked(checked);

                    }

                }

                internalSetChecked(checkedElements, item);

            }

        }

        private List getInputContent() {

            List lst = new ArrayList();

            Parent p1 = new Parent();

            p1.setName("小一");

            List ls = new ArrayList();

            Child c1 = new Child();

            c1.setName("小一一");

            ls.add(c1);

            p1.setChild(ls);

            Parent p2 = new Parent();

            p2.setName("小二");

            List lss = new ArrayList();

            Child c2 = new Child();

            c2.setName("小二二");

            lss.add(c2);

            p2.setChild(lss);

            lst.add(p1);

            lst.add(p2);

            return lst;

        }

    }


--------------------next---------------------

阅读(183) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~