| HtmlDemo.java |
1 /*
2 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * -Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * -Redistribution in binary form must reproduct the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the distribution.
14 *
15 * Neither the name of Sun Microsystems, Inc. or the names of contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23 * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24 * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29 * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30 *
31 * You acknowledge that Software is not designed, licensed or intended for
32 * use in the design, construction, operation or maintenance of any nuclear
33 * facility.
34 */
35
36 /*
37 * @(#)HtmlDemo.java 1.8 03/01/23
38 */
39
40
41 import javax.swing.*;
42 import javax.swing.event.*;
43 import javax.swing.text.*;
44 import javax.swing.text.html.*;
45 import javax.swing.border.*;
46 import javax.swing.colorchooser.*;
47 import javax.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import java.awt.*;
51 import java.awt.event.*;
52 import java.beans.*;
53 import java.util.*;
54 import java.io.*;
55 import java.applet.*;
56 import java.net.*;
57
58 /**
59 * Html Demo
60 *
61 * @version 1.8 03/01/23
62 * @author Jeff Dinkins
63 */
64 public class HtmlDemo extends DemoModule {
65
66 JEditorPane html;
67
68 /**
69 * main method allows us to run as a standalone demo.
70 */
71 public static void main(String[] args) {
72 HtmlDemo demo = new HtmlDemo(null);
73 demo.mainImpl();
74 }
75
76 /**
77 * HtmlDemo Constructor
78 */
79 public HtmlDemo(SwingSet2 swingset) {
80 // Set the title for this demo, and an icon used to represent this
81 // demo inside the SwingSet2 app.
82 super(swingset, "HtmlDemo", "toolbar/JEditorPane.gif");
83
84 try {
85 URL url = null;
86 // System.getProperty("user.dir") +
87 // System.getProperty("file.separator");
88 String path = null;
89 try {
90 path = "/resources/index.html";
91 url = getClass().getResource(path);
92 } catch (Exception e) {
93 System.err.println("Failed to open " + path);
94 url = null;
95 }
96
97 if(url != null) {
98 html = new JEditorPane(url);
99 html.setEditable(false);
100 html.addHyperlinkListener(createHyperLinkListener());
101
102 JScrollPane scroller = new JScrollPane();
103 JViewport vp = scroller.getViewport();
104 vp.add(html);
105 getDemoPanel().add(scroller, BorderLayout.CENTER);
106 }
107 } catch (MalformedURLException e) {
108 System.out.println("Malformed URL: " + e);
109 } catch (IOException e) {
110 System.out.println("IOException: " + e);
111 }
112 }
113
114 public HyperlinkListener createHyperLinkListener() {
115 return new HyperlinkListener() {
116 public void hyperlinkUpdate(HyperlinkEvent e) {
117 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
118 if (e instanceof HTMLFrameHyperlinkEvent) {
119 ((HTMLDocument)html.getDocument()).processHTMLFrameHyperlinkEvent(
120 (HTMLFrameHyperlinkEvent)e);
121 } else {
122 try {
123 html.setPage(e.getURL());
124 } catch (IOException ioe) {
125 System.out.println("IOE: " + ioe);
126 }
127 }
128 }
129 }
130 };
131 }
132
133 }
134