KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > HtmlDemo


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 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.text.*;
44 //import swingwtx.swing.text.html.*;
45
import swingwtx.swing.border.*;
46 import swingwtx.swing.colorchooser.*;
47 import swingwtx.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import swingwt.awt.*;
51 import swingwt.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 JavaDoc[] 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 JavaDoc path = null;
89         try {
90                 // Whilst this works just beautifully on Linux,
91
// IE for Windows does not support jar:file://
92
// hyperlinks, so we need to take this stuff
93
// outside the swingres.jar and hack things a bit
94
// to find it.
95
//
96
//path = "/resources/index.html";
97
//url = getClass().getResource(path);
98

99                 File file = new File("");
100                 String JavaDoc link = file.getAbsolutePath();
101                 System.out.println("Working with: " + link);
102                 
103                 // Kaffe and Classpath based projects return absolutePath() with
104
// a separator on the end.
105
if (link.endsWith(File.separator))
106                     link = link.substring(0, link.length() - 1);
107                 
108                 // Throw away the bin directory
109
link = link.substring(0, link.lastIndexOf(File.separator));
110                 
111                 // Path to the htmldemo in SwingWT/demo/swingset/htmldemo
112
link += File.separator + "demo" + File.separator + "swingset" +
113                         File.separator + "htmldemo" + File.separator + "index.html";
114                 String JavaDoc surl = "file://" + link;
115                 System.out.println("Using: " + surl);
116                 url = new URL(surl);
117                 
118             } catch (Exception JavaDoc e) {
119                 e.printStackTrace();
120         System.err.println("Failed to open " + path);
121         url = null;
122             }
123         
124             if(url != null) {
125                 html = new JEditorPane(url);
126                 html.setEditable(false);
127                 html.addHyperlinkListener(createHyperLinkListener());
128         
129         JScrollPane scroller = new JScrollPane();
130         JViewport vp = scroller.getViewport();
131         vp.add(html);
132                 getDemoPanel().add(scroller, BorderLayout.CENTER);
133             }
134         } catch (MalformedURLException e) {
135             System.out.println("Malformed URL: " + e);
136         } catch (IOException e) {
137             System.out.println("IOException: " + e);
138         }
139     }
140
141     public HyperlinkListener createHyperLinkListener() {
142     return new HyperlinkListener() {
143         public void hyperlinkUpdate(HyperlinkEvent e) {
144         if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
145                     try {
146                         html.setPage(e.getURL());
147                     } catch (IOException ioe) {
148                         System.out.println("IOE: " + ioe);
149                     }
150         }
151         }
152     };
153     }
154     
155 }
156
Popular Tags