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