RSS Viewer Applet: Ready to Rumble (5/6) - exploring XML | WebReference

RSS Viewer Applet: Ready to Rumble (5/6) - exploring XML

RSS Viewer Applet: Ready to Rumble

Standard Applet Routines and Event Handling

Some more standard applet functions to follow:
  public void start() {
    titleFont = new Font(titleFontFamily, titleFontStyle, titleFontSize);
    titleLineBase = getLineBase(titleFont);
    titleLineHeight = getLineHeight(titleFont);
    itemFont = new Font(itemFontFamily, itemFontStyle, itemFontSize);
    itemLineBase = getLineBase(itemFont);
    itemLineHeight = getLineHeight(itemFont);
  }
  public void stop() {
    titleFont = null;
    itemFont = null;
  }
  public void destroy() {}

At applet start-up the necessary fonts get created, and the needed measurements for text height and baseline are calculated. The fonts get disposed of when the applet is stopped. Nothing to do on destroy(), we trust the browser to discard of everything set up in init().

  public String getAppletInfo() { return "RSS Viewer Applet, (c) Michael Claßen"; }
  public String[][] getParameterInfo() {
    String[][] pinfo = {
      {"src", "String", "URL of RSS file"},
      {"box.foreground", "int", "box foreground color"},
      {"box.background", "int", "box background color"},
      {"box.border", "int", "box border width"},
      {"box.scroll", "boolean", "box scrolling"},
      {"box.scroll.horizontal", "boolean", "horizontal/vertical scrolling"},
      {"box.scroll.reverse", "boolean", "forward/backward scrolling"},
      {"title.foreground", "int", "title foreground color"},
      {"title.background", "int", "title background color"},
      {"title.foreground.mouseover", "int", "title foreground color on mouse-over"},
      {"title.background.mouseover", "int", "title background color on mouse-over"},
      {"title.font.family", "String", "title font family"},
      {"title.font.size", "int", "title font size"},
      {"title.font.style", "String", "title font style"},
      {"item.foreground", "int", "item foreground color"},
      {"item.background", "int", "item background color"},
      {"item.foreground.mouseover", "int", "item foreground color on mouse-over"},
      {"item.background.mouseover", "int", "item background color on mouse-over"},
      {"item.font.family", "String", "item font family"},
      {"item.font.size", "int", "item font size"},
      {"item.font.style", "String", "item font style"},
      {"item.margin", "int", "item margin"},
      {"item.separator", "String", "item separator"},
    };
    return pinfo;
  }
The mouse event handling takes care of correct highlighting of items and browser redirection on mouse click: redirects the browser to the URL of the item under the cursor on a mouse click, more specific the mouseUp event. Moving the mouse causes the new and the current item to be redrawn to change the highlighting, and leaving the applet unselects all items:
  public boolean mouseUp(Event e, int x, int y) {
    int pos = getItemFromCoordinates(x, y);
    URL target = (pos < 0) ? channel.getChannelLink() : channel.getItemLink(pos);
    getAppletContext().showDocument(target);
    return true;
  }

When the mouse was clicked, more specifically when the mouse button comes back up, the selected item gets determined from the mouse coordinates. When the position is -1 the channel's title was clicked, otherwise it was the channel item at the designated position, so the browser gets redirected to the appropriate link.

  public boolean mouseMove(Event e, int x, int y) {
    int newItem = getItemFromCoordinates(x, y);
    if (currentItem != newItem) {
      Rectangle r = getItemBounds(currentItem);
      repaint(r.x, r.y, r.width, r.height);
      r = getItemBounds(newItem);
      repaint(r.x, r.y, r.width, r.height);
      currentItem = newItem;
    }
    return true;
  }

On moving the mouse between two items both regions of the old and the new current item need to be repainted so that the correct highlighting is shown.

  public boolean mouseExit(Event e, int x, int y) {
    Rectangle r = getItemBounds(currentItem);
    currentItem = -2;
    repaint(r.x, r.y, r.width, r.height);
    return true;
  }

When the mouse leaves the applet's surface no item is selected and the highlighting deactivated. Finally some helpers and the conclusion.

https://www.internet.com

Produced by Michael Claßen
All Rights Reserved. Legal Notices.

URL: https://www.webreference.com/xml/column9/2.html
Created: Mar. 12, 2000
Revised: Mar. 12, 2000