dtree的应用

前几天在javaeye上看到有这样一个帖子(http://www.iteye.com/topic/245728),今天把我的解决代码贴在我的blog中。数据库是这样的记录
year,month,title
2004 3 第一条标题
2004 5 第二条标题
2005 6 第三条标题
2005 6 第四条标题
2002 6 第五条标题
要求在前台以年,月,日,树状展示,比如点击2004,然后出来下面所有的月,在点击月,出来下面所有的标题

这里算是做一个dtree的简单应用吧。

Java代码
package app;

public class Title {
private int year;
private int month;
private String name;

public Title() {
}

public Title(int year, int month, String name) {
this.year = year;
this.month = month;
this.name = name;
}

public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

package app;

public class Title {
private int year;
private int month;
private String name;

public Title() {
}

public Title(int year, int month, String name) {
this.year = year;
this.month = month;
this.name = name;
}

public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}


Java代码
package app;

public class TreeDaoImpl {

public Title[] queryAllTitle() {
Title[] titles = new Title[5];
titles[0] = new Title(2004, 3, "第一条标题");
titles[1] = new Title(2004, 5, "第二条标题");
titles[2] = new Title(2005, 6, "第三条标题");
titles[3] = new Title(2005, 6, "第四条标题");
titles[4] = new Title(2002, 6, "第五条标题");

return titles;
}
}

package app;

public class TreeDaoImpl {

public Title[] queryAllTitle() {
Title[] titles = new Title[5];
titles[0] = new Title(2004, 3, "第一条标题");
titles[1] = new Title(2004, 5, "第二条标题");
titles[2] = new Title(2005, 6, "第三条标题");
titles[3] = new Title(2005, 6, "第四条标题");
titles[4] = new Title(2002, 6, "第五条标题");

return titles;
}
}

Java代码
package app;

import java.util.*;

public class Node {
private Object data;
private Node parent;
private List<Node> children;

public Node() {
children = new ArrayList<Node>();
}

public Node(Object data, Node parent) {
this.data = data;
this.parent = parent;
children = new ArrayList<Node>();
}

public boolean hasChildren() {
return children.size() != 0;
}

public boolean containChildData(Object data) {
return queryChildByData(data) != null;
}

public Node queryChildByData(Object data) {
Node r = null;
for(Node node : children) {
if(node.getData().equals(data)) {
r = node;
break;
}
}

return r;
}

public void addChild(Node node) {
children.add(node);
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

public Node getParent() {
return parent;
}

public void setParent(Node parent) {
this.parent = parent;
}

public List<Node> getChildren() {
return children;
}

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

package app;

import java.util.*;

public class Node {
private Object data;
private Node parent;
private List<Node> children;

public Node() {
children = new ArrayList<Node>();
}

public Node(Object data, Node parent) {
this.data = data;
this.parent = parent;
children = new ArrayList<Node>();
}

public boolean hasChildren() {
return children.size() != 0;
}

public boolean containChildData(Object data) {
return queryChildByData(data) != null;
}

public Node queryChildByData(Object data) {
Node r = null;
for(Node node : children) {
if(node.getData().equals(data)) {
r = node;
break;
}
}

return r;
}

public void addChild(Node node) {
children.add(node);
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

public Node getParent() {
return parent;
}

public void setParent(Node parent) {
this.parent = parent;
}

public List<Node> getChildren() {
return children;
}

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


Java代码
package app;

public class MenuTreeBuilder {

public MenuTreeBuilder(TreeDaoImpl dao) {
this.dao = dao;
idx = 1;
buffer = new StringBuffer();
}

public void generateMenuTree(String rootName) {

root = new Node(rootName, null);

Title[] titles = dao.queryAllTitle();
for(int i = 0; titles != null && i < titles.length; i++) {

Node yearNode = null;
Node monthNode = null;
String year = String.valueOf(titles[i].getYear());
String month = String.valueOf(titles[i].getMonth());

if( !root.containChildData(year) ) {
yearNode = new Node(year, root);
root.addChild(yearNode);
} else {
yearNode = root.queryChildByData(year);
}

if( !yearNode.containChildData(month) ) {
monthNode = new Node(month, yearNode);
yearNode.addChild(monthNode);
} else {
monthNode = yearNode.queryChildByData(month);
}

monthNode.addChild(new Node(titles[i].getName(), monthNode));
}

}

public String generateJS() {

buffer.append("d = new dTree('d');\n");
buffer.append("d.add(" + idx + "," + "-1, '" + root.getData().toString() + "');\n");

privateGenerateJs(root);
return buffer.toString();
}

private void privateGenerateJs(Node parentNode) {
int rootIdx = idx;
for(int i = 0; i < parentNode.getChildren().size(); i++) {
Node node = parentNode.getChildren().get(i);
++idx;
buffer.append("d.add(" + idx + "," + rootIdx + ", '" + node.getData().toString() + "');\n");

privateGenerateJs(node);
}
}

private TreeDaoImpl dao;
private Node root;
private int idx;
private StringBuffer buffer;
}

package app;

public class MenuTreeBuilder {

public MenuTreeBuilder(TreeDaoImpl dao) {
this.dao = dao;
idx = 1;
buffer = new StringBuffer();
}

public void generateMenuTree(String rootName) {

root = new Node(rootName, null);

Title[] titles = dao.queryAllTitle();
for(int i = 0; titles != null && i < titles.length; i++) {

Node yearNode = null;
Node monthNode = null;
String year = String.valueOf(titles[i].getYear());
String month = String.valueOf(titles[i].getMonth());

if( !root.containChildData(year) ) {
yearNode = new Node(year, root);
root.addChild(yearNode);
} else {
yearNode = root.queryChildByData(year);
}

if( !yearNode.containChildData(month) ) {
monthNode = new Node(month, yearNode);
yearNode.addChild(monthNode);
} else {
monthNode = yearNode.queryChildByData(month);
}

monthNode.addChild(new Node(titles[i].getName(), monthNode));
}

}

public String generateJS() {

buffer.append("d = new dTree('d');\n");
buffer.append("d.add(" + idx + "," + "-1, '" + root.getData().toString() + "');\n");

privateGenerateJs(root);
return buffer.toString();
}

private void privateGenerateJs(Node parentNode) {
int rootIdx = idx;
for(int i = 0; i < parentNode.getChildren().size(); i++) {
Node node = parentNode.getChildren().get(i);
++idx;
buffer.append("d.add(" + idx + "," + rootIdx + ", '" + node.getData().toString() + "');\n");

privateGenerateJs(node);
}
}

private TreeDaoImpl dao;
private Node root;
private int idx;
private StringBuffer buffer;
}


Java代码
package app;

public class Test {

public static void main(String[] args) {
TreeDaoImpl dao = new TreeDaoImpl();
MenuTreeBuilder mtb = new MenuTreeBuilder(dao);
mtb.generateMenuTree("年份");
String s = mtb.generateJS();
System.out.println(s);
}
}

package app;

public class Test {

public static void main(String[] args) {
TreeDaoImpl dao = new TreeDaoImpl();
MenuTreeBuilder mtb = new MenuTreeBuilder(dao);
mtb.generateMenuTree("年份");
String s = mtb.generateJS();
System.out.println(s);
}
} 最后,把程序生成的JS替换使用dtree的例子中的部分JS即可。当然,实际应用可能不是这个样子,这里只是做个例子,这个程序可以根据自己的需要更改。程序控制台输出

d = new dTree('d');
d.add(1,-1, '年份');
d.add(2,1, '2004');
d.add(3,2, '3');
d.add(4,3, '第一条标题');
d.add(5,2, '5');
d.add(6,5, '第二条标题');
d.add(7,1, '2005');
d.add(8,7, '6');
d.add(9,8, '第三条标题');
d.add(10,8, '第四条标题');
d.add(11,1, '2002');
d.add(12,11, '6');
d.add(13,12, '第五条标题');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值