helpscreen.java

来自「moblie syncml mail javame」· Java 代码 · 共 236 行

JAVA
236
字号
/*
 * Funambol is a mobile platform developed by Funambol, Inc.
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 *
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */
package com.funambol.mailclient.ui.view;

import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.ui.utils.UiUtils;
import com.funambol.util.ChunkedString;
import com.funambol.util.StreamReaderFactory;
import com.funambol.util.StreamReader;
import com.funambol.util.Log;

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Spacer;

import java.util.Vector;
import java.io.IOException;
import java.io.InputStream;

/**
 * This class implements the HelpScreen. At the moment we do not support HTML
 * pages, instead we have a simple mechanism to make help navigation a bit
 * easier.
 * The help screen is a list containing "links" to help sections. Each section
 * is just text displayed as it is, without any formatting. This may change in
 * the future.
 * Help files are gzipped text files containing the whole help. The syntax is
 * pretty much plain text, except for a tag indicating the beginning of a new
 * section. The syntax is borrowed from Wiki syntax.
 *
 * === Section #1 title
 * Section #1 description
 * === Section #2 title
 * Section #2 description
 *
 * The triple === marks the beginning of a new section and what comes after the
 * marker it the title. A new line indicates the end of the title and the
 * beginning of the description. The description cannot contain === (no way to
 * escape it). The descritpion terminates where a new section begins or at the
 * end of the file. It is mandatory for each section to have both a title and a
 * descritpion.
 *
 */
public class HelpScreen extends List implements CommandListener {

    /** Name of the help file */
    private static final String helpFileName = "/res/help.hlp";
    /** About command */
    private Command aboutCommand;
    private Command backCommand;
    private Command viewCommand;
    /** This is a list of sections titles */
    private Vector sectionsTitle;
    /** This is a list of sections contents (note this must be same size as the
     * sectionsTitle) */
    private Vector sectionsContent;

    /**
     * This class is responsible for showing the content of an help section.
     * At the moment the content is not formatted. The section title is
     * displayed, then a space and finally the whole content.
     */
    private class SectionView extends Form implements CommandListener {

        /**
         * Construct the help section view with the given title and content
         *
         * @param title the section title
         * @param content the section content
         */
        public SectionView(String title, String content) {
            super(title);
            setCommandListener(this);
            //#ifdef isBlackberry
            //# this.addCommand( UIController.blackberryExitCommand );
            //#endif
            this.addCommand(UIController.backCommand);
            StringItem contentItem = new StringItem(null, content);
            this.append(contentItem);
        }

        /**
         * Commands handler
         */
        public void commandAction(Command command, Displayable displayable) {
            if (command == UIController.backCommand) {
                UIController.showBackScreen();
            } else if (command == UIController.blackberryExitCommand) {
                UIController.midlet.destroyApp(false);
            }
        }
    }

    /**
     * Creates a new instance of HelpScreen
     */
    public HelpScreen(MIDlet midlet) {
        super(Localization.getMessages().HELP_PAGE_TITLE, List.IMPLICIT);
        aboutCommand = new Command(Localization.getMessages().ABOUT_COMMAND_LABEL, UIController.COMMAND_TYPE, 60);
        viewCommand = new Command(Localization.getMessages().OPEN_COMMAND_LABEL, UIController.COMMAND_TYPE, 0);
        backCommand = new Command(Localization.getMessages().BACK_COMMAND, Command.CANCEL, 10);
        setCommandListener(this);
        this.addCommand(aboutCommand);
        this.addCommand(backCommand);
        //#ifdef isBlackberry
//#        this.addCommand( UIController.blackberryExitCommand );
        //#endif
        loadHelp(midlet);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == backCommand) {
            UIController.showBackScreen();
        } else if (command == aboutCommand) {
            UIController.showAbout(this);
        } else if (command == viewCommand) {
            viewContent();
        } else if (command == UIController.blackberryExitCommand) {
            UIController.midlet.destroyApp(false);
        }
    }

    /**
     * Load the help from the midlet resource location.
     * The help file is expected to be a gzipped text file as described in this
     * class header.
     * If the help file cannot be found then a list with an error message is
     * built.
     * This method is also responsible for parsing the content and splitting
     * sections title from content (but not for content parsing).
     *
     * @param midlet the running midlet
     */
    private void loadHelp(MIDlet midlet) {

        InputStream is = midlet.getClass().getResourceAsStream(helpFileName);
        // Decode stream
        String helpText = null;
        try {
            StreamReader streamReader = StreamReaderFactory.getStreamReader("gzip");
            byte[] text = streamReader.readStream(is, is.available());
            helpText = new String(text);
        } catch (Exception ex) {
            helpText = Localization.getMessages().HELP_NOT_AVAILABLE;
            Log.error("Cannot load help file " + helpFileName);
            // Creates a simple text with the error
            this.append(helpText, null);
            return;
        }

        // parse the text to split it into sections
        sectionsTitle = new Vector();
        sectionsContent = new Vector();

        String[] titleSep = {"==="};
        String[] contentSep = {"\n", "\r\n"};
        ChunkedString cs = new ChunkedString(helpText);
        String title;

        // TODO: what do we do with the description?
        String description = cs.getNextString(titleSep);

        while ((title = cs.getNextString(contentSep)) != null) {
            // Found a new paragraph
            String content = cs.getNextString(titleSep);

            if (content == null) {
                content = "";
            }

            title = title.trim();
            content = content.trim();

            sectionsTitle.addElement(title);
            sectionsContent.addElement(content);

            this.append(title, null);
        }

        this.setSelectCommand(viewCommand);
    }

    /**
     * View the content of the selected section. If no section is selected no
     * action is performed.
     */
    private void viewContent() {
        int idx = getSelectedIndex();
        if (idx != -1) {
            String content = (String) sectionsContent.elementAt(idx);
            String title = (String) sectionsTitle.elementAt(idx);
            SectionView sectionView = new SectionView(title, content);
            UIController.showHelpSection(this, sectionView);
        }
    }
}

⌨️ 快捷键说明

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