Path: blob/master/src/demo/share/jfc/Metalworks/MetalworksHelp.java
41149 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import javax.swing.*;42import java.awt.*;43import java.net.URL;44import java.net.MalformedURLException;45import java.io.*;46import javax.swing.text.*;47import javax.swing.event.*;484950/**51* @author Steve Wilson52* @author Alexander Kouznetsov53*/54@SuppressWarnings("serial")55public class MetalworksHelp extends JInternalFrame {5657public MetalworksHelp() {58super("Help", true, true, true, true);5960setFrameIcon((Icon) UIManager.get("Tree.openIcon")); // PENDING(steve) need more general place to get this icon61setBounds(200, 25, 400, 400);62HtmlPane html = new HtmlPane();63setContentPane(html);64}65}666768@SuppressWarnings("serial")69class HtmlPane extends JScrollPane implements HyperlinkListener {7071JEditorPane html;7273@SuppressWarnings("LeakingThisInConstructor")74public HtmlPane() {75try {76URL url = getClass().getResource("/resources/HelpFiles/toc.html");77html = new JEditorPane(url);78html.setEditable(false);79html.addHyperlinkListener(this);80html.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,81Boolean.TRUE);82JViewport vp = getViewport();83vp.add(html);84} catch (MalformedURLException e) {85System.out.println("Malformed URL: " + e);86} catch (IOException e) {87System.out.println("IOException: " + e);88}89}9091/**92* Notification of a change relative to a93* hyperlink.94*/95public void hyperlinkUpdate(HyperlinkEvent e) {96if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {97linkActivated(e.getURL());98}99}100101/**102* Follows the reference in an103* link. The given url is the requested reference.104* By default this calls <a href="#setPage">setPage</a>,105* and if an exception is thrown the original previous106* document is restored and a beep sounded. If an107* attempt was made to follow a link, but it represented108* a malformed url, this method will be called with a109* null argument.110*111* @param u the URL to follow112*/113protected void linkActivated(URL u) {114Cursor c = html.getCursor();115Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);116html.setCursor(waitCursor);117SwingUtilities.invokeLater(new PageLoader(u, c));118}119120121/**122* temporary class that loads synchronously (although123* later than the request so that a cursor change124* can be done).125*/126class PageLoader implements Runnable {127128PageLoader(URL u, Cursor c) {129url = u;130cursor = c;131}132133public void run() {134if (url == null) {135// restore the original cursor136html.setCursor(cursor);137138// PENDING(prinz) remove this hack when139// automatic validation is activated.140Container parent = html.getParent();141parent.repaint();142} else {143Document doc = html.getDocument();144try {145html.setPage(url);146} catch (IOException ioe) {147html.setDocument(doc);148getToolkit().beep();149} finally {150// schedule the cursor to revert after151// the paint has happended.152url = null;153SwingUtilities.invokeLater(this);154}155}156}157URL url;158Cursor cursor;159}160}161162163