大大毛 的笔记

  DDM's Note

哪怕没有办法一定有说法,
就算没有鸽子一定有乌鸦,
固执无罪 梦想有价,
让他们惊讶.

posts - 14, comments - 23, trackbacks - 0, articles - 58
   :: 首页 ::  :: 联系 ::  :: 管理

实现树的类

Posted on 2006-03-09 02:22 大大毛 阅读(265) 评论(0)  编辑  收藏 所属分类: JAVA
JavaBean类:
        DeptBean,用于支持那棵树,因为是从以前作业中抠出来的,所以肯定是不会通用的,但是改改再用还是可行的。
import java.util.*;
/**用于支持部门的Tree视图
 * 
@author tw
 * xx:主要是通过setRootDept()方法来得到一棵树所必须的数据,所需参数指树从哪个部门开始,我的table的结构如下:
 * create table t_dept (
 * dept_id int primary key,    ----主键
 * dept_name varchar(),     ----显示用的名称
 * dept_sit  varchar() );   ----代表部门位置的字符串,例如总经理室 00,部门1 0001,部门2 0002
 * hasChild()方法用于判断该部门是否具有下属部门
 * hasNotShowBrother()方法用于判断该部门是否还有尚未处理的同级部门
 * getIndex()方法用于从一个部门的位置,得到该部门在记录源中的序号(记录集是排过序的)
 
*/

public class DeptBean {
    
public static final int SIT_STEP=2;
    
public static final int DEPT_ID=0;
    
public static final int DEPT_NAME=1;
    
public static final int DEPT_SIT=2;
    
    
private DbConnector connector;
    
private ArrayList data;
    
    
public DeptBean(){
    }

    
public void setDbConnector(DbConnector connector){
        
this.connector=connector;
    }

    
/**设置根部门
    * 
@param id:部门ID
    
*/

    
public void setRootDept(int id){
        String sql;
        sql
="select dept_sit from t_dept where dept_id=" + id;
        
try{
            ArrayList table,row;
            table
=this.connector.executeQuery(sql);
            row
=(ArrayList)table.get(0);
            
            String sit
=(String)row.get(0);
            setRootDept(sit);
        }
catch(Exception e){
            System.out.println(e.getMessage());
        }

    }

    
/**设置根部门
    * 
@param sit:部门位置
    
*/

    
public void setRootDept(String sit){
        String sql;
        sql
="select dept_id,dept_name,dept_sit from t_dept " +
                
"where INSTR(dept_sit,'" + sit +
                
"')=1 order by dept_sit";
        
        ArrayList row;
        
        
try{
            data
=this.connector.executeQuery(sql);
            System.out.println(
"共找到"+data.size()+"个部门");
        }
catch(Exception e){
            System.out.println(e.getMessage());
        }

    }

    
/**获取数据
    
*/

    
public ArrayList getData(){
        
return this.data;
    }

    
/**检查是/否具有子部门
     * 
@param dept_index:data中部门序号
     
*/

    
public boolean hasChild(int dept_index){
        
boolean result=false;
        
        ArrayList theDept,nextDept;
        String theSit,nextSit;
        
        
if(dept_index < this.data.size()-1){    //还有未处理部门
            
            theDept
=(ArrayList)this.data.get(dept_index);        //提取部门信息
            theSit=(String)theDept.get(DeptBean.DEPT_SIT);
            nextDept
=(ArrayList)this.data.get(dept_index+1);    //提取下一部门信息
            nextSit=(String)nextDept.get(DeptBean.DEPT_SIT);
            
            
if(nextSit.indexOf(theSit)==0){        //根据位置进行检查
                result=true;
            }

            
        }

        
        
return result;
    }

    
/**检查是/否具有未处理的兄弟部门
     * 
@param dept_index:data中部门序号
     
*/

    
public boolean hasNotShowBrother(int dept_index){
        
boolean result=false;
        
        ArrayList theDept,nextDept;
        String parentSit,theSit,nextSit;
        
        
if(dept_index < this.data.size()-1){    //还有未处理部门
            
            theDept
=(ArrayList)this.data.get(dept_index);        //提取部门信息
            theSit=(String)theDept.get(DeptBean.DEPT_SIT);
            
            
if(theSit.length() > DeptBean.SIT_STEP){
                parentSit
=theSit.substring(0,theSit.length() - DeptBean.SIT_STEP);
                
//System.out.println(parentSit);
                for(int i=dept_index+1;i<this.data.size();i++){    //检查下面部门
                    nextDept=(ArrayList)this.data.get(i);    //提取下一部门信息
                    nextSit=(String)nextDept.get(DeptBean.DEPT_SIT);
                    
                    
if(nextSit.indexOf(parentSit)==0 && nextSit.length()== theSit.length()){        //根据位置进行检查
                        result=true;
                        
break;
                    }

                    
                }

            }

            
        }

        
        
return result;
    }

    
/**检查是/否具有未处理的兄弟部门
     * 
@param dept_Sit:data中部门位置
     
*/

    
public boolean hasNotShowBrother(String dept_Sit){
        
return this.hasNotShowBrother(this.getIndex(dept_Sit));
    }

    
/**得到指定部门的序号
    * 
@param dept_Sit:部门位置
    
*/

    
public int getIndex(String dept_Sit){
        
int dept_index=0;
        ArrayList theDept;
        String theSit;
        
for(int i=0;i<this.data.size();i++){
            theDept
=(ArrayList)this.data.get(i);
            theSit
=(String)theDept.get(DeptBean.DEPT_SIT);
            
if(theSit.equalsIgnoreCase(dept_Sit)){
                dept_index
=i;
                
break;
            }

        }

        
return dept_index;
    }

    
    
public static void main(String[] args) {
        
        DeptBean bean1
=new DeptBean();
        
    }

}



JSP:
<% @page contentType = " text/html;charset=gb2312 " %>
<% @page  import = " java.io.* " %>
<% @page  import = " bean.* " %>

< html >
    
< style type = ' text/css ' >
        A:link
{color: # 000000 ; TEXT - DECORATION: none;}
        A:visited 
{COLOR: # 000000 ; TEXT - DECORATION: none}
        A:active 
{COLOR: #3333ff; TEXT - DECORATION: none}
        A:hover 
{COLOR: #3333ff; TEXT - DECORATION: none}
    
</ style >
    
< body bgcolor = ' #FFFFFF ' >
    
< jsp:useBean id = ' dept '  scope = ' page '   class = ' bean.DeptBean ' />

    
<%
        DbConnector connector
= new  DbConnector();     // 新建一个数据库连接器
        dept.setDbConnector(connector);                 // 将连接器与BEAN绑定
        dept.setRootDept( " 00 " );                         // Init BEAN
        
        String name,sit;
        String proId
= null ,theId;
        
        
int  tmp1;
        
        
int  rooLevel = 0 ,proLevel = 0 ,theLevel = 0 ;
        java.util.ArrayList data 
=  dept.getData(),row = null ;     // 拿出BEAN中的记录源,定义一个ROW来代表行
        
        
int  deptCount = data.size();     // 取得部门总数
        
        
for ( int  dept_index = 0 ;dept_index < deptCount;dept_index ++ {
            
            row
= (java.util.ArrayList)data.get(dept_index);     // 取得当前部门资料(id,name,sit)
            
            theId
= ((Integer)row.get(DeptBean.DEPT_ID)).toString();
            theId
= " MEMU_ "   +  theId;
            
            name
= (String)row.get(DeptBean.DEPT_NAME);
            sit
= (String)row.get(DeptBean.DEPT_SIT);
            
            theLevel
= sit.length() / DeptBean.SIT_STEP;     // 计算当前部门所处级别
            
            
            
if (dept_index  ==   0 ) {
                
// 根部门
                rooLevel = proLevel = theLevel - 1 ;
            }

            
            
            
if (theLevel > proLevel) {         // 部门级数递增
                
// 新建一个控制表格,控制与当前同等级的部门
                 if (dept_index == 0 ) {
                    out.println(
" <table border='0' cellspacing='0' width='100%' cellpadding='0' height='100%'> " );
                    out.println(
"     <tr> " );
                    out.println(
"     <td valign='top' align='left' style='BORDER-top: #000000 1px inset' bgcolor='#dee7ff'> " );
                }
else {
                    out.println(
" <table border='0' cellspacing='0' cellpadding='0' id=' "   +  proId  +   " d' style='display:none'> " );
                    out.println(
"     <tr> " );
                    out.println(
"     <td> " );
                }

                
                
if (dept_index > 0   &&  dept.hasChild(dept_index)) {
                    
// 新建一个容器表格
                    out.println( " <table style='font-size:10pt' border='0' cellspacing='0' cellpadding='0'> " );
                    out.println(
"     <tr> " );
                    out.println(
"     <td> " );
                }

                
            }
else {
                
if (theLevel == proLevel) {         // 同级部门
                    
                }
else {                         // 另一支树干
                    
// 封闭多层控制表格
                    tmp1 = 0 ;
                    
do {
                        out.println(
"     </td> " );
                        out.println(
"     </tr> " );
                        out.println(
" </table> " );
                        tmp1
++ ;
                    }
while (tmp1  <  (proLevel - theLevel - 1 ) * 2 );
                }

            }

            
            
// 新建一个数据表格
            out.println( " <table style='font-size:10pt' border='0' cellspacing='0' cellpadding='0'> " );
            
// 画行首
            out.println( "     <tr> " );
            
            
// 画前导空格
             if (theLevel  !=   1 ) {
                out.println(
"         <td><img src='images/tree/tree_transp.gif'></td> " );
            }

            
            
// 画treeline
             for (tmp1 = 2 ;tmp1 < theLevel;tmp1 ++ ) {
                
                String proSit
= sit.substring( 0 ,tmp1  *  DeptBean.DEPT_SIT);
                
if (dept.hasNotShowBrother(proSit)) {
                    out.println(
"         <td><img src='images/tree/tree_line.gif'></td> " );
                }
else {
                    out.println(
"         <td><img src='images/tree/tree_transp.gif'></td> " );
                }

            }

            
            
// System.out.println(dept_index);
            
// 画控制线
             if (dept.hasChild(dept_index)) {
                
// 具有下级部门
                
                
if (dept.hasNotShowBrother(dept_index)) {
                    
// 具有同级部门
                    out.println( "         <td><img src='images/tree/tree_plus.gif' id=' "   +  theId  +   " ' class='outline' style='cursor:hand'></td> " );
                }
else {
                    out.println(
"         <td><img src='images/tree/tree_plusl.gif' id=' "    +  theId  +   " ' class='outline' style='cursor:hand'></td> " );
                }

            }
else {
                
if (dept.hasNotShowBrother(dept_index)) {
                    
// 具有同级部门
                    out.println( "         <td><img src='images/tree/tree_blank.gif'></td> " );
                }
else {
                    out.println(
"         <td><img src='images/tree/tree_blankl.gif'></td> " );
                }

            }

            
            
// 画名称
             if (dept.hasChild(dept_index)) {
                out.println(
"         <td><a href='#' onclick=' "   +  theId  +   " .click();openURL(\ ""  + theId +  " \ " );'> "   +  name  +   " </a></td> " );
            }
else {
                out.println(
"         <td><a href='#' onclick='openURL(\ ""  + theId +  " \ " );'> "   +  name  +   " </a></td> " );
            }

            
            
// 画行结尾
            out.println( "     </tr> " );
            
            
//
            out.println( " </table> " );
            
            
// 保留上节点数据
            proId = theId;
            proLevel
= theLevel;
            
        }

        
        
// 封闭全部表格
         for (tmp1  =   0 ;tmp1 <  (theLevel  -  rooLevel  - 1 ) * 2   -   1 ;tmp1 ++ ) {
            out.println(
"     </td> " );
            out.println(
"     </tr> " );
            out.println(
" </table> " );
        }

        
        connector.close();
    
%>
    
</ body >
    
< script language = ' JavaScript ' >
    function clickHandler() 
{
        var targetid,srcelement,targetelement;
        var strbuf;
        srcelement 
=  window.event.srcElement;

        
if (srcelement.className  ==   ' outline ' {
            targetid 
=  srcelement.id + ' d ' ;
                targetelement 
=  document.all(targetid);
                
if  (targetelement.style.display  ==   ' none ' {
                    targetelement.style.display 
=   '' ;
                        strbuf 
=  srcelement.src;
                        
if  (strbuf.indexOf ( ' plus.gif ' ) >- 1 )
                            srcelement.src 
=   ' images/tree/tree_minus.gif ' ;
                        
else
                            srcelement.src 
=   ' images/tree/tree_minusl.gif ' ;
                }
else {
                    targetelement.style.display 
=   ' none ' ;
                    strbuf 
=  srcelement.src;
                    
if  (strbuf.indexOf ( ' minus.gif ' ) >- 1 )
                        srcelement.src 
=   ' images/tree/tree_plus.gif ' ;
                    
else
                        srcelement.src 
=   ' images/tree/tree_plusl.gif ' ;
                 }

        }

    }

    document.onclick 
=  clickHandler;
    
    function openURL(URL) 
{
        
if  (URL  ==   '' )
            
return   false ;
        parent.frmShow.location
= URL;
    }

    
</ script >
</ html >



只有注册用户登录后才能发表评论。


网站导航:
 

i am ddm