import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import java.lang.System; import java.util.Timer; import java.util.TimerTask; public class MeetingTimerMID extends MIDlet implements CommandListener { // Forms & fields private Form mMainForm; // Main form for the screen private Form mSettingsForm; // Enter salary & attendee values private Form mHelpForm; // Help screen private TextField mSalary; // Avg salary field in mSettingsForm private TextField mAttendees; // Num of atendees field in mSettingsForm private Command mStartCmd; // "Start Timer" command private Command mStopCmd; // "Stop Timer" command private StringItem mUpdateItem; // The item in the main form to display // MeetingTimer object to calculate the cost of the meeting private MyMeetingTimer mMeetingTimer = null; public MeetingTimerMID() { // Default settings - 5 employees at $50K long salary = 50000; long attendees = 5; // Create a main form and add a welcome string mMainForm = new Form("Meeting Timer"); mMainForm.append(new StringItem(null, "Total cost of this meeting:\n")); // Create a string item to be overwritten by the display refresh mUpdateItem = new StringItem(null, "-Timer Off-"); mMainForm.append(mUpdateItem); // Add commands and command listener mMainForm.addCommand(new Command("Exit", Command.EXIT, 0)); mStartCmd = new Command("Start Timer", Command.SCREEN, 1); // Toggle Start/Stop menu mStopCmd = new Command("Stop Timer", Command.SCREEN, 1); mMainForm.addCommand(mStartCmd); mMainForm.addCommand(new Command("Settings", Command.SCREEN, 2)); mMainForm.addCommand(new Command("Reset", Command.SCREEN, 3)); mMainForm.addCommand(new Command("Help", Command.SCREEN, 4)); mMainForm.setCommandListener(this); // Now, create a form to enter the data for the meeting mSettingsForm = new Form("Enter Settings"); mAttendees = new TextField("People attending:", String.valueOf(attendees), 5, TextField.NUMERIC); mSalary = new TextField("(Estimated) Avg Salary ($/yr):", String.valueOf(salary), 6, TextField.NUMERIC); mSettingsForm.append(mAttendees); mSettingsForm.append(mSalary); mSettingsForm.addCommand(new Command("Back", Command.BACK, 0)); mSettingsForm.addCommand(new Command("Help", Command.SCREEN, 1)); mSettingsForm.setCommandListener(this); // Finally, create the help screen mHelpForm = new Form("Help"); mHelpForm.append(new StringItem(null, "- Use the \"SETTINGS\" screen to enter the number of people attending your meeting and their average salary.\n")); mHelpForm.append(new StringItem(null, "- Use the \"START TIMER\" or \"STOP TIMER\" buttons to count down the meeting cost.\n")); mHelpForm.append(new StringItem(null, "- Use \"RESET\" to zero out the meeting cost.\n")); mHelpForm.append(new StringItem(null, "- Visit www.theHaws.org for more stuff!\n")); mHelpForm.addCommand(new Command("Back", Command.BACK, 0)); mHelpForm.setCommandListener(this); // Set up and start the timer to refresh the display. mMeetingTimer = new MyMeetingTimer(); // Modify Meeting Timer values to match fields on Settings form mMeetingTimer.setSalary(salary); mMeetingTimer.setAttendees(attendees); } public void startApp() { Display.getDisplay(this).setCurrent(mMainForm); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) { if (c.getLabel().equals("Exit") ) { notifyDestroyed(); } else if (c.getLabel().equals("Settings") ) { // Go to Settings form from Main form Display.getDisplay(this).setCurrent(mSettingsForm); } else if (c.getLabel().equals("Back") ) { // Modify Meeting Timer values to match fields on Settings form mMeetingTimer.setSalary((long)Integer.parseInt(mSalary.getString())); mMeetingTimer.setAttendees((long)Integer.parseInt(mAttendees.getString())); // Go to Main form, returning from Settings form Display.getDisplay(this).setCurrent(mMainForm); } else if (c.getLabel().equals("Start Timer") ) { mMeetingTimer.startTimer(); mMainForm.removeCommand(mStartCmd); mMainForm.addCommand(mStopCmd); } else if (c.getLabel().equals("Stop Timer") ) { mMeetingTimer.stopTimer(); mMainForm.addCommand(mStartCmd); mMainForm.removeCommand(mStopCmd); } else if (c.getLabel().equals("Reset") ) { mMeetingTimer.reset(); // Reset count totals } else if (c.getLabel().equals("Help") ) { // Go to Help screen Display.getDisplay(this).setCurrent(mHelpForm); } } private class MyMeetingTimer extends MeetingTimer { // The method to repaint the screen public void paint() { // Now, update the timer on the main form mUpdateItem.setText(" Cost: "+getCost()+"\n Hours: "+getHours()); } } }