package displayables;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
public class DirectoryList extends List{
Image folderImage = null ;
Image mp3Image = null;
Image txtImage = null;
public DirectoryList(String title) {
super(title, List.IMPLICIT);
try {
folderImage = Image.createImage("/folder.png");
txtImage = Image.createImage("/format-justify-center.png");
mp3Image = Image.createImage("/format-justify-center.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.getFileList(title);
}
protected void getFileList(String path) {
try {
//Opens a file connection in READ mode
FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
Enumeration filelist = fc.list("*", true); //also hidden files are shown
String filename;
while(filelist.hasMoreElements()) {
filename = (String) filelist.nextElement();
fc = (FileConnection)Connector.open(path + filename, Connector.READ);
if(fc.isDirectory()) { //checks if fc is a directory
this.append(filename, folderImage);
} else if(filename.endsWith(".mp3")){ //otherwise, is a file
this.append(filename, mp3Image);
} else if(filename.endsWith(".txt")){
this.append(filename, txtImage);
}
}
fc.close();
}
catch (IOException ioe) {
System.out.println("IOException: "+ioe.getMessage());
}
catch (SecurityException se) {
System.out.println("SecurityException: "+se.getMessage());
}
}
}