home *** CD-ROM | disk | FTP | other *** search
- import java.util.Vector;
-
- class tree_grid_builder {
- public Vector web_tree;
- public int max_level;
- public int my_max_width;
- public tree_entry[][] tree_grid;
- public static final int MAP_WEB_TREE = 0;
- public static final int TRANSFER_TO_GRID = 1;
- private int[] row_position;
-
- tree_grid_builder(Vector web_tree, int max_level) {
- this.web_tree = web_tree;
- this.max_level = max_level;
- this.init_row_position();
- this.recurse_tree(web_tree, 0);
- this.tree_grid = new tree_entry[this.find_max_width() + 2][max_level + 2];
- this.recurse_tree(web_tree, 1);
- }
-
- private int find_below(int level) {
- int below = -1;
-
- for(int i = level + 1; i < this.row_position.length; ++i) {
- if (this.row_position[i] > below) {
- below = this.row_position[i];
- }
- }
-
- return below + 1;
- }
-
- public void remap_grid(int max_level) {
- this.max_level = max_level;
- this.clear_tree_grid();
- this.init_row_position();
- this.recurse_tree(this.web_tree, 0);
- this.recurse_tree(this.web_tree, 1);
- }
-
- public int find_max_width() {
- int max = 0;
-
- for(int i = 0; i < this.row_position.length; ++i) {
- if (this.row_position[i] > max) {
- max = this.row_position[i];
- }
- }
-
- this.my_max_width = max;
- return max;
- }
-
- private void recurse_tree(Vector branch, int mode) {
- for(int i = 0; i < branch.size(); ++i) {
- tree_entry entry = (tree_entry)branch.elementAt(i);
- if (entry.in_use) {
- switch (mode) {
- case 0:
- this.map_element(entry);
- if (entry.children != null) {
- this.recurse_tree(entry.children, mode);
- }
- break;
- case 1:
- if (entry.children != null) {
- this.recurse_tree(entry.children, mode);
- }
-
- this.tree_grid[this.find_child_median(entry)][entry.grid_y] = entry;
- break;
- default:
- System.out.println("recursing error");
- return;
- }
- }
- }
-
- }
-
- private int find_child_median(tree_entry entry) {
- if (entry.children != null && ((tree_entry)entry.children.firstElement()).in_use) {
- tree_entry first = (tree_entry)entry.children.firstElement();
- tree_entry last = (tree_entry)entry.children.lastElement();
- entry.grid_x = first.grid_x + (last.grid_x - first.grid_x) / 2;
- }
-
- return entry.grid_x;
- }
-
- public int find_grid_width(int max_level) {
- int new_max = 0;
-
- for(int i = 0; i < max_level + 1; ++i) {
- for(int j = 0; j < this.my_max_width + 1; ++j) {
- if (this.tree_grid[j][i] != null && j > new_max) {
- new_max = j;
- }
- }
- }
-
- return new_max;
- }
-
- private int find_above(int level) {
- return level > 0 ? this.row_position[level - 1] : -1;
- }
-
- private void init_row_position() {
- this.row_position = new int[this.max_level + 1];
-
- for(int i = 0; i < this.max_level + 1; ++i) {
- this.row_position[i] = -1;
- }
-
- }
-
- private void print_row_position() {
- for(int i = 0; i < this.row_position.length; ++i) {
- System.out.println("level " + i + ": " + this.row_position[i]);
- }
-
- }
-
- private void map_element(tree_entry entry) {
- int level = entry.get_level();
- int var10002 = this.row_position[level]++;
- int above = this.find_above(level);
- int at = this.row_position[level];
- int below = this.find_below(level);
- this.row_position[level] = Math.max(above, Math.max(at, below));
- entry.grid_x = this.row_position[level];
- entry.grid_y = level;
- }
-
- private void clear_tree_grid() {
- for(int i = 0; i < this.max_level + 1; ++i) {
- for(int j = 0; j < this.my_max_width + 1; ++j) {
- this.tree_grid[j][i] = null;
- }
- }
-
- }
- }
-