jdk/src/share/classes/javax/swing/JEditorPane.java
author rriggs
Tue, 03 Jun 2014 11:18:03 -0400
changeset 25122 1ecc464c69d2
parent 22574 7f8ce0c8c20a
child 25126 112171727d16
permissions -rw-r--r--
8044460: Cleanup new Boolean and single character strings Reviewed-by: pchelko, serb, rriggs Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22260
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
3737
83fb4621a129 6824395: Several Swing core components prevent using them in wrapper classes
alexp
parents: 1301
diff changeset
    27
import sun.swing.SwingUtilities2;
83fb4621a129 6824395: Several Swing core components prevent using them in wrapper classes
alexp
parents: 1301
diff changeset
    28
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.text.html.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A text component to edit various kinds of content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * You can find how-to information and examples of using editor panes in
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 9035
diff changeset
    46
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/text.html">Using Text Components</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This component uses implementations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>EditorKit</code> to accomplish its behavior. It effectively
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * morphs into the proper kind of text editor for the kind
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of content it is given.  The content type that editor is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * to at any given time is determined by the <code>EditorKit</code> currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * installed.  If the content is set to a new URL, its type is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * to determine the <code>EditorKit</code> that should be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * load the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * By default, the following types of content are known:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <dt><b>text/plain</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <dd>Plain text, which is the default the type given isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * recognized.  The kit used in this case is an extension of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>DefaultEditorKit</code> that produces a wrapped plain text view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <dt><b>text/html</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <dd>HTML text.  The kit used in this case is the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <code>javax.swing.text.html.HTMLEditorKit</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * which provides HTML 3.2 support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <dt><b>text/rtf</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <dd>RTF text.  The kit used in this case is the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <code>javax.swing.text.rtf.RTFEditorKit</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * which provides a limited support of the Rich Text Format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * There are several ways to load content into this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * The {@link #setText setText} method can be used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * the component from a string.  In this case the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <code>EditorKit</code> will be used, and the content type will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * expected to be of this type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * The {@link #read read} method can be used to initialize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * component from a <code>Reader</code>.  Note that if the content type is HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * relative references (e.g. for things like images) can't be resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * unless the &lt;base&gt; tag is used or the <em>Base</em> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * on <code>HTMLDocument</code> is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * In this case the current <code>EditorKit</code> will be used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * and the content type will be expected to be of this type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * The {@link #setPage setPage} method can be used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * the component from a URL.  In this case, the content type will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * determined from the URL, and the registered <code>EditorKit</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * for that content type will be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Some kinds of content may provide hyperlink support by generating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * hyperlink events.  The HTML <code>EditorKit</code> will generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * hyperlink events if the <code>JEditorPane</code> is <em>not editable</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * (<code>JEditorPane.setEditable(false);</code> has been called).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * If HTML frames are embedded in the document, the typical response would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * to change a portion of the current document.  The following code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * fragment is a possible hyperlink listener implementation, that treats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * HTML frame events specially, and simply displays any other activated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * hyperlinks.
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 20458
diff changeset
   106
 * <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
&nbsp;    class Hyperactive implements HyperlinkListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
&nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
&nbsp;        public void hyperlinkUpdate(HyperlinkEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
&nbsp;            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
&nbsp;                JEditorPane pane = (JEditorPane) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
&nbsp;                if (e instanceof HTMLFrameHyperlinkEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
&nbsp;                    HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
&nbsp;                    HTMLDocument doc = (HTMLDocument)pane.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
&nbsp;                    doc.processHTMLFrameHyperlinkEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
&nbsp;                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
&nbsp;                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
&nbsp;                        pane.setPage(e.getURL());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
&nbsp;                    } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
&nbsp;                        t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
&nbsp;                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
&nbsp;                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
&nbsp;            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
&nbsp;        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
&nbsp;    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 20458
diff changeset
   128
 * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * For information on customizing how <b>text/html</b> is rendered please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * {@link #W3C_LENGTH_UNITS} and {@link #HONOR_DISPLAY_PROPERTIES}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * Culturally dependent information in some documents is handled through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * a mechanism called character encoding.  Character encoding is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * unambiguous mapping of the members of a character set (letters, ideographs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * digits, symbols, or control functions) to specific numeric code values. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * represents the way the file is stored. Example character encodings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * ISO-8859-1, ISO-8859-5, Shift-jis, Euc-jp, and UTF-8. When the file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * passed to an user agent (<code>JEditorPane</code>) it is converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * the document character set (ISO-10646 aka Unicode).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * There are multiple ways to get a character set mapping to happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * with <code>JEditorPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * One way is to specify the character set as a parameter of the MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * type.  This will be established by a call to the
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 7668
diff changeset
   148
 * {@link #setContentType setContentType} method.  If the content
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 7668
diff changeset
   149
 * is loaded by the {@link #setPage setPage} method the content
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * type will have been set according to the specification of the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * It the file is loaded directly, the content type would be expected to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * have been set prior to loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * Another way the character set can be specified is in the document itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * This requires reading the document prior to determining the character set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * that is desired.  To handle this, it is expected that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <code>EditorKit</code>.read operation throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <code>ChangedCharSetException</code> which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * be caught.  The read is then restarted with a new Reader that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * the character set specified in the <code>ChangedCharSetException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * (which is an <code>IOException</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * </ol>
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21592
diff changeset
   163
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <dl>
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21592
diff changeset
   165
 * <dt><b>Newlines</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * For a discussion on how newlines are handled, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <a href="text/DefaultEditorKit.html">DefaultEditorKit</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   182
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *   attribute: isContainer false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * description: A text component to edit various types of content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22260
diff changeset
   192
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
public class JEditorPane extends JTextComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Creates a new <code>JEditorPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * The document model is set to <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public JEditorPane() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        setFocusCycleRoot(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                public Component getComponentAfter(Container focusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                                   Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    if (focusCycleRoot != JEditorPane.this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        (!isEditable() && getComponentCount() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        return super.getComponentAfter(focusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                                       aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        Container rootAncestor = getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        return (rootAncestor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            ? rootAncestor.getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                  getComponentAfter(rootAncestor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                                    JEditorPane.this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                public Component getComponentBefore(Container focusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                                    Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    if (focusCycleRoot != JEditorPane.this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        (!isEditable() && getComponentCount() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        return super.getComponentBefore(focusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                                        aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        Container rootAncestor = getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        return (rootAncestor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            ? rootAncestor.getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                  getComponentBefore(rootAncestor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                                     JEditorPane.this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                public Component getDefaultComponent(Container focusCycleRoot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    return (focusCycleRoot != JEditorPane.this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                            (!isEditable() && getComponentCount() > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        ? super.getDefaultComponent(focusCycleRoot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                protected boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    return (aComponent != JEditorPane.this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        ? super.accept(aComponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        LookAndFeel.installProperty(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                    "focusTraversalKeysForward",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                    JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                    getManagingFocusForwardTraversalKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        LookAndFeel.installProperty(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                    "focusTraversalKeysBackward",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                    JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                    getManagingFocusBackwardTraversalKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Creates a <code>JEditorPane</code> based on a specified URL for input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param initialPage the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @exception IOException if the URL is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *          or cannot be accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public JEditorPane(URL initialPage) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        setPage(initialPage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Creates a <code>JEditorPane</code> based on a string containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * a URL specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param url the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @exception IOException if the URL is <code>null</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *          cannot be accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public JEditorPane(String url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        setPage(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Creates a <code>JEditorPane</code> that has been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * to the given text.  This is a convenience constructor that calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <code>setContentType</code> and <code>setText</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param type mime type of the given text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param text the text to initialize with; may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @exception NullPointerException if the <code>type</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *          is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public JEditorPane(String type, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        setContentType(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Adds a hyperlink listener for notification of any changes, for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * when a link is selected and entered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param listener the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public synchronized void addHyperlinkListener(HyperlinkListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        listenerList.add(HyperlinkListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Removes a hyperlink listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param listener the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public synchronized void removeHyperlinkListener(HyperlinkListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        listenerList.remove(HyperlinkListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Returns an array of all the <code>HyperLinkListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * to this JEditorPane with addHyperlinkListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return all of the <code>HyperLinkListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public synchronized HyperlinkListener[] getHyperlinkListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   325
        return listenerList.getListeners(javax.swing.event.HyperlinkListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * notification on this event type.  This is normally called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * by the currently installed <code>EditorKit</code> if a content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * that supports hyperlinks is currently active and there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * was activity with a link.  The listener list is processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * last to first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public void fireHyperlinkUpdate(HyperlinkEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (listeners[i]==HyperlinkListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                ((HyperlinkListener)listeners[i+1]).hyperlinkUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Sets the current URL being displayed.  The content type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * pane is set, and if the editor kit for the pane is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * non-<code>null</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * a new default document is created and the URL is read into it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * If the URL contains and reference location, the location will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * be scrolled to by calling the <code>scrollToReference</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * method. If the desired URL is the one currently being displayed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * the document will not be reloaded. To force a document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * reload it is necessary to clear the stream description property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * of the document. The following code shows how this can be done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *   Document doc = jEditorPane.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *   doc.putProperty(Document.StreamDescriptionProperty, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * If the desired URL is not the one currently being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * displayed, the <code>getStream</code> method is called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * give subclasses control over the stream provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * This may load either synchronously or asynchronously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * depending upon the document returned by the <code>EditorKit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * If the <code>Document</code> is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <code>AbstractDocument</code> and has a value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <code>AbstractDocument.getAsynchronousLoadPriority</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * that is greater than or equal to zero, the page will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * loaded on a separate thread using that priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * If the document is loaded synchronously, it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * filled in with the stream prior to being installed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * the editor with a call to <code>setDocument</code>, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * is bound and will fire a property change event.  If an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <code>IOException</code> is thrown the partially loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * document will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * be discarded and neither the document or page property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * change events will be fired.  If the document is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * successfully loaded and installed, a view will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * built for it by the UI which will then be scrolled if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * necessary, and then the page property change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * will be fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * If the document is loaded asynchronously, the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * will be installed into the editor immediately using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * call to <code>setDocument</code> which will fire a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * document property change event, then a thread will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * created which will begin doing the actual loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * In this case, the page property change event will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * fired by the call to this method directly, but rather will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * fired when the thread doing the loading has finished.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * It will also be fired on the event-dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Since the calling thread can not throw an <code>IOException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * in the event of failure on the other thread, the page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * property change event will be fired when the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * thread is done whether the load was successful or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param page the URL of the page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @exception IOException for a <code>null</code> or invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *          page specification, or exception from the stream being read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see #getPage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *  description: the URL used to set content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public void setPage(URL page) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (page == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            throw new IOException("invalid url");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        URL loaded = getPage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // reset scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (!page.equals(loaded) && page.getRef() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            scrollRectToVisible(new Rectangle(0,0,1,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        boolean reloaded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        Object postData = getPostData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if ((loaded == null) || !loaded.sameFile(page) || (postData != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // different url or POST method, load the new content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            int p = getAsynchronousLoadPriority(getDocument());
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   434
            if (p < 0) {
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   435
                // open stream synchronously
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                InputStream in = getStream(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (kit != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    Document doc = initializeModel(kit, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    // At this point, one could either load up the model with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    // view notifications slowing it down (i.e. best synchronous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    // behavior) or set the model and start to feed it on a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    // thread (best asynchronous behavior).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    p = getAsynchronousLoadPriority(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    if (p >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        // load asynchronously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        setDocument(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        synchronized(this) {
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   449
                            pageLoader = new PageLoader(doc, in, loaded, page);
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   450
                            pageLoader.execute();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    read(in, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    setDocument(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    reloaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            } else {
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   459
                // we may need to cancel background loading
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   460
                if (pageLoader != null) {
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   461
                    pageLoader.cancel(true);
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   462
                }
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   463
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   464
                // Do everything in a background thread.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                // Model initialization is deferred to that thread, too.
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   466
                pageLoader = new PageLoader(null, null, loaded, page);
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   467
                pageLoader.execute();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        final String reference = page.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (reference != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (!reloaded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                scrollToReference(reference);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                // Have to scroll after painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        scrollToReference(reference);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            getDocument().putProperty(Document.StreamDescriptionProperty, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        firePropertyChange("page", loaded, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Create model and initialize document properties from page properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    private Document initializeModel(EditorKit kit, URL page) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        Document doc = kit.createDefaultDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (pageProperties != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            // transfer properties discovered in stream to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            // document property collection.
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   497
            for (Enumeration<String> e = pageProperties.keys(); e.hasMoreElements() ;) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   498
                String key = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                doc.putProperty(key, pageProperties.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            pageProperties.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (doc.getProperty(Document.StreamDescriptionProperty) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            doc.putProperty(Document.StreamDescriptionProperty, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Return load priority for the document or -1 if priority not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    private int getAsynchronousLoadPriority(Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        return (doc instanceof AbstractDocument ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            ((AbstractDocument) doc).getAsynchronousLoadPriority() : -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * This method initializes from a stream.  If the kit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * set to be of type <code>HTMLEditorKit</code>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * <code>desc</code> parameter is an <code>HTMLDocument</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * then it invokes the <code>HTMLEditorKit</code> to initiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * the read. Otherwise it calls the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * method which loads the model as plain text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @param in the stream from which to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param desc an object describing the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @exception IOException as thrown by the stream being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *          used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @see JTextComponent#read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see #setDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public void read(InputStream in, Object desc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (desc instanceof HTMLDocument &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            kit instanceof HTMLEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            HTMLDocument hdoc = (HTMLDocument) desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            setDocument(hdoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            read(in, hdoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            String charset = (String) getClientProperty("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            Reader r = (charset != null) ? new InputStreamReader(in, charset) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                new InputStreamReader(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            super.read(r, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * This method invokes the <code>EditorKit</code> to initiate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * read.  In the case where a <code>ChangedCharSetException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * is thrown this exception will contain the new CharSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Therefore the <code>read</code> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * is then restarted after building a new Reader with the new charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @param in the inputstream to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @param doc the document to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    void read(InputStream in, Document doc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (! Boolean.TRUE.equals(doc.getProperty("IgnoreCharsetDirective"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            final int READ_LIMIT = 1024 * 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            in = new BufferedInputStream(in, READ_LIMIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            in.mark(READ_LIMIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            String charset = (String) getClientProperty("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            Reader r = (charset != null) ? new InputStreamReader(in, charset) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                new InputStreamReader(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            kit.read(r, doc, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        } catch (ChangedCharSetException changedCharSetException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            String charSetSpec = changedCharSetException.getCharSetSpec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (changedCharSetException.keyEqualsCharSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                putClientProperty("charset", charSetSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                setCharsetFromContentTypeParameters(charSetSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                in.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } catch (IOException exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                //mark was invalidated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                URL url = (URL)doc.getProperty(Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    URLConnection conn = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    in = conn.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    //there is nothing we can do to recover stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    throw changedCharSetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                doc.remove(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            } catch (BadLocationException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            doc.putProperty("IgnoreCharsetDirective", Boolean.valueOf(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            read(in, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /**
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   603
     * Loads a stream into the text document model.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   605
    class PageLoader extends SwingWorker<URL, Object> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * Construct an asynchronous page loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         */
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   610
        PageLoader(Document doc, InputStream in, URL old, URL page) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            this.old = old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            this.page = page;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            this.doc = doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * Try to load the document, then scroll the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         * to the reference (if specified).  When done, fire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         * a page property change event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         */
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   622
        protected URL doInBackground() {
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   623
            boolean pageLoaded = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    in = getStream(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    if (kit == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        // We received document of unknown content type.
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   629
                        UIManager.getLookAndFeel().
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   630
                                provideErrorFeedback(JEditorPane.this);
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   631
                        return old;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   634
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                if (doc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        SwingUtilities.invokeAndWait(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                                doc = initializeModel(kit, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                setDocument(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    } catch (InvocationTargetException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        UIManager.getLookAndFeel().provideErrorFeedback(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                                            JEditorPane.this);
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   646
                        return old;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        UIManager.getLookAndFeel().provideErrorFeedback(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                                            JEditorPane.this);
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   650
                        return old;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                read(in, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                URL page = (URL) doc.getProperty(Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                String reference = page.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                if (reference != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    // scroll the page if necessary, but do it on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    // event thread... that is the only guarantee that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    // modelToView can be safely called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    Runnable callScrollToReference = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                            URL u = (URL) getDocument().getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                (Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                            String ref = u.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                            scrollToReference(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    SwingUtilities.invokeLater(callScrollToReference);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                pageLoaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            } finally {
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   675
                if (pageLoaded) {
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   676
                    SwingUtilities.invokeLater(new Runnable() {
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   677
                        public void run() {
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   678
                            JEditorPane.this.firePropertyChange("page", old, page);
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   679
                        }
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   680
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                }
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
   682
                return (pageLoaded ? page : old);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
         * The stream to load the document with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * URL of the old page that was replaced (for the property change event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        URL old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         * URL of the page being loaded (for the property change event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        URL page;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         * The Document instance to load into. This is cached in case a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         * new Document is created between the time the thread this is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         * and run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        Document doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Fetches a stream for the given URL, which is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * be loaded by the <code>setPage</code> method.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * default, this simply opens the URL and returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * stream.  This can be reimplemented to do useful things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * like fetch the stream from a cache, monitor the progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * of the stream, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * This method is expected to have the the side effect of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * establishing the content type, and therefore setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * appropriate <code>EditorKit</code> to use for loading the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * If this the stream was an http connection, redirects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * will be followed and the resulting URL will be set as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * the <code>Document.StreamDescriptionProperty</code> so that relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * URL's can be properly resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @param page  the URL of the page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    protected InputStream getStream(URL page) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        final URLConnection conn = page.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (conn instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            HttpURLConnection hconn = (HttpURLConnection) conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            hconn.setInstanceFollowRedirects(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            Object postData = getPostData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (postData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                handlePostData(hconn, postData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            int response = hconn.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            boolean redirect = (response >= 300 && response <= 399);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
             * In the case of a redirect, we want to actually change the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
             * that was input to the new, redirected URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            if (redirect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                String loc = conn.getHeaderField("Location");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                if (loc.startsWith("http", 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    page = new URL(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    page = new URL(page, loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                return getStream(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // Connection properties handler should be forced to run on EDT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // as it instantiates the EditorKit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            handleConnectionProperties(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                SwingUtilities.invokeAndWait(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                        handleConnectionProperties(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return conn.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Handle URL connection properties (most notably, content type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    private void handleConnectionProperties(URLConnection conn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (pageProperties == null) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   780
            pageProperties = new Hashtable<String, Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        String type = conn.getContentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (type != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            setContentType(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            pageProperties.put("content-type", type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        pageProperties.put(Document.StreamDescriptionProperty, conn.getURL());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        String enc = conn.getContentEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (enc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            pageProperties.put("content-encoding", enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    private Object getPostData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        return getDocument().getProperty(PostDataProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    private void handlePostData(HttpURLConnection conn, Object postData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                                            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        conn.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        DataOutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            conn.setRequestProperty("Content-Type",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    "application/x-www-form-urlencoded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            os = new DataOutputStream(conn.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            os.writeBytes((String) postData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            if (os != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Scrolls the view to the given reference location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * (that is, the value returned by the <code>UL.getRef</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * method for the URL being displayed).  By default, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * method only knows how to locate a reference in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * HTMLDocument.  The implementation calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * <code>scrollRectToVisible</code> method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * accomplish the actual scrolling.  If scrolling to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * reference location is needed for document types other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * than HTML, this method should be reimplemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * This method will have no effect if the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * is not visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @param reference the named location to scroll to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    public void scrollToReference(String reference) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        Document d = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (d instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            HTMLDocument doc = (HTMLDocument) d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            HTMLDocument.Iterator iter = doc.getIterator(HTML.Tag.A);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            for (; iter.isValid(); iter.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                AttributeSet a = iter.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                String nm = (String) a.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                if ((nm != null) && nm.equals(reference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    // found a matching reference in the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                        int pos = iter.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                        Rectangle r = modelToView(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        if (r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                            // the view is visible, scroll it to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                            // center of the current visible area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                            Rectangle vis = getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                            //r.y -= (vis.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                            r.height = vis.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                            scrollRectToVisible(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                            setCaretPosition(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * Gets the current URL being displayed.  If a URL was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * not specified in the creation of the document, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * will return <code>null</code>, and relative URL's will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @return the URL, or <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    public URL getPage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        return (URL) getDocument().getProperty(Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Sets the current URL being displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param url the URL for display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @exception IOException for a <code>null</code> or invalid URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *          specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    public void setPage(String url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            throw new IOException("invalid url");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        URL page = new URL(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        setPage(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * Gets the class ID for the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @return the string "EditorPaneUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Creates the default editor kit (<code>PlainEditorKit</code>) for when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * the component is first created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @return the editor kit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    protected EditorKit createDefaultEditorKit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return new PlainEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Fetches the currently installed kit for handling content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <code>createDefaultEditorKit</code> is called to set up a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @return the editor kit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    public EditorKit getEditorKit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (kit == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            kit = createDefaultEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            isUserSetEditorKit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        return kit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Gets the type of content that this editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * is currently set to deal with.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * defined to be the type associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * currently installed <code>EditorKit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @return the content type, <code>null</code> if no editor kit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public final String getContentType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return (kit != null) ? kit.getContentType() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * Sets the type of content that this editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * handles.  This calls <code>getEditorKitForContentType</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * and then <code>setEditorKit</code> if an editor kit can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * be successfully located.  This is mostly convenience method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * that can be used as an alternative to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * <code>setEditorKit</code> directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * If there is a charset definition specified as a parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * of the content type specification, it will be used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * loading input streams using the associated <code>EditorKit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * For example if the type is specified as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * <code>text/html; charset=EUC-JP</code> the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * will be loaded using the <code>EditorKit</code> registered for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * <code>text/html</code> and the Reader provided to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * the <code>EditorKit</code> to load unicode into the document will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * use the <code>EUC-JP</code> charset for translating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * to unicode.  If the type is not recognized, the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * will be loaded using the <code>EditorKit</code> registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * for plain text, <code>text/plain</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @param type the non-<code>null</code> mime type for the content editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *   support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * @see #getContentType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *  description: the type of content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * @throws NullPointerException if the <code>type</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *          is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    public final void setContentType(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        // The type could have optional info is part of it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        // for example some charset info.  We need to strip that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        // of and save it.
25122
1ecc464c69d2 8044460: Cleanup new Boolean and single character strings
rriggs
parents: 22574
diff changeset
   968
        int parm = type.indexOf(';');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        if (parm > -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            // Save the paramList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            String paramList = type.substring(parm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            // update the content type string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            type = type.substring(0, parm).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            if (type.toLowerCase().startsWith("text/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                setCharsetFromContentTypeParameters(paramList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        if ((kit == null) || (! type.equals(kit.getContentType()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                || !isUserSetEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            EditorKit k = getEditorKitForContentType(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            if (k != null && k != kit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                setEditorKit(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                isUserSetEditorKit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * This method gets the charset information specified as part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * of the content type in the http header information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    private void setCharsetFromContentTypeParameters(String paramlist) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   994
        String charset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            // paramlist is handed to us with a leading ';', strip it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            int semi = paramlist.indexOf(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            if (semi > -1 && semi < paramlist.length()-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                paramlist = paramlist.substring(semi + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            if (paramlist.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                // parse the paramlist into attr-value pairs & get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                // charset pair's value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                HeaderParser hdrParser = new HeaderParser(paramlist);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                charset = hdrParser.findValue("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                if (charset != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    putClientProperty("charset", charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            // malformed parameter list, use charset we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            // malformed parameter list, use charset we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            // malformed parameter list, use charset we have; but complain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            System.err.println("JEditorPane.getCharsetFromContentTypeParameters failed on: " + paramlist);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Sets the currently installed kit for handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * content.  This is the bound property that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * establishes the content type of the editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * Any old kit is first deinstalled, then if kit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * non-<code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * the new kit is installed, and a default document created for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * A <code>PropertyChange</code> event ("editorKit") is always fired when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * <code>setEditorKit</code> is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * <em>NOTE: This has the side effect of changing the model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * because the <code>EditorKit</code> is the source of how a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * particular type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * of content is modeled.  This method will cause <code>setDocument</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * to be called on behalf of the caller to ensure integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * of the internal state.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @param kit the desired editor behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @see #getEditorKit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *  description: the currently installed kit for handling content
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    public void setEditorKit(EditorKit kit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        EditorKit old = this.kit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        isUserSetEditorKit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            old.deinstall(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        this.kit = kit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        if (this.kit != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            this.kit.install(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            setDocument(this.kit.createDefaultDocument());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        firePropertyChange("editorKit", old, kit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * Fetches the editor kit to use for the given type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * of content.  This is called when a type is requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * that doesn't match the currently installed type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * If the component doesn't have an <code>EditorKit</code> registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * for the given type, it will try to create an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * <code>EditorKit</code> from the default <code>EditorKit</code> registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * If that fails, a <code>PlainEditorKit</code> is used on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * assumption that all text documents can be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * as plain text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * This method can be reimplemented to use some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * other kind of type registry.  This can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * be reimplemented to use the Java Activation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Framework, for example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @param type the non-<code>null</code> content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @return the editor kit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    public EditorKit getEditorKitForContentType(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        if (typeHandlers == null) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1085
            typeHandlers = new Hashtable<String, EditorKit>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1087
        EditorKit k = typeHandlers.get(type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            k = createEditorKitForContentType(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            if (k != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                setEditorKitForContentType(type, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            k = createDefaultEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * Directly sets the editor kit to use for the given type.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * look-and-feel implementation might use this in conjunction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * with <code>createEditorKitForContentType</code> to install handlers for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * content types with a look-and-feel bias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @param type the non-<code>null</code> content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @param k the editor kit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    public void setEditorKitForContentType(String type, EditorKit k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if (typeHandlers == null) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1111
            typeHandlers = new Hashtable<String, EditorKit>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        typeHandlers.put(type, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * Replaces the currently selected content with new content
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * represented by the given string.  If there is no selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * this amounts to an insert of the given text.  If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * is no replacement text (i.e. the content string is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * or <code>null</code>) this amounts to a removal of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * current selection.  The replacement text will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * attributes currently defined for input.  If the component is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * editable, beep and return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @param content  the content to replace the selection with.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *   value can be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
3750
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  1129
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    public void replaceSelection(String content) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        if (! isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        EditorKit kit = getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        if(kit instanceof StyledEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                Caret caret = getCaret();
3750
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  1140
                boolean composedTextSaved = saveComposedText(caret.getDot());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                int p0 = Math.min(caret.getDot(), caret.getMark());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                int p1 = Math.max(caret.getDot(), caret.getMark());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    ((AbstractDocument)doc).replace(p0, p1 - p0, content,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                              ((StyledEditorKit)kit).getInputAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                    if (p0 != p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                        doc.remove(p0, p1 - p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    if (content != null && content.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                        doc.insertString(p0, content, ((StyledEditorKit)kit).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                                         getInputAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                }
3750
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  1156
                if (composedTextSaved) {
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  1157
                    restoreComposedText();
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  1158
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            super.replaceSelection(content);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Creates a handler for the given type from the default registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * of editor kits.  The registry is created if necessary.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * registered class has not yet been loaded, an attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * is made to dynamically load the prototype of the kit for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * given type.  If the type was registered with a <code>ClassLoader</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * that <code>ClassLoader</code> will be used to load the prototype.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * If there was no registered <code>ClassLoader</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * <code>Class.forName</code> will be used to load the prototype.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * Once a prototype <code>EditorKit</code> instance is successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * located, it is cloned and the clone is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @param type the content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @return the editor kit, or <code>null</code> if there is nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *   registered for the given type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    public static EditorKit createEditorKitForContentType(String type) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1186
        Hashtable<String, EditorKit> kitRegistry = getKitRegisty();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1187
        EditorKit k = kitRegistry.get(type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            // try to dynamically load the support
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1190
            String classname = getKitTypeRegistry().get(type);
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1191
            ClassLoader loader = getKitLoaderRegistry().get(type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                Class c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    c = loader.loadClass(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    // Will only happen if developer has invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    // registerEditorKitForContentType(type, class, null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    c = Class.forName(classname, true, Thread.currentThread().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                                      getContextClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                k = (EditorKit) c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                kitRegistry.put(type, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                k = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        // create a copy of the prototype or null if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        // is no prototype.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        if (k != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            return (EditorKit) k.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * Establishes the default bindings of <code>type</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * <code>classname</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * The class will be dynamically loaded later when actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * needed, and can be safely changed before attempted uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * to avoid loading unwanted classes.  The prototype
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * <code>EditorKit</code> will be loaded with <code>Class.forName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * when registered with this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @param type the non-<code>null</code> content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * @param classname the class to load later
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    public static void registerEditorKitForContentType(String type, String classname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        registerEditorKitForContentType(type, classname,Thread.currentThread().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                                        getContextClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * Establishes the default bindings of <code>type</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * <code>classname</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * The class will be dynamically loaded later when actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * needed using the given <code>ClassLoader</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * and can be safely changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * before attempted uses to avoid loading unwanted classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @param type the non-<code>null</code> content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * @param classname the class to load later
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * @param loader the <code>ClassLoader</code> to use to load the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    public static void registerEditorKitForContentType(String type, String classname, ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        getKitTypeRegistry().put(type, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        getKitLoaderRegistry().put(type, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        getKitRegisty().remove(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * Returns the currently registered <code>EditorKit</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * class name for the type <code>type</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @param type  the non-<code>null</code> content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    public static String getEditorKitClassNameForContentType(String type) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1261
        return getKitTypeRegistry().get(type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1264
    private static Hashtable<String, String> getKitTypeRegistry() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        loadDefaultKitsIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        return (Hashtable)SwingUtilities.appContextGet(kitTypeRegistryKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1269
    private static Hashtable<String, ClassLoader> getKitLoaderRegistry() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        loadDefaultKitsIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        return (Hashtable)SwingUtilities.appContextGet(kitLoaderRegistryKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1274
    private static Hashtable<String, EditorKit> getKitRegisty() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        Hashtable ht = (Hashtable)SwingUtilities.appContextGet(kitRegistryKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        if (ht == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            ht = new Hashtable(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            SwingUtilities.appContextPut(kitRegistryKey, ht);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        return ht;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * This is invoked every time the registries are accessed. Loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * is done this way instead of via a static as the static is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * called once when running in plugin resulting in the entries only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * appearing in the first applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    private static void loadDefaultKitsIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        if (SwingUtilities.appContextGet(kitTypeRegistryKey) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            synchronized(defaultEditorKitMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                if (defaultEditorKitMap.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                    defaultEditorKitMap.put("text/plain",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                                            "javax.swing.JEditorPane$PlainEditorKit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    defaultEditorKitMap.put("text/html",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                                            "javax.swing.text.html.HTMLEditorKit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                    defaultEditorKitMap.put("text/rtf",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                                            "javax.swing.text.rtf.RTFEditorKit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                    defaultEditorKitMap.put("application/rtf",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                                            "javax.swing.text.rtf.RTFEditorKit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            Hashtable ht = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            SwingUtilities.appContextPut(kitTypeRegistryKey, ht);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            ht = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            SwingUtilities.appContextPut(kitLoaderRegistryKey, ht);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            for (String key : defaultEditorKitMap.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                registerEditorKitForContentType(key,defaultEditorKitMap.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    // --- java.awt.Component methods --------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * Returns the preferred size for the <code>JEditorPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * The preferred size for <code>JEditorPane</code> is slightly altered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * from the preferred size of the superclass.  If the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * of the viewport has become smaller than the minimum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * of the component, the scrollable definition for tracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * width or height will turn to false.  The default viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * layout will give the preferred size, and that is not desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * in the case where the scrollable is tracking.  In that case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * the <em>normal</em> preferred size is adjusted to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * minimum size.  This allows things like HTML tables to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * shrink down to their minimum size and then be laid out at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * their minimum size, refusing to shrink any further.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * @return a <code>Dimension</code> containing the preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        Dimension d = super.getPreferredSize();
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1334
        Container parent = SwingUtilities.getUnwrappedParent(this);
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1335
        if (parent instanceof JViewport) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1336
            JViewport port = (JViewport) parent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            TextUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            int prefWidth = d.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            int prefHeight = d.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            if (! getScrollableTracksViewportWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                int w = port.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                Dimension min = ui.getMinimumSize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                if (w != 0 && w < min.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                    // Only adjust to min if we have a valid size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    prefWidth = min.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            if (! getScrollableTracksViewportHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                int h = port.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                Dimension min = ui.getMinimumSize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                if (h != 0 && h < min.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    // Only adjust to min if we have a valid size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    prefHeight = min.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            if (prefWidth != d.width || prefHeight != d.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                d = new Dimension(prefWidth, prefHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    // --- JTextComponent methods -----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * Sets the text of this <code>TextComponent</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * content,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * which is expected to be in the format of the content type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * this editor.  For example, if the type is set to <code>text/html</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * the string should be specified in terms of HTML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * This is implemented to remove the contents of the current document,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * and replace them by parsing the given string using the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * <code>EditorKit</code>.  This gives the semantics of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * superclass by not changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * out the model, while supporting the content type currently set on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * this component.  The assumption is that the previous content is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * small, and that the previous content doesn't have side effects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * Both of those assumptions can be violated and cause undesirable results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * To avoid this, create a new document,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * <code>getEditorKit().createDefaultDocument()</code>, and replace the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * existing <code>Document</code> with the new one. You are then assured the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * previous <code>Document</code> won't have any lingering state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * Leaving the existing model in place means that the old view will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * torn down, and a new view created, where replacing the document would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * avoid the tear down of the old view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * Some formats (such as HTML) can install things into the document that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * can influence future contents.  HTML can have style information embedded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * that would influence the next content installed unexpectedly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * An alternative way to load this component with a string would be to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * create a StringReader and call the read method.  In this case the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * would be replaced after it was initialized with the contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * @param t the new text to be set; if <code>null</code> the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *    text will be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @see #getText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * description: the text of this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    public void setText(String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            doc.remove(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            if (t == null || t.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            Reader r = new StringReader(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            EditorKit kit = getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            kit.read(r, doc, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * Returns the text contained in this <code>TextComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * in terms of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * content type of this editor.  If an exception is thrown while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * attempting to retrieve the text, <code>null</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * This is implemented to call <code>JTextComponent.write</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * a <code>StringWriter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * @see #setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    public String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        String txt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            StringWriter buf = new StringWriter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            txt = buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            txt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        return txt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    // --- Scrollable  ----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * Returns true if a viewport should always force the width of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * <code>Scrollable</code> to match the width of the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @return true if a viewport should force the Scrollables width to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * match its own, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    public boolean getScrollableTracksViewportWidth() {
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1457
        Container parent = SwingUtilities.getUnwrappedParent(this);
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1458
        if (parent instanceof JViewport) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1459
            JViewport port = (JViewport) parent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            TextUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            int w = port.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            Dimension min = ui.getMinimumSize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            Dimension max = ui.getMaximumSize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            if ((w >= min.width) && (w <= max.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * Returns true if a viewport should always force the height of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * <code>Scrollable</code> to match the height of the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * @return true if a viewport should force the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     *          <code>Scrollable</code>'s height to match its own,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *          false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    public boolean getScrollableTracksViewportHeight() {
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1480
        Container parent = SwingUtilities.getUnwrappedParent(this);
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1481
        if (parent instanceof JViewport) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  1482
            JViewport port = (JViewport) parent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            TextUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            int h = port.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            Dimension min = ui.getMinimumSize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            if (h >= min.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                Dimension max = ui.getMaximumSize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                if (h <= max.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    // --- Serialization ------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * See <code>readObject</code> and <code>writeObject</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * <code>JComponent</code> for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    // --- variables ---------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
409
eaa6e0cc0aea 4714674: JEditorPane.setPage(url) blocks AWT thread when HTTP protocol is used
peterz
parents: 2
diff changeset
  1516
    private SwingWorker<URL, Object> pageLoader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * Current content binding of the editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    private EditorKit kit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    private boolean isUserSetEditorKit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1524
    private Hashtable<String, Object> pageProperties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    /** Should be kept in sync with javax.swing.text.html.FormView counterpart. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    final static String PostDataProperty = "javax.swing.JEditorPane.postdata";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * Table of registered type handlers for this editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1532
    private Hashtable<String, EditorKit> typeHandlers;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * Private AppContext keys for this class's static variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    private static final Object kitRegistryKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        new StringBuffer("JEditorPane.kitRegistry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    private static final Object kitTypeRegistryKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        new StringBuffer("JEditorPane.kitTypeRegistry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    private static final Object kitLoaderRegistryKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        new StringBuffer("JEditorPane.kitLoaderRegistry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    private static final String uiClassID = "EditorPaneUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * Key for a client property used to indicate whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * <a href="http://www.w3.org/TR/CSS21/syndata.html#length-units">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * w3c compliant</a> length units are used for html rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * By default this is not enabled; to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * it set the client {@link #putClientProperty property} with this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * to <code>Boolean.TRUE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    public static final String W3C_LENGTH_UNITS = "JEditorPane.w3cLengthUnits";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * Key for a client property used to indicate whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * the default font and foreground color from the component are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * used if a font or foreground color is not specified in the styled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * The default varies based on the look and feel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * to enable it set the client {@link #putClientProperty property} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * this name to <code>Boolean.TRUE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    public static final String HONOR_DISPLAY_PROPERTIES = "JEditorPane.honorDisplayProperties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    static final Map<String, String> defaultEditorKitMap = new HashMap<String, String>(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * Returns a string representation of this <code>JEditorPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * @return  a string representation of this <code>JEditorPane</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        String kitString = (kit != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                            kit.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        String typeHandlersString = (typeHandlers != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                                     typeHandlers.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        ",kit=" + kitString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        ",typeHandlers=" + typeHandlersString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * Gets the AccessibleContext associated with this JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * For editor panes, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * AccessibleJEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * A new AccessibleJEditorPane instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * @return an AccessibleJEditorPane that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     *         AccessibleContext of this JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        if (getEditorKit() instanceof HTMLEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            if (accessibleContext == null || accessibleContext.getClass() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                    AccessibleJEditorPaneHTML.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                accessibleContext = new AccessibleJEditorPaneHTML();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        } else if (accessibleContext == null || accessibleContext.getClass() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                       AccessibleJEditorPane.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            accessibleContext = new AccessibleJEditorPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * <code>JEditorPane</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * Java Accessibility API appropriate to editor pane user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  1640
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22260
diff changeset
  1644
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    protected class AccessibleJEditorPane extends AccessibleJTextComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
         * Gets the accessibleDescription property of this object.  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
         * property isn't set, returns the content type of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
         * <code>JEditorPane</code> instead (e.g. "plain/text", "html/text").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
         * @return the localized description of the object; <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
         *      if this object does not have a description
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
         * @see #setAccessibleName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        public String getAccessibleDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            String description = accessibleDescription;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            // fallback to client property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            if (description == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                description = (String)getClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            if (description == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                description = JEditorPane.this.getContentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
         * Gets the state set of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         * @return an instance of AccessibleStateSet describing the states
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
         * of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
         * @see AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            states.add(AccessibleState.MULTI_LINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * This class provides support for <code>AccessibleHypertext</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * and is used in instances where the <code>EditorKit</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * installed in this <code>JEditorPane</code> is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * <code>HTMLEditorKit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  1695
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22260
diff changeset
  1699
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    protected class AccessibleJEditorPaneHTML extends AccessibleJEditorPane {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        private AccessibleContext accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            return new JEditorPaneAccessibleHypertextSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        protected AccessibleJEditorPaneHTML () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            HTMLEditorKit kit = (HTMLEditorKit)JEditorPane.this.getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            accessibleContext = kit.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
         * Returns the number of accessible children of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
         * @return the number of accessible children of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                return accessibleContext.getAccessibleChildrenCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
         * Returns the specified Accessible child of the object.  The Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
         * children of an Accessible object are zero-based, so the first child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
         * of an Accessible child is at index 0, the second child is at index 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
         * and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
         * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
         * @return the Accessible child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
         * @see #getAccessibleChildrenCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                return accessibleContext.getAccessibleChild(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
         * Returns the Accessible child, if one exists, contained at the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
         * coordinate Point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
         * @param p The point relative to the coordinate system of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
         * @return the Accessible, if it exists, at the specified location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
         * otherwise null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        public Accessible getAccessibleAt(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            if (accessibleContext != null && p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                    AccessibleComponent acomp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                        accessibleContext.getAccessibleComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                    if (acomp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                        return acomp.getAccessibleAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                } catch (IllegalComponentStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * What's returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * <code>AccessibleJEditorPaneHTML.getAccessibleText</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * Provides support for <code>AccessibleHypertext</code> in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * there is an HTML document being displayed in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * <code>JEditorPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    protected class JEditorPaneAccessibleHypertextSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    extends AccessibleJEditorPane implements AccessibleHypertext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        public class HTMLLink extends AccessibleHyperlink {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            Element element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            public HTMLLink(Element e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                element = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
             * Since the document a link is associated with may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
             * changed, this method returns whether this Link is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
             * anymore (with respect to the document it references).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
             * @return a flag indicating whether this link is still valid with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
             *         respect to the AccessibleHypertext it belongs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            public boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                return JEditorPaneAccessibleHypertextSupport.this.linksValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
             * Returns the number of accessible actions available in this Link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
             * If there are more than one, the first one is NOT considered the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
             * "default" action of this LINK object (e.g. in an HTML imagemap).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
             * In general, links will have only one AccessibleAction in them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
             * @return the zero-based number of Actions in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            public int getAccessibleActionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
             * Perform the specified Action on the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
             * @param i zero-based index of actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
             * @return true if the the action was performed; else false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
             * @see #getAccessibleActionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            public boolean doAccessibleAction(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                if (i == 0 && isValid() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                    URL u = (URL) getAccessibleActionObject(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                    if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                        HyperlinkEvent linkEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                            new HyperlinkEvent(JEditorPane.this, HyperlinkEvent.EventType.ACTIVATED, u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                        JEditorPane.this.fireHyperlinkUpdate(linkEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                return false;  // link invalid or i != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
             * Return a String description of this particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
             * link action.  The string returned is the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
             * within the document associated with the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
             * which contains this link.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
             * @param i zero-based index of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
             * @return a String description of the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
             * @see #getAccessibleActionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            public String getAccessibleActionDescription(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                if (i == 0 && isValid() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                    Document d = JEditorPane.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                    if (d != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                            return d.getText(getStartIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                                             getEndIndex() - getStartIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                        } catch (BadLocationException exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
             * Returns a URL object that represents the link.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
             * @param i zero-based index of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
             * @return an URL representing the HTML link itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
             * @see #getAccessibleActionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            public Object getAccessibleActionObject(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                if (i == 0 && isValid() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                    AttributeSet as = element.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                    AttributeSet anchor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                        (AttributeSet) as.getAttribute(HTML.Tag.A);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                    String href = (anchor != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                        (String) anchor.getAttribute(HTML.Attribute.HREF) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                    if (href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                        URL u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                            u = new URL(JEditorPane.this.getPage(), href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                        } catch (MalformedURLException m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                            u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                        return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                return null;  // link invalid or i != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
             * Return an object that represents the link anchor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
             * as appropriate for that link.  E.g. from HTML:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
             *   <a href="http://www.sun.com/access">Accessibility</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
             * this method would return a String containing the text:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
             * 'Accessibility'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
             * Similarly, from this HTML:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
             *   &lt;a HREF="#top"&gt;&lt;img src="top-hat.gif" alt="top hat"&gt;&lt;/a&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
             * this might return the object ImageIcon("top-hat.gif", "top hat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
             * @param i zero-based index of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
             * @return an Object representing the hypertext anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
             * @see #getAccessibleActionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            public Object getAccessibleActionAnchor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                return getAccessibleActionDescription(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
             * Get the index with the hypertext document at which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
             * link begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
             * @return index of start of link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            public int getStartIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                return element.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
             * Get the index with the hypertext document at which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
             * link ends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
             * @return index of end of link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            public int getEndIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                return element.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1927
        private class LinkVector extends Vector<HTMLLink> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            public int baseElementIndex(Element e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                HTMLLink l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                for (int i = 0; i < elementCount; i++) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1931
                    l = elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                    if (l.element == e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        LinkVector hyperlinks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        boolean linksValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
         * Build the private table mapping links to locations in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        private void buildLinkTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            hyperlinks.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            Document d = JEditorPane.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            if (d != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                ElementIterator ei = new ElementIterator(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                Element e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                AttributeSet as;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                AttributeSet anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                String href;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                while ((e = ei.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                    if (e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                        as = e.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                    anchor = (AttributeSet) as.getAttribute(HTML.Tag.A);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                    href = (anchor != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                        (String) anchor.getAttribute(HTML.Attribute.HREF) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                        if (href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                            hyperlinks.addElement(new HTMLLink(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            linksValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
         * Make one of these puppies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        public JEditorPaneAccessibleHypertextSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            hyperlinks = new LinkVector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            Document d = JEditorPane.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            if (d != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                d.addDocumentListener(new DocumentListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                    public void changedUpdate(DocumentEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                        linksValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    public void insertUpdate(DocumentEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                        linksValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                    public void removeUpdate(DocumentEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                        linksValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
         * Returns the number of links within this hypertext doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
         * @return number of links in this hypertext doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        public int getLinkCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            if (linksValid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                buildLinkTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            return hyperlinks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
         * Returns the index into an array of hyperlinks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
         * is associated with this character index, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
         * is no hyperlink associated with this index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
         * @param  charIndex index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
         * @return index into the set of hyperlinks for this hypertext doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        public int getLinkIndex(int charIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            if (linksValid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                buildLinkTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            Element e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            Document doc = JEditorPane.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                for (e = doc.getDefaultRootElement(); ! e.isLeaf(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                    int index = e.getElementIndex(charIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                    e = e.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            // don't need to verify that it's an HREF element; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            // not, then it won't be in the hyperlinks Vector, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            // so indexOf will return -1 in any case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            return hyperlinks.baseElementIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
         * Returns the index into an array of hyperlinks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
         * index.  If there is no hyperlink at this index, it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
         * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
         * @param linkIndex into the set of hyperlinks for this hypertext doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
         * @return string representation of the hyperlink
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        public AccessibleHyperlink getLink(int linkIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            if (linksValid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                buildLinkTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            if (linkIndex >= 0 && linkIndex < hyperlinks.size()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  2043
                return hyperlinks.elementAt(linkIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
         * Returns the contiguous text within the document that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
         * is associated with this hyperlink.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
         * @param linkIndex into the set of hyperlinks for this hypertext doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
         * @return the contiguous text sharing the link at this index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        public String getLinkText(int linkIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            if (linksValid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                buildLinkTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            Element e = (Element) hyperlinks.elementAt(linkIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
            if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                Document d = JEditorPane.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                if (d != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                        return d.getText(e.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                                         e.getEndOffset() - e.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                    } catch (BadLocationException exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    static class PlainEditorKit extends DefaultEditorKit implements ViewFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
         * Fetches a factory that is suitable for producing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
         * views of any models that are produced by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
         * kit.  The default is to have the UI produce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
         * factory, so this method has no implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
         * @return the view factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        public ViewFactory getViewFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
         * Creates a view from the given structural element of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
         * document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
         * @param elem  the piece of the document to build a view of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
         * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
         * @see View
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        public View create(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            Document doc = elem.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
            Object i18nFlag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                // build a view that support bidi
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                return createI18N(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                return new WrappedPlainView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        View createI18N(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            String kind = elem.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            if (kind != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                if (kind.equals(AbstractDocument.ContentElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                    return new PlainParagraph(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                } else if (kind.equals(AbstractDocument.ParagraphElementName)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                    return new BoxView(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
         * Paragraph for representing plain-text lines that support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
         * bidirectional text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        static class PlainParagraph extends javax.swing.text.ParagraphView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            PlainParagraph(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                layoutPool = new LogicalView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                layoutPool.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            protected void setPropertiesFromAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                if ((c != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                    && (! c.getComponentOrientation().isLeftToRight()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                    setJustification(StyleConstants.ALIGN_RIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                    setJustification(StyleConstants.ALIGN_LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
             * Fetch the constraining span to flow against for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
             * the given child index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            public int getFlowSpan(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                if (c instanceof JTextArea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                    JTextArea area = (JTextArea) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                    if (! area.getLineWrap()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                        // no limit if unwrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                        return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                return super.getFlowSpan(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            protected SizeRequirements calculateMinorAxisRequirements(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                                                            SizeRequirements r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                SizeRequirements req
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                    = super.calculateMinorAxisRequirements(axis, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                if (c instanceof JTextArea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                    JTextArea area = (JTextArea) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                    if (! area.getLineWrap()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                        // min is pref if unwrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                        req.minimum = req.preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                return req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
             * This class can be used to represent a logical view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
             * a flow.  It keeps the children updated to reflect the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
             * of the model, gives the logical child views access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
             * view hierarchy, and calculates a preferred span.  It doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
             * do any rendering, layout, or model/view translation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            static class LogicalView extends CompositeView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                LogicalView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                protected int getViewIndexAtPosition(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                    if (elem.getElementCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                        return elem.getElementIndex(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                protected boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                updateChildren(DocumentEvent.ElementChange ec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                               DocumentEvent e, ViewFactory f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                    Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                    if (elem.getElementCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                        super.loadChildren(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                        View v = new GlyphView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                        append(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                    if( getViewCount() != 1 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                        throw new Error("One child view is assumed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                    View v = getView(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                    //((GlyphView)v).setGlyphPainter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                    return v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                 * Forward the DocumentEvent to the given child view.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                 * is implemented to reparent the child to the logical view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                 * (the children may have been parented by a row in the flow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                 * if they fit without breaking) and then execute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                 * superclass behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                 * @param v the child view to forward the event to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                 * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                 * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                 * @param f the factory to use to rebuild if the view has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                 *          children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                 * @see #forwardUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                protected void forwardUpdateToView(View v, DocumentEvent e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                                                   Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                    v.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                    super.forwardUpdateToView(v, e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                // The following methods don't do anything useful, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                // simply keep the class from being abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                public void paint(Graphics g, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                protected boolean isBefore(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                protected boolean isAfter(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                protected View getViewAtPoint(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                protected void childAllocation(int index, Rectangle a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
/* This is useful for the nightmare of parsing multi-part HTTP/RFC822 headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
 * sensibly:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
 * From a String like: 'timeout=15, max=5'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
 * create an array of Strings:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
 * { {"timeout", "15"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
 *   {"max", "5"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
 * From one like: 'Basic Realm="FuzzFace" Foo="Biz Bar Baz"'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
 * create one like (no quotes in literal):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
 * { {"basic", null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
 *   {"realm", "FuzzFace"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
 *   {"foo", "Biz Bar Baz"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
 * keys are converted to lower case, vals are left as is....
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
 * author Dave Brown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
static class HeaderParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
    /* table of key/val pairs - maxes out at 10!!!!*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
    String raw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
    String[][] tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    public HeaderParser(String raw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        this.raw = raw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        tab = new String[10][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    private void parse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        if (raw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            raw = raw.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            char[] ca = raw.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            int beg = 0, end = 0, i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            boolean inKey = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            boolean inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
            int len = ca.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            while (end < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                char c = ca[end];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                if (c == '=') { // end of a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                    tab[i][0] = new String(ca, beg, end-beg).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                    inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                    end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                    beg = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                } else if (c == '\"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                    if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                        tab[i++][1]= new String(ca, beg, end-beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                        inQuote=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                            end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                        } while (end < len && (ca[end] == ' ' || ca[end] == ','));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                        inKey=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                        beg=end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                        inQuote=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                        end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                        beg=end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                } else if (c == ' ' || c == ',') { // end key/val, of whatever we're in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                    if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                        end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                    } else if (inKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                        tab[i++][0] = (new String(ca, beg, end-beg)).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                        tab[i++][1] = (new String(ca, beg, end-beg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                    while (end < len && (ca[end] == ' ' || ca[end] == ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                        end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                    inKey = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                    beg = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                    end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            // get last key/val, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            if (--end > beg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                if (!inKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                    if (ca[end] == '\"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                        tab[i++][1] = (new String(ca, beg, end-beg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                        tab[i++][1] = (new String(ca, beg, end-beg+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                    tab[i][0] = (new String(ca, beg, end-beg+1)).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            } else if (end == beg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                if (!inKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                    if (ca[end] == '\"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                        tab[i++][1] = String.valueOf(ca[end-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                        tab[i++][1] = String.valueOf(ca[end]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                    tab[i][0] = String.valueOf(ca[end]).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
    public String findKey(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        if (i < 0 || i > 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        return tab[i][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    public String findValue(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        if (i < 0 || i > 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        return tab[i][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
    public String findValue(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        return findValue(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
    public String findValue(String k, String Default) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        if (k == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            return Default;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        k = k.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        for (int i = 0; i < 10; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            if (tab[i][0] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                return Default;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            } else if (k.equals(tab[i][0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                return tab[i][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        return Default;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
    public int findInt(String k, int Default) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            return Integer.parseInt(findValue(k, String.valueOf(Default)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            return Default;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
}