网上关于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_id |
tree_menu_text |
tree_menu_pid |
tree_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) list.get(i);
assembleTreeList(tList);
}
return ((TreeMenu) tList.get(0)).getChildren();//去掉顶级节点
}
public void assembleTreeList(List list) {//对已经存在的list链进行循环遍历
for (int i = 0; i < list.size(); i++) {
TreeMenu treeMenu = (TreeMenu) list.get(i);
// System.out.println("--" + treeMenu.getTreeMenuText());
if (treeMenu.getTreeMenuId().equals(this.tm.getTreeMenuPid())) {//取得父子对应关系
treeMenu.getChildren().add(this.tm);//挂载子
}
if (treeMenu.getChildren() != null) {
assembleTreeList(treeMenu.getChildren());
}
}
}
}
第四部:应用
package com.project.layout.treemenu;
import org.eclipse.jface.viewers.*;
import java.util.List;
public class TreeMenuContentProvider implements ITreeContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List) {
List input = (List) inputElement;
return input.toArray();
}
return new Object[0];
}
public Object[] getChildren(Object parentElement) {
TreeMenu tm = (TreeMenu) parentElement;
List list = tm.getChildren();
if (list == null) {
return new Object[0];
}
return list.toArray();
}
public boolean hasChildren(Object element) {
TreeMenu tm = (TreeMenu) element;
List list = tm.getChildren();
return !(list == null || list.isEmpty());
}
public Object getParent(Object element) {
return null;
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
package com.project.layout.treemenu;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
public class TreeMenuLableProvider implements ILabelProvider {
public Image getImage(Object element) {
// ISharedImages sharedImages =
// PlatformUI.getWorkbench().getSharedImages();
// return sharedImages.getImage(ISharedImages.IMG_OBJS_WARN_TSK);
return null;
}
public void dispose() {
}
public void addListener(ILabelProviderListener listener) {
}
/**
* 显示什么文字
*
* @param 结点
* @return 不能为null值
*/
public String getText(Object element) {
return (String) ((TreeMenu)element).getTreeMenuText();
}
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) {}
}
最终只需要维护好数据库表,就能够成功展现treeviewer了,不知道大家有没有更好的办法互相交流一下。