Source code: jpicedt/ui/util/HtmlViewer.java
1 /*
2 HtmlViewer.java - March 3, 2002 - jPicEdt 1.3.2, a picture editor for LaTeX.
3 Copyright (C) 1999-2002 Sylvain Reynal
4
5 Département de Physique
6 Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
7 6, avenue du Ponceau
8 F-95014 CERGY CEDEX
9
10 Tel : +33 130 736 245
11 Fax : +33 130 736 667
12 e-mail : reynal@ensea.fr
13 jPicEdt web page : http://www.jpicedt.org
14
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 2
18 of the License, or any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29 package jpicedt.ui.util;
30
31 import jpicedt.Localizer;
32
33 import javax.swing.*;
34 import java.awt.*;
35 import java.util.*;
36 import java.io.*;
37 import java.awt.event.*;
38 import javax.swing.event.*;
39 import javax.swing.text.html.HTMLDocument;
40 import javax.swing.text.html.HTMLFrameHyperlinkEvent;
41 import java.net.*;
42
43 /**
44 * A frame for displaying HTML content (e.g. on-line help, license,...)
45 *
46 * @author Sylvain Reynal.
47 * @since PicEdt 1.1.2
48 */
49 public class HtmlViewer extends JFrame implements HyperlinkListener{
50
51 private JEditorPane textPane;
52
53 /**
54 * Construct a new HTML Window with the given file and title
55 * @param fileName name of resource accessible from the current classloader
56 */
57 public HtmlViewer(String fileName, String title) {
58 this(HtmlViewer.class.getResource(fileName),title);
59 }
60 /**
61 * Construct a new HTML Window with the given file and title
62 */
63 public HtmlViewer(URL contentUrl, String title) {
64
65 super(title);
66 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
67 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
68
69 // install HTML page
70 textPane = new JEditorPane();
71 try {
72 textPane.setPage(contentUrl);
73 }
74 catch (Exception ex){
75 //t.printStackTrace();
76 JOptionPane.showMessageDialog(this,
77 Localizer.currentLocalizer().get("NetworkError"),
78 Localizer.currentLocalizer().get("PicEdtHelp"),
79 JOptionPane.ERROR_MESSAGE);
80 //textPane.setText(ex.toString());
81 dispose();
82 return;
83 }
84
85 textPane.setEditable(false);
86 textPane.addHyperlinkListener(this);
87
88 // sizing
89 JScrollPane scroller = new JScrollPane(textPane);
90 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
91 scroller.setPreferredSize(new Dimension(screenSize.width/2, screenSize.height/2));
92 this.getContentPane().add(scroller);
93
94 //pack();
95 setSize(getPreferredSize());
96 Dimension frameSize = getSize();
97 if (frameSize.height > screenSize.height)
98 frameSize.height = screenSize.height;
99 if (frameSize.width > screenSize.width)
100 frameSize.width = screenSize.width;
101 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
102 setVisible(true);
103 }
104
105 /**
106 * called when a click occurs on a HTML hyperlink (aka <a href=...>)
107 */
108 public void hyperlinkUpdate(HyperlinkEvent e) {
109
110 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
111
112 JEditorPane pane = (JEditorPane) e.getSource();
113 //System.out.println(e.getDescription());
114
115 // relative link :
116 if (e instanceof HTMLFrameHyperlinkEvent && !e.getDescription().startsWith("http://")) {
117 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
118 HTMLDocument doc = (HTMLDocument)pane.getDocument();
119 doc.processHTMLFrameHyperlinkEvent(evt);
120 }
121 else {
122 new HtmlViewer(e.getURL(),e.getURL().toString());
123 }
124 }
125 }
126
127 /** test */
128 public static void main(String args[]){
129 new HtmlViewer("/"+args[0],args[0]);
130 }
131 } // class
132
133
134