java+jquery easyui动态绑定Tree

java底层封装数据树并导出json绑定easyui tree

[b]pojo类[/b]

public class Catalog {
private String catalogId;
private String catalogName;
private String parentId;
private Date create;

public String getCatalogId() {
return catalogId;
}

public void setCatalogId(String catalogId) {
this.catalogId = catalogId;
}

public String getCatalogName() {
return catalogName;
}

public void setCatalogName(String catalogName) {
this.catalogName = catalogName;
}

public String getParentId() {
return parentId;
}

public void setParentId(String parentId) {
this.parentId = parentId;
}

public Date getCreate() {
return create;
}

public void setCreate(Date create) {
this.create = create;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

private String description;
}

Tree 类

public abstract class Tree {
protected static Log log = LogFactory.getLog(Tree.class);

private Map treeNodeMaps = new Hashtable();

private TreeNode root;

private List treeNodeList=new Vector();


public void setTreeNodeList(List treeNodeList) {
this.treeNodeList = treeNodeList;
}

/**
* root if it's parent is empty
*/
protected void reload(List nodes) {
log.info("tree will start reload all data");

synchronized (this) {
// initialize
treeNodeMaps.clear();
root = null;

treeNodeList.clear();

List treeNodes = new Vector(nodes.size());
for (int i = 0; i < nodes.size(); i++)
{
TreeNode node = this.transform(nodes.get(i)); // transform
treeNodes.add(node);
node.setTree(this);
treeNodeMaps.put(node.getNodeId(), node);
}

for (int i = 0; i < treeNodes.size(); i++) {
TreeNode node = (TreeNode) treeNodes.get(i);
String parentId = node.getParentId();
if (this.isRootNode(node)) {

treeNodeList.add(node);

if (root == null)
{
root = node;
}
else
{
log.error("find more then one root node. ignore.");
}
}
else
{
TreeNode parent = (TreeNode) treeNodeMaps.get(parentId);
if (parent != null)
{
parent.addChild(node);
node.setParent(parent);
}
else
{
log.warn("node [id=" + node.getNodeId()
+ "]: missing parent node.");
}
}
}
}

if (root == null) {
log.error("the root node is not be defined");
}
}

protected boolean isRootNode(TreeNode node) {
return StringUtils.isBlank(node.getParentId());
}

public TreeNode getRootNode() {
return root;
}

public TreeNode getTreeNode(String nodeId) {
return (TreeNode) treeNodeMaps.get(nodeId);
}



public List getTreeNodeList() {
return treeNodeList;
}

public void addTreeNode(TreeNode node) {
synchronized (this) {
treeNodeMaps.put(node.getNodeId(), node);

String parentId = node.getParentId();
if (StringUtils.isNotBlank(parentId)) {
TreeNode parent = getTreeNode(parentId);
if (parent != null) {
parent.addChild(node);
node.setParent(parent);
} else {
log.error("parent cannot be found: " + node.getParentId());
}
} else {
if (root == null) {
root = node;
} else {
log.error("find more then one root node. ignore.");
}
}
}
}

public void deleteTreeNode(String nodeId) {
synchronized (this) {
TreeNode node = getTreeNode(nodeId);
if (node == null)
throw new IllegalArgumentException(nodeId + " cannot be found.");

if (node.getParent() == null) {
root = null;
treeNodeMaps.clear();
log.warn("the root node has been removed.");
} else {
node.getParent().getChildren().remove(node);

treeNodeMaps.remove(nodeId);
List children = node.getAllChildren();
for (int i = 0; i < children.size(); i++) {
TreeNode n = (TreeNode) children.get(i);
treeNodeMaps.remove(n.getNodeId());
}
}
}
}

/**
*
 
  
* Usage: Office ->
*
* public TreeNode transform(Object info) {
* OfficeInfo office_info = (OfficeInfo) info;
* TreeNode node = new TreeNode();
* node.setNodeId(office_info.getOfficeId());
* node.setParentId(office_info.getParentId());
* node.setBindData(office_info);
* return node;
* }
*

*/
protected abstract TreeNode transform(Object info);
}

TreeNode 类

public class TreeNode {

private Tree tree;
private TreeNode parent;
private List children = new Vector();
private List childrenGroup = new Vector();
private String nodeId;
private String parentId;
private Object bindData;
private TreeNode node;

public TreeNode getNode() {
return node;
}

public void setNode(TreeNode node) {
this.node = node;
}

public String getNodeId() {
return nodeId;
}

public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}

public String getParentId() {
return parentId;
}

public void setParentId(String parentId) {
this.parentId = parentId;
}

public Object getBindData() {
return bindData;
}

public void setBindData(Object bindData) {
this.bindData = bindData;
}

public Tree getTree() {
return tree;
}

public void setTree(Tree tree) {
this.tree = tree;
}

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

public TreeNode getParent() {
return this.parent;
}

public List getChildren() {
return this.children;
}

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

/**
* get all children, and chilren's children
*/
public List getAllChildren() {
if (this.childrenGroup.isEmpty()) {
synchronized (this.tree) {
for (int i = 0; i < this.children.size(); i++) {
TreeNode node = (TreeNode) this.children.get(i);
this.childrenGroup.add(node);
this.childrenGroup.addAll(node.getAllChildren());//递归添加所有字节点
}
}
}
return this.childrenGroup;
}

/**
* get all children, and chilren's children
*/
public List getAllChildren(Predicate predicate) {
List groups = new Vector();
fillAllChildren(groups, predicate);
return groups;
}

private void fillAllChildren(List groups, Predicate predicate) {
for (int i = 0; i < this.children.size(); i++) {
TreeNode node = (TreeNode) this.children.get(i);
if (predicate.evaluate(node)) {
groups.add(node);
node.fillAllChildren(groups, predicate);
}
}
}

/**
* get all parents, and parent's parent
*/
public List getParents() {
List results = new Vector();
TreeNode parent = this.getParent();
while (parent != null) {
results.add(parent);
parent = parent.getParent();
}
return results;
}

/**
* A.isMyParent(B) == B is A' parent ?

* root.isMyParent(null) == true;

* root.isMyParent(*) == false

* *.isMyParent(null) == false
*/
public boolean isMyParent(String nodeId) {
TreeNode target = tree.getTreeNode(nodeId);
TreeNode parent = this.getParent();
if (parent == null) {
return target == null;
} else {
return parent.equals(target);
}
}

/**
* A.isMyAncestor(B) == B is A' ancestor ?

* *.isMyAncestor(null) == true;
*/
public boolean isMyAncestor(String nodeId) {
TreeNode target = tree.getTreeNode(nodeId);
if (target == null)
return true;

return target.getAllChildren().contains(this);
}

/**
* A.isMyBrother(B) == B is A' brother ?

* *.isMyBrother(null) == false
*/
public boolean isMyBrother(String nodeId) {
TreeNode target = tree.getTreeNode(nodeId);
if (target == null)
return false;

TreeNode p1 = this.getParent();
TreeNode p2 = target.getParent();
return ObjectUtils.equals(p1, p2);
}

}

CatalogTree 类

public class CatalogTree extends Tree {
private static CatalogTree instance = null;

private CatalogTree() {}

public static synchronized CatalogTree getInstance() {
if (instance == null) {
instance = new CatalogTree();
instance.reloadCatalogs();
}
return instance;
}

protected TreeNode transform(Object info) {
Catalog catalog = (Catalog)info;
TreeNode node = new TreeNode();
node.setNodeId(catalog.getCatalogId());
node.setParentId(catalog.getParentId());
node.setBindData(catalog);
node.setNode(node);
return node;
}

public void reloadCatalogs() {
List nodes = CalalogDao.getInstance().findAll();
super.reload(nodes);
}

public Catalog getCatalogNode(String catalogId) {
TreeNode node = super.getTreeNode(catalogId);
return node == null ? null : (Catalog) node.getBindData();
}
}

JsonTree 类

public class JsonTree {

private StringBuffer str=new StringBuffer();
private Tree tree;

public JsonTree(){
tree = CatalogTree.getInstance();
}

public String getJsonToTree(Tree tree){
if(tree==null){
readTreeToJson(this.tree);
}else{
readTreeToJson(tree);
}

filterJson(str);
return str.toString();
}

public String getJsonToTree(){
return getJsonToTree(null);
}

//读树
private void readTreeToJson(Tree tree){
Iterator iter=tree.getTreeNodeList().iterator();
str.append("[");
while(iter.hasNext()){
TreeNode node=(TreeNode)iter.next();
Catalog catalog=(Catalog)node.getBindData();
str.append("{");
str.append("\"id\":"+catalog.getCatalogId()+",");
str.append("\"text\":"+"\""+catalog.getCatalogName()+"\"");

if(node.getChildren().size()>0){
str.append(",");
}else{
str.append("},");
}
readTreeNodeToJson(node);
}
str.append("]");
}

//读节点
private void readTreeNodeToJson(TreeNode treeNode){
Iterator iter=treeNode.getChildren().iterator();
if(iter.hasNext()){
str.append("\"children\":[");
}
while(iter.hasNext()){
TreeNode node=(TreeNode)iter.next();
Catalog catalog=(Catalog)node.getBindData();
str.append("{");
str.append("\"id\":"+catalog.getCatalogId()+",");
str.append("\"text\":"+"\""+catalog.getCatalogName()+"\"");

if(node.getNode().getChildren().size()<=0){
str.append("}");
}
if(iter.hasNext()){
str.append(",");
}else{
str.append("]},");
}
readTreeNodeToJson(node.getNode());
}
}

private StringBuffer filterJson(StringBuffer str){
if(!StringUtils.isBlank(str)){
str.delete(str.length()-2, str.length()-1);
}
return str;
}

}

依赖包
commons-lang3-3.0.1.jar
commons-logging-1.0.4.jar
commons-collections-3.2.1.jar

你可能感兴趣的:(json)