home *** CD-ROM | disk | FTP | other *** search
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.ImageObserver;
-
- public class nav_canvas extends Canvas {
- tree_entry[][] tree_grid;
- int max_level;
- int my_max_width;
- int x_factor;
- int y_factor;
- Sitemapper parent_applet;
- String current_selection;
- boolean repeat_current;
- boolean tmp_nox = true;
- boolean tmp_noy = true;
- private Image offScreenImage;
- private Graphics offScreenGraphics;
- private Dimension offScreenSize;
-
- nav_canvas(tree_entry[][] tree_grid, int max_level, int my_max_width, Sitemapper parent_applet) {
- this.tree_grid = tree_grid;
- this.max_level = max_level;
- this.my_max_width = my_max_width;
- this.parent_applet = parent_applet;
- }
-
- tree_entry find_location(int x, int y) {
- int grid_x = 0;
- int grid_y = 0;
- Dimension dim = ((Component)this).size();
- this.x_factor = dim.width / (this.my_max_width + 1);
- this.y_factor = dim.height / (this.max_level + 1);
- boolean nox = true;
- boolean noy = true;
-
- for(int i = 0; i < this.my_max_width + 2; ++i) {
- if (x < i * this.x_factor - this.x_factor / 2 && x > (i - 1) * this.x_factor) {
- grid_x = i - 1;
- nox = false;
- break;
- }
- }
-
- for(int ii = 0; ii < this.max_level + 2; ++ii) {
- if (y < ii * this.y_factor - this.y_factor / 2 && y > (ii - 1) * this.y_factor) {
- grid_y = ii - 1;
- noy = false;
- break;
- }
- }
-
- if (this.tree_grid[grid_x][grid_y] != null && this.tree_grid[grid_x][grid_y].publish) {
- String temp_selection = this.tree_grid[grid_x][grid_y].get_url();
- if (temp_selection.equals(this.current_selection)) {
- this.repeat_current = true;
- } else {
- this.repeat_current = false;
- }
-
- if (this.tmp_nox != nox || this.tmp_noy != noy) {
- this.repeat_current = false;
- }
-
- this.tmp_nox = nox;
- this.tmp_noy = noy;
- this.current_selection = temp_selection;
- return !nox && !noy ? this.tree_grid[grid_x][grid_y] : null;
- } else {
- this.current_selection = null;
- return null;
- }
- }
-
- public void paint(Graphics g) {
- Dimension dim = ((Component)this).size();
- g.setColor(Color.white);
- g.fillRect(0, 0, dim.width, dim.height);
- g.setColor(Color.black);
- this.x_factor = dim.width / (this.my_max_width + 1);
- this.y_factor = dim.height / (this.max_level + 1);
-
- for(int i = 0; i < this.my_max_width + 1; ++i) {
- for(int j = 0; j < this.max_level + 1; ++j) {
- if (this.tree_grid[i][j] != null && this.tree_grid[i][j].in_use) {
- boolean publish = this.tree_grid[i][j].publish;
- this.draw_node(g, i, j, dim, publish);
- this.draw_branches(this.tree_grid[i][j], dim, g);
- }
- }
- }
-
- }
-
- public boolean mouseUp(Event evt, int x, int y) {
- System.out.println("current_selection: " + this.current_selection);
- if (this.current_selection != null) {
- this.parent_applet.show_url(this.current_selection);
- }
-
- return false;
- }
-
- void draw_branches(tree_entry entry, Dimension dim, Graphics g) {
- if (entry.children != null) {
- tree_entry first = (tree_entry)entry.children.firstElement();
- if (!first.in_use) {
- return;
- }
-
- tree_entry last = (tree_entry)entry.children.lastElement();
- 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);
- 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);
-
- for(int i = 0; i < entry.children.size(); ++i) {
- tree_entry child = (tree_entry)entry.children.elementAt(i);
- 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);
- }
- }
-
- }
-
- public final synchronized void update(Graphics theG) {
- Dimension d = ((Component)this).size();
- if (this.offScreenImage == null || d.width != this.offScreenSize.width || d.height != this.offScreenSize.height) {
- this.offScreenImage = ((Component)this).createImage(d.width, d.height);
- this.offScreenSize = d;
- this.offScreenGraphics = this.offScreenImage.getGraphics();
- this.offScreenGraphics.setFont(((Component)this).getFont());
- }
-
- this.offScreenGraphics.fillRect(0, 0, d.width, d.height);
- this.paint(this.offScreenGraphics);
- theG.drawImage(this.offScreenImage, 0, 0, (ImageObserver)null);
- }
-
- void draw_node(Graphics g, int i, int j, Dimension dim, boolean publish) {
- if (publish) {
- g.setColor(Color.yellow);
- } else {
- g.setColor(Color.lightGray);
- }
-
- g.fillRect(i * this.x_factor, j * this.y_factor, this.x_factor / 2, this.y_factor / 2);
- g.setColor(Color.black);
- g.drawRect(i * this.x_factor, j * this.y_factor, this.x_factor / 2, this.y_factor / 2);
- }
-
- public boolean mouseMove(Event e, int x, int y) {
- tree_entry entry = this.find_location(x, y);
- Graphics g = ((Component)this).getGraphics();
- if (entry != null && entry.publish) {
- if (!this.repeat_current) {
- this.update(g);
- g.setColor(Color.red);
- 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);
- g.setColor(Color.black);
- FontMetrics fm = g.getFontMetrics(g.getFont());
- int str_width = fm.stringWidth(entry.get_name());
- Dimension d = ((Component)this).size();
- int horiz_offset = 0;
- int vert_offset = 0;
- if (str_width + entry.grid_x * this.x_factor + this.x_factor / 2 + 2 > d.width) {
- horiz_offset = str_width + entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - d.width;
- }
-
- if (entry.grid_y * this.y_factor - fm.getHeight() - 2 < 0) {
- vert_offset = 0 - (entry.grid_y * this.y_factor - fm.getHeight() - 2);
- }
-
- g.setColor(Color.white);
- 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());
- g.setColor(Color.black);
- 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);
- return true;
- } else {
- return false;
- }
- } else {
- this.update(g);
- return false;
- }
- }
- }
-