大大毛 的筆記

            DDM's Note

          哪怕沒有辦法一定有說法,
          就算沒有鴿子一定有烏鴉,
          固執無罪 夢想有價,
          讓他們驚訝.

          posts - 14, comments - 23, trackbacks - 0, articles - 58
             :: 首頁 ::  :: 聯系 ::  :: 管理

          實現樹的類

          Posted on 2006-03-09 02:22 大大毛 閱讀(279) 評論(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

          主站蜘蛛池模板: 铅山县| 南和县| 达拉特旗| 华安县| 苗栗县| 岢岚县| 小金县| 咸丰县| 沅江市| 稻城县| 商洛市| 道真| 景宁| 凤城市| 司法| 昌吉市| 宜君县| 榕江县| 漳州市| 绥滨县| 噶尔县| 常州市| 界首市| 祥云县| 安丘市| 库尔勒市| 驻马店市| 汪清县| 吴川市| 峨山| 象山县| 贵州省| 繁峙县| 兖州市| 宿迁市| 铅山县| 济南市| 永定县| 普宁市| 来宾市| 永兴县|