import javax.microedition.lcdui.Command;
import java.util.Stack;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import ID3Editor.FileEditor; //import javax.microedition.lcdui.Display;
import displayables.DirectoryEditor;
import displayables.DirectoryList;
import displayables.ListRootFiles;
import displayables.VisorDeTexto;
public class Automata implements CommandListener {
int q = 0;
MIDlet m = null;
Command backCommand;
Command exitCommand;
Command editTagCommand;
Command saveCommand;
Stack lifo;
String currentPath = null;
public Automata(MIDlet m) {
lifo = new Stack();
exitCommand = new Command("Salir", Command.EXIT, 0);
backCommand = new Command("Volver", Command.BACK, 1);
editTagCommand = new Command("Editar", Command.OK, 2);
saveCommand = new Command("Guarda", Command.OK, 3);
this.m = m;
this.setStatus(0);
}
public void setStatus(int s) {
switch (s) {
case 0:
ListRootFiles first = new ListRootFiles(
"Seleccione un sistema de archivos");
Display.getDisplay(m).setCurrent(first);
first.setCommandListener(this);
first.addCommand(exitCommand);
currentPath = null;
break;
case 1:
List p = (List) Display.getDisplay(m).getCurrent();
int i = p.getSelectedIndex();
String path = p.getString(i);
if (!(path.endsWith(".mp3") || path.endsWith(".txt"))) {
DirectoryList fileList;
if (q != 0) {
String title = p.getTitle();
fileList = new DirectoryList(title + path);
currentPath = title + path;
} else {
fileList = new DirectoryList("file:///" + path);
currentPath = "file:///" + path;
}
Display.getDisplay(m).setCurrent(fileList);
fileList.setCommandListener(this);
fileList.addCommand(backCommand);
fileList.addCommand(editTagCommand);
break;
}
case 2:
List l = (List) Display.getDisplay(m).getCurrent();
int j = l.getSelectedIndex();
String file_or_path = l.getTitle() + l.getString(j);
Displayable f = null;
currentPath = file_or_path;
if (file_or_path.endsWith("/")) {
f = new DirectoryEditor(file_or_path);
f.addCommand(saveCommand);
} else if (file_or_path.endsWith(".mp3")) {
f = new FileEditor(file_or_path);
f.addCommand(saveCommand);
} else if (file_or_path.endsWith(".txt")) {
f = new VisorDeTexto(file_or_path);
}
Display.getDisplay(m).setCurrent(f);
f.setCommandListener(this);
f.addCommand(backCommand);
}
q = s;
}
public void commandAction(Command c, Displayable d) {
if (c == saveCommand) {
if (d instanceof DirectoryEditor) {
DirectoryEditor fe_d = (DirectoryEditor)d;
fe_d.saveTag();
} else {
FileEditor fe_d = (FileEditor) d;
fe_d.saveTag();
}
c = backCommand;
q = 2;
}
if (c == backCommand) {
if (q == 2) {
q--;
}
Displayable d2 = (Displayable) lifo.pop();
if (lifo.isEmpty())
q = 0;
Display.getDisplay(m).setCurrent(d2);
}
if (c == editTagCommand) {
lifo.push(d);
setStatus(2);
}
if (c == exitCommand) {
m.notifyDestroyed();
}
if (q == 0 && c == List.SELECT_COMMAND) {
lifo.push(d);
setStatus(1);
} else if (q == 1 && c == List.SELECT_COMMAND) {
lifo.push(d);
setStatus(1);
}
}
}