this class is shown in Listing 1.
The AdvancedImageContentHandler
constructor uses the registry to set itself
as the listener for any responses that
might be required, and, therefore, it
implements the
invocationResponseNotify(Registry registry) method, which is
empty in this example. Then, it locates
the ContentHandlerServer using the static
registry method getServer(String classname). The handler is then notified that
all new requests for content handling
are to be done by this class using the
invocationRequestNotify(ContentHandler-Server handler) method. Finally, it sets up
the UI to display the image and the text
below it.
When a new request is initiated, the
invocationRequestNotify(ContentHandler-Server handler) method is called, as
shown in Listing 2.
We first check whether there is an
existing request, and we let the content
handler server finish that up. Next, if
there is not an existing request, we get
the details of the new request, and we
pass the new request on to the
display-Image() method with the details.
Although fairly straightforward (in the
example shown in Listing 3), the
display-Image() method does all the magic of displaying the image with the added text.
We opened up a connection to the
requested file (using the FileConnection
API), and if the image is found, we display it on the form with the added text.
We do some error checking to make sure
we can load the image, and if not, we
display a message accordingly.
On the caller’s side, it takes only a
three-step process to call this con-
tent handler, as shown in Listing 4
in the excerpt from the calling class
(CHAPIExample).
public AdvancedImageContentHandler() {
// notify the registry that this class is a listener
registry = Registry.getRegistry( this.getClass().getName());
registry.setListener(this);
// now, get the handler which was registered by making entries in the JAD
// file
try {
handler = Registry.getServer(CH_CLASSNAME);
} catch (ContentHandlerException che) {
System.err.println("Registration not done! Check JAD file");
}
JAVA IN ACTION
// this class is the handler for all new requests
handler.setListener(this);
// setup the ui
display = Display.getDisplay(this);
form = new Form("Advanced Image");
backCommand = new Command("Back", Command.BACK, 1);
form.setCommandListener(this);
imageItem = new ImageItem(null, null, Item.LAYOUT_CENTER, "--");
}
ABOUT US
LEARN MORE
•;Read the final release of the JSR-211 API
at the Java Community Process (JCP)
Website
•;Download Java ME
blog
See all listings as text