📄 oaabrowser.java
字号:
/**
* @author Saidul M. Islam
* @note Modified from the Notepad example provided by Swing packages
* @date March 28, 1998
*
* This program will implement a subset of Web browser functionalities which can display only
* HTML 3.2 compatible html pages.
*
* Modified and extended by Didier Guzzoni, SRI International
**/
package com.sri.oaa2.guiutils;
import java.awt.*;
import java.io.*;
import java.net.URL;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
class OaaBrowser extends JPanel implements HyperlinkListener, Cloneable {
private String homeURL = "http://www.ai.sri.com";
private String docURL = homeURL;
private JEditorPane mainBrowserPanel;
private int MAX_HISTORY = 100;
private String[] mHistory = new String[MAX_HISTORY];
private int mHistoryIndex = -1;
private int mHistorySize = 0;
public OaaBrowser() {
super(true);
// Creates the main HTML panel
mainBrowserPanel = new JEditorPane();
mainBrowserPanel.setEditable(false);
mainBrowserPanel.addHyperlinkListener(this);
// Creates the Sroll Panel
JScrollPane scroller = new JScrollPane();
JViewport port = scroller.getViewport();
port.add(mainBrowserPanel);
// Main layout
setLayout(new BorderLayout());
add(scroller);
}
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
setURL(e.getURL());
}
}
public boolean setURL(String inUrl, boolean inUpdateHistory) {
// DEBUG
//System.out.println("Loading " + inUrl);
Document currentDocument = mainBrowserPanel.getDocument();
try {
mainBrowserPanel.setPage(inUrl);
if (inUpdateHistory) {
mHistoryIndex = (mHistoryIndex+1)%MAX_HISTORY;
mHistory[mHistoryIndex]=inUrl;
mHistorySize++;
}
return true;
}
catch (IOException ioe) {
mainBrowserPanel.setDocument(currentDocument);
System.out.println("Could not load URL");
return false;
}
}
public boolean setURL(URL inUrl, boolean inUpdateHistory) {
return setURL(inUrl.toString());
}
public boolean setURL(URL inUrl) {
return setURL(inUrl, true);
}
public boolean setURL(String inUrl) {
return setURL(inUrl, true);
}
public boolean back(){
// Updates the index within the history
if ((mHistoryIndex<0) ||
((mHistoryIndex==0) && (mHistoryIndex<MAX_HISTORY)))
return false;
mHistoryIndex--;
if (mHistoryIndex < 0)
mHistoryIndex = MAX_HISTORY;
return setURL(mHistory[mHistoryIndex], false);
}
public boolean forward(){
// Updates the index within the history
if ((mHistoryIndex<0) || ((mHistoryIndex+1)==mHistorySize))
return false;
mHistoryIndex = (mHistoryIndex+1)%MAX_HISTORY;
return setURL(mHistory[mHistoryIndex], false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -