package net.sf.nfp.mini.model;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import net.sf.log.mobile.Log;
import net.sf.mvc.mobile.Navigation;
import net.sf.mvc.mobile.command.ActionCommand;
import net.sf.nfp.mini.dao.LogicDAO;
import net.sf.nfp.mini.dao.ObservationDAO;
import net.sf.nfp.mini.dao.PeriodDAO;
import net.sf.nfp.mini.data.Observation;
import net.sf.nfp.mini.data.Period;
import net.sf.nfp.mini.main.NFPControler;
import net.sf.nfp.mini.misc.Utils;
import net.sf.nfp.mini.view.GraphView;
import net.sf.nfp.mini.view.KeyListener;
public class GraphModel extends CommonModel implements KeyListener {
// private static final int PREGNANCY_PROBABILITY_COLORS[] = { 0xffff00,
// 0xff00ff, 0x00ffff };
private static final int OVULATION_TEMP_LINE_COLOR = 0x800000;
private static final int OVULATION_COLOR = 0x0000ff;
private static final int PERIOD_COLOR = 0xff0000;
private static final int SHORTEST_PERIOD_COLOR = 0xffff00;
private static final int EARLIEST_HIGH_TEMP_COLOR = 0x00ff00;
private GraphView view;
private Period period;
private ActionCommand selectCommand = null;
public GraphModel(NFPControler controler) {
super(controler);
}
public void setView(Displayable v) {
super.setView(v);
this.view = (GraphView) v;
selectCommand = new ActionCommand("${select}", Command.SCREEN, 1) {
public Navigation execute(Displayable d) throws Exception {
GraphView view = (GraphView) d;
Observation observation = view.getSelectedObservation();
return new Navigation("input", period, observation);
}
};
view.addCommand(selectCommand);
view.addCommand(new ActionCommand("${enter.new}", Command.SCREEN, 2) {
public Navigation execute(Displayable d) throws Exception {
return new Navigation("input", period, new Observation());
}
});
view.addCommand(new ActionCommand("${history}", Command.SCREEN, 5) {
public Navigation execute(Displayable d) throws Exception {
return new Navigation("history", period);
}
});
view.addCommand(new ActionCommand("${delete}", Command.SCREEN, 3){
public Navigation execute(Displayable d) throws Exception {
Observation observation = view.getSelectedObservation();
if(observation != null) {
LogicDAO.removeObservation(controler, observation, period);
return new Navigation("graph", period, observation);
}else return null;
}
});
view.setKeyListener(this);
view.setExceptionListener(controler);
}
/**
* @param parameter Object[] = { Period periodToShow NULL, Observation selected NULL}
*/
public void load(Object parameter) throws Exception {
view.removeCommand(selectCommand);
if (parameter == null)
return;
Object[] params = (Object[]) parameter;
period = (Period) params[0];
if (period == null)
return;
final PeriodDAO periodDAO = controler.getPeriodDAO();
final ObservationDAO observationDAO = controler.getObservationDAO();
int days = Utils.getDaysBetween(period.getStart(), period.getEnd());
Vector ids = periodDAO.getObservationIds(period);
Vector observations = new Vector(days);
observations.setSize(days + 1);
controler.progressListner.setMaxValue(ids.size());
for (int i1 = 0; i1 < ids.size(); ++i1) {
int id = ((Integer) ids.elementAt(i1)).intValue();
Observation o1 = observationDAO.find(id);
int day = Utils.getDaysBetween(period.getStart(), o1.getDate());
observations.setElementAt(o1, day);
controler.progressListner.setValue(i1);
}
Log.log("observations: " + observations);
int selectedDay = 0;
Observation observation = (Observation) params[1];
if(observation != null) {
for(int i=0;i<observations.size();++i) {
Observation o = (Observation) observations.elementAt(i);
if(o == null)
continue;
if(observation.getId() == o.getId()) {
selectedDay = i;
break;
}
}
}
setAnalysisResults(observation);
view.setObservations(observations);
view.setTitle(period.toString());
view.setSelectedDay(selectedDay);
view.addCommand(selectCommand);
}
private void setAnalysisResults(Observation observation) {
view.additional.removeAllElements();
//TODO: setAnalysisResults
}
public void keyPressed(int gameAction) {
System.out.println("GraphModel.keyPressed()");
switch (gameAction) {
case Canvas.LEFT:
view.selectNextDot(Utils.DIRECTION_LEFT);
break;
case Canvas.RIGHT:
view.selectNextDot(Utils.DIRECTION_RIGHT);
break;
case Canvas.UP:
for(int i=0;i<5;++i)
view.selectNextDot(Utils.DIRECTION_LEFT);
break;
case Canvas.DOWN:
for(int i=0;i<5;++i)
view.selectNextDot(Utils.DIRECTION_RIGHT);
break;
}
}
}