[J2ME] ResourceList

来源:互联网 发布:youtube软件 编辑:程序博客网 时间:2024/06/03 20:42

package midview;

import java.io.IOException;
import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;

public class ResourceList extends List implements CommandListener{
    final static String DATHHEAD = "file:///";
    final static String ROOT = "SDCard/";
    final char SLASH = '/';
    final Image upIcon = null;
    final Image folderIcon = null;
    final Image fileIcon = null;
   
    private MIDlet midlet = null;
   
    private String currFile = null;
   
    private Command openCommand = new Command("Open", Command.SCREEN, 1);
    private Command exitCommand = new Command("Exit", Command.EXIT, 2);
   
    private Runnable listRunable = new Runnable(){
        public void run() {           
            try {
                listFiles(currFile);
            } catch (IOException e) {
                e.printStackTrace();
            }           
        }       
    };
   
    private Runnable openFileRunable = new Runnable(){
        public void run() {           
            try {
                openFile(currFile);
            } catch (Exception e) {
                e.printStackTrace();
            }           
        }       
    };
   
    public ResourceList(MIDlet midlet, String title, int listType) {
        super(title, listType);
        this.midlet = midlet;
        this.setCommandListener(this);
        this.addCommand(openCommand);
        this.addCommand(exitCommand);
        try {
            listFiles(ROOT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void listFiles(String dir) throws IOException{
        Enumeration enums;
        FileConnection fc = null;
        this.deleteAll();
       
        if (dir==null || dir.equals(ROOT)) {
            enums = FileSystemRegistry.listRoots();
            this.currFile = ROOT;
        } else {
            append("..", upIcon);
            fc = (FileConnection) Connector.open(DATHHEAD + dir);
            enums = fc.list();
        }
        while (enums.hasMoreElements()) {
            String file = (String) enums.nextElement();
            if (file.charAt(file.length() - 1) == SLASH) {
                // Folder
                append(file, folderIcon);
            } else {
                // File
                append(file, fileIcon);
            }
        }
        if (fc != null) {
            fc.close();
        }
    }
   
   
    public void commandAction(Command command, Displayable display) {
        if(command==openCommand){
            int index = this.getSelectedIndex();
            String oldDir = this.currFile;
            try {               
                String file = this.getString(index);
                if (file.charAt(file.length() - 1) == SLASH ) { //dir
                    currFile += file;
                    new Thread(this.listRunable).start();
                }else if(file.equals("..")){
                    currFile += file;
                    new Thread(this.listRunable).start();
                }else{ //file, open
                   
                }
            } catch (Exception e) {
                this.currFile = oldDir;
                e.printStackTrace();
            }
        }else if(command==exitCommand){
            openFile(null);
        }
    }
    /**
     * open file
     */
    private void openFile(String filename){   
        filename = "/test.gif";
        Displayable d = (Displayable)getRegApp(filename);
        Display.getDisplay(this.midlet).setCurrent(d);
        ((IOpen)d).open(filename);
    }
    /**
     * get associated class
     * @param ext
     * @return
     */
    private IOpen getRegApp(String ext){
        return new ImageViewer();
    }
}

原创粉丝点击