home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Netfusion / data1.cab / Components / SiteMapper / nav_canvas.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-12-10  |  5.6 KB  |  190 lines

  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8. import java.awt.Image;
  9. import java.awt.image.ImageObserver;
  10.  
  11. public class nav_canvas extends Canvas {
  12.    tree_entry[][] tree_grid;
  13.    int max_level;
  14.    int my_max_width;
  15.    int x_factor;
  16.    int y_factor;
  17.    Sitemapper parent_applet;
  18.    String current_selection;
  19.    boolean repeat_current;
  20.    boolean tmp_nox = true;
  21.    boolean tmp_noy = true;
  22.    private Image offScreenImage;
  23.    private Graphics offScreenGraphics;
  24.    private Dimension offScreenSize;
  25.  
  26.    nav_canvas(tree_entry[][] tree_grid, int max_level, int my_max_width, Sitemapper parent_applet) {
  27.       this.tree_grid = tree_grid;
  28.       this.max_level = max_level;
  29.       this.my_max_width = my_max_width;
  30.       this.parent_applet = parent_applet;
  31.    }
  32.  
  33.    tree_entry find_location(int x, int y) {
  34.       int grid_x = 0;
  35.       int grid_y = 0;
  36.       Dimension dim = ((Component)this).size();
  37.       this.x_factor = dim.width / (this.my_max_width + 1);
  38.       this.y_factor = dim.height / (this.max_level + 1);
  39.       boolean nox = true;
  40.       boolean noy = true;
  41.  
  42.       for(int i = 0; i < this.my_max_width + 2; ++i) {
  43.          if (x < i * this.x_factor - this.x_factor / 2 && x > (i - 1) * this.x_factor) {
  44.             grid_x = i - 1;
  45.             nox = false;
  46.             break;
  47.          }
  48.       }
  49.  
  50.       for(int ii = 0; ii < this.max_level + 2; ++ii) {
  51.          if (y < ii * this.y_factor - this.y_factor / 2 && y > (ii - 1) * this.y_factor) {
  52.             grid_y = ii - 1;
  53.             noy = false;
  54.             break;
  55.          }
  56.       }
  57.  
  58.       if (this.tree_grid[grid_x][grid_y] != null && this.tree_grid[grid_x][grid_y].publish) {
  59.          String temp_selection = this.tree_grid[grid_x][grid_y].get_url();
  60.          if (temp_selection.equals(this.current_selection)) {
  61.             this.repeat_current = true;
  62.          } else {
  63.             this.repeat_current = false;
  64.          }
  65.  
  66.          if (this.tmp_nox != nox || this.tmp_noy != noy) {
  67.             this.repeat_current = false;
  68.          }
  69.  
  70.          this.tmp_nox = nox;
  71.          this.tmp_noy = noy;
  72.          this.current_selection = temp_selection;
  73.          return !nox && !noy ? this.tree_grid[grid_x][grid_y] : null;
  74.       } else {
  75.          this.current_selection = null;
  76.          return null;
  77.       }
  78.    }
  79.  
  80.    public void paint(Graphics g) {
  81.       Dimension dim = ((Component)this).size();
  82.       g.setColor(Color.white);
  83.       g.fillRect(0, 0, dim.width, dim.height);
  84.       g.setColor(Color.black);
  85.       this.x_factor = dim.width / (this.my_max_width + 1);
  86.       this.y_factor = dim.height / (this.max_level + 1);
  87.  
  88.       for(int i = 0; i < this.my_max_width + 1; ++i) {
  89.          for(int j = 0; j < this.max_level + 1; ++j) {
  90.             if (this.tree_grid[i][j] != null && this.tree_grid[i][j].in_use) {
  91.                boolean publish = this.tree_grid[i][j].publish;
  92.                this.draw_node(g, i, j, dim, publish);
  93.                this.draw_branches(this.tree_grid[i][j], dim, g);
  94.             }
  95.          }
  96.       }
  97.  
  98.    }
  99.  
  100.    public boolean mouseUp(Event evt, int x, int y) {
  101.       System.out.println("current_selection: " + this.current_selection);
  102.       if (this.current_selection != null) {
  103.          this.parent_applet.show_url(this.current_selection);
  104.       }
  105.  
  106.       return false;
  107.    }
  108.  
  109.    void draw_branches(tree_entry entry, Dimension dim, Graphics g) {
  110.       if (entry.children != null) {
  111.          tree_entry first = (tree_entry)entry.children.firstElement();
  112.          if (!first.in_use) {
  113.             return;
  114.          }
  115.  
  116.          tree_entry last = (tree_entry)entry.children.lastElement();
  117.          g.drawLine(first.grid_x * this.x_factor + this.x_factor / 4, first.grid_y * this.y_factor - this.y_factor / 2 + 5, last.grid_x * this.x_factor + this.x_factor / 4, last.grid_y * this.y_factor - this.y_factor / 2 + 5);
  118.          g.drawLine(entry.grid_x * this.x_factor + this.x_factor / 4, entry.grid_y * this.y_factor + this.y_factor / 2, entry.grid_x * this.x_factor + this.x_factor / 4, entry.grid_y * this.y_factor + this.y_factor / 2 + 5);
  119.  
  120.          for(int i = 0; i < entry.children.size(); ++i) {
  121.             tree_entry child = (tree_entry)entry.children.elementAt(i);
  122.             g.drawLine(child.grid_x * this.x_factor + this.x_factor / 4, child.grid_y * this.y_factor - this.y_factor / 2 + 5, child.grid_x * this.x_factor + this.x_factor / 4, child.grid_y * this.y_factor);
  123.          }
  124.       }
  125.  
  126.    }
  127.  
  128.    public final synchronized void update(Graphics theG) {
  129.       Dimension d = ((Component)this).size();
  130.       if (this.offScreenImage == null || d.width != this.offScreenSize.width || d.height != this.offScreenSize.height) {
  131.          this.offScreenImage = ((Component)this).createImage(d.width, d.height);
  132.          this.offScreenSize = d;
  133.          this.offScreenGraphics = this.offScreenImage.getGraphics();
  134.          this.offScreenGraphics.setFont(((Component)this).getFont());
  135.       }
  136.  
  137.       this.offScreenGraphics.fillRect(0, 0, d.width, d.height);
  138.       this.paint(this.offScreenGraphics);
  139.       theG.drawImage(this.offScreenImage, 0, 0, (ImageObserver)null);
  140.    }
  141.  
  142.    void draw_node(Graphics g, int i, int j, Dimension dim, boolean publish) {
  143.       if (publish) {
  144.          g.setColor(Color.yellow);
  145.       } else {
  146.          g.setColor(Color.lightGray);
  147.       }
  148.  
  149.       g.fillRect(i * this.x_factor, j * this.y_factor, this.x_factor / 2, this.y_factor / 2);
  150.       g.setColor(Color.black);
  151.       g.drawRect(i * this.x_factor, j * this.y_factor, this.x_factor / 2, this.y_factor / 2);
  152.    }
  153.  
  154.    public boolean mouseMove(Event e, int x, int y) {
  155.       tree_entry entry = this.find_location(x, y);
  156.       Graphics g = ((Component)this).getGraphics();
  157.       if (entry != null && entry.publish) {
  158.          if (!this.repeat_current) {
  159.             this.update(g);
  160.             g.setColor(Color.red);
  161.             g.fillRect(entry.grid_x * this.x_factor - 1, entry.grid_y * this.y_factor - 1, this.x_factor / 2 + 2, this.y_factor / 2 + 2);
  162.             g.setColor(Color.black);
  163.             FontMetrics fm = g.getFontMetrics(g.getFont());
  164.             int str_width = fm.stringWidth(entry.get_name());
  165.             Dimension d = ((Component)this).size();
  166.             int horiz_offset = 0;
  167.             int vert_offset = 0;
  168.             if (str_width + entry.grid_x * this.x_factor + this.x_factor / 2 + 2 > d.width) {
  169.                horiz_offset = str_width + entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - d.width;
  170.             }
  171.  
  172.             if (entry.grid_y * this.y_factor - fm.getHeight() - 2 < 0) {
  173.                vert_offset = 0 - (entry.grid_y * this.y_factor - fm.getHeight() - 2);
  174.             }
  175.  
  176.             g.setColor(Color.white);
  177.             g.fillRect(entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - horiz_offset, entry.grid_y * this.y_factor - fm.getHeight() - 2 + vert_offset, str_width, fm.getHeight());
  178.             g.setColor(Color.black);
  179.             g.drawString(entry.get_name(), entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - horiz_offset, entry.grid_y * this.y_factor - 2 + vert_offset);
  180.             return true;
  181.          } else {
  182.             return false;
  183.          }
  184.       } else {
  185.          this.update(g);
  186.          return false;
  187.       }
  188.    }
  189. }
  190.