一:java web目录树效果展示:
所有栏目
777777777
tttt
6666
卫星信息网
政策法规
444444
农业气象
市场信息
ttttttt
最新更新
新农村建设
选派园地
时政要闻
远教电教
城市生活
测试我很好
8888888
市场信息
222
555
安徽农网农网
商务天地
新农村建设
政策法规
农业科技
农网快讯
最新更新
二 TreeView.java说明:
本类使用了TreeNode和TreeHelper两个类,使用方法是:先通过treeHelper生成TreeView,
TreeView treeView=new TreeView(treeHelper.getRoot());然后在jsp页面中通过下面的语句输出树型下拉筐:<%out.print(treeView.displayView());%> 。TreeNode和TreeHelper两个类的源码和使用方法参考:http://www.jiafuwu.com/ziliao/javaTree.htm
三:TreeView源码:
import java.util.Iterator;
import java.util.Iterator;
public class TreeView {
public TreeView(){}
public TreeView(TreeNode treeNode){this.treeNode=treeNode;}
private int checkId=-100;
private String ckNodeName="ckNode";
public String getCkNodeName() {
return ckNodeName;
}
public void setCkNodeName(String ckNodeName) {
this.ckNodeName = ckNodeName;
}
public int getCheckId() {
return checkId;
}
public void setCheckId(int checkId) {
this.checkId = checkId;
}
public String displayView(TreeNode treeNode)
{
String childView=getChildView(treeNode,1);
String wholeView=this.getRootView(treeNode)+childView+this.getJsCode();
return wholeView;
}
public String displayLeafCheckView(TreeNode treeNode)
{
String childView=getChildLeafCheckView(treeNode,1);
String wholeView=this.getRootLeafCheckView(treeNode)+childView+this.getJsCode();
return wholeView;
}
public String displayView()
{
return this.displayView(this.treeNode);
}
public String displayLeafCheckView()
{
return this.displayLeafCheckView(this.treeNode);
}
public String getExpandImg() {
return expandImg;
}
public void setExpandImg(String expandImg) {
this.expandImg = expandImg;
}
public String getLeafImg() {
return leafImg;
}
public void setLeafImg(String leafImg) {
this.leafImg = leafImg;
}
public String getLineImg() {
return lineImg;
}
public void setLineImg(String lineImg) {
this.lineImg = lineImg;
}
public String getShrinkImg() {
return shrinkImg;
}
public void setShrinkImg(String shrinkImg) {
this.shrinkImg = shrinkImg;
}
private TreeNode treeNode=null;
private String imgPath="/wis/images";
private String CKEXP=" checked ";
private String expandImg=imgPath+"/Darrow11.gif";
private String shrinkImg=imgPath+"/Rarrow11.gif";
private String leafImg=imgPath+"/darrow51.gif";
private String lineImg=imgPath+"/darrow61.gif";
private String branchEndImg=imgPath+"/branch_end.gif";
private String plusEndImg=imgPath+"/plus_end.gif";
private String lableStyle=" style=/"cursor:hand;font-family:verdana;font-size:12px/" ";
private String onMouseOver=" onMouseOver=/"this.style.color='red'/" ";
private String onMouseOut=" onMouseOut=/"this.style.color='black'/" ";
private String getJsCode()
{
String retView=
"/r/n<script>"+
"/r/nfunction toggleMenuDisplay(id)"+
"{"+
"var divCtl=document.getElementById('divId_'+id);" +
"var imgCtl=document.getElementById('imgId_'+id);"+
"if(divCtl.style.display == 'none'){" +
" imgCtl.src=/""+expandImg+"/";"+
" divCtl.style.display='block';" +
"}"+
"else {"+
" imgCtl.src=/""+shrinkImg+"/";"+
" divCtl.style.display='none';" +
"}"+
"}/r/n"+
"function onCheck(chCtl)" +
"{" +
" var checkCount=0;" +
" var ckNodes= document.getElementsByName(/""+ckNodeName+"/");" +
" for(var i=0;i<ckNodes.length;i++){" +
" if(ckNodes[i].checked) checkCount++;}" +
" if(checkCount>=2) {" +
" alert(/"只能选择一个节点/");" +
" chCtl.checked=false;" +
" }" +
"}/r/n"+
"</script>/r/n";
return retView;
}
private String getRootView(TreeNode treeNode)
{
String imgId=" id=/"imgId_"+treeNode.getSelfId()+"/"";
String checkedString="";
if(treeNode.getSelfId()==this.getCheckId()) checkedString=this.CKEXP;
String onClick=" onClick=/"toggleMenuDisplay('"+treeNode.getSelfId()+"')/"";
if(treeNode.isLeaf())onClick="";
String retView="<img src=/""+expandImg +"/" "+onClick+imgId+" width='26' height='22' align='absmiddle'>"
+"<input type='checkbox' name=/""+ckNodeName+"/" value=/""+treeNode.getSelfId()
+"/" "+checkedString+"onClick=/"onCheck(this)/">"
+"<label "+onClick+ onMouseOver + onMouseOut+lableStyle+">"+treeNode.getNodeName()+"</label>";
return retView;
}
private String getRootLeafCheckView(TreeNode treeNode)
{
String checkBox="";
String checkedString="";
if(treeNode.getSelfId()==this.getCheckId()) checkedString=this.CKEXP;
if(treeNode.isLeaf())checkBox="<input type='checkbox' name=/""+ckNodeName+"/" value=/""+treeNode.getSelfId()
+"/""+checkedString+" onClick=/"onCheck(this)/">";
String imgId=" id=/"imgId_"+treeNode.getSelfId()+"/"";
String onClick=" onClick=/"toggleMenuDisplay('"+treeNode.getSelfId()+"')/"";
if(treeNode.isLeaf())onClick="";
String retView="<img src=/""+expandImg +"/" "+onClick+imgId+" width='26' height='22' align='absmiddle'>"
+checkBox
+"<label "+onClick+ onMouseOver + onMouseOut+lableStyle+">"+treeNode.getNodeName()+"</label>";
return retView;
}
private String getLeafView(TreeNode treeNode,int lastFlag)
{
String checkedString="";
if(treeNode.getSelfId()==this.getCheckId()) checkedString=this.CKEXP;
String imgSrc=leafImg;
if(lastFlag==1)imgSrc=this.branchEndImg;
String retView="<img src=/""+imgSrc+"/" width='26' height='22' align='absmiddle'>"
+"<input type='checkbox' name=/""+ckNodeName+"/" value=/""+treeNode.getSelfId()
+"/""+checkedString+" onClick=/"onCheck(this)/">"
+"<label"+onMouseOver + onMouseOut+lableStyle+">"+treeNode.getNodeName()+"</label><br>";
return retView;
}
private String getDirView(TreeNode treeNode,int lastFlag)
{
String checkedString="";
if(treeNode.getSelfId()==this.getCheckId()) checkedString=this.CKEXP;
String imgSrc=shrinkImg;
// if(lastFlag==1)imgSrc=this.plusEndImg;
String imgId=" id=/"imgId_"+treeNode.getSelfId()+"/"";
String onClick=" onClick=/"toggleMenuDisplay('"+treeNode.getSelfId()+"')/"";
String retView="<img src=/""+imgSrc+"/" "+onClick+imgId +" width='26' height='22' align='absmiddle'>"
+"<input type='checkbox' name=/""+ckNodeName+"/" value=/""+treeNode.getSelfId()
+"/""+checkedString+ "onClick=/"onCheck(this)/">"
+"<label "+onClick+ onMouseOver + onMouseOut+lableStyle+" >"+treeNode.getNodeName()+"</label><br>";
return retView;
}
private String getDirNoCheckView(TreeNode treeNode,int lastFlag)
{
String imgSrc=shrinkImg;
// if(lastFlag==1)imgSrc=this.plusEndImg;
String imgId=" id=/"imgId_"+treeNode.getSelfId()+"/"";
String onClick=" onClick=/"toggleMenuDisplay('"+treeNode.getSelfId()+"')/"";
String retView="<img src=/""+imgSrc+"/" "+onClick+imgId +" width='26' height='22' align='absmiddle'>"
+"<label "+onClick+ onMouseOver + onMouseOut+lableStyle+" >"+treeNode.getNodeName()+"</label><br>";
return retView;
}
private String getLineView()
{
String retView="<img src=/""+lineImg+"/" width='26' height='22' align='absmiddle'>";
return retView;
}
public String getChildView(TreeNode treeNode,int depth)
{
String disPlayView="style='display:none'";
if(depth==1)disPlayView="style='display:block'";
String nodeView="<div id=/"divId_"+treeNode.getSelfId()+"/""+disPlayView+">";
String frontLineView="";
for(int i=0;i<depth;i++) frontLineView+=this.getLineView();
Iterator it=treeNode.getChildList().iterator();
while(it.hasNext())
{
TreeNode tmpNode=(TreeNode)it.next();
int lastFlag=0;
if(it.hasNext()==false)lastFlag=1;
if(tmpNode.isLeaf())
{
nodeView+=frontLineView+this.getLeafView(tmpNode,lastFlag);
}
else
{
nodeView+=frontLineView+this.getDirView(tmpNode,lastFlag)+getChildView(tmpNode,depth+1);
}
}
return nodeView+="</div>";
}
public String getChildLeafCheckView(TreeNode treeNode,int depth)
{
String disPlayView="style='display:none'";
if(depth==1)disPlayView="style='display:block'";
String nodeView="<div id=/"divId_"+treeNode.getSelfId()+"/""+disPlayView+">";
String frontLineView="";
for(int i=0;i<depth;i++) frontLineView+=this.getLineView();
Iterator it=treeNode.getChildList().iterator();
while(it.hasNext())
{
TreeNode tmpNode=(TreeNode)it.next();
int lastFlag=0;
if(it.hasNext()==false)lastFlag=1;
if(tmpNode.isLeaf())
{
nodeView+=frontLineView+this.getLeafView(tmpNode,0);
}
else
{
nodeView+=frontLineView+this.getDirNoCheckView(tmpNode,0)+getChildLeafCheckView(tmpNode,depth+1);
}
}
return nodeView+="</div>";
}
public TreeNode getTreeNode() {
return treeNode;
}
public void setTreeNode(TreeNode treeNode) {
this.treeNode = treeNode;
}
public String getBranchEndImg() {
return branchEndImg;
}
public void setBranchEndImg(String branchEndImg) {
this.branchEndImg = branchEndImg;
}
public String getPlusEndImg() {
return plusEndImg;
}
public void setPlusEndImg(String plusEndImg) {
this.plusEndImg = plusEndImg;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
}
一:树节点的定义(TreeNode.java)
import java.util.List;
import java.util.ArrayList;
import java.io.Serializable;
public class TreeNode implements Serializable
{
private int parentId;
private int selfId;
protected String nodeName;
protected Object obj;
protected TreeNode parentNode;
protected List<TreeNode> childList;
public TreeNode()
{
initChildList();
}
public TreeNode(TreeNode parentNode)
{
this.getParentNode();
initChildList();
}
public boolean isLeaf()
{
if(childList==null)
{
return true;
}
else
{
if(childList.isEmpty())
{
return true;
}
else
{
return false;
}
}
}
/*插入一个child节点到当前节点中*/
public void addChildNode(TreeNode treeNode)
{
initChildList();
childList.add(treeNode);
}
public void initChildList()
{
if(childList==null)
childList=new ArrayList<TreeNode>();
}
public boolean isValidTree()
{
return true;
}
/*返回当前节点的父辈节点集合*/
public List<TreeNode> getElders()
{
List<TreeNode> elderList=new ArrayList<TreeNode>();
TreeNode parentNode=this.getParentNode();
if(parentNode==null)
{
return elderList;
}
else
{
elderList.add(parentNode);
elderList.addAll(parentNode.getElders());
return elderList;
}
}
/*返回当前节点的晚辈集合*/
public List<TreeNode> getJuniors()
{
List<TreeNode> juniorList=new ArrayList<TreeNode>();
List<TreeNode> childList=this.getChildList();
if(childList==null)
{
return juniorList;
}
else
{
int childNumber=childList.size();
for(int i=0;i<childNumber;i++)
{
TreeNode junior=childList.get(i);
juniorList.add(junior);
juniorList.addAll(junior.getJuniors());
}
return juniorList;
}
}
/*返回当前节点的孩子集合*/
public List<TreeNode> getChildList() {
return childList;
}
/*删除节点和它下面的晚辈*/
public void deleteNode()
{
TreeNode parentNode=this.getParentNode();
int id=this.getSelfId();
if(parentNode!=null)
{
parentNode.deleteChildNode(id);
}
}
/*删除当前节点的某个子节点*/
public void deleteChildNode(int childId)
{
List<TreeNode> childList=this.getChildList();
int childNumber=childList.size();
for(int i= 0 ; i < childNumber;i++)
{
TreeNode child=childList.get(i);
if(child.getSelfId()==childId)
{
childList.remove(i);
return;
}
}
}
/*动态的插入一个新的节点到当前树中*/
public boolean insertJuniorNode(TreeNode treeNode)
{
int juniorParentId=treeNode.getParentId();
if(this.parentId==juniorParentId)
{
addChildNode(treeNode);
return true;
}
else
{
List<TreeNode> childList=this.getChildList();
int childNumber=childList.size();
boolean insertFlag;
for(int i=0;i<childNumber;i++)
{
TreeNode childNode= childList.get(i);
insertFlag=childNode.insertJuniorNode(treeNode);
if(insertFlag==true)
return true;
}
return false;
}
}
/*找到一颗树中某个节点*/
public TreeNode findTreeNodeById(int id)
{
if(this.selfId==id)
return this;
if(childList.isEmpty() || childList==null)
{
return null;
}
else
{
int childNumber=childList.size();
for(int i= 0 ; i < childNumber;i++)
{
TreeNode child=childList.get(i);
TreeNode resultNode=child.findTreeNodeById(id);
if(resultNode!=null)
{
return resultNode;
}
}
return null;
}
}
/*遍历一棵树,层次遍历*/
public void traverse()
{
if(selfId<0)
return;
print(this.selfId);
if(childList==null || childList.isEmpty())
return;
int childNumber=childList.size();
for(int i= 0 ; i < childNumber;i++)
{
TreeNode child=childList.get(i);
child.traverse();
}
}
public void print(String content)
{
System.out.println(content);
}
public void print(int content)
{
System.out.println(String.valueOf(content));
}
public void setChildList(List<TreeNode> childList) {
this.childList = childList;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public int getSelfId() {
return selfId;
}
public void setSelfId(int selfId) {
this.selfId = selfId;
}
public TreeNode getParentNode() {
return parentNode;
}
public void setParentNode(TreeNode parentNode) {
this.parentNode = parentNode;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
二:TreeHelper.java
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.HashMap;
public class TreeHelper {
private TreeNode root;
private List<TreeNode> tempNodeList;
private boolean isValidTree=true;
public TreeHelper()
{
}
public TreeHelper(List<TreeNode> treeNodeList)
{
tempNodeList=treeNodeList;
generateTree();
}
public static TreeNode getTreeNodeById(TreeNode tree,int id)
{
if(tree==null)
return null;
TreeNode treeNode;
treeNode=tree.findTreeNodeById(id);
return treeNode;
}
/** generate a tree from the given treeNode or entity list
*/
public void generateTree()
{
HashMap nodeMap = putNodesIntoMap();
putChildIntoParent(nodeMap);
}
/** put all the treeNodes into a hash table by its id as the key
*@return hashmap that contains the treenodes
*/
protected HashMap putNodesIntoMap()
{
int maxId=Integer.MAX_VALUE;
HashMap nodeMap=new HashMap<String,TreeNode>();
Iterator it=tempNodeList.iterator();
while(it.hasNext())
{
TreeNode treeNode=(TreeNode)it.next();
int id=treeNode.getSelfId();
if(id<maxId)
{
maxId=id;
this.root=treeNode;
}
String keyId=String.valueOf(id);
nodeMap.put(keyId,treeNode);
//System.out.println("keyId: " +keyId);
}
return nodeMap;
}
/** set the parent nodes point to the child nodes
*@param nodeMap a hashmap that contains all the treenodes by its id as the key
*/
protected void putChildIntoParent(HashMap nodeMap)
{
Iterator it=nodeMap.values().iterator();
while(it.hasNext())
{
TreeNode treeNode=(TreeNode)it.next();
int parentId=treeNode.getParentId();
String parentKeyId=String.valueOf(parentId);
if(nodeMap.containsKey(parentKeyId))
{
TreeNode parentNode=(TreeNode)nodeMap.get(parentKeyId);
if(parentNode==null)
{
this.isValidTree=false;
return ;
}
else
{
parentNode.addChildNode(treeNode);
//System.out.println("childId: " +treeNode.getSelfId()+" parentId: "+parentNode.getSelfId());
}
}
}
}
/**initialize the tempNodeList property
*/
protected void initTempNodeList()
{
if(this.tempNodeList==null)
{
this.tempNodeList=new ArrayList<TreeNode>();
}
}
/** add a tree node to the tempNodeList
*/
public void addTreeNode(TreeNode treeNode)
{
initTempNodeList();
this.tempNodeList.add(treeNode);
}
/** insert a tree node to the tree generated already
* @return show the insert operation is ok or not
*/
public boolean insertTreeNode(TreeNode treeNode)
{
boolean insertFlag= root.insertJuniorNode(treeNode);
return insertFlag;
}
/** adapt the entities to the corresponding treeNode
*@param entityList list that contains the entities
*@return the list containg the corresponding treeNodes of the entities
*/
public static List<TreeNode> changeEnititiesToTreeNodes(List entityList)
{
OrganizationEntity orgEntity=new OrganizationEntity();
List<TreeNode> tempNodeList=new ArrayList<TreeNode>();
TreeNode treeNode;
Iterator it = entityList.iterator();
while(it.hasNext())
{
orgEntity= (OrganizationEntity)it.next();
treeNode=new TreeNode();
treeNode.setObj(orgEntity);
treeNode.setParentId(orgEntity.getParentId());
treeNode.setSelfId(orgEntity.getOrgId());
treeNode.setNodeName(orgEntity.getOrgName());
tempNodeList.add(treeNode);
}
return tempNodeList;
}
public boolean isValidTree()
{
return this.isValidTree;
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
public List<TreeNode> getTempNodeList() {
return tempNodeList;
}
public void setTempNodeList(List<TreeNode> tempNodeList) {
this.tempNodeList = tempNodeList;
}
}