/*
* Natural Family Planning for J2ME
* Copyright (C) 2007 Krzysztof Rzymkowski <rzymek@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
package net.sf.nfp.mini.view;
import java.util.Date;
import java.util.Vector;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import net.sf.log.mobile.Log;
import net.sf.nfp.mini.data.Mucus;
import net.sf.nfp.mini.data.MucusRegistry;
import net.sf.nfp.mini.data.Observation;
import net.sf.nfp.mini.misc.Utils;
public class InputView extends Form {
private final static String DEFAULT_TEMP = "36"
//#ifndef midp1.0
+".6"
//#endif
;
private TextField temperatureField;
private ChoiceGroup mucusChoise;
private DateField dateField = new DateField("${date}", DateField.DATE_TIME);
private ChoiceGroup flags = new ChoiceGroup(null, ChoiceGroup.MULTIPLE, new String[] {
"${bleeding}",
"${disturbed}"
}, null);
private StringItem periodTitle = new StringItem("${period}", "");
public InputView() {
super("${observation}");
Log.log("InputView.InputView()");
//#ifdef midp1.0
temperatureField = new TextField("${temperature}", "", 5, TextField.NUMERIC);
mucusChoise = new ChoiceGroup("${mucus}", ChoiceGroup.EXCLUSIVE, new String[0], null);
//#else
temperatureField = new TextField("${temperature}", "", 5, TextField.DECIMAL);
mucusChoise = new ChoiceGroup("${mucus}", ChoiceGroup.POPUP, new String[0], null);
//#endif
refreshMucusList();
append(periodTitle);
append(temperatureField);
append(mucusChoise);
append(flags);
append(dateField);
append("${copyright}");
}
public void refreshMucusList() {
Log.log("InputView.refreshMucusList()");
Utils.deleteAll(mucusChoise);
Vector all = MucusRegistry.instance().getAll();
for(int i=0;i<all.size();++i) {
Mucus mucus = (Mucus) all.elementAt(i);
mucusChoise.append(mucus.toString(), MucusRegistry.instance().getImage(mucus));
}
}
public Observation getObservation() {
boolean[] selectedFlags = new boolean[2];
flags.getSelectedFlags(selectedFlags);
boolean bleeding = selectedFlags[0];
boolean disturbed = selectedFlags[1];
int temperature = Observation.NO_TEMPERATURE;
if (bleeding == false) {
String tempString = temperatureField.getString();
if ("".equals(tempString))
temperature = Observation.NO_TEMPERATURE;
else {
//#ifdef midp1.0
temperature = Integer.parseInt(tempString);
//#else
temperature = Utils.parseTemperature(tempString);
//#endif
}
}
Mucus mucus = (Mucus) MucusRegistry.instance().getAll().elementAt(mucusChoise.getSelectedIndex());
return new Observation(dateField.getDate(), temperature, mucus, bleeding, disturbed, "");
}
public void setObservation(Observation o) {
String temperature = DEFAULT_TEMP;
if(o.hasTemperature()) {
//#ifdef midp1.0
temperature = Integer.toString(o.getTemperature());
//#else
temperature = Utils.formatTemperature(o.getTemperature());
//#endif
}
temperatureField.setString(temperature);
dateField.setDate(o.getDate());
mucusChoise.setSelectedIndex(getMucusIndex(o.getMucus().getId()), true);
flags.setSelectedIndex(0, o.isBleeding());
flags.setSelectedIndex(1, o.isDisturbed());
}
private int getMucusIndex(int mucusId) {
Vector all = MucusRegistry.instance().getAll();
for (int i = 0; i < all.size(); i++) {
Mucus mucus = (Mucus) all.elementAt(i);
if(mucus.getId() == mucusId)
return i;
}
return -1;
}
public Date getDate() {
return dateField.getDate();
}
public void setHeader(String title) {
super.setTitle(title);
periodTitle.setText(title);
}
}