|
package com.robohobby.me;
import java.io.UnsupportedEncodingException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
@Ads
@author
public class RobotMIDlet extends MIDlet implements CommandListener {
public boolean midletPaused = false;
CallBacks callBacks = CallBacks.getInstance();
private Command exitCommand;
private Command writeToDiscCommand;
private Command infoItemCommand;
private Command itemCommand;
private Command rootsCommand;
private Command readRemoteCmdItemCommand;
private Form form;
private StringItem timeInfoStringItem;
private StringItem stateStringItem;
private StringItem rootsStringItem;
public RobotMIDlet() {
callBacks.robotMIDlet = this;
callBacks.discUtils = new DiscUtils();
}
<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 == readRemoteCmdItemCommand) {
readCommandFromDisc();
} else if (command == rootsCommand) {
this.getRoots();
} else if (command == writeToDiscCommand) {
writeToDisc();
}
}
}
@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[] { getRootsStringItem(), getTimeInfoStringItem(), getStateStringItem() });
form.addCommand(getExitCommand());
form.addCommand(getWriteToDiscCommand());
form.addCommand(getRootsCommand());
form.addCommand(getReadRemoteCmdItemCommand());
form.setCommandListener(this);
}
return form;
}
@return
public StringItem getTimeInfoStringItem() {
if (timeInfoStringItem == null) {
timeInfoStringItem = new StringItem("Time: ", "", Item.PLAIN);
}
return timeInfoStringItem;
}
@return
public Command getWriteToDiscCommand() {
if (writeToDiscCommand == null) {
writeToDiscCommand = new Command("Write to Disc", Command.ITEM, 0);
}
return writeToDiscCommand;
}
@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 StringItem getRootsStringItem() {
if (rootsStringItem == null) {
rootsStringItem = new StringItem("Roots: ", "");
}
return rootsStringItem;
}
@return
public Command getRootsCommand() {
if (rootsCommand == null) {
rootsCommand = new Command("Get Roots", Command.ITEM, 0);
}
return rootsCommand;
}
@return
public Command getReadRemoteCmdItemCommand() {
if (readRemoteCmdItemCommand == null) {
readRemoteCmdItemCommand = new Command("Item", Command.ITEM, 0);
}
return readRemoteCmdItemCommand;
}
private void readCommandFromDisc()
{
if ( callBacks.discUtils.isDiskCommand() )
{
String cmd = callBacks.discUtils.readDiskCommand();
this.stateStringItem.setText("cmd=" + cmd);
callBacks.discUtils.deleteDiskCommand();
}
}
private void getRoots()
{
callBacks.discUtils.getRoots();
}
@return
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public void writeToDisc()
{
Calendar c = Calendar.getInstance();
String nowStr = StrCalendar.calendarToStrFs( c );
System.out.println( "writeToDisc() nowStr="+nowStr );
this.timeInfoStringItem.setText(nowStr);
String testText = "testText";
byte[] outBuf;
try {
outBuf = testText.getBytes("UTF-8");
callBacks.discUtils.writeToDisk( nowStr+".txt", outBuf);
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
this.stateStringItem.setText( "Error e="+ex );
}
}
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
public void pauseApp() {
midletPaused = true;
}
@param
public void destroyApp(boolean unconditional) {
}
}
| |