用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
yougucao379548695的公告
<script type="text/javascript"><!-- google_ad_client = "pub-1769078480103642"; google_alternate_color = "66B5FF"; google_ad_width = 200; google_ad_height = 200; google_ad_format = "200x200_as"; google_ad_type = "text_image"; //2007-09-03: csdnblog google_ad_channel = "4942708430"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
文章分类
博客导航
fndcz(jbpm工作流引擎)(RSS)
Onion的天空
吕的部落格
大宝天天见
庖丁解牛lagvin
网站导航
禹州论坛
脚本之家
许昌招贴信息网
存档

原创  关于checkboxtree选中的一个小例子非常使用 收藏

有网友抗天提供非常感谢

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<Child> ls = new ArrayList<Child>();

		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<Parent> lst = new ArrayList<Parent>();

		

		Parent p1 = new Parent();

		p1.setName("小一");

		List<Child> ls = new ArrayList<Child>();

		Child c1 = new Child();

		c1.setName("小一一");

		

		ls.add(c1);

		p1.setChild(ls);

		

		Parent p2 = new Parent();

		p2.setName("小二");

		

		List<Child> lss = new ArrayList<Child>();

		Child c2 = new Child();

		c2.setName("小二二");

		lss.add(c2);

		p2.setChild(lss);

		

		lst.add(p1);

		lst.add(p2);

		

		return lst;

	}

	

}

发表于 @ 2008年06月13日 18:36:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:RCP Editor 常见错误处理方法 | 新一篇:MS SQL2000 (Error establishing socket)错误的解决办法-SQL请选用混和安装模式

  • 发表评论
  • 评论内容:
  •  
Copyright © yougucao379548695
Powered by CSDN Blog