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