⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vediofiledialog.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.util.*;
import java.io.*;

/**
 * 该类定义了一个视频文件对话框。
 * 显示设备中所有的MPEG格式的视频文件列表。
 */
public class VedioFileDialog implements CommandListener, Runnable {
    public static final int OPEN = 0;
    public static final int CANCEL = 1;
    
    private MobilPlayerMIDlet midlet;
    private Display display;
    private VedioFileDialogListener fdListener;
    private String currentDir = "/";    //当前目录
    private String selectFile;      //选择的文件
    private Vector fileList;        //当前目录下的文件列表
    private List fileListView;      //当前目录下的文件列表视图
    private Command cmdOpen = new Command("打开", Command.SCREEN, 1);
    private Command cmdCancel = new Command("取消", Command.CANCEL, 1);
    
    //构造方法
    public VedioFileDialog(MobilPlayerMIDlet midlet) {
        this.midlet = midlet;
        display = Display.getDisplay(midlet);
        fileList = new Vector();
        fileListView = new List("选择多媒体文件——" + currentDir, Choice.IMPLICIT);
        fileListView.setSelectCommand(cmdOpen);
        fileListView.addCommand(cmdCancel);
        fileListView.setCommandListener(this);
        update();   //更新文件列表
    }
    
    //显示对话框
    public void show() {
        updateView();   //更新文件列表视图
        display.setCurrent(fileListView);
    }
    
    //更新文件列表
    private void update() {
        try {
            fileList.removeAllElements();
            if(currentDir.equals("/")) {    //根目录
                Enumeration e = FileSystemRegistry.listRoots();
                while(e.hasMoreElements()) {
                    fileList.addElement(e.nextElement());
                }
            }
            else {
                FileConnection fc = (FileConnection)Connector.open("file://" + currentDir);
                Enumeration e = fc.list("*", false);
                fileList.addElement("..");
                String name = "";
                while(e.hasMoreElements()) {
                    name = (String)e.nextElement();
                    if(name.endsWith(".mpg") || name.endsWith("/")) {
                        fileList.addElement(name);
                    }
                }
            }
        }
        catch(IOException ioe) {
            midlet.showAlert("打开文件系统错误!原因:\n" + ioe.toString());
        }
    }
    
    //更新视图
    private void updateView() {
        fileListView.deleteAll();
        fileListView.setTitle("选择多媒体文件——" + currentDir);
        int n = fileList.size();
        for(int i=0; i<n; i++) {
            fileListView.append((String)fileList.elementAt(i), null);
        }
    }
    
    public String getFile() {
        return currentDir+selectFile;
    }
    
    public String getFileName() {
        return selectFile;
    }
    
    public void setVedioFileDialogListener(VedioFileDialogListener fdListener) {
        this.fdListener = fdListener;
    }
    
    public void commandAction(Command cmd, Displayable d) {
        if(cmd == cmdOpen) {
            int index = fileListView.getSelectedIndex();
            String fn = (String)fileList.elementAt(index);
            if(fn.endsWith("/")) {  //目录
                currentDir += fn;
                Thread t = new Thread(this);
                t.start();
            }
            else if(fn.equals("..")) {
                int i = currentDir.lastIndexOf('/', currentDir.length()-2);
                if(i != -1) {
                    currentDir = currentDir.substring(0, i+1);
                }
                //创建更新线程
                Thread t = new Thread(this);
                t.start();
                
            }
            else {
                selectFile = fn;
                if(fdListener != null) {
                    fdListener.handle(this, OPEN);
                }
            }
        }
        else if(cmd == cmdCancel) {
            if(fdListener != null) {
                fdListener.handle(this, CANCEL);
            }
        }
    }
    
    public void run() {
        update();
        updateView();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -