KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MetalworksHelp


1 /*
2  * @(#)MetalworksHelp.java 1.15 05/11/17
3  *
4  * Copyright (c) 2006 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  * @(#)MetalworksHelp.java 1.15 05/11/17
39  */

40
41 import javax.swing.*;
42 import java.awt.*;
43 import java.net.URL JavaDoc;
44 import java.net.MalformedURLException JavaDoc;
45 import java.io.*;
46 import javax.swing.text.*;
47 import javax.swing.event.*;
48
49 /*
50  * @version 1.15 11/17/05
51  * @author Steve Wilson
52  */

53 public class MetalworksHelp extends JInternalFrame {
54   
55     public MetalworksHelp() {
56     super("Help", true, true, true, true);
57
58         setFrameIcon( (Icon)UIManager.get("Tree.openIcon")); // PENDING(steve) need more general palce to get this icon
59
setBounds( 200, 25, 400, 400);
60     HtmlPane html = new HtmlPane();
61     setContentPane(html);
62     }
63
64 }
65
66
67 class HtmlPane extends JScrollPane implements HyperlinkListener {
68     JEditorPane html;
69
70     public HtmlPane() {
71     try {
72         URL JavaDoc url = getClass().getResource("/resources/HelpFiles/toc.html");
73         html = new JEditorPane(url);
74         html.setEditable(false);
75         html.addHyperlinkListener(this);
76             html.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
77                                    Boolean.TRUE);
78         JViewport vp = getViewport();
79         vp.add(html);
80     } catch (MalformedURLException JavaDoc e) {
81         System.out.println("Malformed URL: " + e);
82     } catch (IOException e) {
83         System.out.println("IOException: " + e);
84     }
85     }
86
87     /**
88      * Notification of a change relative to a
89      * hyperlink.
90      */

91     public void hyperlinkUpdate(HyperlinkEvent e) {
92     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
93         linkActivated(e.getURL());
94     }
95     }
96
97     /**
98      * Follows the reference in an
99      * link. The given url is the requested reference.
100      * By default this calls <a HREF="#setPage">setPage</a>,
101      * and if an exception is thrown the original previous
102      * document is restored and a beep sounded. If an
103      * attempt was made to follow a link, but it represented
104      * a malformed url, this method will be called with a
105      * null argument.
106      *
107      * @param u the URL to follow
108      */

109     protected void linkActivated(URL JavaDoc u) {
110     Cursor c = html.getCursor();
111     Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
112     html.setCursor(waitCursor);
113     SwingUtilities.invokeLater(new PageLoader(u, c));
114     }
115
116     /**
117      * temporary class that loads synchronously (although
118      * later than the request so that a cursor change
119      * can be done).
120      */

121     class PageLoader implements Runnable JavaDoc {
122     
123     PageLoader(URL JavaDoc u, Cursor c) {
124         url = u;
125         cursor = c;
126     }
127
128         public void run() {
129         if (url == null) {
130         // restore the original cursor
131
html.setCursor(cursor);
132
133         // PENDING(prinz) remove this hack when
134
// automatic validation is activated.
135
Container parent = html.getParent();
136         parent.repaint();
137         } else {
138         Document doc = html.getDocument();
139         try {
140             html.setPage(url);
141         } catch (IOException ioe) {
142             html.setDocument(doc);
143             getToolkit().beep();
144         } finally {
145             // schedule the cursor to revert after
146
// the paint has happended.
147
url = null;
148             SwingUtilities.invokeLater(this);
149         }
150         }
151     }
152
153     URL JavaDoc url;
154     Cursor cursor;
155     }
156
157 }
158
Popular Tags