/* * ======== NavBar ======== * * NavBar is a java applet for providing a navigation bar at the bottom * of every page of HTML. Configuration parameters are passed in through * the HTML tags which give let the applet know the text to * display on the buttons. * *# Revision History *# ================ *# 26-Aug-1996 jm: Created *# 13-Jan-1997 jm: Converted into an OO and Spectron style. *# */ import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; /* * ======== class NavBar ======== * * Contains methods to control the basic setup and initialization of the * applet. Also contains routines to deal with the interaction with the * user and control what buttons are pressed, passed over, or selected. * */ public class NavBar extends Applet { // Background Color protected Color bgColor; // Font attributes protected Font buttonFont = new Font("Times", Font.BOLD, 12); protected Font textFont = new Font("Times", Font.BOLD, 12); protected Font openFont = new Font("Times", Font.BOLD, 14); protected FontMetrics fmButton = this.getFontMetrics(buttonFont); protected FontMetrics fmText = this.getFontMetrics(textFont); protected FontMetrics fmOpen = this.getFontMetrics(openFont); // Array of the navigation buttons private int numButton = 0; private boolean buttonSelection = false; private ButtonElement buttonList[] = new ButtonElement[15]; private ButtonElement currentButton = null; private ButtonElement depressedButton = null; /* * ======== init ======== * * Read in the parameters passed in via the tag. * Creates the various button objects needed for the nav bar. * */ public void init() { // Default Colors of the navigation buttons Color buttonColor = Color.orange; Color buttonHighlight = Color.red; Color buttonShadow = Color.lightGray; Color textColor = Color.black; Color textHighlight = Color.black; Color textDescription = Color.blue; // URL to go to URL url = null; // Applet width, height, NavBar width, spacing between buttons int appWidth, appHeight, navBarWidth; int describeWidth; int buttonSpacing = 6; // Input values StringTokenizer st; String s, label = "", labelDescription = ""; // Retrieve basic parameters - background color, width, height bgColor = getColorParameter("bgcolor", Color.white); this.setBackground(bgColor); appWidth = Integer.parseInt(getParameter("width")); appHeight = Integer.parseInt(getParameter("height")); showStatus("Initializing buttons..."); // Initialize buttons while ((numButton < 15) && (s = getParameter("button" + numButton)) != null) { st = new StringTokenizer(s, "|"); switch (st.countTokens()) { case 9: try { buttonColor = new Color(Integer.parseInt(st.nextToken(), 16)); buttonHighlight = new Color(Integer.parseInt(st.nextToken(), 16)); buttonShadow = new Color(Integer.parseInt(st.nextToken(), 16)); textColor = new Color(Integer.parseInt(st.nextToken(), 16)); textHighlight = new Color(Integer.parseInt(st.nextToken(), 16)); textDescription = new Color(Integer.parseInt(st.nextToken(), 16)); } catch (NumberFormatException e) { System.out.println("Invalid color format (button " + numButton + "): " + e); } // Fall through - get labels & url case 3: label = st.nextToken(); labelDescription = st.nextToken(); try { url = new URL(getDocumentBase(), st.nextToken()); } catch (MalformedURLException e) { System.out.println("Invalid URL format (button " + numButton + "): " + e); } break; default: System.out.println("Wrong # of args for button " + numButton); label = "Button " + numButton; labelDescription = "Wrong number of arguments for " + "button " + numButton; break; } buttonList[numButton] = new RoundButton(bgColor, buttonColor, buttonHighlight, buttonShadow, textColor, textHighlight, textDescription, label, labelDescription, url); numButton++; } // Calculate width of the button bar and assign each button a position System.out.println("Calculating button positions..."); navBarWidth = -buttonSpacing; for (int i = 0; i < numButton; i++) { navBarWidth += buttonList[i].getWidth() + buttonSpacing; } int x = (appWidth - navBarWidth) / 2; int tmp1 = (int) (x + buttonList[0].getWidth() / 2); int tmp2 = (int) (x + buttonList[numButton - 1].getWidth() / 2); describeWidth = ((tmp1 < tmp2) ? tmp1 : tmp2) * 2; for (int i = 0; i < numButton; i++) { buttonList[i].setPosition(x, 0, describeWidth); x += buttonList[i].getWidth() + buttonSpacing; } } /* * ======== getColorParameter ======== * * Attempts to retrieve the color string in a tag and * return the appropriate color object. If this fails the default * color is returned * */ private Color getColorParameter(String name, Color defaultColor) { String value = getParameter(name); int intvalue; try { intvalue = Integer.parseInt(value, 16); } catch (NumberFormatException e) { return defaultColor; } return new Color(intvalue); } /* * ======== paint ======== * * Draws the NavBar. Any button that is in a "Open" state is now reset. * */ public void paint(Graphics g) { showStatus("Welcome to Spectron Microsystems!"); for (int i = 0; i < numButton; i++) { buttonList[i].draw(g); } } /* * ======== update ======== * */ public void update(Graphics g) { paint(g); } /* * ======== handleEvent ======== * */ public boolean handleEvent(Event e) { if (buttonSelection) { switch (e.id) { case Event.MOUSE_ENTER: case Event.MOUSE_EXIT: currentButton.unlockState(ButtonElement.UnHighlight, getGraphics()); currentButton = null; buttonSelection = false; } } else { return super.handleEvent(e); } return true; } /* * ======== mouseMove ======== * */ public boolean mouseMove(Event e, int x, int y) { Graphics g = getGraphics(); for (int i = 0; i < numButton; i++) { if (buttonList[i].inside(x, y)) { if (currentButton != buttonList[i]) { // Unhighlight the previous button if (currentButton != null) { currentButton.drawState(ButtonElement.UnHighlight, g); } currentButton = buttonList[i]; currentButton.drawState(ButtonElement.Highlight, g); } return true; } } // We are not inside any buttons - unhighlight the previous one if (currentButton != null) { currentButton.drawState(ButtonElement.UnHighlight, g); currentButton = null; } return true; } /* * ======== mouseDrag ======== * */ public boolean mouseDrag(Event e, int x, int y) { boolean test = (depressedButton != currentButton); mouseMove(e, x, y); // Only draw the button depressed if we are moving into it again if ((depressedButton == currentButton) && test) { currentButton.drawState(ButtonElement.Depressed, getGraphics()); } return true; } /* * ======== mouseDown ======== * */ public boolean mouseDown(Event e, int x, int y) { mouseMove(e, x, y); depressedButton = currentButton; if (currentButton != null) { currentButton.drawState(ButtonElement.Depressed, getGraphics()); } return true; } /* * ======== mouseUp ======== * */ public boolean mouseUp(Event e, int x, int y) { mouseMove(e, x, y); if (currentButton == depressedButton) { if (buttonSelection) { currentButton.drawState(ButtonElement.Highlight, getGraphics()); } else { currentButton.drawState(ButtonElement.Open, getGraphics()); // currentButton.openURL(); getAppletContext().showDocument(currentButton.getURL()); buttonSelection = true; } } depressedButton = null; return true; } /* * ======== mouseExit ======== * */ public boolean mouseExit(Event e, int x, int y) { if (currentButton != null) { currentButton.drawState(ButtonElement.UnHighlight, getGraphics()); } currentButton = null; return true; } } /* * ======== class ButtonElement ======== * * A class encompassing the many types of button elements you could have * in your navigation bar. * */ abstract class ButtonElement extends NavBar { // Various colors of the object protected Color bgColor, buttonColor, buttonHighlight, buttonShadow, textColor, textHighlight, textDescription; protected String label, labelDescription; protected String openLabel = "Opening page..."; /* protected Font buttonFont, textFont; protected FontMetrics fmButton, fmText; */ protected int x, y, describeWidth, width, height; protected URL url; // State of the button static final int Raised = 1; static final int Depressed = 2; static final int Highlight = 3; static final int UnHighlight = 4; static final int Open = 5; protected int state; // Constructor public ButtonElement(Color bg, Color b, Color bh, Color bs, Color t, Color th, Color td, String l, String ld, URL url) { bgColor = bg; buttonColor = b; buttonHighlight = bh; buttonShadow = bs; textColor = t; textHighlight = th; textDescription = td; label = " " + l + " "; labelDescription = ld; /* buttonFont = bf; textFont = tf; fmButton = fmB; fmText = fmT; */ this.url = url; state = Raised; width = fmButton.stringWidth(label); height = fmButton.getHeight(); System.out.println("Initializing button: " + label); } // Sets the upper left hand coordinates (x, y) of the button public void setPosition(int x, int y, int describeWidth) { this.x = x; this.y = y; this.describeWidth = describeWidth; } // Return width public int getWidth() { return width; } // Return url public URL getURL() { return url; } // Drawing routines public void draw(Graphics g) { switch (state) { case Raised: drawRaised(g); break; case Depressed: drawDepressed(g); break; case Highlight: drawHighlight(g); break; case UnHighlight: drawUnHighlight(g); break; case Open: drawOpen(g); break; default: drawRaised(g); break; } } public void unlockState(int state, Graphics g) { String old = labelDescription; this.state = state; labelDescription = openLabel; draw(g); labelDescription = old; } public void drawState(int state, Graphics g) { if (this.state != Open) { this.state = state; draw(g); } } public abstract boolean inside(int x, int y); protected abstract void drawRaised(Graphics g); protected abstract void drawDepressed(Graphics g); protected abstract void drawHighlight(Graphics g); protected abstract void drawUnHighlight(Graphics g); protected abstract void drawOpen(Graphics g); protected void drawLabelDescription(Graphics g) { String word, lastline; StringTokenizer st; int x1, y1; float lineNumber; lastline = ""; st = new StringTokenizer(labelDescription, " ", true); lineNumber = (float) 2.4; // Go through the whole line while (st.hasMoreTokens()) { word = st.nextToken(); // If we went beyond max, print previous line. if (fmText.stringWidth(lastline + word) >= describeWidth) { x1 = (x + (width / 2) - (fmText.stringWidth(lastline) / 2)); y1 = (int) (y + (height * lineNumber)); lineNumber++; g.drawString(lastline, x1, y1); lastline = word; } else { lastline = lastline + word; } } x1 = (x + (width / 2) - (fmText.stringWidth(lastline) / 2)); y1 = (int) (y + (height * lineNumber)); g.drawString(lastline, x1, y1); } } /* * ======== class RoundButton ======== * * Implements a rounded rectangle button * */ class RoundButton extends ButtonElement { // ht = height * 1.4 int ht; public RoundButton(Color bg, Color b, Color bh, Color bs, Color t, Color th, Color td, String l, String ld, URL url) { super(bg, b, bh, bs, t, th, td, l, ld, url); ht = (int) (height * 1.4); } // Check to see if the coordinates are inside the button public boolean inside(int x, int y) { if ((x >= this.x) && (x <= this.x + width) && (y >= this.y) && (y <= this.y + ht)) { return true; } return false; } // Draw the button in a 3D raised style protected void drawRaised(Graphics g) { // System.out.println("Drawing button raised: " + label); // Background of the button g.setColor(buttonShadow); g.fillRoundRect(x + 2, y + 2, width, ht, 8, 8); // Button itself if (state == Open) { g.setColor(buttonHighlight); } else { g.setColor(buttonColor); } g.fillRoundRect(x, y, width, ht, 8, 8); // Button Label g.setFont(buttonFont); g.setColor(textColor); g.drawString(label, x, height); } protected void drawBlankDescription(Graphics g) { // Draw Blanked description g.setFont(textFont); g.setColor(bgColor); drawLabelDescription(g); } protected void drawLitDescription(Graphics g) { // Draw Blanked description g.setFont(textFont); g.setColor(textDescription); drawLabelDescription(g); } // Draw the button depressed protected void drawDepressed(Graphics g) { System.out.println("Drawing button depressed: " + label); // Erase old button g.setColor(bgColor); g.fillRoundRect(x, y, width, (int) (height * 1.4), 8, 8); // Background of the button g.setColor(buttonHighlight); g.fillRoundRect(x + 2, y + 2, width, (int) (height * 1.4), 8, 8); // Button Label g.setFont(buttonFont); g.setColor(textHighlight); g.drawString(label, x + 2, height + 2); } // Draw the button highlighted with corresponding text protected void drawHighlight(Graphics g) { System.out.println("Drawing button highlight: " + label); // Background of the button g.setColor(buttonShadow); g.fillRoundRect(x + 2, y + 2, width, (int) (height * 1.4), 8, 8); // Button itself g.setColor(buttonHighlight); g.fillRoundRect(x, y, width, (int) (height * 1.4), 8, 8); // Button Label g.setFont(buttonFont); g.setColor(textHighlight); g.drawString(label, x, height); // Draw description drawLitDescription(g); } protected void drawUnHighlight(Graphics g) { drawRaised(g); drawBlankDescription(g); } protected void drawOpen(Graphics g) { System.out.println("Drawing open..." + label); String old = labelDescription; drawUnHighlight(g); labelDescription = openLabel; drawLitDescription(g); labelDescription = old; } }