1/* 2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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 Oracle 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 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 42import javax.swing.*; 43import java.awt.*; 44import java.net.URL; 45import java.net.MalformedURLException; 46import java.io.*; 47import javax.swing.text.*; 48import javax.swing.event.*; 49 50 51/** 52 * @author Steve Wilson 53 * @author Alexander Kouznetsov 54 */ 55@SuppressWarnings("serial") 56public class MetalworksHelp extends JInternalFrame { 57 58 public MetalworksHelp() { 59 super("Help", true, true, true, true); 60 61 setFrameIcon((Icon) UIManager.get("Tree.openIcon")); // PENDING(steve) need more general place to get this icon 62 setBounds(200, 25, 400, 400); 63 HtmlPane html = new HtmlPane(); 64 setContentPane(html); 65 } 66} 67 68 69@SuppressWarnings("serial") 70class HtmlPane extends JScrollPane implements HyperlinkListener { 71 72 JEditorPane html; 73 74 @SuppressWarnings("LeakingThisInConstructor") 75 public HtmlPane() { 76 try { 77 URL url = getClass().getResource("/resources/HelpFiles/toc.html"); 78 html = new JEditorPane(url); 79 html.setEditable(false); 80 html.addHyperlinkListener(this); 81 html.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, 82 Boolean.TRUE); 83 JViewport vp = getViewport(); 84 vp.add(html); 85 } catch (MalformedURLException e) { 86 System.out.println("Malformed URL: " + e); 87 } catch (IOException e) { 88 System.out.println("IOException: " + e); 89 } 90 } 91 92 /** 93 * Notification of a change relative to a 94 * hyperlink. 95 */ 96 public void hyperlinkUpdate(HyperlinkEvent e) { 97 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 98 linkActivated(e.getURL()); 99 } 100 } 101 102 /** 103 * Follows the reference in an 104 * link. The given url is the requested reference. 105 * By default this calls <a href="#setPage">setPage</a>, 106 * and if an exception is thrown the original previous 107 * document is restored and a beep sounded. If an 108 * attempt was made to follow a link, but it represented 109 * a malformed url, this method will be called with a 110 * null argument. 111 * 112 * @param u the URL to follow 113 */ 114 protected void linkActivated(URL u) { 115 Cursor c = html.getCursor(); 116 Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); 117 html.setCursor(waitCursor); 118 SwingUtilities.invokeLater(new PageLoader(u, c)); 119 } 120 121 122 /** 123 * temporary class that loads synchronously (although 124 * later than the request so that a cursor change 125 * can be done). 126 */ 127 class PageLoader implements Runnable { 128 129 PageLoader(URL u, Cursor c) { 130 url = u; 131 cursor = c; 132 } 133 134 public void run() { 135 if (url == null) { 136 // restore the original cursor 137 html.setCursor(cursor); 138 139 // PENDING(prinz) remove this hack when 140 // automatic validation is activated. 141 Container parent = html.getParent(); 142 parent.repaint(); 143 } else { 144 Document doc = html.getDocument(); 145 try { 146 html.setPage(url); 147 } catch (IOException ioe) { 148 html.setDocument(doc); 149 getToolkit().beep(); 150 } finally { 151 // schedule the cursor to revert after 152 // the paint has happended. 153 url = null; 154 SwingUtilities.invokeLater(this); 155 } 156 } 157 } 158 URL url; 159 Cursor cursor; 160 } 161} 162