// In this case, everything we need is in the javax.tv.xlet package. For more complex
// examples we would also import classes from org.dvb.* and the other MHP or Java APIs.
import javax.tv.xlet.*;
// The main class of every Xlet must implement this interface - if it doesn't do this,
// the MHP middleware can't run it.
public class FirstXletExample implements javax.tv.xlet.Xlet
{
// Every Xlet has an Xlet context, just like the Applet context that applets in a
// web page are given. This is created by the MHP middleware and passed in to the
// Xlet as a parameter to the initXlet() method.
private javax.tv.xlet.XletContext context;
// A private field to hold the current state. This is needed because the startXlet()
// method is called both to start the Xlet for the first time and also to make the
// Xlet resume from the paused state. This filed lets us keep track of whether we're
// starting for the first time.
private boolean hasBeenStarted;
/**
* Every Xlet should have a default constructor that takes no arguments.
* No other constructor will get called.
*/
public FirstXletExample()
{
// The constructor should contain nothing. Any initialisation
// should be done in the initXlet() method, or in the startXlet method
// if it's time- or resource-intensive. That way, the MHP middleware
// can control when the initialisation happens in a much more predictable
// way
}
/**
* Initialise the Xlet. The context for this Xlet will get passed in to this
* method, and a reference to it should be stored in case it's needed later.
* This is the place where any initialisation should be done, unless it takes
* a lot of time or resources. If something goes wrong, then an
* XletStateChangeException should get thrown to let the runtime system know
* that the Xlet can't be initialised.
*/
public void initXlet(javax.tv.xlet.XletContext context) throws javax.tv.xlet.XletStateChangeException
{
// store a reference to the Xlet context that the Xlet is executing in
this.context = context;
// The Xlet has not yet been started for the first time, so set
// this variable to false.
hasBeenStarted = false;
// Since this is a simple Xlet, we'll just print a message to the debug output
System.out.println("The initXlet() method has been called. Our Xlet context is " + context);
}
/**
* Start the Xlet. At this point the Xlet can display itself on the screen and
* start interacting with the user, or do any resource-intensive tasks. These
* kinds of function should be kept in startXlet(), and should *not* be done
* in initXlet().
*
* As with initXlet(), if there is any problem this method should throw an
* XletStateChangeException to tell the runtime system that it can't start.
*
* One of the common pitfalls is that the startXlet() method must return to its
* caller. This means that the main functions of the Xlet should be done in
* another thread. The startXlet() method should really just create that thread
* and start it, then return.
*/
public void startXlet() throws javax.tv.xlet.XletStateChangeException
{
// Again, we print a message on the debug output to tell the user that
// something is happening. In this case, what we print depends on
// whether the Xlet is starting for the first time, or whether it's
// been paused and is resuming
// have we been started?
if (hasBeenStarted) {
System.out.println("The startXlet() method has been called to resume the Xlet after it's been paused. Hello again, world!");
}
else {
System.out.println("The startXlet() method has been called to start the Xlet for the first time. Hello, world!");
// set the variable that tells us we have actually been started
hasBeenStarted = true;
}
}
/**
* Pause the Xlet. Unfortunately, it's not clear to anyone (including the
* folks who wrote the MHP specification) what this means. Generally, it
* means that the Xlet should free any scarce resources that it's using,
* stop any unnecessary threads and remove itself from the screen.
*
* Unlike the other methods, pauseXlet() can't throw an exception to indicate
* a problem with changing state. When the Xlet is told to pause itself, it
* must do that.
*/
public void pauseXlet()
{
// Since we have nothing to pause, we will tell the user that we are
// pausing by printing a message on the debug output.
System.out.println("The pauseXlet() method has been called. Bedtime...");
}
/**
* Stop the Xlet. The boolean parameter tells the method whether the Xlet has to
* obey this request. If the value of the parameter is true, the Xlet must terminate
* and the runtime system will assume that when the method returns, the Xlet has
* terminated. If the value f the parameter is false, the Xlet can request that it
* not be killed, by throwing an XletStateChangeException. if the MHP middleware
* still wants to kill the Xlet, it shoudl call destroyXlet() again with the
* parameter set to true.
*/
public void destroyXlet(boolean unconditional) throws javax.tv.xlet.XletStateChangeException
{
if (unconditional) {
// We have been ordered to terminate, so we obey the order politely and release any
//scarce resources that we are holding.
System.out.println("The destroyXlet() method has been called telling the Xlet to stop unconditionally. Goodbye, cruel world!");
}
else {
// We have had a polite request to die, so we can refuse this request if we want.
System.out.println("The destroyXlet() method has been called requesting that the application stops, but giving it the choice. So, I'll decide not to stop.");
// Throwing an XletStateChangeException tells the MHP middleware that the
// application would like to keep running if it's allowed to.
throw new XletStateChangeException("Please don't kill me!");
}
}
}
|