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