Ch04 code
From SCMAD Book
Contents |
Hello World (Listing 4.1)
package com.scmadkit.ch04; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class HelloWorldMIDlet extends MIDlet { protected void startApp() throws MIDletStateChangeException { System.out.println("Hello world!"); } protected void pauseApp() { System.out.println("Hold on for a second, world!"); } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { System.out.println("Good Bye world!"); } }
Alarm Clock
MainScreen (Listing 4.2)
package com.scmadkit.ch04; import javax.microedition.lcdui.*; /** * This is the main screen for the application. This screen simply provides some * information about the current status of the application. * * @author Sathya.Srinivasan */ public class MainScreen extends TextBox { /** Command to set the alarm */ public static final Command SET_ALARM_COMMAND = new Command("Set alarm", Command.SCREEN, 1); /** Command to stop the application */ public static final Command EXIT_COMMAND = new Command("Exit", Command.EXIT, 2); private static final String MESSAGE = "Set alarm by selecting the \"Set Alarm\" command."; /** * Creates a new instance of MainScreen. */ public MainScreen() { super("Main Screen", MESSAGE, 100, TextField.ANY); this.initialize(); } /** * Initializes this screen with necessary UI objects. */ private void initialize() { this.addCommand(SET_ALARM_COMMAND); this.addCommand(EXIT_COMMAND); } }
AlarmSetForm (Listing 4.3)
package com.scmadkit.ch04; import java.util.Calendar; import java.util.Date; import javax.microedition.lcdui.*; /** * This screen is used to take user input regarding the time when the alarm * should be set. * * @author Sathya.Srinivasan */ public class AlarmSetForm extends Form implements TimeDisplay { /** Command to set the time */ public static final Command OK_COMMAND = new Command("OK", Command.OK, 1); /** Command to go back to the main screen */ public static final Command CANCEL_COMMAND = new Command("Cancel", Command.CANCEL, 2); private DateField dateField = new DateField("Date", DateField.TIME); private StringItem stringItem = new StringItem("Current Time", new Date() .toString()); /** * Creates a new instance of AlarmSetForm. */ public AlarmSetForm() { super("Alarm Details"); this.initialize(); } /** * Initializes this screen with necessary UI objects. */ private void initialize() { // Add UI Objects. this.append(dateField); this.append(stringItem); // Add Commands to this screen. this.addCommand(OK_COMMAND); this.addCommand(CANCEL_COMMAND); } /** * Gets the time that was set by the user. */ public Date getAlarmTime() { // Get the time set by the user. Date dateValue = dateField.getDate(); // If no time has been set by user, return null. if (dateValue == null) { return null; } Calendar cal = Calendar.getInstance(); Calendar ref = Calendar.getInstance(); cal.setTime(dateValue); cal.set(Calendar.MONTH, ref.get(Calendar.MONTH)); cal.set(Calendar.YEAR, ref.get(Calendar.YEAR)); cal.set(Calendar.DAY_OF_MONTH, ref.get(Calendar.DAY_OF_MONTH)); return cal.getTime(); } /** * Sets the current time. * * @param time The current time. */ public void setTime(Date time) { stringItem.setText(time.toString()); } }
AlarmForm (Listing 4.4)
package com.scmadkit.ch04; import javax.microedition.lcdui.*; /** * This screen is displayed when the alarm occurs. * * @author Sathya.Srinivasan */ public class AlarmForm extends TextBox { public static final Command OK_COMMAND = new Command("Dismiss", Command.OK, 1); /** Creates a new instance of AlarmForm */ public AlarmForm() { super("ALARM!", "*BEEP* *BEEP* *BEEP*", 100, TextField.ANY); this.initialize(); } /** * Initializes the UI objects of this screen. */ private void initialize() { this.addCommand(OK_COMMAND); } }
AlarmTask (Listing 4.5)
package com.scmadkit.ch04; import java.util.TimerTask; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.midlet.MIDlet; /** * This is a customization of the TimerTask class to show the alarm screen at * the scheduled time. * * @author Sathya.Srinivasan */ public class AlarmTask extends TimerTask { private Displayable dis; private MIDlet midlet; /** * Sets the screen that should be displayed when the scheduled event occurs. * */ public void setDisplayable(MIDlet midlet, Displayable dis) { this.dis = dis; this.midlet = midlet; } public void run() { Display.getDisplay(midlet).setCurrent(dis); } }
ClockTask (Listing 4.6)
package com.scmadkit.ch04; import java.util.Date; import java.util.TimerTask; /** * This task acts like a clock. The task itself simply changes the * contents of the associated display with the current date. * * @author Sathya.Srinivasan */ public class ClockTask extends TimerTask { private TimeDisplay display; /** Creates a new instance of ClockTask */ public ClockTask(TimeDisplay display) { this.display = display; } public void run() { display.setTime(new Date()); } }
TimeDisplay (Listing 4.6)
package com.scmadkit.ch04; import java.util.Date; /** * Interface to an UI that sets the current time. * * @author Sathya.Srinivasan */ public interface TimeDisplay { /** * Sets the current time on the display. * * @param time The current time. */ public void setTime(Date time); }
The main application that calls the shots (Listing 4.7)
package com.scmadkit.ch04; import java.util.Date; import java.util.Timer; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * This is the main class that defines * the lifecycle of the application. * The AMS will call the startApp() method * when this application is launched. * * @author Sathya.Srinivasan */ public class AlarmMIDlet extends MIDlet implements CommandListener { private AlarmSetForm alarmSetForm = new AlarmSetForm(); private AlarmForm alarmForm = new AlarmForm(); private MainScreen mainForm = new MainScreen(); private Timer timer = new Timer(); private AlarmTask task; private ClockTask clock; private Timer clockTimer = new Timer(); /** * Starts this application. */ public void startApp() { // Adds this class as a listener to commands in various screens. mainForm.setCommandListener(this); alarmSetForm.setCommandListener(this); alarmForm.setCommandListener(this); // Sets the clock timer. clock = new ClockTask(alarmSetForm); clockTimer.scheduleAtFixedRate(clock, 1000, 1000); // Shows the main screen. Display.getDisplay(this).setCurrent(mainForm); } /** * Pauses this application. We don't have to do much here for this * example. */ public void pauseApp() { } /** * This method is called when the application is destroyed by the * AMS. If a Timer has been set, we might want to cancel it before * closing the app. */ public void destroyApp(boolean unconditional) { // Cancels any tasks in the timer. timer.cancel(); } /** * This method is invoked whenever a command is executed by the * user, as this class has registered itself as a listener to all * the screens. */ public void commandAction(Command c, Displayable d) { if(c == AlarmSetForm.OK_COMMAND) { mainForm.setString("Alarm has been set for " + alarmSetForm.getAlarmTime()); // The user wants to set the timer. // Cancels the currently running task, if any. if(task != null) { task.cancel(); timer.cancel(); } // Sets the timer task. task = new AlarmTask(); task.setDisplayable(this, alarmForm); timer.schedule(task, alarmSetForm.getAlarmTime()); // Goes back to the main screen. Display.getDisplay(this).setCurrent(mainForm); } else if (c == AlarmSetForm.CANCEL_COMMAND) { Display.getDisplay(this).setCurrent(mainForm); } else if (c == MainScreen.SET_ALARM_COMMAND) { Display.getDisplay(this).setCurrent(alarmSetForm); } else if (c == MainScreen.EXIT_COMMAND) { this.notifyDestroyed(); } else if (c == AlarmForm.OK_COMMAND) { StringBuffer msg = new StringBuffer(); msg.append("Last alarm occurred at "); msg.append(new Date(task.scheduledExecutionTime())); mainForm.setString(msg.toString()); Display.getDisplay(this).setCurrent(mainForm); } } }
