how to update the data of tree with richfaces?
gringalet Aug 19, 2007 10:54 AMi have some code for showing and editing the tree. i am a stranger for richfaces and feel puzzle . my code like this:
Library.java
package com.WCRM.departmenttree;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.Component;
import org.jboss.seam.Seam;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;
import org.richfaces.component.TreeNode;
import com.WCRM.department.Department;
import com.WCRM.department.DepartmentFinder;
import com.WCRM.departmenttree.Department1;
public class Library implements TreeNode {
/**
*
*/
private static final long serialVersionUID = -3530085227471752526L;
private Map brachs = null;
@Logger
private static Log log;
private Map getBrachs() {
//System.out.println("private Map getBrachs() ");
if (this.brachs == null) {
initData2();
}
return this.brachs;
}
public void addBrach(Branch brach) {
///System.out.println("public void addBrach(Brach brach) ");
addChild(Long.toString(brach.getId()), brach);
brach.setParent(this);
}
public void addChild(Object identifier, TreeNode child) {
//System.out.println("public void addChild(Object identifier, TreeNode child)");
getBrachs().put(identifier, child);
}
public TreeNode getChild(Object id) {
//System.out.println("public TreeNode getChild(Object id) ");
return (TreeNode) getBrachs().get(id);
}
public Iterator getChildren() {
//System.out.println("public Iterator getChildren()");
return getBrachs().entrySet().iterator();
}
public Object getData() {
//System.out.println("public Object getData()");
return this;
}
public TreeNode getParent() {
//System.out.println("public TreeNode getParent()");
return null;
}
public boolean isLeaf() {
System.out.println("public boolean isLeaf() ");
// return false;
return getBrachs().isEmpty();
}
public void removeChild(Object id) {
//System.out.println("public void removeChild(Object id)");
getBrachs().remove(id);
}
public void setData(Object data) {
}
public void setParent(TreeNode parent) {
}
public String getType() {
// System.out.println("public String getType()");
return "library";
}
private long nextId = 0;
private long getNextId() {
// System.out.println("private long getNextId()");
return nextId++;
}
private Map departmentCache = new HashMap();
private Map departmentCache1 = new HashMap();
private Map departmentCache2 = new HashMap();
//private Map departmentCache3 = new HashMap();
private Map brachCache = new HashMap();
private Branch getBrachByName(String name, Library library) {
Branch brach = (Branch) brachCache.get(name);
if (brach == null) {
brach = new Branch(getNextId());
brach.setName(name);
brachCache.put(name, brach);
library.addBrach(brach);
}
return brach;
}
private Department1 getDepartment1ByTitle(String title, Branch brach) {
Department1 department1 = (Department1) departmentCache1.get(title);
if (department1 == null) {
department1 = new Department1(getNextId());
department1.setTitle(title);
departmentCache1.put(title, department1);
brach.addDepartment(department1);
}
return department1;
}
private Department2 getDepartment2ByTitle(String title,
Department1 department1) {
Department2 department2 = (Department2) departmentCache2.get(title);
if (department2 == null) {
department2 = new Department2(getNextId());
department2.setTitle2(title);
departmentCache2.put(title, department2);
// brach.addDepartment(department);
department1.addDepartment(department2);
}
return department2;
}
private void initData2() {
brachs = new HashMap();
List<Department> items = null;
DepartmentFinder em =
(DepartmentFinder)Component.getInstance("departmentFinder");
items=em.findDepartments2();
if (items.size() > 0) {
for (Department item : items) {
System.out.println("line is started:");
System.out.println("the selected dept:"+item.getDeptId());
String employerTitle = item.getEmployer();
String brachName = item.getBranch();
String department1name = item.getDepartment1();
String department2name = item.getDepartment2();
Branch brach = getBrachByName(brachName, this);
Department1 department1 = getDepartment1ByTitle(
department1name, brach);
Department2 department2 = getDepartment2ByTitle(
department2name, department1);
Employer employer = new Employer(getNextId());
employer.setEmployername(employerTitle);
department2.addEmployer(employer);
System.out.println("line is ended:");
}
}
}
public String test()
{
return null;
}
}mytest.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
<%@ taglib uri="http://richfaces.ajax4jsf.org/rich" prefix="rich"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<f:view>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><h:outputText value="#{messages.Home}" /></title>
<style type="text/css" media="all">
@import "style/default/screen.css";
</style>
</head>
<body>
<h:form>
<div class="sample-container" >
<rich:tree style="width:300px" value="#{library2.data}" var="item" nodeFace="#{item.type}">
<rich:treeNode type="brach">
<h:commandLink value="#{item.name}"
action="#{library2.test}" style="text-decoration:none" />
</rich:treeNode>
<rich:treeNode type="department1">
<h:outputText value="#{item.title}" />
</rich:treeNode>
<rich:treeNode type="department2">
<h:outputText value="#{item.title2}" />
</rich:treeNode>
<rich:treeNode type="employer">
<h:outputText value="#{item.employername}" />
</rich:treeNode>
</rich:tree>
</div>
</h:form>
</body>
</f:view>
</html>
faces-config.xml
<managed-bean> <managed-bean-name>library2</managed-bean-name> <managed-bean-class>com.WCRM.departmenttree.Library</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean>
now i can get the treeview in mytest.jsp. and i want use the "library2.test" method to update the data of tree. who can give me a hand to write some codes or give me some example. i don't know how to get the data of node form the mytest.jsp and give it ot Library.java.