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

📄 simplehttpbrowser.java

📁 j2me手机上的短信开发实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)SimpleHttpBrowser.java	1.2 04/02/12 * * Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved.  * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms */package example.mmademo;import java.io.*;import java.util.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;import javax.microedition.rms.*;import javax.microedition.media.*;/** * An http link browser. Used in SimplePlayer * * @version 1.2 */public class SimpleHttpBrowser extends List    implements CommandListener, Utils.ContentHandler, Utils.QueryListener {    private static final boolean MASS_TEST=false;    private Command exitCommand = new Command("Exit Browser", Command.EXIT, 1);    private Command backCommand = new Command("Back", Command.BACK, 1);    private Command openCommand = new Command("Open", Command.ITEM, 1);    private Command selectCommand = new Command("Select", Command.ITEM, 1);    private Command saveCommand = new Command("Save in RMS", Command.ITEM, 1);    private Command menuCommand = new Command("Menu", Command.ITEM, 1);    private Command refreshCommand = new Command("Refresh", Command.ITEM, 1);    private Command allCommands[] = {	exitCommand,	backCommand,	openCommand,	selectCommand,	saveCommand,	menuCommand,	refreshCommand    };    private Vector names = new Vector();    private Vector urls = new Vector();    private Stack history = new Stack();    private Stack historyIndex = new Stack();    private String currURL = null;    // "global" variables for user queries    private String queryDefault;    private String queryInputURL;    private Utils.BreadCrumbTrail parent;    private List menuList;    public SimpleHttpBrowser(String title, Utils.BreadCrumbTrail parent) {	super(title, Choice.IMPLICIT);	this.parent = parent;    }    public void displayHTML(String url, int selectedIndex) {	boolean error = false;	currURL = url;	clearLists();	try {	    readHTML(url);	} catch (Exception e) {	    append("["+Utils.friendlyException(e)+"]", null);	    error = true;	}	addCommand(backCommand);	setCommandListener(this);	if (!error) {	    for (int i=0; i<names.size(); i++) {		append((String) names.elementAt(i), null);	    }	    setListIndex(selectedIndex);	    addCommand(menuCommand);	} else {	    addCommand(refreshCommand);	}	if (MASS_TEST) {	    massTest();	}    }    private void exit() {	currURL = null;	clearLists();	parent.goBack();    }    private static void internalError(Throwable t, String desc) {	if (Utils.DEBUG) if (desc!="") System.out.println(desc);	if (Utils.DEBUG) t.printStackTrace();    }    private void clearLists() {	names.setSize(0);	urls.setSize(0);	for (int i=size()-1; i>=0; i--) {	    delete(i);	}	// remove commands, just to be sure that they	// don't appear erroneously	removeCommand(refreshCommand);	removeCommand(menuCommand);	// good moment to collect garbage	System.gc();    }    // interface Utils.ContentHandler    public void close() {	clearLists();	history.setSize(0);	historyIndex.setSize(0);    }    // interface Utils.ContentHandler    public boolean canHandle(String url) {	return isHTML(url);    }    // interface Utils.ContentHandler    public void handle(String name, String url) {	Utils.debugOut("SimpleHttpBrowser: handle "+url);	displayHTML(url, 0);    }    /**     * Respond to commands     */    public void commandAction(Command c, Displayable s) {	try {	    // respond to menu items	    if ((s == menuList) && (menuList != null) && menuList.isShown()) {		int selIndex = menuList.getSelectedIndex();		parent.goBack();		if (c == backCommand) {		    return;		}		if (c == List.SELECT_COMMAND || c == selectCommand) {		    c = menuItem2Command(selIndex);		    // fall through - the commands will then be handled as if		    // they came from the main list		}	    }	    if ((c == List.SELECT_COMMAND && isShown()) || (c == openCommand)) {		gotoURL(getSelectedIndex());	    }	    else if (c == saveCommand) {		// save to RMS must be called in a different thread !		saveToRms((String) urls.elementAt(getSelectedIndex()));	    }	    else if (c == backCommand) {		goBack();	    }	    else if (c == refreshCommand) {		refresh();	    }	    else if (c == menuCommand) {		showMenu(getSelectedIndex());	    }	    else if (c == exitCommand) {		exit();	    }	} catch (Throwable t) {	    internalError(t, "in commandAction");	}    }    private Command menuItem2Command(int index) {	// go through all commands and test if their label	// matches this menuList item's string	if ((index < 0) || (index >= menuList.size())) {	    return null;	}	String menuStr = menuList.getString(index);	for (int i = 0; i < allCommands.length; i++) {	    if (allCommands[i].getLabel().equals(menuStr)) {		return allCommands[i];	    }	}	return null;    }    private void showMenu(int index) {	String url=(String) urls.elementAt(index);	boolean html = isHTML(url);	menuList = new List("Menu", Choice.IMPLICIT);	menuList.setCommandListener(this);	menuList.append(openCommand.getLabel(), null);	menuList.append(refreshCommand.getLabel(), null);	if (!html) {	    // do not show "Save to RMS" for html/directory links	    menuList.append(saveCommand.getLabel(), null);	}	menuList.append(exitCommand.getLabel(), null);	menuList.addCommand(backCommand);	menuList.addCommand(selectCommand);	parent.go(menuList);    }    private void gotoURL(int index) {	String url=(String) urls.elementAt(index);	gotoURL(url, index);    }    private void gotoURL(String url, int index) {	try {	    if (index>=names.size() || isHTML(url)) {		if (currURL != null) {		    history.push(currURL);		    historyIndex.push(new Integer(index));		}		displayHTML(url, 0);	    } else {		parent.handle((String) names.elementAt(index), url);	    }	} catch (Exception e) {	    Utils.error(e, parent);	}	if (Utils.DEBUG) {	    Utils.debugOut("SimpleHttpBrowser: after gotoURL. History contains "+history.size()+" entries.");	    for (int i = history.size()-1; i>=0; i--) {		Utils.debugOut("     "+i+": "+((String) history.elementAt(i)));	    }	}    }    private void goBack() {	if (Utils.DEBUG) {	    Utils.debugOut("SimpleHttpBrowser: before goBack. History contains "+history.size()+" entries.");	    for (int i = history.size()-1; i>=0; i--) {		Utils.debugOut("     "+i+": "+((String) history.elementAt(i))+"  #"+((Integer) historyIndex.elementAt(i)));	    }	}	if (!history.empty()) {	    String url=(String) history.pop();	    int index=((Integer) historyIndex.pop()).intValue();	    displayHTML(url, index);	} else {	    exit();	}    }    private void refresh() {	int selIndex = getSelectedIndex();	Utils.debugOut("SimpleHttpBrowser.Refresh: index "+selIndex);	displayHTML(currURL, selIndex);    }    // somehow this doesn't work if there was a screen switch before !    private void setListIndex(int index) {	if (index>=0 && index<size()) {	    setSelectedIndex(index, true);	}    }    ////////////////////// interface Utils.QueryListener /////////////////////    public void queryOK(String text) {	try {	    boolean queryAgain = true;	    // if text is null, then queryOK was called in order	    // to display the query for the first time.	    if (text != null) {		if (text.indexOf("/")>=0 || text.indexOf(":")>=0) {		    Utils.error("record store name cannot contain / or :", parent);		}		else if (text.length()>32) {		    Utils.error("record store name cannot exceed 32 characters", parent);		}		else if (text.length()==0) {		    Utils.error("record store name empty. please try again", parent);		} else {		    queryAgain = false;		}	    }	    if (queryAgain) {		Utils.query("Enter record store name:", queryDefault, 32, this, parent);	    } else {		int id = saveToRms(queryInputURL, "rms:/"+text);		Utils.FYI("The file was saved successfully in RMS. The record ID is "+id+".", parent);	    }	} catch (Throwable t) {	    Utils.error(t, parent);	}    }    public void queryCancelled() {	// don't do anything if query is cancelled.	// the previous visible Displayable will be	// displayed automatically    }    private void saveToRms(String inputURL) {	try {	    String[] splitInputURL = Utils.splitURL(inputURL);	    queryDefault = ""; // instance variable, to be used in queryOK handler

⌨️ 快捷键说明

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