home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Netobjs / Install.exe / data1.cab / Components / SiteMapper / tree_grid_builder.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-12-16  |  3.5 KB  |  145 lines

  1. import java.util.Vector;
  2.  
  3. class tree_grid_builder {
  4.    public Vector web_tree;
  5.    public int max_level;
  6.    public int my_max_width;
  7.    public tree_entry[][] tree_grid;
  8.    public static final int MAP_WEB_TREE = 0;
  9.    public static final int TRANSFER_TO_GRID = 1;
  10.    private int[] row_position;
  11.  
  12.    tree_grid_builder(Vector web_tree, int max_level) {
  13.       this.web_tree = web_tree;
  14.       this.max_level = max_level;
  15.       this.init_row_position();
  16.       this.recurse_tree(web_tree, 0);
  17.       this.tree_grid = new tree_entry[this.find_max_width() + 2][max_level + 2];
  18.       this.recurse_tree(web_tree, 1);
  19.    }
  20.  
  21.    private int find_below(int level) {
  22.       int below = -1;
  23.  
  24.       for(int i = level + 1; i < this.row_position.length; ++i) {
  25.          if (this.row_position[i] > below) {
  26.             below = this.row_position[i];
  27.          }
  28.       }
  29.  
  30.       return below + 1;
  31.    }
  32.  
  33.    public void remap_grid(int max_level) {
  34.       this.max_level = max_level;
  35.       this.clear_tree_grid();
  36.       this.init_row_position();
  37.       this.recurse_tree(this.web_tree, 0);
  38.       this.recurse_tree(this.web_tree, 1);
  39.    }
  40.  
  41.    public int find_max_width() {
  42.       int max = 0;
  43.  
  44.       for(int i = 0; i < this.row_position.length; ++i) {
  45.          if (this.row_position[i] > max) {
  46.             max = this.row_position[i];
  47.          }
  48.       }
  49.  
  50.       this.my_max_width = max;
  51.       return max;
  52.    }
  53.  
  54.    private void recurse_tree(Vector branch, int mode) {
  55.       for(int i = 0; i < branch.size(); ++i) {
  56.          tree_entry entry = (tree_entry)branch.elementAt(i);
  57.          if (entry.in_use) {
  58.             switch (mode) {
  59.                case 0:
  60.                   this.map_element(entry);
  61.                   if (entry.children != null) {
  62.                      this.recurse_tree(entry.children, mode);
  63.                   }
  64.                   break;
  65.                case 1:
  66.                   if (entry.children != null) {
  67.                      this.recurse_tree(entry.children, mode);
  68.                   }
  69.  
  70.                   this.tree_grid[this.find_child_median(entry)][entry.grid_y] = entry;
  71.                   break;
  72.                default:
  73.                   System.out.println("recursing error");
  74.                   return;
  75.             }
  76.          }
  77.       }
  78.  
  79.    }
  80.  
  81.    private int find_child_median(tree_entry entry) {
  82.       if (entry.children != null && ((tree_entry)entry.children.firstElement()).in_use) {
  83.          tree_entry first = (tree_entry)entry.children.firstElement();
  84.          tree_entry last = (tree_entry)entry.children.lastElement();
  85.          entry.grid_x = first.grid_x + (last.grid_x - first.grid_x) / 2;
  86.       }
  87.  
  88.       return entry.grid_x;
  89.    }
  90.  
  91.    public int find_grid_width(int max_level) {
  92.       int new_max = 0;
  93.  
  94.       for(int i = 0; i < max_level + 1; ++i) {
  95.          for(int j = 0; j < this.my_max_width + 1; ++j) {
  96.             if (this.tree_grid[j][i] != null && j > new_max) {
  97.                new_max = j;
  98.             }
  99.          }
  100.       }
  101.  
  102.       return new_max;
  103.    }
  104.  
  105.    private int find_above(int level) {
  106.       return level > 0 ? this.row_position[level - 1] : -1;
  107.    }
  108.  
  109.    private void init_row_position() {
  110.       this.row_position = new int[this.max_level + 1];
  111.  
  112.       for(int i = 0; i < this.max_level + 1; ++i) {
  113.          this.row_position[i] = -1;
  114.       }
  115.  
  116.    }
  117.  
  118.    private void print_row_position() {
  119.       for(int i = 0; i < this.row_position.length; ++i) {
  120.          System.out.println("level " + i + ": " + this.row_position[i]);
  121.       }
  122.  
  123.    }
  124.  
  125.    private void map_element(tree_entry entry) {
  126.       int level = entry.get_level();
  127.       int var10002 = this.row_position[level]++;
  128.       int above = this.find_above(level);
  129.       int at = this.row_position[level];
  130.       int below = this.find_below(level);
  131.       this.row_position[level] = Math.max(above, Math.max(at, below));
  132.       entry.grid_x = this.row_position[level];
  133.       entry.grid_y = level;
  134.    }
  135.  
  136.    private void clear_tree_grid() {
  137.       for(int i = 0; i < this.max_level + 1; ++i) {
  138.          for(int j = 0; j < this.my_max_width + 1; ++j) {
  139.             this.tree_grid[j][i] = null;
  140.          }
  141.       }
  142.  
  143.    }
  144. }
  145.