001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui.util;
022:
023: import java.awt.*;
024: import java.awt.event.*;
025: import javax.swing.*;
026: import javax.swing.border.*;
027: import java.io.*;
028: import java.util.*;
029: import javax.swing.text.html.*;
030: import javax.swing.event.*;
031: import java.net.*;
032:
033: /**
034: *
035: */
036:
037: public class HTMLPane extends JPanel {
038: private JScrollPane m_scroller;
039: private JEditorPane m_html;
040: private URL m_baseurl;
041:
042: edu.stanford.ejalbert.BrowserLauncher m_launcher;
043:
044: class Hyperactive implements HyperlinkListener {
045:
046: public void hyperlinkUpdate(HyperlinkEvent e) {
047: if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
048:
049: JEditorPane pane = (JEditorPane) e.getSource();
050: if (e instanceof HTMLFrameHyperlinkEvent) {
051: HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
052: HTMLDocument doc = (HTMLDocument) pane
053: .getDocument();
054: doc.processHTMLFrameHyperlinkEvent(evt);
055: } else {
056: try {
057: URL nurl = e.getURL();
058: if (nurl == null)
059: nurl = new URL(m_baseurl, e
060: .getDescription());
061: if (jsmooth.Native.isAvailable()) {
062: jsmooth.Native.shellExecute(
063: jsmooth.Native.SHELLEXECUTE_OPEN,
064: nurl.toString(), null, null,
065: jsmooth.Native.SW_NORMAL);
066: } else
067: m_launcher.openURLinBrowser(nurl
068: .toExternalForm());
069: } catch (Throwable t) {
070: t.printStackTrace();
071: }
072: }
073: }
074: }
075: }
076:
077: public HTMLPane() {
078: try {
079: m_baseurl = new File(".").toURL();
080: } catch (Exception ex) {
081: ex.printStackTrace();
082: }
083: m_html = new JEditorPane("text/html", "<html></html>") {
084: public boolean getScrollableTracksViewportWidth() {
085: return true;
086: }
087: };
088: HTMLEditorKit hek = new HTMLEditorKit();
089: m_html.setEditorKit(hek);
090:
091: m_scroller = new JScrollPane(m_html);
092: setLayout(new BorderLayout());
093: m_html.setEditable(false);
094: add(m_scroller, BorderLayout.CENTER);
095: //add(m_html, BorderLayout.CENTER);
096: m_html.addHyperlinkListener(new Hyperactive());
097:
098: try {
099: m_launcher = new edu.stanford.ejalbert.BrowserLauncher();
100: } catch (Exception ex) {
101: ex.printStackTrace();
102: }
103:
104: }
105:
106: public java.awt.Dimension getPreferredSize() {
107: return new java.awt.Dimension(200, 200);
108: }
109:
110: public void setPage(URL url) {
111: try {
112: URL u = new URL(m_baseurl, url.toExternalForm());
113: m_html.setPage(u);
114: } catch (Exception ex) {
115: ex.printStackTrace();
116: }
117: }
118:
119: public void setText(String s) {
120: m_html.setContentType("text/html");
121: m_html.setText(s);
122: m_html.setCaretPosition(0);
123: }
124:
125: }
|