/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package laskin;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author vesal
*/
public class Ynnaa extends MIDlet implements CommandListener, ItemStateListener {
private boolean midletPaused = false;
////GEN-BEGIN:|fields|0|
private Command exitCommand;
private Command laskeCommand;
private Command screenCommand;
private Form form;
private TextField lasku;
private TextField tulos;
////GEN-END:|fields|0|
/**
* The HelloMIDlet constructor.
*/
public Ynnaa() {
}
////GEN-BEGIN:|methods|0|
////GEN-END:|methods|0|
////GEN-BEGIN:|0-initialize|0|0-preInitialize
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the startMIDlet
method.
*/
private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
// write pre-initialize user code here
exitCommand = new Command("Exit", Command.EXIT, 2);//GEN-BEGIN:|0-initialize|1|0-postInitialize
laskeCommand = new Command("Laske", Command.OK, 0);
screenCommand = new Command("Screen", Command.SCREEN, 0);
lasku = new TextField("Lasku", "", 32, TextField.ANY);
lasku.setLayout(ImageItem.LAYOUT_LEFT);
lasku.setPreferredSize(12, -1);
tulos = new TextField("Tulos", "0", 32, TextField.ANY);
tulos.setLayout(ImageItem.LAYOUT_DEFAULT | Item.LAYOUT_2);
form = new Form("Welcome", new Item[] { lasku, tulos });
form.addCommand(exitCommand);
form.addCommand(laskeCommand);
form.setCommandListener(this);//GEN-END:|0-initialize|1|0-postInitialize
form.setItemStateListener(this);
}//GEN-BEGIN:|0-initialize|2|
////GEN-END:|0-initialize|2|
////GEN-BEGIN:|3-startMIDlet|0|3-preAction
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
// write pre-action user code here
switchDisplayable(null, form);//GEN-LINE:|3-startMIDlet|1|3-postAction
// write post-action user code here
}//GEN-BEGIN:|3-startMIDlet|2|
////GEN-END:|3-startMIDlet|2|
////GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
// write pre-action user code here
switchDisplayable(null, form);//GEN-LINE:|4-resumeMIDlet|1|4-postAction
// write post-action user code here
}//GEN-BEGIN:|4-resumeMIDlet|2|
////GEN-END:|4-resumeMIDlet|2|
////GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
/**
* Switches a current displayable in a display. The display
instance is taken from getDisplay
method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if null
, then nextDisplayable
is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
// write pre-switch user code here
Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}//GEN-END:|5-switchDisplayable|1|5-postSwitch
// write post-switch user code here
}//GEN-BEGIN:|5-switchDisplayable|2|
////GEN-END:|5-switchDisplayable|2|
////GEN-BEGIN:|7-commandAction|0|7-preCommandAction
/**
* Called by a system to indicated that a command has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
// write pre-action user code here
if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|19-preAction
if (command == exitCommand) {//GEN-END:|7-commandAction|1|19-preAction
// write pre-action user code here
exitMIDlet();//GEN-LINE:|7-commandAction|2|19-postAction
// write post-action user code here
} else if (command == laskeCommand) {//GEN-LINE:|7-commandAction|3|40-preAction
laske();//GEN-LINE:|7-commandAction|4|40-postAction
// write post-action user code here
}//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
}//GEN-END:|7-commandAction|5|7-postCommandAction
// write post-action user code here
}//GEN-BEGIN:|7-commandAction|6|
////GEN-END:|7-commandAction|6|
/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}
/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}
/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}
/**
* Mukaeltu erota-aliohjelma
* @param jono jono josta erotellaan seuraava pala, jono lyhenee
* @param merkki merkki jonka kohdalta katkaistaan
* @return seuraava jonon pala
*/
public static String erota(StringBuffer jono, char merkki) {
if ( jono == null ) return "";
int p;
String s = jono.toString();
p = s.indexOf(""+merkki);
String alku;
if ( p < 0 ) {
alku = jono.toString();
jono.delete(0,jono.length());
return alku;
}
alku = s.substring(0,p);
jono.delete(0,p+1);
return alku;
}
/**
* Laskun suorittaminen
*/
public void laske() {
StringBuffer jono = new StringBuffer(lasku.getString());
double summa = 0;
while ( jono.length() != 0 ) {
String pala = erota(jono,' ');
try {
summa +=Double.parseDouble(pala);
} catch (NumberFormatException e) {
}
}
tulos.setString(""+summa);
}
public void itemStateChanged(Item item) {
if ( item == lasku ) laske();
}
}