RSS Viewer Applet: Window to the World of News (4/4) - exploring XML | WebReference

RSS Viewer Applet: Window to the World of News (4/4) - exploring XML

RSS Viewer Applet: Window to the World of News

RSSViewerApplet class

This class is responsible for initiating the RSS fetching and parsing process, and displaying the channel with its items afterwards:

package com.exploringxml.rss.applet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;
import com.exploringxml.rss.RSSChannel;
public class RSSViewerApplet extends Applet {
  String srcRSS;
  RSSChannel channel;
  public String getParameter(String key, String def) {
    return (getParameter(key) != null ? getParameter(key) : def);
  }
  public void init() {
    try {
      srcRSS = getParameter("src", "http://localhost/xml/index.rss");
      channel = new RSSChannel();
      channel.load(srcRSS);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

The applet's init() method gets the URL for the RSS file to parse and tells the channel to load itself, which in turn starts the XML parser.

  public void paint(Graphics g) {
    g.drawString(channel.getChannelTitle(), 0, LineHeight);
    for (int i=0; i < channel.getNumberOfItems(); i++) {
      g.drawString(channel.getItemTitle(i), 0, LineHeight * (i+2));
    }
  }
  public void start() {}
  public void stop() {}
  public void destroy() {}
  public String getAppletInfo() { return "RSS Viewer Applet"; }
  public String[][] getParameterInfo() {
    String[][] pinfo = { {"src", "String", "URL of RSS file"}, };
    return pinfo;
  }

The paint() method gets called by the applet framework whenever there is a need to update the part of the screen that is owned by the applet. All we currently do is to draw the channel title and the list of item titles onto the canvas. Some more standard applet methods follow.

  public boolean mouseUp(Event e, int x, int y) {
    int pos = y / LineHeight - 1;
    URL target = (pos 

When we get notified of a mouse click we calculate the item position from the mouse coordinates; a value of -1 denotes the channel title. As a result we pick the correct target URL and send the browser there. Et voilà! While this solution is neither pretty nor ergonomic (who would guess to click on the text?) it represents the minimal foundation on which we will develop the full-blown luxury RSS applet over the next installments.

The current feature wish list:

  1. More sophisticated rendering of user interface
  2. Full customization of UI styles
  3. Mouse-over highlighting of text
  4. Line breaks and scrollbars for maximum screen real estate usage
  5. Automatic scrolling, both vertically and horizontally
  6. Your suggestion?
I would like to involve the customer (you) with this field prototype, so please try it out and participate actively in the design. I look forward to receiving your input!

PS: In case you wonder why I use Java 1.0 event handling: Java 1.1 event listeners are not supported in Netscape Navigator 4 for Mac. If you experience more problems let me know.

PPS: No need to ask me for class file jars and source code to download right now. I will release the code under GPL with column9, after some field tests and with some of the improvements from above implemented. Please be patient so we can all enjoy a product of reasonable quality. Thanks.

http://www.internet.com

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

URL: http://www.webreference.com/xml/column7/2.html
Created: Feb. 08, 2000
Revised: Feb. 08, 2000