package net.sf.nfp.mini.main;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import net.sf.log.mobile.Log;
import net.sf.mvc.mobile.Controler;
import net.sf.mvc.mobile.DefaultProgressView;
import net.sf.mvc.mobile.LazyInit;
import net.sf.mvc.mobile.Page;
import net.sf.nfp.mini.dao.ObservationDAO;
import net.sf.nfp.mini.dao.PeriodDAO;
import net.sf.nfp.mini.model.ConfirmOverwriteModel;
import net.sf.nfp.mini.model.GraphModel;
import net.sf.nfp.mini.model.HistoryModel;
import net.sf.nfp.mini.model.InputModel;
import net.sf.nfp.mini.model.MucusEditorModel;
import net.sf.nfp.mini.model.MucusListModel;
import net.sf.nfp.mini.model.SaveObservationModel;
import net.sf.nfp.mini.view.ExceptionListener;
import net.sf.nfp.mini.view.GraphView;
import net.sf.nfp.mini.view.HistoryView;
import net.sf.nfp.mini.view.InputView;
import net.sf.nfp.mini.view.MucusEditorView;
public class NFPControler extends Controler implements ExceptionListener {
public final Command SHOW_LOG_CMD = new Command("Show log",Command.SCREEN,1000);
protected PeriodDAO periodDAO = null;
protected ObservationDAO observationDAO = null;
public NFPControler(MIDlet midlet) {
super(midlet);
DefaultProgressView progressView = (DefaultProgressView) progressListner.getDisplay();
progressView.setTitle("${please.wait}");
progressView.setLabelPrefix("${loaded}: ");
//#ifdef LOGGING
progressView.addCommand(SHOW_LOG_CMD);
//#endif
}
public void start() {
try {
Log.log("=========START 2=======");
show("input", null);
} catch (Exception e) {
show(e);
}
}
protected void createNavigation() {
pages.put("input", new LazyInit() {
public Page create() {
return new Page(new InputModel(NFPControler.this), new InputView());
}
});
pages.put("graph", new LazyInit() {
public Page create() {
return new Page(new GraphModel(NFPControler.this), new GraphView());
}
});
pages.put("history", new LazyInit() {
public Page create() {
return new Page(new HistoryModel(NFPControler.this), new HistoryView());
}
});
pages.put("confirm-overwrite", new LazyInit() {
public Page create() {
return new Page(new ConfirmOverwriteModel(), new Alert("${confirm}"));
}
});
pages.put("save-observation", new LazyInit() {
public Page create() {
return new Page(new SaveObservationModel(NFPControler.this), new Alert("${information}"));
}
});
pages.put("mucus-editor", new LazyInit() {
public Page create() {
return new Page(new MucusListModel(NFPControler.this),
new List("${mucusRegistry}", List.IMPLICIT, new String[0], null));
}
});
pages.put("edit-mucus", new LazyInit() {
public Page create() {
return new Page(new MucusEditorModel(), new MucusEditorView());
}
});
}
public PeriodDAO getPeriodDAO() {
return periodDAO == null ? (periodDAO = new PeriodDAO()) : periodDAO;
}
public ObservationDAO getObservationDAO() {
return observationDAO == null ? observationDAO = new ObservationDAO() : observationDAO;
}
protected void show(Object name, Object parameter) throws Exception {
//#ifdef LOGGING
Log.log("NFPControler.show("+name+","+parameter+")");
if (parameter instanceof Object[]) {
Object[] arr = (Object[]) parameter;
StringBuffer b = new StringBuffer("[");
for(int i=0;i<arr.length;++i)
b.append(arr[i]+",");
b.append("]");
Log.log(b);
}
//#endif
if("quit".equals(name))
midlet.notifyDestroyed();
else super.show(name,parameter);
}
public void exception(Exception ex) {
show(ex);
}
public void show(Exception ex) {
Log.log(ex);
super.show(ex);
}
protected void setView(Displayable display) {
display.addCommand(SHOW_LOG_CMD);
super.setView(display);
}
//#ifdef LOGGING
public void commandAction(Command c, Displayable d) {
if(c==SHOW_LOG_CMD)
Log.show(getMidlet());
else
super.commandAction(c,d);
}
//#endif
}