|
package com.robohobby.me;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
@Ads
@author
public class RobotMIDlet extends MIDlet implements CommandListener {
public boolean midletPaused = false;
CallBacks callBacks = CallBacks.getInstance();
private Command exitCommand;
private Command startStopTimerCommand;
private Command itemCommand;
private Command infoItemCommand;
private Form form;
private StringItem timerInfoStringItem;
private StringItem stateStringItem;
public RobotMIDlet() {
callBacks.robotMIDlet = this;
}
<code></code>
private void initialize() {
}
public void startMIDlet() {
switchDisplayable(null, getForm());
}
public void resumeMIDlet() {
}
<code></code> <code></code>
@param
<code></code> <code></code>
@param
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
@param
@param
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == startStopTimerCommand) {
doStartStopTimer();
}
}
}
@return
public Command getExitCommand() {
if (exitCommand == null) {
exitCommand = new Command("Exit", Command.EXIT, 0);
}
return exitCommand;
}
@return
public Form getForm() {
if (form == null) {
form = new Form("www.RoboHobby.com", new Item[] { getTimerInfoStringItem(), getStateStringItem() });
form.addCommand(getExitCommand());
form.addCommand(getStartStopTimerCommand());
form.setCommandListener(this);
}
return form;
}
@return
public StringItem getTimerInfoStringItem() {
if (timerInfoStringItem == null) {
timerInfoStringItem = new StringItem("Timer: ", "", Item.PLAIN);
}
return timerInfoStringItem;
}
@return
public Command getStartStopTimerCommand() {
if (startStopTimerCommand == null) {
startStopTimerCommand = new Command("Start/Stop Timer", Command.ITEM, 0);
}
return startStopTimerCommand;
}
@return
public Command getItemCommand() {
if (itemCommand == null) {
itemCommand = new Command("Item", Command.ITEM, 0);
}
return itemCommand;
}
@return
public Command getInfoItemCommand() {
if (infoItemCommand == null) {
infoItemCommand = new Command("State: ", Command.ITEM, 0);
}
return infoItemCommand;
}
@return
public StringItem getStateStringItem() {
if (stateStringItem == null) {
stateStringItem = new StringItem("State: ", "");
}
return stateStringItem;
}
@return
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
boolean timerIsRunning = false;
java.util.Timer timer = null;
int timerDelay = 300;
int keyCounter = 0;
RobotTimerTask robotTimerTask = null;
public void doStartStopTimer()
{
System.out.println( "timerIsRunning="+timerIsRunning + " keyCounter="+keyCounter );
keyCounter++;
if ( timerIsRunning )
{
System.out.println( "Stopping Timer." );
try
{
this.stateStringItem.setText( "Stopped" );
robotTimerTask.doStop();
}
catch ( Exception e )
{
System.out.println( "Stopping Timer Error e=" + e );
}
timerIsRunning = false;
}
else
{
System.out.println( "Starting Timer:" );
try
{
this.stateStringItem.setText( "Started" );
robotTimerTask = new RobotTimerTask();
timer = null;
timer = new java.util.Timer();
timer.schedule( robotTimerTask, 0L, timerDelay );
}
catch ( Exception e )
{
System.out.println( "Starting Timer Error e=" + e );
}
timerIsRunning = true;
}
}
public void setTimerInfo( String timerInfo )
{
this.timerInfoStringItem.setText( timerInfo );
}
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
public void pauseApp() {
midletPaused = true;
}
@param
public void destroyApp(boolean unconditional) {
}
}
| |