Monday, November 24, 2008

Source code : Folder Explorer



FolderExplorerMIDlet.java

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class FolderExplorerMIDlet extends MIDlet {
static FolderExplorerMIDlet midlet;

public FolderExplorerMIDlet() {
midlet = this;
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

protected void pauseApp() {

}

protected void startApp() throws MIDletStateChangeException {
Display display = Display.getDisplay(this);
FolderExplorer instance = new FolderExplorer();
instance.show(display);
}
}


FolderExplorer.java

import java.io.*;
import java.util.*;

import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.*;

public final class FolderExplorer implements CommandListener, Runnable {
private final String PREFIX = "file:///";

private Command cmdOpen = new Command("open", Command.OK, 1);
private Command cmdBack = new Command("back", Command.BACK, 2);
private Command cmdDelete = new Command("delete", Command.OK, 3);

private final List listDir = new List(null, List.IMPLICIT);

private final Stack stackPath = new Stack();
private final Stack stackSubPath = new Stack();

private Command command;
private Displayable displayable;

public FolderExplorer() {
listDir.addCommand(cmdOpen);
listDir.addCommand(cmdBack);
listDir.addCommand(cmdDelete);
listDir.setCommandListener(this);

new Thread(this).start();
}

void show(Display display) {
openRoots();
display.setCurrent(listDir);
}

public void commandAction(Command c, Displayable d) {
command = c;
displayable = d;

synchronized (stackPath) {
stackPath.notify();
}
}

private void execute(Command c, Displayable d) {
if (d == listDir) {
if (c == cmdBack) {
int size = stackPath.size();
if (size == 0) {
FolderExplorerMIDlet.midlet.notifyDestroyed();
} else {
Object obj = stackPath.pop();
if (size - 1 == 0) {
listDir.setTitle("roots");
} else {
listDir.setTitle((String) stackPath.peek());
}
Vector subFolders = (Vector) stackSubPath.pop();
openFolder((String) obj, subFolders);
}
}
else if (listDir.size() > 0) {
if (c == cmdDelete) {
int size = stackPath.size();
if (size > 0) {
try {
StringBuffer sb = new StringBuffer(PREFIX);
for (int i = 0 ; i < size ; i++) {
sb.append(stackPath.elementAt(i));
}
String selectedText = listDir.getString(listDir.getSelectedIndex());
sb.append(selectedText);
FileConnection fileConn = (FileConnection) Connector.open(sb.toString());
fileConn.delete();
} catch (SecurityException e) {

} catch (IOException e) {
e.printStackTrace();
}
}
}
else /*if (c == cmdOpen)*/ {
String selectedText = listDir.getString(listDir.getSelectedIndex());
stackPath.push(selectedText);
listDir.setTitle((String) stackPath.peek());
openFolder(null, null);
}
}
}
}

private void openRoots() {
listDir.deleteAll();

stackPath.removeAllElements();
stackSubPath.removeAllElements();

listDir.setTitle("roots");

Enumeration enumRoots = FileSystemRegistry.listRoots();
Vector subFolder = new Vector();
while (enumRoots.hasMoreElements()) {
String text = (String) enumRoots.nextElement();
subFolder.addElement(text);
listDir.append(text, null);
}
stackSubPath.push(subFolder);
}

private void openFolder(String highlight, Vector subFolder) {
if (subFolder == null) {
Vector subFolder1 = new Vector();
for (int i = 0 ; i < listDir.size() ; i++) {
subFolder1.addElement(listDir.getString(i));
}
stackSubPath.push(subFolder1);

listDir.deleteAll();

try {
StringBuffer sb = new StringBuffer(PREFIX);

int size = stackPath.size();
for (int i = 0 ; i < size ; i++) {
sb.append(stackPath.elementAt(i));
}
FileConnection fileConn = (FileConnection) Connector.open(sb.toString());
Enumeration enumFiles = fileConn.list();
int num = 0;
while (enumFiles.hasMoreElements()) {
String text = (String) enumFiles.nextElement();
listDir.append(text, null);
if (highlight != null && text.equals(highlight)) {
listDir.setSelectedIndex(num, true);
}
num++;
}
} catch (SecurityException e) {

} catch (IOException e) {
e.printStackTrace();
}
} else {
listDir.deleteAll();

Enumeration enumFiles = subFolder.elements();
int num = 0;
while (enumFiles.hasMoreElements()) {
String text = (String) enumFiles.nextElement();
listDir.append(text, null);
if (highlight != null && text.equals(highlight)) {
listDir.setSelectedIndex(num, true);
}
num++;
}
}
}

public void run() {
while (true) {
synchronized (stackPath) {
try {
stackPath.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
execute(command, displayable);
}
}
}