/* This project creates an interactive Gauge - we will use methods to change the value of the gauge. A time will provide us with periodic interruptions */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.Timer; import java.util.TimerTask; public class NonIntGauge extends MIDlet implements CommandListener{ private Display display; // refer to a display private Form fmMain; // Form private Command cmExit; // exit command private Command cmStop; // stop the download command private Gauge gaProg; // Progress gauge private Timer tm; // Timer private DownloadTimer tt; // the task to run public NonIntGauge() { display = Display.getDisplay(this); // Create the gauge - exit - and stop commands gaProg = new Gauge("DownLoad Progress", false, 40, 1); cmExit = new Command("Exit", Command.EXIT, 1); cmStop = new Command("Stop", Command.STOP, 1); // Create form - add commands - listen for events fmMain = new Form("Interactive Gauge"); fmMain.addCommand(cmExit); fmMain.addCommand(cmStop); fmMain.append(gaProg); fmMain.setCommandListener(this); } // start app public void startApp() { display.setCurrent(fmMain); // Create a timer that fires off every 1 second tm = new Timer(); // the timer tt = new DownloadTimer(); // the run task tm.scheduleAtFixedRate(tt, 0, 1000); // in miliseconds } // pause app public void pauseApp() { } // destroy app public void destroyApp(boolean unconditional){ } public void commandAction(Command c, Displayable s){ if (c == cmExit){ destroyApp(false); notifyDestroyed(); } else if (c == cmStop){ // if stop command is pressed - cancel timer - remove stop command and add // Exit command to the form - create alaber and set it to Download cancel tm.cancel(); fmMain.removeCommand(cmStop); fmMain.addCommand(cmExit); gaProg.setLabel("Download Cancelled"); } } /* ***************************************************************** handle the timer task ****************************************************************/ private class DownloadTimer extends TimerTask { public final void run() { // is current value of the gauge equal to the max if (gaProg.getValue() - gaProg.getMaxValue() < 0) gaProg.setValue(gaProg.getValue() + 1) ; else { // Remove stop command and replace with exit fmMain.removeCommand(cmStop); fmMain.addCommand(cmExit); // Change gauge label gaProg.setLabel("DownLoad Complete!"); // stop the timer cancel(); } } } }