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

📄 fileselector.java

📁 很好的基于java的手机文本阅读器anyview 2.0源码,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ismyway.anyview;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.util.Vector;

/**
 * <p>Title: AnyView</p>
 *
 * <p>Description: E680(I) Reader</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: www.ismyway.com</p>
 *
 * @author ZhangJian
 * @version 1.0
 */
public class FileSelector extends AbstractWindow {
    //private String[] realFiles;
    protected Image windowImage; //保存当前图体的Image对象

    private String root;
    private Vector fileList = new Vector();
    final static int HorPad = 10; //距两边的水平距离
    final static int LinePad = 5; //行间距
    final static int ButtonHeight = 20; //滚动条需要空间的高
    private int ShowItems = 12; //每页显示的文件数
    private int VecPad = 25; //正文开始的Y坐标
    boolean showScrollBar = false; //是否需要滚动条
    private int earseScrollBarHeight; //可重绘部分的高(即滚动条占的高)
    private int cubeWidth = 0; //选择框的宽
    private int itemIndex = 0; //用户停留时列表中文件的索引编号
    private int topIndex = 0; //当前总页数
    private boolean pageup = false, pagedown = false; //记录用户点击触摸屏时的动作
    protected Image cube;
    private SplashCanvas sc;

    public final static int DIR = 1;
    public final static int TXT = 2;
    public final static int IAV = 3;

    public FileSelector(SplashCanvas sc) {
        this.sc = sc;
        WINDOW_STATE = OPENING;
        if ("".equals(AnyView.rootPath)) {
            root = AnyView.Root;
        } else {
            root = AnyView.rootPath;
        }

        openDir();
    }

    /**
     * 读取目录列表
     */
    /*synchronized */ void openDir() {
        FileSystemReader fsr = new FileSystemReader(root);
        String[] list = fsr.list();
        fsr.close();

        //删除原来的文件列表
        fileList.removeAllElements();
        itemIndex = 0; //用户停留时列表中文件的索引编号
        topIndex = 0; //当前总页数
        ShowItems = 12; //每页显示的文件数

        if (!root.equals(AnyView.Root)) { //不在根目录,提供返回上级目录的选项
            fileList.addElement(new Files("<↑Up One Level>", DIR));
        }

        int offset = root.length();
        for (int i = 0; i < list.length; i++) {
            String filename = list[i];
            if (filename.startsWith(root)) { //文件名中包含了路径信息
                filename = filename.substring(offset);
            }

            fsr = new FileSystemReader(root + filename);
            //System.out.println(root + filename + " = " + fsr.isHidden());
            if (!AnyView.showhiddenfiles && fsr.isHidden()) {
                continue;
            }
            if (fsr.isDirectory()) { //是目录
                fileList.addElement(new Files(filename, DIR));
            } else {
                if (filename.toLowerCase().endsWith(".txt")) { //txt文件
                    fileList.addElement(new Files(filename, TXT));
                } else if (filename.toLowerCase().endsWith(".iav")) { //iav文件
                    fileList.addElement(new Files(filename, IAV));
                }
            }
            fsr.close();
        }

        defineWindow();
        refreshList();
    }

    void defineWindow() {
        //确定标题栏的高
        VecPad = AnyView.cf.FontHeight + 10;

        //确定当前窗口显示的条目
        if (AnyView.readType == 1) { //横屏
            if ((AnyView.cf.FontHeight + LinePad) * ShowItems + VecPad +
                LinePad + ButtonHeight > AnyView.ScreenWidth) { //需要翻页
                ShowItems = (AnyView.ScreenWidth - LinePad - VecPad -
                             ButtonHeight) / (AnyView.cf.FontHeight + LinePad);
            }
        } else {
            if ((AnyView.cf.FontHeight + LinePad) * ShowItems + VecPad +
                LinePad + ButtonHeight > AnyView.ScreenHeight) { //需要翻页
                ShowItems = (AnyView.ScreenHeight - LinePad - VecPad -
                             ButtonHeight) / (AnyView.cf.FontHeight + LinePad);
            }
        }

        //确定窗口的宽
        if (AnyView.readType == 1) { //横屏
            width = AnyView.ScreenHeight - 40;
        } else {
            width = AnyView.ScreenWidth - 40;
        }

        //确定窗口的高
        if (fileList.size() <= ShowItems) { //当前可以显示完成
            this.height = fileList.size() * (AnyView.cf.FontHeight + LinePad) +
                          LinePad + VecPad;
            ShowItems = fileList.size();
            showScrollBar = false;
        } else {
            this.height = ShowItems * (AnyView.cf.FontHeight + LinePad) +
                          LinePad + VecPad + ButtonHeight; //6是翻页按钮的高
            showScrollBar = true;
        }

        if (AnyView.readType == 1) { //横屏
            this.LEFT = (AnyView.ScreenWidth - this.height) >> 1;
            this.TOP = (AnyView.ScreenHeight + this.width) >> 1;
        } else {
            this.LEFT = (AnyView.ScreenWidth - this.width) >> 1;
            this.TOP = (AnyView.ScreenHeight - this.height) >> 1;
        }
        this.LEFT = this.LEFT < 0 ? 0 : this.LEFT;
        this.TOP = this.TOP < 0 ? 0 : this.TOP;

        //是否需要滚动条
        cubeWidth = width - 4;
        if (showScrollBar) {
            earseScrollBarHeight = height - VecPad - ButtonHeight; //确定需要擦除滚动条背景的高
            cubeWidth -= 4;
        } else {
            earseScrollBarHeight = height - VecPad - 2;
        }

        //创建选择时的背景条
        int[] c = new int[cubeWidth * (AnyView.cf.FontHeight + 2)];
        for (int i = 0; i < c.length; i++) {
            c[i] = 0x7FFFFFFF;
        }
        cube = Image.createRGBImage(c, cubeWidth,
                                    CustomFont.FontHeight + 2, true);
        c = null;
    }

    void refreshList() {
        windowImage = Image.createImage(width, height);
        Graphics g = windowImage.getGraphics();
        g.setColor(AnyView.background);
        g.fillRect(0, 0, width, height);
        g.setColor(AnyView.bordorcolor);
        g.drawRect(0, 0, width - 1, height - 1);
        g.drawLine(0, VecPad, width, VecPad);

        //绘制标题
        byte[] t = AnyView.cf.str2bytes(root);
        AnyView.cf.setColor(AnyView.titlecolor);
        Image img = AnyView.cf.buildImage(t);
        g.drawImage(img, 5, 5, ANCHOR);
        img = null;

        //绘制选项
        int top = VecPad + 5;
        for (int i = 0; i < ShowItems; i++) {
            Files f = (Files) fileList.elementAt(i + topIndex);
            if (f.type == DIR) {
                AnyView.cf.setColor(AnyView.foldercolor);
            } else if (f.type == TXT) {
                AnyView.cf.setColor(0xFFFFFF);
            } else if (f.type == IAV) {
                AnyView.cf.setColor(0xFFFF00);
            }
            img = AnyView.cf.buildImage(f.filename);
            g.drawImage(img, HorPad, top, ANCHOR);
            top += AnyView.cf.FontHeight + LinePad;
        }

        //绘制滚动条背景
        g.setColor(AnyView.background);
        g.fillRect(width - HorPad, VecPad + 1, HorPad, earseScrollBarHeight);
        g.setColor(AnyView.bordorcolor);
        g.drawRect(0, 0, width - 1, height - 1);

        //绘制滚动条
        if (showScrollBar) {
            g.setColor(AnyView.bordorcolor);
            g.drawLine(0, height - ButtonHeight, width, height - ButtonHeight);
            g.drawLine(width >> 1, height - ButtonHeight, width >> 1, height);
            g.drawLine(width - 5, VecPad + 1, width - 5, height - ButtonHeight);

            int x = ((width >> 1) - 11) >> 1;
            int y = height - ButtonHeight + 7;
            g.drawImage(AnyView.UpArrow, x, y, ANCHOR);
            x += width >> 1;
            g.drawImage(AnyView.DownArrow, x, y, ANCHOR);

            int h = height - VecPad - ButtonHeight - 4;
            x = topIndex * h / fileList.size();
            y = (topIndex + ShowItems) * h / fileList.size();
            g.drawLine(width - 3, VecPad + 2 + x, width - 3, VecPad + 2 + y);
        }
    }

    public Image getWindow() {

⌨️ 快捷键说明

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