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

📄 websimview.java

📁 一款即时通讯软件
💻 JAVA
字号:
package edu.ou.kmi.buddyspace.plugins.simlink.gui;


import javax.swing.*;
import java.util.StringTokenizer;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.net.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.StyleSheet;
import javax.swing.text.Document;

import edu.ou.kmi.buddyspace.gui.BSMainFrame;

public class WebSimView extends JTextPane implements HyperlinkListener
{
    String simServer = "http://pckm126.open.ac.uk:8080/simlink/simlet";
    URL currentPage;

    private void addIdentity()
    {
        String jid = "";
        jid += BSMainFrame.username;
        jid += "@" + BSMainFrame.server;
        jid += "/" + BSMainFrame.resource;
        try {
            // This escapes any special chars in the URL parameter
            jid = URLEncoder.encode(jid, "UTF-8");
        } catch (java.io.UnsupportedEncodingException ex) { }

        try {
            currentPage = new URL(simServer + "?JID=" + jid);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public WebSimView()
    {
        addIdentity();
        setContentType("text/html");
        setEditorKit(kit);
        setEditable(false);
        addHyperlinkListener(this);
        refresh();
    }

    int refCount = 0;
    public void refresh()
    {
        try {
            System.out.println(currentPage.toString() + "&ref=" + refCount);
            setPage(currentPage.toString() + "&ref=" + refCount++);
        } catch (java.io.IOException e) {
            setText("Error initialising page with URL " + currentPage);
        }
    }

    public void hyperlinkUpdate(HyperlinkEvent e)
    {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            final JEditorPane pane = (JEditorPane) e.getSource();
            try {
                String protocol = e.getURL().getProtocol();
                if (!"http".equalsIgnoreCase(protocol))
                {
                    return;
                }
                currentPage = e.getURL();
                System.out.println(currentPage.toString());
                pane.setPage(currentPage);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }

    // There is a problem with HTML form processing in (seen in Java 1.5 and 1.4)
    // When package javax.swing.text.html.FormView processes a form submission,
    // it simply appends the http GET query string to the base url. This is
    // ok except where the base URL already contains parameters, in which case
    // parameters will not be processed correctly by the server because the
    // query string then contains two question mark characters.
    // This override is a workround that strips off parameters from the URL
    // returned by the getBase() method which is called by code in the
    // FormView module.
    // Chris Denham - chris@iconographics.co.uk
    class CustomHTMLDocument extends HTMLDocument {
        public CustomHTMLDocument(StyleSheet styles) {
            super(styles);
        }

        public URL getBase() {
            try {
                URL base = super.getBase();
                String file = base.getFile();
                file = new StringTokenizer(file, "?").nextToken();
                base = new URL(base.getProtocol(), base.getHost(), base.getPort(),
                               file);
                return base;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return super.getBase();
            }
        }
    }


    class CustomHTMLEditorKit extends HTMLEditorKit {
        public Document createDefaultDocument() {
            HTMLDocument oldDoc = (HTMLDocument)super.createDefaultDocument();

            // Replicates the original doc as far a possible whilst overriding
            // the getBase method. (this is not entirely safe because it is
            // only replicating properties that were known to be set in the
            // super class createDefaultDocument by looking at the sources for
            // HTMLEditorKit in Java 1.4.
            // Chris Denham - chris@iconographics.co.uk
            HTMLDocument doc = new CustomHTMLDocument(oldDoc.getStyleSheet());
            doc.setParser(oldDoc.getParser());
            doc.setAsynchronousLoadPriority(-1);//oldDoc.getAsynchronousLoadPriority());
            doc.setTokenThreshold(oldDoc.getTokenThreshold());
            return doc;
        }
    }

    private HTMLEditorKit kit = new CustomHTMLEditorKit();

}

⌨️ 快捷键说明

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