日期:2014-05-16  浏览次数:20529 次

SWT/JFace从数据库表生成treeviewer所需要的内容(list对象)
网上关于SWT/JFace的Treeviewer应用的文章有很多,值得推荐的是:http://www.cnblogs.com/huqingyu/archive/2008/04/10/1146209.html
,但是至于如何从数据库中获得TreeMenuContentProvider所需要的树状list,没有太多实例,下面是我写的一个例子,供大家参考:
共分为以下几步:
一、创建数据库表tree_menu_t(tree_menu_id,tree_menu_text,tree_menu_pid,tree_menu_action);
并填充数据
举例:
tree_menu_idtree_menu_texttree_menu_pidtree_menu_action
1      系统设置   0           null  
2      用户设置   1       user_action
3      角色设置   1       role_action
4      权限设置   1       privilege_action
5      业务模块   0           null
6      销售管理   5       sale_action

创建对应类:
package com.project.layout.treemenu;

import java.util.*;

import com.bsm.user.User;

public class TreeMenu {
	private String treeMenuId;
	private String treeMenuPid;
	private String treeMenuText;
	private String treeMenuAction;
	private List children = new ArrayList();

	public TreeMenu(String treeMenuId, String treeMenuPid,String treeMenuText,String treeMenuAction, ArrayList children) {
		this.treeMenuId = treeMenuId;
		this.treeMenuPid = treeMenuPid;
		this.treeMenuText = treeMenuText;
		this.treeMenuAction = treeMenuAction;
		this.children = children;
	}

	public String getTreeMenuAction() {
		return treeMenuAction;
	}

	public void setTreeMenuAction(String treeMenuAction) {
		this.treeMenuAction = treeMenuAction;
	}

	public TreeMenu() {

	}

	public String getTreeMenuId() {
		return treeMenuId;
	}

	public void setTreeMenuId(String treeMenuId) {
		this.treeMenuId = treeMenuId;
	}

	public String getTreeMenuPid() {
		return treeMenuPid;
	}

	public void setTreeMenuPid(String treeMenuPid) {
		this.treeMenuPid = treeMenuPid;
	}

	public String getTreeMenuText() {
		return treeMenuText;
	}

	public void setTreeMenuText(String treeMenuText) {
		this.treeMenuText = treeMenuText;
	}

	public List getChildren() {
		return children;
	}

	public void setChildren(List children) {
		this.children = children;
	}
}


第二步:创建DAO,获取对应数据list
package com.project.layout.treemenu;

import java.util.ArrayList;
import java.util.List;
public class TreeMenuDAO {
	public List getTreeMenuList() {
		String sql = "SELECT TREE_MENU_ID , TREE_MENU_TEXT , TREE_MENU_PID , TREE_MENU_ACTION FROM TREE_MENU_T ORDER BY TREE_MENU_PID,TREE_MENU_ID;";
		
 list = new ArrayList();
//具体取语句就不多说了,因为关于从表中取得数据的实例太多了,各个人又个人的不同实现方法
    treeMenu = new TreeMenu();
    treeMenu.setTreeMenuId(s[i][j++]);
    treeMenu.setTreeMenuText(s[i][j++]);
    treeMenu.setTreeMenuPid(s[i][j++]);
    treeMenu.setTreeMenuAction(s[i][j++]);
    list.add(treeMenu);
    return list;//返回list对象
	}
}

第三步:拼装成链状list
package com.project.layout.treemenu;

import com.bsm.user.User;
import java.util.List;
import java.util.ArrayList;

public class TreeMenuBO {
	private TreeMenu tm = new TreeMenu();
	private List tList = new ArrayList();
	public List getTreeViewList() {
		TreeMenu treeMenu = new TreeMenu();
		TreeMenuDAO dao = new TreeMenuDAO();
		List list = dao.getTreeMenuList();
		this.tm = new TreeMenu("0", "0", "root", "", new ArrayList());// 设置顶级元素,为了便于子list的挂载,在返回时候,去掉
		tList.add(tm);

		for (int i = 0; i < list.size(); i++) {
			this.tm = (TreeMenu) li