Better memory use. Reuse of screens.
package horaris;
import javax.microedition.lcdui.*;
import java.util.Hashtable;
import java.util.Vector;
import ogdl.*;
public class MainList implements CommandListener, Ekrano
{
private Graph list;
private List mylist;
private Command cmd_eliri;
private Command cmd_about;
private Main main;
private Hashtable table;
public MainList(Main _main)
{
main = _main;
loadList();
mylist = new List("Horaris", List.IMPLICIT);
/* Show the Titles (they're also in the hashtable) */
for (int i=0; i < list.size(); i++)
mylist.append(list.get(i).get(0).getName(), null);
cmd_eliri = new Command("Sortir", Command.BACK, 0);
cmd_about = new Command("Programa", Command.HELP, 0);
mylist.addCommand(cmd_eliri);
mylist.addCommand(cmd_about);
mylist.setCommandListener(this);
System.err.println("Setting screen");
}
public void show()
{
main.display.setCurrent(mylist);
}
private void loadList()
{
/* Open file */
String res = "/data/list.g";
list = new Graph("Root");
File2Graph.load(res, list, main);
table = new Hashtable();
/* Hashtable "Title" - "filename" */
for (int i=0; i < list.size(); i++)
table.put(list.get(i).get(0).getName(),
list.get(i).getName());
}
public void commandAction(Command c, Displayable d)
{
if (c == cmd_eliri)
{
System.out.println("Exit");
main.quit();
} else if (c == List.SELECT_COMMAND)
{
String dosiero;
dosiero = (String) table.get(mylist.getString(mylist.getSelectedIndex()));
System.err.println("Loading file " + dosiero);
loadHoraro(dosiero);
} else if (c == cmd_about)
{
Vector str = new Vector();
str.addElement("Programa \"Horaris\"");
str.addElement("Llicència GPL 1.2");
str.addElement("Autor: Lluís Batlle i Rossell");
ShowText st = new ShowText(str, this);
}
}
private void loadHoraro(String dosiero)
{
Graph horaro = new Graph("RootHoraro");;
File2Graph.load("/data/"+dosiero, horaro, main);
int speco = Horaroj.distingi(horaro);
/* if (speco == Horaroj.CXEESTA)
{
newCxeesta(horaro);
} else */ if (speco == Horaroj.TRANSPORTA);
{
try {
newTransporta(horaro);
} catch (Exception e)
{
System.err.println("No es pot carregar" +
" l'horari " + dosiero);
}
}
}
private void newTransporta(Graph g) throws Exception
{
HoraroTransporta horaro = new HoraroTransporta(g, this);
}
/*
private void newCxeesta(Graph g)
{
HoraroCxeesta horaro = new HoraroCxeesta(g, main);
}
*/
}