ZK AJAX Status Bar

Seeing that my research project is a mini accounting system, I thought it necessary to be able to display items in a status bar, like a real application. It is very slightly unfortunate that ZK doesn’t have something integrated right in for this purpose. But, given the versatility of ZK, it’s easy enough to resolve.

Features

I thought it important to have some useful features that make sense for what I’ve seen in status bars.

a method to set the status, with a delay before the status bar is cleared

a method to simply set the status, and leave it as is. It would be up to the developer to clear the status later, if need be. Or, the next status item would take over.

a very simple interface abstraction in case we want to change the type of component we use later, or something of that nature. This will help us prevent the need to refactor a lot of code that could be using the status bar; we would only need to refactor the implementing class of the interface, leaving all of the code accessing the status bar entirely intact.

Future features might include…

methods for setting the status with color text

methods for setting the status with html

any other suggestions are welcome.

Window Width Status Bar

We first, of course, need to setup the actual status bar. So, right before the end of the window, we do just that, with a “textbox”. Now, there’s no reason you can’t use some other type of control, if it makes sense to.

The text box is implemented by a custom class, which we’ll delve into in a moment. We also have a ZK timer, for implementing the automatic clearing feature, because it is illegal to access ZK elements from outside of a ZK event. i.e. You cannot write your own thread to do it at a later time, as ZK will throw an IllegalStateException.

The Code

Interface Abstraction

We’ve kept the interface very simple, just two methods. For more information, read the javadoc comments.

setStatus(String) allows setting of the status bar permanently

setStatus(String, int) allows setting of the status bar, and automatic clearing of it after a specified delay

packagecom.example.system;/**
* Handles setting of status bar messages. All calls to this object must be
* done from within the ZK framework, such as inside an event.
*
* Hides the details of what component is actually a status bar. It could be a
* textbox, or something else, but we don't want to be dependant on any specific
* type of control, in case it changes in the future.
*
* Created : Jan 31, 2010 1:39:37 AM MST
*
* Modified : $Date$ UTC
*
* Revision : $Revision$
*
* @author Trenton D. Adams
*/publicinterface IStatusBar
{/**
* Sets the status bar text for the time period indicated.
*
* @param statusText status text
* @param timePeriod delay in seconds, before the status bar will be
* cleared.
*/publicvoid setStatus(finalString statusText, finalint timePeriod);/**
* Sets the status bar text. Pass null to clear.
*
* @param statusText status text or null to clear
*/publicvoid setStatus(finalString statusText);/**
* @return the status text of the status bar.
*/publicString getStatus();}

Using the Status Bar

The following code must be used inside of a ZK event handler, such as one that comes from the click of a button, or a mouse over event, etc. Basically, all we’re doing is grabbing the status bar object from the session, and request the status be set. That’s all there is to it.