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