Geertjan is a DZone Zone Leader and has posted 466 posts at DZone. You can read more from them at their website. View Full User Profile

Rendering New Content Types in the Lobo Java Web Browser

02.07.2008
| 7613 views |
  • submit to reddit

Yesterday we saw how easy it is to get started extending Lobo's Java Web Browser. However, now that we've got the "hello world" scenario under our belt... let's get serious. Let's create a plugin that provides a new Lobo clientlet. A clientlet renders content in the Lobo browser. For example, HTML and Java are already rendered, to varying degrees of sophistication. How would one add a renderer for a new file type? Read on for a full explanation.

Start off by reading the Clientlets in Lobo page and you will have a lot of information, with some significant gaps. First of all, exactly as described in that document, I created a Clientlet for rendering my content:

public class TextClientlet implements Clientlet {

@Override
public void process(ClientletContext context) throws ClientletException {
try {
InputStream in = context.getResponse().getInputStream();
try {
String text = IORoutines.loadAsText(in, "ISO-8859-1");
JTextArea textArea = new JTextArea(text);
textArea.setEditable(false);
JScrollPane pane = new JScrollPane(textArea);
context.setResultingContent(pane);
} finally {
in.close();
}
} catch (IOException ioe) {
throw new ClientletException(ioe);
}
}
}

So, based on a context received from somewhere, a Swing component is created for rendering our content. Here it is very rudimentary, since it is just an example. But we would typically use the class above for parsing the received content and then deciding how to color it, for example. Here, no styling is done at all. The received content is simply put into a text area, which is put into a scrollpane, which in turn is passed to ClientletContext.setResultingContext , for display in the browser. Also note the org.lobobrowser.util.io.IORoutines class, which provides convenience methods for loading our content. If you want to use this class, you will need the lobo.jar on your classpath, in addition to the lobo-pub.jar, which was mentioned yesterday.

That's all well and good. But, how to pass content to the Clientlet? And how to determine which content to pass? Let's say, for the sake of argument, that we want to render Manifest files in the Lobo browser. By default, when you try to open a Manifest file, you will get this message:

So, we need to have Manifest files recognized and, when they are recognized, we want to have the Clientlet above render the Manifest file's content. Yesterday we looked at the NavigatorExtension class, which is the "main class" for a Lobo plugin, that is, its entry point. At that point, we only looked at the NavigatorExtension.windowOpening method. This time, we need to work with the NavigatorExtension.init method. This is where the Clientlet page is pretty scanty in details: "Currently only extensions (plugins) can set up clientlets in Lobo, by calling addClientletSelector when the extension is initiliazed."

So, after studying the related Javadoc (which is all pretty good, if you piece the bits together), I came up with this in my NavigatorExtension.init method:

private ClientletSelector selector;
private TextClientlet textClient;

@Override
public void init(final NavigatorExtensionContext ctx) {
selector = new ClientletSelector() {
public Clientlet select(ClientletRequest request, ClientletResponse response) {
if (response.matches("", new String[]{".mf"})){
textClient = new TextClientlet();
return textClient;
}
return null;
}
public Clientlet lastResortSelect(ClientletRequest arg0, ClientletResponse arg1) {
return null;
}
};
ctx.addClientletSelector(selector);
}

The most interesting line is line 8. Here we can EITHER specify a MIME type OR a (list of) file extension(s). So, in this case, I put a file extension there, that is, the file extension of Manifest files. By the way, I challenge you to find the above snippet anywhere online (or, anywhere else). It's the bit of magic that connects the Lobo plugin's main class to the clientlet that will render the Manifest file.

And that's all you need. Now you're good to go. Remembering the properties file discussed previously, compile the plugin, put the JAR in the distro's ext folder, and when you restart the browser, you will be able to open Manifest files in the browser:

Of course, instead of a Manifest file, you could have anything else. Maybe JavaFX, for example.

 

Published at DZone with permission of its author, Geertjan Wielenga.