树的操作方法
////////////////////////////////////////////////////////////// //// ////使用方法:只需将以下代码拷贝到项目的page.java文件中 //// /////////////////////////////////////////////////////////// ////返回选中节点 private TreeNode getSelectedTreeNode(Tree t) { String id = t.getCookieSelectedTreeNode(); if (id == null) return null; id = id.substring(id.lastIndexOf(':') + 1); return (TreeNode) this.getForm1().findComponentById(id); } /////删除选中节点 public void Remove_SelectNode(Tree t) { TreeNode selectnode = getSelectedTreeNode(t); if(selectnode==null) return; TreeNode parentNode =(TreeNode) selectnode.getParent(); java.util.List childrenList = parentNode.getChildren(); childrenList.remove(selectnode); } ///更改选中节点名 public void Rename_SelectNodeText(Tree t, String txt) { if(txt==null || txt.trim().length()==0) return; TreeNode selectnode = getSelectedTreeNode(t); if(selectnode==null) return; selectnode.setText(txt); } ///上移选中节点 public void Up_SelectNode(Tree t) { TreeNode selectnode = getSelectedTreeNode(t); if(selectnode==null) return; java.util.List childrenList = ((TreeNode)selectnode.getParent()).getChildren(); int index = childrenList.indexOf(selectnode); if (index > 0) { childrenList.remove(selectnode); childrenList.add(index - 1, selectnode); } } ///下移选中节点 public void Down_SelectNode(Tree t) { TreeNode selectnode = getSelectedTreeNode(t); if(selectnode==null) return; java.util.List childrenList = ((TreeNode)selectnode.getParent()).getChildren(); int index = childrenList.indexOf(selectnode); if (index < childrenList.size()-1) { childrenList.remove(selectnode); childrenList.add(index + 1, selectnode); } } ///在指定树下插入根节点 public TreeNode AddRoot_To_SelectTree(Tree t, String txt) { TreeNode rs = null; if(txt==null || txt.trim().length()==0) return rs; java.util.List childrenlist =t.getChildren(); String selectnodeid = t.getId(); ////下面代码保证ID不重复 String inserID; int NO=1; while(true){ inserID=selectnodeid+NO; boolean F_exist=false; int N=childrenlist.size(); for(int i=0;i<N;i++) { String st=((TreeNode)childrenlist.get(i)).getId(); if(inserID.equals(st)){ F_exist=true; break;} } if(F_exist==false)break; NO++; } /////////// TreeNode newnode = new TreeNode(); newnode.setText(txt); newnode.setImageURL("/resources/document.gif"); newnode.setId(inserID); childrenlist.add(newnode); rs = newnode; return rs; } ///在选中节点下插入孩子节点 public TreeNode AddChild_To_SelectNode(Tree t, String txt) { TreeNode rs = null; if(txt==null || txt.trim().length()==0) return rs; TreeNode selectnode = getSelectedTreeNode(t); if(selectnode==null) return rs; java.util.List childrenlist =selectnode.getChildren(); String selectnodeid = selectnode.getId(); ////下面代码保证ID不重复 String inserID; int NO=1; while(true){ inserID=selectnodeid+NO; boolean F_exist=false; int N=childrenlist.size(); for(int i=0;i<N;i++) { String st=((TreeNode)childrenlist.get(i)).getId(); if(inserID.equals(st)){ F_exist=true; break;} } if(F_exist==false)break; NO++; } /////////// TreeNode newnode = new TreeNode(); newnode.setText(txt); newnode.setImageURL("/resources/document.gif"); newnode.setId(inserID); childrenlist.add(newnode); rs = newnode; return rs; } //设置节点的超连接mode=_self | _blank | _parent | _top //url=""则取消超连接 public void Set_SelectNode_Url(Tree t, String url, String mode) { TreeNode selectnode = getSelectedTreeNode(t); if(selectnode==null) return; if(url=="") selectnode.setUrl(null); else selectnode.setUrl(url); selectnode.setTarget(mode); } //定义树结点 class NodeType{ public String ParentID; public String SelfID; public String SelfName; } ////////////////////////////////////////////////////////// // // 该函数被Tree_Construct(Tree t,ArrayList list)调用,用来递归的生成树 // 此处的list元素为Node // ////////////////////////////////////////////////////////// private void Node_Construct(Tree t, TreeNode nd, ArrayList list){ String TreeID = t.getId(); int TreeIDLength = TreeID.length(); java.util.List childrenlist =nd.getChildren(); int N = list.size(); for(int i=0; i<N; i++){ NodeType pd = (NodeType)list.get(i); if( pd.ParentID.equals(nd.getId().substring(TreeIDLength)) ){ TreeNode newnd = new TreeNode(); newnd.setText(pd.SelfName); newnd.setImageURL("/resources/document.gif"); newnd.setId(TreeID+pd.SelfID); childrenlist.add(newnd); } } int M = childrenlist.size(); for(int j=0; j<M; j++){ TreeNode tnd = (TreeNode)childrenlist.get(j); Node_Construct(t, tnd, list); } } /////////////////////////////////////////////////////// // // 该函数用参数list传入的节点信息生成树t的整个结构, // 此处的list元素为Node // /////////////////////////////////////////////////////// public void Tree_Construct(Tree t,ArrayList list){ String TreeID = t.getId(); java.util.List childrenlist =t.getChildren(); childrenlist.clear(); int N = list.size(); for(int i=0; i<N; i++){ NodeType pd = (NodeType)list.get(i); if( pd.ParentID.equals("ROOT") ){ TreeNode newnd = new TreeNode(); newnd.setText(pd.SelfName); newnd.setImageURL("/resources/document.gif"); newnd.setId(TreeID+pd.SelfID); childrenlist.add(newnd); } } int M = childrenlist.size(); for(int j=0; j<M; j++){ TreeNode tnd = (TreeNode)childrenlist.get(j); Node_Construct(t, tnd, list); } } //节点遍历的第归调用;被getTreeOffspring和getNodeOffspring调用 private void getNext(TreeNode nd, ArrayList output) { java.util.List childrenList = nd.getChildren(); int num=childrenList.size(); if( num >0 ) { for(int i=0;i<num;i++) { TreeNode newnode = (TreeNode)(childrenList.get(i)); output.add( newnode ); TreeNode nextnode = (TreeNode)(childrenList.get(i)); getNext(nextnode, output); } } } //返回树的所有节点 public ArrayList getTreeOffspring(Tree t) { ArrayList output = new ArrayList(); getNext(t, output); return output; } //返回指定节点下的所有节点 public ArrayList getNodeOffspring(TreeNode nd) { ArrayList output = new ArrayList(); getNext(nd, output); return output; } ///////////////////////////////////////////// 对树进行节点的更改是EJB如何更改 //////////////////////////////////////////// 1.插入根节点时,父ID应为:ROOT 自身ID应为去掉树ID名后的数字编号 TreeNode newnode = AddRoot_To_SelectTree(treeXX,this.textField7.getText().toString()); String parentid = "ROOT"; int TreeIDLength = treeXX.getId().length(); String selfid = newnode.getId().substring(TreeIDLength); String name = newnode.getText(); client.InsertRecord(parentid, selfid, name, ...); 2.插入子节点时,父ID和自身ID应为去掉树ID名后的数字编号 TreeNode newnode = AddChild_To_SelectNode(treeXX,this.textField5.getText().toString()); int TreeIDLength = treeXX.getId().length(); String parentid = newnode.getParent().getId().substring(TreeIDLength); String selfid = newnode.getId().substring(TreeIDLength); String name = newnode.getText(); client.InsertRecord(parentid, selfid, name,....); 3.节点改名,从界面获取去掉树ID后的节点ID,以次为主健调用EJB刷新纪录 Rename_SelectNodeText(treeXX,this.textField6.getText().toString()); int TreeIDLength = treeXX.getId().length(); String selfid = getSelectedTreeNode(treeXX).getId().substring(TreeIDLength); String name = this.textField6.getText().toString(); client.UpdataRecord(selfid, null, null, name, null); 4.删除节点,从界面获取要删除的所有节点列表,以次为据调用EJB删除纪录 //删除数据库对应纪录 int TreeIDLength = treeXX.getId().length(); TreeNode selectnode = getSelectedTreeNode(treeXX); TreeNode parentnode = (TreeNode)selectnode.getParent(); ArrayList list = getNodeOffspring(selectnode); list.add(selectnode); int N = list.size(); for(int i=0; i<N; i++){ String selfid = ((TreeNode)list.get(i)).getId().substring(TreeIDLength); client.DeleteRecord(selfid); } //删除界面树对应节点 Remove_SelectNode(treeXX);
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
作者:风来风往风伤
出处:http://www.cnblogs.com/amwuau/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。