大大毛 的筆記

            DDM's Note

          哪怕沒(méi)有辦法一定有說(shuō)法,
          就算沒(méi)有鴿子一定有烏鴉,
          固執(zhí)無(wú)罪 夢(mèng)想有價(jià),
          讓他們驚訝.

          posts - 14, comments - 23, trackbacks - 0, articles - 58
             :: 首頁(yè) ::  :: 聯(lián)系 ::  :: 管理

          實(shí)現(xiàn)樹(shù)的類

          Posted on 2006-03-09 02:22 大大毛 閱讀(278) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
          JavaBean類:
          ??????? DeptBean,用于支持那棵樹(shù),因?yàn)槭菑囊郧白鳂I(yè)中摳出來(lái)的,所以肯定是不會(huì)通用的,但是改改再用還是可行的。
          import?java.util.*;
          /**用于支持部門(mén)的Tree視圖
          ?*?
          @author?tw
          ?*?xx:主要是通過(guò)setRootDept()方法來(lái)得到一棵樹(shù)所必須的數(shù)據(jù),所需參數(shù)指樹(shù)從哪個(gè)部門(mén)開(kāi)始,我的table的結(jié)構(gòu)如下:
          ?*?create?table?t_dept?(
          ?*?dept_id?int?primary?key,????----主鍵
          ?*?dept_name?varchar(),?????----顯示用的名稱
          ?*?dept_sit??varchar()?);???----代表部門(mén)位置的字符串,例如總經(jīng)理室?00,部門(mén)1?0001,部門(mén)2?0002
          ?*?hasChild()方法用于判斷該部門(mén)是否具有下屬部門(mén)
          ?*?hasNotShowBrother()方法用于判斷該部門(mén)是否還有尚未處理的同級(jí)部門(mén)
          ?*?getIndex()方法用于從一個(gè)部門(mén)的位置,得到該部門(mén)在記錄源中的序號(hào)(記錄集是排過(guò)序的)
          ?
          */

          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;
          ????}

          ????
          /**設(shè)置根部門(mén)
          ????*?
          @param?id:部門(mén)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());
          ????????}

          ????}

          ????
          /**設(shè)置根部門(mén)
          ????*?
          @param?sit:部門(mén)位置
          ????
          */

          ????
          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()+"個(gè)部門(mén)");
          ????????}
          catch(Exception?e){
          ????????????System.out.println(e.getMessage());
          ????????}

          ????}

          ????
          /**獲取數(shù)據(jù)
          ????
          */

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

          ????
          /**檢查是/否具有子部門(mén)
          ?????*?
          @param?dept_index:data中部門(mén)序號(hào)
          ?????
          */

          ????
          public?boolean?hasChild(int?dept_index){
          ????????
          boolean?result=false;
          ????????
          ????????ArrayList?theDept,nextDept;
          ????????String?theSit,nextSit;
          ????????
          ????????
          if(dept_index?<?this.data.size()-1){????//還有未處理部門(mén)
          ????????????
          ????????????theDept
          =(ArrayList)this.data.get(dept_index);????????//提取部門(mén)信息
          ????????????theSit=(String)theDept.get(DeptBean.DEPT_SIT);
          ????????????nextDept
          =(ArrayList)this.data.get(dept_index+1);????//提取下一部門(mén)信息
          ????????????nextSit=(String)nextDept.get(DeptBean.DEPT_SIT);
          ????????????
          ????????????
          if(nextSit.indexOf(theSit)==0){????????//根據(jù)位置進(jìn)行檢查
          ????????????????result=true;
          ????????????}

          ????????????
          ????????}

          ????????
          ????????
          return?result;
          ????}

          ????
          /**檢查是/否具有未處理的兄弟部門(mén)
          ?????*?
          @param?dept_index:data中部門(mén)序號(hào)
          ?????
          */

          ????
          public?boolean?hasNotShowBrother(int?dept_index){
          ????????
          boolean?result=false;
          ????????
          ????????ArrayList?theDept,nextDept;
          ????????String?parentSit,theSit,nextSit;
          ????????
          ????????
          if(dept_index?<?this.data.size()-1){????//還有未處理部門(mén)
          ????????????
          ????????????theDept
          =(ArrayList)this.data.get(dept_index);????????//提取部門(mén)信息
          ????????????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++){????//檢查下面部門(mén)
          ????????????????????nextDept=(ArrayList)this.data.get(i);????//提取下一部門(mén)信息
          ????????????????????nextSit=(String)nextDept.get(DeptBean.DEPT_SIT);
          ????????????????????
          ????????????????????
          if(nextSit.indexOf(parentSit)==0?&&?nextSit.length()==?theSit.length()){????????//根據(jù)位置進(jìn)行檢查
          ????????????????????????result=true;
          ????????????????????????
          break;
          ????????????????????}

          ????????????????????
          ????????????????}

          ????????????}

          ????????????
          ????????}

          ????????
          ????????
          return?result;
          ????}

          ????
          /**檢查是/否具有未處理的兄弟部門(mén)
          ?????*?
          @param?dept_Sit:data中部門(mén)位置
          ?????
          */

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

          ????
          /**得到指定部門(mén)的序號(hào)
          ????*?
          @param?dept_Sit:部門(mén)位置
          ????
          */

          ????
          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();???? // 新建一個(gè)數(shù)據(jù)庫(kù)連接器
          ????????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中的記錄源,定義一個(gè)ROW來(lái)代表行
          ????????
          ????????
          int ?deptCount = data.size();???? // 取得部門(mén)總數(shù)
          ????????
          ????????
          for ( int ?dept_index = 0 ;dept_index < deptCount;dept_index ++ )? {
          ????????????
          ????????????row
          = (java.util.ArrayList)data.get(dept_index);???? // 取得當(dāng)前部門(mén)資料(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;???? // 計(jì)算當(dāng)前部門(mén)所處級(jí)別
          ????????????
          ????????????
          ????????????
          if (dept_index? == ? 0 ) {
          ????????????????
          // 根部門(mén)
          ????????????????rooLevel = proLevel = theLevel - 1 ;
          ????????????}

          ????????????
          ????????????
          ????????????
          if (theLevel > proLevel) {???????? // 部門(mén)級(jí)數(shù)遞增
          ????????????????
          // 新建一個(gè)控制表格,控制與當(dāng)前同等級(jí)的部門(mén)
          ???????????????? 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)) {
          ????????????????????
          // 新建一個(gè)容器表格
          ????????????????????out.println( " <table?style='font-size:10pt'?border='0'?cellspacing='0'?cellpadding='0'> " );
          ????????????????????out.println(
          " ????<tr> " );
          ????????????????????out.println(
          " ????<td> " );
          ????????????????}

          ????????????????
          ????????????}
          else {
          ????????????????
          if (theLevel == proLevel) {???????? // 同級(jí)部門(mén)
          ????????????????????
          ????????????????}
          else {???????????????????????? // 另一支樹(shù)干
          ????????????????????
          // 封閉多層控制表格
          ????????????????????tmp1 = 0 ;
          ????????????????????
          do {
          ????????????????????????out.println(
          " ????</td> " );
          ????????????????????????out.println(
          " ????</tr> " );
          ????????????????????????out.println(
          " </table> " );
          ????????????????????????tmp1
          ++ ;
          ????????????????????}
          while (tmp1? < ?(proLevel - theLevel - 1 ) * 2 );
          ????????????????}

          ????????????}

          ????????????
          ????????????
          // 新建一個(gè)數(shù)據(jù)表格
          ????????????out.println( " <table?style='font-size:10pt'?border='0'?cellspacing='0'?cellpadding='0'> " );
          ????????????
          // 畫(huà)行首
          ????????????out.println( " ????<tr> " );
          ????????????
          ????????????
          // 畫(huà)前導(dǎo)空格
          ???????????? if (theLevel? != ? 1 ) {
          ????????????????out.println(
          " ????????<td><img?src='images/tree/tree_transp.gif'></td> " );
          ????????????}

          ????????????
          ????????????
          // 畫(huà)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);
          ????????????
          // 畫(huà)控制線
          ???????????? if (dept.hasChild(dept_index)) {
          ????????????????
          // 具有下級(jí)部門(mén)
          ????????????????
          ????????????????
          if (dept.hasNotShowBrother(dept_index)) {
          ????????????????????
          // 具有同級(jí)部門(mén)
          ????????????????????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)) {
          ????????????????????
          // 具有同級(jí)部門(mén)
          ????????????????????out.println( " ????????<td><img?src='images/tree/tree_blank.gif'></td> " );
          ????????????????}
          else {
          ????????????????????out.println(
          " ????????<td><img?src='images/tree/tree_blankl.gif'></td> " );
          ????????????????}

          ????????????}

          ????????????
          ????????????
          // 畫(huà)名稱
          ???????????? 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> " );
          ????????????}

          ????????????
          ????????????
          // 畫(huà)行結(jié)尾
          ????????????out.println( " ????</tr> " );
          ????????????
          ????????????
          //
          ????????????out.println( " </table> " );
          ????????????
          ????????????
          // 保留上節(jié)點(diǎn)數(shù)據(jù)
          ????????????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

          主站蜘蛛池模板: 武强县| 收藏| 尉犁县| 锡林郭勒盟| 遂川县| 武邑县| 西乡县| 平和县| 会昌县| 天镇县| 云和县| 沁阳市| 竹山县| 临沭县| 英德市| 延寿县| 遂川县| 博野县| 尚义县| 理塘县| 安丘市| 乌拉特中旗| 青神县| 赣州市| 仲巴县| 汤原县| 左贡县| 灵丘县| 浦县| 丰宁| 二连浩特市| 收藏| 佳木斯市| 台湾省| 宿松县| 阿拉尔市| 长乐市| 乐陵市| 茶陵县| 彭阳县| 讷河市|