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