What you get
The tea timer is my "Hello World!" for
JavaTM Web Start (JWS). It is
thought as a simple test for use of the
JNLP
API. If you are starting with JWS, too, you may want to have a look
at the sources.
Download
Here are the complete sources of the
Tea Timer. You may use them anyway you like. The JNLP file is not
included, because you will need a different one for your site (or
a web server on your local host, as I would recommend). If you need an
example for a JNLP file, just do Save As on
this link.
How to program for Java Web Start
The application is designed to work in a JWS environment as well
as standalone.
All classes are in the de/caff/timer directory.
Most of them
don't know whether they are running standalone or under JWS.
The decision is made at the beginning of the main method
in the de.caff.timer.TeaTimer class.
If we are running under JWS, a
de.caff.timer.JnlpPersistentDataRetriever is created to
handle loading and saving of the configuration data. There you will find
code how to use one of the JNLP services. Note how
getCodeBase() form the
BasicService is used to make the application
relocatable.
Last not least direct access to resources (e.g. loading from disk) is
not possible. Everything has to be read from jar files. See
de.caff.timer.ShortTermTimerConfigurationWindow if you need
an example how to retrieve the images shown on the up and down buttons
(and stored in the images directory which is packed into the jar,
too). Of course this works for standalone applications as well.
Possible Enhancements
As with all programs, there are a lot of possible enhancements.
Here are some ideas which you may like to add:
-
Allow the user to add images and/or sounds to the timers using a
javax.jnlp.FileOpenService. Because you should only
read them once (without the user asking every time he starts a timer
to open the required files) and they can get quite big, you may not
want to save them via PersistenceService but instead
upload them to the server and create some kind of extension which can
be downloaded via DownloadService.
-
Start with a platform dependent look&feel instead of the default
Metal. This is so simple you should make that configurable by
the user to have some more fun.
-
Allow for different timer views like a tradional short term timer
(the ones you twist to run and which come in a lot of forms: eggs,
hens etc.). A simple clock display should do for the beginning.
A possibly helpful hint for support of the JNLP MIME type
If you've read the documentation on JWS/JNLP carefully you know that your
web server has to return a special MIME type for JNLP files:
application/x-java-jnlp-file.
It's not always easy to convince your web space provider of changing his
configuration. So if your provider runs the free Apache Web Server as
many does and hasn't switched off support for the .htaccess
feature (which is usually true) there's an easier way:
Just add these two lines to the .htaccess file at the top
directory of your site (and just create one, if there is none):
AddType application/x-java-jnlp-file .jnlp
AddType application/x-java-archive-diff .jardiff
Now try to load a JNLP file from your site. If you still see just the
content of the file instead starting JWS be sure that you shift-click
the Reload button to really circumvent any caches involved (this
is at least how it works for Netscape, I don't know much of MSIE).
A note on Internationalization
If you already looked at the sources you may have wondered about the
funny comments which follow many of the String constants. That is another
test I made, because I tried the
JavaTM
Internationalization and Localization Toolkit 2.0
to convert the Tea Timer from plain English to an internationalized
version (an euphemism because only English and German are supported
at the moment because that's all I dare -- if you like, send me
a TeaTimerRes_your-iso-language-shortcut-goes-here.java for
your language).
This toolkit allows for a relative simple and straightforward way
of converting from a one-language-only program to an internationalized
one.
It converts the code (mainly the string constants) and inserts the special
comments mentioned above. After it's work is done it's easy enough to
add support for further languages.