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

📄 textreader.java

📁 JAVA手机的电子书编辑器以及阅读器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**
     * Generate the options form with image title and progress gauge.
     * 
     * @param name
     *            the title of the Image to be loaded.
     * @return the generated progress screen
     */
    private Screen genProgress(String name)
    {
        if (progressForm == null)
        {
            progressForm = new Form(name);
            progressForm.addCommand(cancelCommand);
            progressForm.setCommandListener(this);

            progressGauge = new javax.microedition.lcdui.Gauge("Loading file...", false, 9, 0);

            progressForm.append(progressGauge);
        }
        else
        {
            progressGauge.setValue(0);
            progressForm.setTitle(name);
        }
        return progressForm;
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.microedition.lcdui.ItemStateListener#itemStateChanged(javax.microedition.lcdui.Item)
     */
    public void itemStateChanged(Item item)
    {
        if (item == backColorChoice)
        {
            frame.setBackgroundColor(backColorChoice.getSelectedIndex());
        }
        else if (item == forColorChoice)
        {
            frame.setForgroundColor(forColorChoice.getSelectedIndex());
        }

    }

    /**
     * Generate the options form with speed and style choices. Speed choices are
     * stop, slow, medium, and fast. Style choices for borders are none, plain,
     * fancy.
     * 
     * @return the generated options Screen
     */
    private Screen genOptions()
    {
        if (optionsForm == null)
        {
            optionsForm = new Form("选项");
            optionsForm.addCommand(okCommand);
            optionsForm.setCommandListener(this);
            optionsForm.setItemStateListener(this);

            forColorChoice = new ChoiceGroup("字体颜色", Choice.EXCLUSIVE);
            forColorChoice.append("Black", null);
            forColorChoice.append("Green", null);
            forColorChoice.append("Blue", null);
            forColorChoice.append("White", null);
            forColorChoice.append("Red", null);
            forColorChoice.setSelectedIndex(frame.getForgroundColor(), true);
            optionsForm.append(forColorChoice);

            backColorChoice = new ChoiceGroup("背景颜色", Choice.EXCLUSIVE);
            backColorChoice.append("Black", null);
            backColorChoice.append("Green", null);
            backColorChoice.append("Blue", null);
            backColorChoice.append("White", null);
            backColorChoice.append("Red", null);

            backColorChoice.setSelectedIndex(frame.getBackgroundColor(), true);
            optionsForm.append(backColorChoice);
        }
        return optionsForm;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    public void run()
    {
        Thread mythread = Thread.currentThread();
        /* Free images and resources used by current frame. */
        frame.reset();

        //       byte[] content = new byte[102400];
        //       int bufLen = 102400;

        StringBuffer buffer = new StringBuffer();
        try
        { // Catch OutOfMemory Errors

            System.out.println("load content of : " + fileName);

            InputStream in = this.getClass().getResourceAsStream(fileName);

            InputStreamReader sr = new InputStreamReader(in, "UTF-8");

            char[] tmp = new char[1024];
            int len = in.available() / 2;
            //		    byte[] content = new byte[len];
            this.progressGauge.setMaxValue(len);
            int offset = 0;
            int readLen;
            while (true)
            {
                readLen = sr.read(tmp); //sr.read(tmp);            //in.read(content, offset, bufLen - offset );
                if (readLen <= 0)
                    break;

                buffer.append(tmp, 0, readLen);
                offset += readLen;
                progressGauge.setValue(offset);
            }

            //     sr.close();
            in.close();

            System.out.println("Content len:" + offset);

            frame.setContent(buffer.toString()); //new String(content, 0, offset, "UTF-8"));

            if (chapterList.getSelectedIndex() == this.curChapter)
            {
                frame.setCurrentParagraph(this.curPagraph);
            }
            else
            {
                frame.setCurrentParagraph(0);
                curPagraph = 0;
            }

            display.setCurrent(frame);

        } catch (OutOfMemoryError err)
        {

            err.printStackTrace();

            System.out.println(err);

            // If cancelled, discard images and return immediately
            if (thread != mythread)
            {
                return;
            }

            alert.setString("Not enough memory for the chapter.");
        } catch (Exception e)
        {
            alert.setString("failed to read file.");
            frame.setContent("failed to read file." + e);
            display.setCurrent(frame);

        }

    }

    /**
     * Open the store that holds the saved options. If an error occurs, put up
     * an Alert.
     */
    void openOptions()
    {
        try
        {
            optionsStore = RecordStore.openRecordStore(optionsName, true);
        } catch (RecordStoreException ex)
        {
            alert.setString("Could not access options storage");
            display.setCurrent(alert);
            optionsStore = null;
        }
    }

    /**
     * Save the options to persistent storage. The options are retrieved
     * ChoiceGroups and stored in Record 1 of the store which is reserved for
     * it. The two options are stored in bytes 0 and 1 of the record.
     */
    void saveOptions()
    {
        if (optionsStore != null)
        {
            byte[] options = new byte[5];
            options[0] = (byte) frame.getBackgroundColor();
            options[1] = (byte) frame.getForgroundColor();
            short curParagraph = (short) frame.getCurrentParagraph();
            options[2] = (byte) curParagraph;
            options[3] = (byte) (curParagraph / 256);

            options[4] = (byte) this.chapterList.getSelectedIndex();
            try
            {
                optionsStore.setRecord(1, options, 0, options.length);
            } catch (InvalidRecordIDException ridex)
            {
                // Record 1 did not exist, create a new record (Should be 1)
                try
                {
                    int rec = optionsStore.addRecord(options, 0, options.length);
                } catch (RecordStoreException ex)
                {
                    alert.setString("Could not add options record");
                    display.setCurrent(alert);
                }
            } catch (RecordStoreException ex)
            {
                alert.setString("Could not save options");
                display.setCurrent(alert);
            }
        }
    }

    /**
     * Restore the options from persistent storage. The options are read from
     * record 1 and set in the frame and if the optionsForm has been created in
     * the respective ChoiceGroups.
     */
    void restoreOptions()
    {
        if (optionsStore != null)
        {
            try
            {
                byte[] options = optionsStore.getRecord(1);
                if (options.length == 5)
                {
                    frame.setBackgroundColor(options[0]);
                    frame.setForgroundColor(options[1]);

                    int pH = options[3] & 0xff;
                    int pL = options[2] & 0xff;
                    curPagraph = pL + pH * 256;

                    this.curChapter = options[4];

                    System.out.println("Restored curChapter: " + curChapter);
                    if (optionsForm != null)
                    {
                        backColorChoice.setSelectedIndex(options[0], true);
                        forColorChoice.setSelectedIndex(options[1], true);
                    }
                    if (this.chapterList != null)
                        chapterList.setSelectedIndex(curChapter, true);
                    return; // Return all set
                }
            } catch (RecordStoreException ex)
            {
                // Ignore, use normal defaults
            }
        }
    }

    /**
     * Close the options store.
     */
    void closeOptions()
    {
        if (optionsStore != null)
        {
            try
            {
                optionsStore.closeRecordStore();
                optionsStore = null;
            } catch (RecordStoreException ex)
            {
                alert.setString("Could not close options storage");
                display.setCurrent(alert);
            }
        }
    }

    public static void main(String[] args)
    {
    }
}

⌨️ 快捷键说明

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