| J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* | 
|  | 2 | * Copyright 1998-2005 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 | *   - Redistributions in binary form must reproduce the above copyright | 
|  | 12 | *     notice, this list of conditions and the following disclaimer in the | 
|  | 13 | *     documentation and/or other materials provided with the distribution. | 
|  | 14 | * | 
|  | 15 | *   - Neither the name of Sun Microsystems nor the names of its | 
|  | 16 | *     contributors may be used to endorse or promote products derived | 
|  | 17 | *     from this software without specific prior written permission. | 
|  | 18 | * | 
|  | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | 
|  | 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 
|  | 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 
|  | 22 | * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR | 
|  | 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 
|  | 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 
|  | 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 
|  | 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 
|  | 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 
|  | 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
|  | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|  | 30 | */ | 
|  | 31 |  | 
|  | 32 | /* | 
|  | 33 | */ | 
|  | 34 |  | 
|  | 35 | import javax.swing.*; | 
|  | 36 | import java.awt.*; | 
|  | 37 | import java.net.URL; | 
|  | 38 | import java.net.MalformedURLException; | 
|  | 39 | import java.io.*; | 
|  | 40 | import javax.swing.text.*; | 
|  | 41 | import javax.swing.event.*; | 
|  | 42 |  | 
|  | 43 | /* | 
|  | 44 | * @author Steve Wilson | 
|  | 45 | */ | 
|  | 46 | public class MetalworksHelp extends JInternalFrame { | 
|  | 47 |  | 
|  | 48 | public MetalworksHelp() { | 
|  | 49 | super("Help", true, true, true, true); | 
|  | 50 |  | 
|  | 51 | setFrameIcon( (Icon)UIManager.get("Tree.openIcon")); // PENDING(steve) need more general palce to get this icon | 
|  | 52 | setBounds( 200, 25, 400, 400); | 
|  | 53 | HtmlPane html = new HtmlPane(); | 
|  | 54 | setContentPane(html); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 |  | 
|  | 60 | class HtmlPane extends JScrollPane implements HyperlinkListener { | 
|  | 61 | JEditorPane html; | 
|  | 62 |  | 
|  | 63 | public HtmlPane() { | 
|  | 64 | try { | 
|  | 65 | URL url = getClass().getResource("/resources/HelpFiles/toc.html"); | 
|  | 66 | html = new JEditorPane(url); | 
|  | 67 | html.setEditable(false); | 
|  | 68 | html.addHyperlinkListener(this); | 
|  | 69 | html.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, | 
|  | 70 | Boolean.TRUE); | 
|  | 71 | JViewport vp = getViewport(); | 
|  | 72 | vp.add(html); | 
|  | 73 | } catch (MalformedURLException e) { | 
|  | 74 | System.out.println("Malformed URL: " + e); | 
|  | 75 | } catch (IOException e) { | 
|  | 76 | System.out.println("IOException: " + e); | 
|  | 77 | } | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | /** | 
|  | 81 | * Notification of a change relative to a | 
|  | 82 | * hyperlink. | 
|  | 83 | */ | 
|  | 84 | public void hyperlinkUpdate(HyperlinkEvent e) { | 
|  | 85 | if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { | 
|  | 86 | linkActivated(e.getURL()); | 
|  | 87 | } | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | /** | 
|  | 91 | * Follows the reference in an | 
|  | 92 | * link.  The given url is the requested reference. | 
|  | 93 | * By default this calls <a href="#setPage">setPage</a>, | 
|  | 94 | * and if an exception is thrown the original previous | 
|  | 95 | * document is restored and a beep sounded.  If an | 
|  | 96 | * attempt was made to follow a link, but it represented | 
|  | 97 | * a malformed url, this method will be called with a | 
|  | 98 | * null argument. | 
|  | 99 | * | 
|  | 100 | * @param u the URL to follow | 
|  | 101 | */ | 
|  | 102 | protected void linkActivated(URL u) { | 
|  | 103 | Cursor c = html.getCursor(); | 
|  | 104 | Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); | 
|  | 105 | html.setCursor(waitCursor); | 
|  | 106 | SwingUtilities.invokeLater(new PageLoader(u, c)); | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | /** | 
|  | 110 | * temporary class that loads synchronously (although | 
|  | 111 | * later than the request so that a cursor change | 
|  | 112 | * can be done). | 
|  | 113 | */ | 
|  | 114 | class PageLoader implements Runnable { | 
|  | 115 |  | 
|  | 116 | PageLoader(URL u, Cursor c) { | 
|  | 117 | url = u; | 
|  | 118 | cursor = c; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | public void run() { | 
|  | 122 | if (url == null) { | 
|  | 123 | // restore the original cursor | 
|  | 124 | html.setCursor(cursor); | 
|  | 125 |  | 
|  | 126 | // PENDING(prinz) remove this hack when | 
|  | 127 | // automatic validation is activated. | 
|  | 128 | Container parent = html.getParent(); | 
|  | 129 | parent.repaint(); | 
|  | 130 | } else { | 
|  | 131 | Document doc = html.getDocument(); | 
|  | 132 | try { | 
|  | 133 | html.setPage(url); | 
|  | 134 | } catch (IOException ioe) { | 
|  | 135 | html.setDocument(doc); | 
|  | 136 | getToolkit().beep(); | 
|  | 137 | } finally { | 
|  | 138 | // schedule the cursor to revert after | 
|  | 139 | // the paint has happended. | 
|  | 140 | url = null; | 
|  | 141 | SwingUtilities.invokeLater(this); | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | URL url; | 
|  | 147 | Cursor cursor; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | } |