// 6/22/09 M. Brenner import javax.microedition.midlet.*; import javax.microedition.lcdui.*; ////////////////////////////////////////////////////////////////////////// // // // NOTE: To add a new field to Bongoform you should: // // // // 1) add a constant (and String definition) to the list of // // static Strings. // // // // 2) add a default value to defaults[][] if it makes sense // // to have a default value. // // // // 3) define a private refrence variable to refer to an element // // of the appropriate type (ChoiceGroup, Guage or TextField) // // // // 4) adjust the constructor to add the field to the Bongoform // // in the approprate position in the form. // // // // 5) adjust updateprefs() to make sure the value provided by // // the user gets added to the Preferences object // // // ////////////////////////////////////////////////////////////////////////// public class Convertprefs { // Field names (user sees these) static public final String RATE = "RATE", DTOE = "DTOE"; // Default values (user sees these) static public final String defaults_[][] = { {RATE, "150"}, {DTOE, "YES"} }; private ChoiceGroup dtoe_; private ConvertGui gui_; private Display display_; private Command cancel_, save_; private Form form_; private String yesnotext[] = {"Dollars to Euros", "Euros to Dollars"}; private TextField convrate_; public Convertprefs (ConvertGui gui) { int index; Preferences prefs; String value; gui_ = gui; form_ = new Form ("Convert Settings"); form_.append ("Convert \u00A9 2009\nCP-Workshop"); prefs = Preferences.getinstance(); value = prefs.getpreference (RATE); convrate_ = new TextField ("Conversion Rate", value, 15, TextField.DECIMAL); form_.append (convrate_); index = prefs.getpreference (DTOE).equals ("YES") ? 0 : 1; dtoe_ = new ChoiceGroup (get (DTOE), List.EXCLUSIVE, yesnotext, null); dtoe_.setSelectedIndex (index, true); form_.append (dtoe_); save_ = new Command ("Save", Command.OK, 0); form_.addCommand (save_); cancel_ = new Command ("Cancel", Command.CANCEL, 0); form_.addCommand (cancel_); form_.setCommandListener (new CommandListener() { public void commandAction (Command c, Displayable disp) { if (c == save_) { updateprefs(); } gui_.changedprefs(); } }); } private String get (String msg) { return get (msg, true); } private String get (String msg, boolean addcolon) { StringBuffer text; text = new StringBuffer (msg); if (addcolon) { text.append (':'); } return text.toString(); } public Form getform() { return form_; } public void initialize() { Preferences prefs; prefs = Preferences.getinstance(); convrate_.setString (prefs.getpreference (RATE)); } private void updateprefs() { Preferences prefs; String text; prefs = Preferences.getinstance(); prefs.setpreference (RATE, convrate_.getString()); text = dtoe_.getSelectedIndex() == 0 ? "YES" : "NO"; prefs.setpreference (DTOE, text); } }