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

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

RSS Viewer Applet: Ready to Rumble

Helper Functions, Future Plans and Current Distribution

Finally some typographic routines for converting pixel coordinates to and from channel item positions, and calculating line height, string widths and positions. When strings don't fit in the remaining bounding box they get pruned with an ellipsis appended:
  private int getItemFromCoordinates(int x, int y) {
    return (y - (titleLineHeight-itemLineHeight) - boxBorder) / (itemLineHeight + itemMargin) - 1;

This function calculates the item position under the mouse coordinates, used by mouse move and click actions to determine the selected item.

  private Rectangle getItemBounds(int item) {
    Rectangle r = new Rectangle();
    r.x = boxBorder;
    r.y = (item < 0 ? 0 : titleLineHeight-itemLineHeight + boxBorder + (item+1) * (itemLineHeight + itemMargin));
    r.width = getSize().width;
    r.height = (item < 0 ? titleLineHeight : itemLineHeight);
    return r;

This is the opposite calculation of item position to mouse coordinates, the bounding box to be more precise.

  private int getLineHeight(Font f) {
    FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
    return fm.getHeight() + 2;
  private int getLineBase(Font f) {
    FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
    return fm.getAscent() + 1;

The above two functions calculate typographic line heights and base line pixels for correct text rendering.

  private String getPrunedString(String s, Font f, int width) {
    FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
    if (fm.stringWidth(s) < width) return s;
    final String ellipsis = "...";
    width -= fm.stringWidth(ellipsis);
    String str = s;
    while (fm.stringWidth(str) > width) {
      str = str.substring(0, str.length()-1);
    return str + ellipsis;

If a string does not fit in its allocated box, this function abbreviates it with a trailing ellipsis, taking out characters from the string's end until it fits in its allocated space.

  private int getStringStartPosX(String s, Font f, int width, String align) {
    FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
    int blankWidth = width - fm.stringWidth(s);
    if (blankWidth <= 0 ) return 0;
    if (align.equalsIgnoreCase("right")) {
      return blankWidth;
    else if (align.equalsIgnoreCase("center")) {
      return blankWidth / 2;
    else return 0;

Finally we need a function that determines the exact coordinates for a text string depending on its specified alignment. It calculates the blank space remaining and then returns the full offset on right alignment, and half of it when centered. This completes the RSS applet viewing code. The jar file has increased from 25 to 28 kilobytes in size, which should be acceptable for the much improved look and feel we got in return.

Future plans

The Aelfred XML parser weighs in with 19 of the overall 28 kilobytes in our jar archive. Making the parser work via the standard SAX interface would mean including another 10 or so kilobytes of official org.xml.* classes, with no immediate benefit. So before adding more features such as scrolling and fading we should have a look at a custom solution for parsing RSS that does not involve a full generic XML parser and save 15-20 kb in the process. Let's see if this is feasible without sacrificing interoperability, because a fully compliant XML parser can hardly become much smaller than Aelfred.


The applet is available for download in binary and source code form under the GNU Public License. As far as I can see Aelfred's license should be allowing this kind of bundling.

If you own a Web site and want to display RSS news, feel free to use the applet on your site free of charge, and without warranty of any kind. If you are an HTML tool vendor and want to include the applet in your toolbox, let me know.

The source code is also checked into public CVS at SourceForge, feel free to check out the Project at SourceForge. If you encounter problems drop me a line. I cannot guarantee timely support but I'll do my best to help you out. Happy news-serving and reading!

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

Created: Mar. 12, 2000
Revised: Mar. 12, 2000