package converter;
import javax.microedition.lcdui.*;
public class Converter extends Form {
static Converter instance = null;
TextField frTextField = new TextField("", "", 1, 1);
StringItem toStringItem = new StringItem("", "");
Settings settings = null;
/**Construct the displayable*/
public Converter(int initFr, int initTo) {
super("Converter");
// set up this Displayable to listen to command events
setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
this_commandPerformed(c, d);
}
});
setItemStateListener(new ItemStateListener() {
public void itemStateChanged(Item item) {
int fridx = settings.frList.getSelectedIndex();
int toidx = settings.toList.getSelectedIndex();
convert(fridx, toidx);
}
});
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
instance = this;
settings = new Settings(initFr, initTo);
changeFrom(initFr);
changeTo(initTo);
}
/**Component initialization*/
private void jbInit() throws Exception {
toStringItem.setLabel("(to)");
frTextField.setLabel("(from)");
frTextField.setConstraints(TextField.NUMERIC);
frTextField.setMaxSize(10);
// add the Exit command
addCommand(new Command("Exit", Command.EXIT, 1));
addCommand(new Command("Settings", Command.SCREEN, 1));
this.append(frTextField);
this.append(toStringItem);
}
/**Handle command events*/
public void this_commandPerformed(Command c, Displayable d) {
if (c.getCommandType() == Command.EXIT) {
// exit the MIDlet
ConverterMIDlet.quitApp();
} else {
ConverterMIDlet.setScreen(settings);
}
}
public static void goBack(int fridx, int toidx) {
instance.convert(fridx, toidx);
ConverterMIDlet.setScreen(instance);
}
void convert(int fridx, int toidx) {
String frvstr = frTextField.getString();
int toval = 0;
if (frvstr.length() > 0) {
toval = Currencies.convert(
Integer.parseInt(frvstr), fridx, toidx);
}
toStringItem.setText(String.valueOf(toval));
}
static void changeFrom(int newidx) {
instance.frTextField.setLabel(Currencies.currencies[newidx]);
}
static void changeTo(int newidx) {
instance.toStringItem.setLabel(Currencies.currencies[newidx]);
}
}
|