jdk/src/java.desktop/share/classes/javax/swing/text/html/HTMLEditorKit.java
author prr
Mon, 14 Nov 2016 09:59:36 -0800
changeset 42216 621af0ebf6c4
parent 40719 4ae72a69bd3b
child 45025 9ad3afa82b5e
permissions -rw-r--r--
8169518: Suppress Deprecation warnings for deprecated Swing APIs Reviewed-by: alexsch, ssadetsky
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 21982
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
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: 1299
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: 1299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    27
import sun.awt.AppContext;
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.TextUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.ref.*;
24157
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
    41
import java.security.AccessController;
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
    42
import java.security.PrivilegedAction;
40719
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32865
diff changeset
    43
import javax.swing.text.html.parser.ParserDelegator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The Swing JEditorPane text component supports different kinds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * of content via a plug-in mechanism called an EditorKit.  Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * HTML is a very popular format of content, some support is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * by default.  The default support is provided by this class, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * supports HTML version 3.2 (with some extensions), and is migrating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * toward version 4.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The <applet> tag is not supported, but some support is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * for the <object> tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * There are several goals of the HTML EditorKit provided, that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * an effect upon the way that HTML is modeled.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * have influenced its design in a substantial way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Support editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * It might seem fairly obvious that a plug-in for JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * should provide editing support, but that fact has several
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * design considerations.  There are a substantial number of HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * documents that don't properly conform to an HTML specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * These must be normalized somewhat into a correct form if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * is to edit them.  Additionally, users don't like to be presented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * with an excessive amount of structure editing, so using traditional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * text editing gestures is preferred over using the HTML structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * exactly as defined in the HTML document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * The modeling of HTML is provided by the class <code>HTMLDocument</code>.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
    73
 * Its documentation describes the details of how the HTML is modeled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The editing support leverages heavily off of the text package.
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
    75
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Extendable/Scalable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * To maximize the usefulness of this kit, a great deal of effort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * has gone into making it extendable.  These are some of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * features.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *   <li>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
    84
 *   The parser is replaceable.  The default parser is the Hot Java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   parser which is DTD based.  A different DTD can be used, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *   entirely different parser can be used.  To change the parser,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   reimplement the getParser method.  The default parser is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   dynamically loaded when first asked for, so the class files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   will never be loaded if an alternative parser is used.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   default parser is in a separate package called parser below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   The parser drives the ParserCallback, which is provided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   HTMLDocument.  To change the callback, subclass HTMLDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   and reimplement the createDefaultDocument method to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   document that produces a different reader.  The reader controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   how the document is structured.  Although the Document provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   HTML support by default, there is nothing preventing support of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   non-HTML tags that result in alternative element structures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   The default view of the models are provided as a hierarchy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *   View implementations, so one can easily customize how a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   element is displayed or add capabilities for new kinds of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *   by providing new View implementations.  The default set of views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   are provided by the <code>HTMLFactory</code> class.  This can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *   be easily changed by subclassing or replacing the HTMLFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *   and reimplementing the getViewFactory method to return the alternative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *   factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *   The View implementations work primarily off of CSS attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *   which are kept in the views.  This makes it possible to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *   multiple views mapped over the same model that appear substantially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *   different.  This can be especially useful for printing.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *   most HTML attributes, the HTML attributes are converted to CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *   attributes for display.  This helps make the View implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *   more general purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * </ol>
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
   118
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * Asynchronous Loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * Larger documents involve a lot of parsing and take some time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * to load.  By default, this kit produces documents that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * loaded asynchronously if loaded using <code>JEditorPane.setPage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * This is controlled by a property on the document.  The method
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 6862
diff changeset
   126
 * {@link #createDefaultDocument createDefaultDocument} can
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * be overriden to change this.  The batching of work is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * by the <code>HTMLDocument.HTMLReader</code> class.  The actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * work is done by the <code>DefaultStyledDocument</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <code>AbstractDocument</code> classes in the text package.
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
   131
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * Customization from current LAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * HTML provides a well known set of features without exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * specifying the display characteristics.  Swing has a theme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * mechanism for its look-and-feel implementations.  It is desirable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * for the look-and-feel to feed display characteristics into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * HTML views.  An user with poor vision for example would want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * high contrast and larger than typical fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * The support for this is provided by the <code>StyleSheet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * class.  The presentation of the HTML can be heavily influenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * by the setting of the StyleSheet property on the EditorKit.
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
   145
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * Not lossy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * An EditorKit has the ability to be read and save documents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * It is generally the most pleasing to users if there is no loss
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * of data between the two operation.  The policy of the HTMLEditorKit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * will be to store things not recognized or not necessarily visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * so they can be subsequently written out.  The model of the HTML document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * should therefore contain all information discovered while reading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * document.  This is constrained in some ways by the need to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * editing (i.e. incorrect documents sometimes must be normalized).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * The guiding principle is that information shouldn't be lost, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * some might be synthesized to produce a more correct model or it might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * be rearranged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
   164
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
public class HTMLEditorKit extends StyledEditorKit implements Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private JEditorPane theEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Constructs an HTMLEditorKit, creates a StyleContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * and loads the style sheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public HTMLEditorKit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Get the MIME type of the data that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * kit represents support for.  This kit supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * the type <code>text/html</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public String getContentType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return "text/html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Fetch a factory that is suitable for producing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * views of any models that are produced by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * kit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public ViewFactory getViewFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return defaultFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Create an uninitialized text storage model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * that is appropriate for this type of editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public Document createDefaultDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        StyleSheet styles = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        StyleSheet ss = new StyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        ss.addStyleSheet(styles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        HTMLDocument doc = new HTMLDocument(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        doc.setParser(getParser());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        doc.setAsynchronousLoadPriority(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        doc.setTokenThreshold(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Try to get an HTML parser from the document.  If no parser is set for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * the document, return the editor kit's default parser.  It is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * if no parser could be obtained from the editor kit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private Parser ensureParser(HTMLDocument doc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        Parser p = doc.getParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            p = getParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new IOException("Can't load parser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Inserts content from the given stream. If <code>doc</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * an instance of HTMLDocument, this will read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * HTML 3.2 text. Inserting HTML into a non-empty document must be inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * the body Element, if you do not insert into the body an exception will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * be thrown. When inserting into a non-empty document all tags outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * of the body (head, title) will be dropped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   242
     * @param in the stream to read from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param doc the destination for the insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param pos the location in the document to place the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *   content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *   location within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @exception RuntimeException (will eventually be a BadLocationException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *            if pos is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (doc instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            HTMLDocument hdoc = (HTMLDocument) doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (pos > doc.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                throw new BadLocationException("Invalid location", pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            Parser p = ensureParser(hdoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            ParserCallback receiver = hdoc.getReader(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            Boolean ignoreCharset = (Boolean)doc.getProperty("IgnoreCharsetDirective");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            p.parse(in, receiver, (ignoreCharset == null) ? false : ignoreCharset.booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            receiver.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            super.read(in, doc, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Inserts HTML into an existing document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   273
     * @param doc the document to insert into
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   274
     * @param offset the offset to insert HTML at
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   275
     * @param popDepth the number of ElementSpec.EndTagTypes to generate
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   276
     *                  before inserting
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   277
     * @param html the HTML string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param pushDepth the number of ElementSpec.StartTagTypes with a direction
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   279
     *                  of ElementSpec.JoinNextDirection that should be generated
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   280
     *                  before inserting, but after the end tags have been generated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param insertTag the first tag to start inserting into document
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   282
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   283
     * @throws BadLocationException if {@code offset} is invalid
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   284
     * @throws IOException on I/O error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @exception RuntimeException (will eventually be a BadLocationException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *            if pos is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public void insertHTML(HTMLDocument doc, int offset, String html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                           int popDepth, int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                           HTML.Tag insertTag) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                       BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (offset > doc.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw new BadLocationException("Invalid location", offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        Parser p = ensureParser(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        ParserCallback receiver = doc.getReader(offset, popDepth, pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                                insertTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        Boolean ignoreCharset = (Boolean)doc.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                ("IgnoreCharsetDirective");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        p.parse(new StringReader(html), receiver, (ignoreCharset == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                false : ignoreCharset.booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        receiver.flush();
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
     * Write content from a document to the given stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * in a format appropriate for this kind of content handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   310
     * @param out the stream to write to
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   311
     * @param doc the source for the write
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   312
     * @param pos the location in the document to fetch the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *   content
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   314
     * @param len the amount to write out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception IOException on any I/O error
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   316
     * @exception BadLocationException if {@code pos} represents an invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *   location within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public void write(Writer out, Document doc, int pos, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        throws IOException, BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (doc instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            HTMLWriter w = new HTMLWriter(out, (HTMLDocument)doc, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            w.write();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            MinimalHTMLWriter w = new MinimalHTMLWriter(out, (StyledDocument)doc, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            w.write();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            super.write(out, doc, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Called when the kit is being installed into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * a JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param c the JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public void install(JEditorPane c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        c.addMouseListener(linkHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        c.addMouseMotionListener(linkHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        c.addCaretListener(nextLinkAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        super.install(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        theEditor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Called when the kit is being removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * JEditorPane.  This is used to unregister any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * listeners that were attached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param c the JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public void deinstall(JEditorPane c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        c.removeMouseListener(linkHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        c.removeMouseMotionListener(linkHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        c.removeCaretListener(nextLinkAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        super.deinstall(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        theEditor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Default Cascading Style Sheet file that sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * up the tag views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public static final String DEFAULT_CSS = "default.css";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Set the set of styles to be used to render the various
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * HTML elements.  These styles are specified in terms of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * CSS specifications.  Each document produced by the kit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * will have a copy of the sheet which it can add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * document specific styles to.  By default, the StyleSheet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * specified is shared by all HTMLEditorKit instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * This should be reimplemented to provide a finer granularity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * if desired.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   377
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   378
     * @param s a StyleSheet
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public void setStyleSheet(StyleSheet s) {
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   381
        if (s == null) {
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   382
            AppContext.getAppContext().remove(DEFAULT_STYLES_KEY);
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   383
        } else {
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   384
            AppContext.getAppContext().put(DEFAULT_STYLES_KEY, s);
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   385
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Get the set of styles currently being used to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * HTML elements.  By default the resource specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * DEFAULT_CSS gets loaded, and is shared by all HTMLEditorKit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * instances.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   393
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   394
     * @return the StyleSheet
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public StyleSheet getStyleSheet() {
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   397
        AppContext appContext = AppContext.getAppContext();
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   398
        StyleSheet defaultStyles = (StyleSheet) appContext.get(DEFAULT_STYLES_KEY);
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   399
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (defaultStyles == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            defaultStyles = new StyleSheet();
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   402
            appContext.put(DEFAULT_STYLES_KEY, defaultStyles);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                InputStream is = HTMLEditorKit.getResourceAsStream(DEFAULT_CSS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                Reader r = new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        new InputStreamReader(is, "ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                defaultStyles.loadRules(r, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                // on error we simply have no styles... the html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // will look mighty wrong but still function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return defaultStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Fetch a resource relative to the HTMLEditorKit classfile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * If this is called on 1.2 the loading will occur under the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * protection of a doPrivileged call to allow the HTMLEditorKit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * to function when used in an applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param name the name of the resource, relative to the
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   424
     *             HTMLEditorKit class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @return a stream representing the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
24157
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
   427
    static InputStream getResourceAsStream(final String name) {
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
   428
        return AccessController.doPrivileged(
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
   429
                new PrivilegedAction<InputStream>() {
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
   430
                    public InputStream run() {
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
   431
                        return HTMLEditorKit.class.getResourceAsStream(name);
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
   432
                    }
84ccec128c31 8038962: KSS: javax.swing.text.html[.parser].ResourceLoader
malenkov
parents: 23697
diff changeset
   433
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Fetches the command list for the editor.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * the list of commands supported by the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * augmented by the collection of commands defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * locally for style operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @return the command list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public Action[] getActions() {
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 21982
diff changeset
   445
        return TextAction.augmentList(super.getActions(), defaultActions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Copies the key/values in <code>element</code>s AttributeSet into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * <code>set</code>. This does not copy component, icon, or element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * names attributes. Subclasses may wish to refine what is and what
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * isn't copied here. But be sure to first remove all the attributes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * are in <code>set</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * This is called anytime the caret moves over a different location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    protected void createInputAttributes(Element element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                         MutableAttributeSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        set.removeAttributes(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        set.addAttributes(element.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        set.removeAttribute(StyleConstants.ComposedTextAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        Object o = set.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (o instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            HTML.Tag tag = (HTML.Tag)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            // PENDING: we need a better way to express what shouldn't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            // copied when editing...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if(tag == HTML.Tag.IMG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // Remove the related image attributes, src, width, height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                set.removeAttribute(HTML.Attribute.SRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                set.removeAttribute(HTML.Attribute.HEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                set.removeAttribute(HTML.Attribute.WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                set.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                 HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            else if (tag == HTML.Tag.HR || tag == HTML.Tag.BR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                // Don't copy HRs or BRs either.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                set.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                 HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            else if (tag == HTML.Tag.COMMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                // Don't copy COMMENTs either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                set.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                 HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                set.removeAttribute(HTML.Attribute.COMMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            else if (tag == HTML.Tag.INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                // or INPUT either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                set.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                 HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                set.removeAttribute(HTML.Tag.INPUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            else if (tag instanceof HTML.UnknownTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                // Don't copy unknowns either:(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                set.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                 HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                set.removeAttribute(HTML.Attribute.ENDTAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Gets the input attributes used for the styled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * editing actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @return the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public MutableAttributeSet getInputAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (input == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            input = getStyleSheet().addStyle(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Sets the default cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   518
     * @param cursor a cursor
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   519
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public void setDefaultCursor(Cursor cursor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        defaultCursor = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Returns the default cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   529
     * @return the cursor
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   530
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public Cursor getDefaultCursor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return defaultCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Sets the cursor to use over links.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   540
     * @param cursor a cursor
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   541
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public void setLinkCursor(Cursor cursor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        linkCursor = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Returns the cursor to use over hyper links.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   550
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   551
     * @return the cursor
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   552
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public Cursor getLinkCursor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return linkCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Indicates whether an html form submission is processed automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * or only <code>FormSubmitEvent</code> is fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @return true  if html form submission is processed automatically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *         false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @see #setAutoFormSubmission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public boolean isAutoFormSubmission() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return isAutoFormSubmission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Specifies if an html form submission is processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * automatically or only <code>FormSubmitEvent</code> is fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * By default it is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   578
     * @param isAuto if {@code true}, html form submission is processed automatically.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   579
     *
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 6862
diff changeset
   580
     * @see #isAutoFormSubmission()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @see FormSubmitEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    public void setAutoFormSubmission(boolean isAuto) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        isAutoFormSubmission = isAuto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Creates a copy of the editor kit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        HTMLEditorKit o = (HTMLEditorKit)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            o.input = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            o.linkHandler = new LinkController();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Fetch the parser to use for reading HTML streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * This can be reimplemented to provide a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * parser.  The default implementation is loaded dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * to avoid the overhead of loading the default parser if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * it's not used.  The default parser is the HotJava parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * using an HTML 3.2 DTD.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   609
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   610
     * @return the parser
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    protected Parser getParser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (defaultParser == null) {
40719
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32865
diff changeset
   614
            defaultParser = new ParserDelegator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return defaultParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    // ----- Accessibility support -----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private AccessibleContext accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * returns the AccessibleContext associated with this editor kit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @return the AccessibleContext associated with this editor kit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if (theEditor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            AccessibleHTML a = new AccessibleHTML(theEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            accessibleContext = a.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    // --- variables ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    private static final Cursor MoveCursor = Cursor.getPredefinedCursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                                    (Cursor.HAND_CURSOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    private static final Cursor DefaultCursor = Cursor.getPredefinedCursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                    (Cursor.DEFAULT_CURSOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /** Shared factory for creating HTML Views. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    private static final ViewFactory defaultFactory = new HTMLFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    MutableAttributeSet input;
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   650
    private static final Object DEFAULT_STYLES_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    private LinkController linkHandler = new LinkController();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    private static Parser defaultParser = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    private Cursor defaultCursor = DefaultCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    private Cursor linkCursor = MoveCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    private boolean isAutoFormSubmission = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Class to watch the associated component and fire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * hyperlink events on it when appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
   661
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public static class LinkController extends MouseAdapter implements MouseMotionListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        private Element curElem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         * If true, the current element (curElem) represents an image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        private boolean curElemImage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        private String href = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        /** This is used by viewToModel to avoid allocing a new array each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         * time. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        private transient Position.Bias[] bias = new Position.Bias[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * Current offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        private int curOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * Called for a mouse click event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         * If the component is read-only (ie a browser) then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         * the clicked event is used to drive an attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         * follow the reference specified by a link.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         * @see MouseListener#mouseClicked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         */
42216
621af0ebf6c4 8169518: Suppress Deprecation warnings for deprecated Swing APIs
prr
parents: 40719
diff changeset
   686
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            JEditorPane editor = (JEditorPane) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (! editor.isEditable() && editor.isEnabled() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    SwingUtilities.isLeftMouseButton(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                Point pt = new Point(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                int pos = editor.viewToModel(pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                if (pos >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    activateLink(pos, editor, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        // ignore the drags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        // track the moving of the mouse.
42216
621af0ebf6c4 8169518: Suppress Deprecation warnings for deprecated Swing APIs
prr
parents: 40719
diff changeset
   705
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            JEditorPane editor = (JEditorPane) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (!editor.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            HTMLEditorKit kit = (HTMLEditorKit)editor.getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            boolean adjustCursor = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            Cursor newCursor = kit.getDefaultCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (!editor.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                Point pt = new Point(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                int pos = editor.getUI().viewToModel(editor, pt, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                if (bias[0] == Position.Bias.Backward && pos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    pos--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (pos >= 0 &&(editor.getDocument() instanceof HTMLDocument)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    HTMLDocument hdoc = (HTMLDocument)editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    Element elem = hdoc.getCharacterElement(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    if (!doesElementContainLocation(editor, elem, pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                                    e.getX(), e.getY())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        elem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    if (curElem != elem || curElemImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        Element lastElem = curElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        curElem = elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        String href = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        curElemImage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                            AttributeSet a = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                            AttributeSet anchor = (AttributeSet)a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                                                   getAttribute(HTML.Tag.A);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                            if (anchor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                curElemImage = (a.getAttribute(StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                            NameAttribute) == HTML.Tag.IMG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                                if (curElemImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                    href = getMapHREF(editor, hdoc, elem, a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                                      pos, e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                                href = (String)anchor.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                                    (HTML.Attribute.HREF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                        if (href != this.href) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                            // reference changed, fire event(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                            fireEvents(editor, hdoc, href, lastElem, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                            this.href = href;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                            if (href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                newCursor = kit.getLinkCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                            adjustCursor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        adjustCursor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    curOffset = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            if (adjustCursor && editor.getCursor() != newCursor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                editor.setCursor(newCursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         * Returns a string anchor if the passed in element has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
         * USEMAP that contains the passed in location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
         */
42216
621af0ebf6c4 8169518: Suppress Deprecation warnings for deprecated Swing APIs
prr
parents: 40719
diff changeset
   778
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                                  Element elem, AttributeSet attr, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                                  int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (useMap != null && (useMap instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                Map m = hdoc.getMap((String)useMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                if (m != null && offset < hdoc.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    Rectangle bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    TextUI ui = html.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                        Shape lBounds = ui.modelToView(html, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                                                   Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        Shape rBounds = ui.modelToView(html, offset + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                                   Position.Bias.Backward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                        bounds = lBounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                        bounds.add((rBounds instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                                    (Rectangle)rBounds : rBounds.getBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                        bounds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                        AttributeSet area = m.getArea(x - bounds.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                                      y - bounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                                      bounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                                      bounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        if (area != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                            return (String)area.getAttribute(HTML.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                                             HREF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return null;
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
         * Returns true if the View representing <code>e</code> contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * the location <code>x</code>, <code>y</code>. <code>offset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * gives the offset into the Document to check for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         */
42216
621af0ebf6c4 8169518: Suppress Deprecation warnings for deprecated Swing APIs
prr
parents: 40719
diff changeset
   819
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        private boolean doesElementContainLocation(JEditorPane editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                                                   Element e, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                                   int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            if (e != null && offset > 0 && e.getStartOffset() == offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    TextUI ui = editor.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    Shape s1 = ui.modelToView(editor, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                              Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    if (s1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                                    s1.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    Shape s2 = ui.modelToView(editor, e.getEndOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                                              Position.Bias.Backward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    if (s2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                    s2.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                        r1.add(r2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    return r1.contains(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         * Calls linkActivated on the associated JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         * if the given position represents a link.<p>This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         * to forward to the method with the same name, but with the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         * args both == -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         * @param pos the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         * @param editor the editor pane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        protected void activateLink(int pos, JEditorPane editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            activateLink(pos, editor, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
         * Calls linkActivated on the associated JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         * if the given position represents a link. If this was the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
         * of a mouse click, <code>x</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
         * <code>y</code> will give the location of the mouse, otherwise
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
   865
         * they will be {@literal <} 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
         * @param pos the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         * @param html the editor pane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        void activateLink(int pos, JEditorPane html, MouseEvent mouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            Document doc = html.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            if (doc instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                HTMLDocument hdoc = (HTMLDocument) doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                Element e = hdoc.getCharacterElement(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                AttributeSet a = e.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                AttributeSet anchor = (AttributeSet)a.getAttribute(HTML.Tag.A);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                HyperlinkEvent linkEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                String description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                int x = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                int y = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                if (mouseEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    x = mouseEvent.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    y = mouseEvent.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                if (anchor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    href = getMapHREF(html, hdoc, e, a, pos, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                    href = (String)anchor.getAttribute(HTML.Attribute.HREF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                if (href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    linkEvent = createHyperlinkEvent(html, hdoc, href, anchor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                                     e, mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                if (linkEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    html.fireHyperlinkUpdate(linkEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
         * Creates and returns a new instance of HyperlinkEvent. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         * <code>hdoc</code> is a frame document a HTMLFrameHyperlinkEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
         * will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        HyperlinkEvent createHyperlinkEvent(JEditorPane html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                                            HTMLDocument hdoc, String href,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                                            AttributeSet anchor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                            Element element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                                            MouseEvent mouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            URL u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                URL base = hdoc.getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                u = new URL(base, href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                // Following is a workaround for 1.2, in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                // new URL("file://...", "#...") causes the filename to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                // be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                if (href != null && "file".equals(u.getProtocol()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                    href.startsWith("#")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                    String baseFile = base.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    String newFile = u.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    if (baseFile != null && newFile != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        !newFile.startsWith(baseFile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                        u = new URL(base, baseFile + href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            } catch (MalformedURLException m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   933
            HyperlinkEvent linkEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            if (!hdoc.isFrameDocument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                linkEvent = new HyperlinkEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                        html, HyperlinkEvent.EventType.ACTIVATED, u, href,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                        element, mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                String target = (anchor != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    (String)anchor.getAttribute(HTML.Attribute.TARGET) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                if ((target == null) || (target.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    target = hdoc.getBaseTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                if ((target == null) || (target.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    target = "_self";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    linkEvent = new HTMLFrameHyperlinkEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        html, HyperlinkEvent.EventType.ACTIVATED, u, href,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                        element, mouseEvent, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            return linkEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        void fireEvents(JEditorPane editor, HTMLDocument doc, String href,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                        Element lastElem, MouseEvent mouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (this.href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                // fire an exited event on the old link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                URL u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    u = new URL(doc.getBase(), this.href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                } catch (MalformedURLException m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                HyperlinkEvent exit = new HyperlinkEvent(editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                                 HyperlinkEvent.EventType.EXITED, u, this.href,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                                 lastElem, mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                editor.fireHyperlinkUpdate(exit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            if (href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                // fire an entered event on the new link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                URL u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    u = new URL(doc.getBase(), href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                } catch (MalformedURLException m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                HyperlinkEvent entered = new HyperlinkEvent(editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                                            HyperlinkEvent.EventType.ENTERED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                                            u, href, curElem, mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                editor.fireHyperlinkUpdate(entered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            }
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
     * Interface to be supported by the parser.  This enables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * providing a different parser while reusing some of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * implementation provided by this editor kit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   991
    public abstract static class Parser {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         * Parse the given stream and drive the given callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
         * with the results of the parse.  This method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
         * be implemented to be thread-safe.
24269
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
   996
         *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   997
         * @param r a reader
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   998
         * @param cb a parser callback
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
   999
         * @param ignoreCharSet if {@code true} charset is ignoring
24269
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1000
         * @throws IOException if an I/O exception occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        public abstract void parse(Reader r, ParserCallback cb, boolean ignoreCharSet) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * The result of parsing drives these callback methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * The open and close actions should be balanced.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * <code>flush</code> method will be the last method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * called, to give the receiver a chance to flush any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * pending data into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * <p>Refer to DocumentParser, the default parser used, for further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * information on the contents of the AttributeSets, the positions, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * other info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @see javax.swing.text.html.parser.DocumentParser
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    public static class ParserCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
         * This is passed as an attribute in the attributeset to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
         * the element is implied eg, the string '&lt;&gt;foo&lt;\t&gt;'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
         * contains an implied html element and an implied body element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        public static final Object IMPLIED = "_implied_";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1028
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1029
         * The last method called on the reader. It allows
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1030
         * any pending changes to be flushed into the document.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1031
         * Since this is currently loading synchronously, the entire
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1032
         * set of changes are pushed in at this point.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1033
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1034
         * @throws BadLocationException if the given position does not
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1035
         *   represent a valid location in the associated document.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1036
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        public void flush() throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1040
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1041
         * Called by the parser to indicate a block of text was
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1042
         * encountered.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1043
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1044
         * @param data a data
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1045
         * @param pos a position
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1046
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        public void handleText(char[] data, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1050
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1051
         * Called by the parser to indicate a block of comment was
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1052
         * encountered.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1053
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1054
         * @param data a data
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1055
         * @param pos a position
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1056
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        public void handleComment(char[] data, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1060
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1061
         * Callback from the parser. Route to the appropriate
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1062
         * handler for the tag.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1063
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1064
         * @param t an HTML tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1065
         * @param a a set of attributes
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1066
         * @param pos a position
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1067
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1071
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1072
         * Callback from the parser. Route to the appropriate
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1073
         * handler for the tag.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1074
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1075
         * @param t an HTML tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1076
         * @param pos a position
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1077
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        public void handleEndTag(HTML.Tag t, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1081
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1082
         * Callback from the parser. Route to the appropriate
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1083
         * handler for the tag.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1084
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1085
         * @param t an HTML tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1086
         * @param a a set of attributes
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1087
         * @param pos a position
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1088
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1092
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1093
         * Callback from the parser. Route to the appropriate
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1094
         * handler for the error.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1095
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1096
         * @param errorMsg a error message
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1097
         * @param pos a position
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1098
         */
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1099
        public void handleError(String errorMsg, int pos) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
         * This is invoked after the stream has been parsed, but before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
         * <code>flush</code>. <code>eol</code> will be one of \n, \r
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
         * or \r\n, which ever is encountered the most in parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
         * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
         *
24269
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1108
         * @param eol value of eol
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1109
         *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        public void handleEndOfLineString(String eol) {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * A factory to build views for HTML.  The following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * table describes what this factory will build by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * <table summary="Describes the tag and view created by this factory by default">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * <th align=left>Tag<th align=left>View created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * <td>HTML.Tag.CONTENT<td>InlineView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * <td>HTML.Tag.IMPLIED<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * <td>HTML.Tag.P<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * <td>HTML.Tag.H1<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * <td>HTML.Tag.H2<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <td>HTML.Tag.H3<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * <td>HTML.Tag.H4<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * <td>HTML.Tag.H5<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * <td>HTML.Tag.H6<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * <td>HTML.Tag.DT<td>javax.swing.text.html.ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * <td>HTML.Tag.MENU<td>ListView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * <td>HTML.Tag.DIR<td>ListView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * <td>HTML.Tag.UL<td>ListView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * <td>HTML.Tag.OL<td>ListView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <td>HTML.Tag.LI<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * <td>HTML.Tag.DL<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <td>HTML.Tag.DD<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * <td>HTML.Tag.BODY<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * <td>HTML.Tag.HTML<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * <td>HTML.Tag.CENTER<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * <td>HTML.Tag.DIV<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * <td>HTML.Tag.BLOCKQUOTE<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * <td>HTML.Tag.PRE<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * <td>HTML.Tag.BLOCKQUOTE<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * <td>HTML.Tag.PRE<td>BlockView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <td>HTML.Tag.IMG<td>ImageView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * <td>HTML.Tag.HR<td>HRuleView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * <td>HTML.Tag.BR<td>BRView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * <td>HTML.Tag.TABLE<td>javax.swing.text.html.TableView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * <td>HTML.Tag.INPUT<td>FormView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * <td>HTML.Tag.SELECT<td>FormView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * <td>HTML.Tag.TEXTAREA<td>FormView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * <td>HTML.Tag.OBJECT<td>ObjectView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * <td>HTML.Tag.FRAMESET<td>FrameSetView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * </tr><tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * <td>HTML.Tag.FRAME<td>FrameView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    public static class HTMLFactory implements ViewFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
         * Creates a view from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
         * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
         * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        public View create(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            AttributeSet attrs = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            Object elementName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                attrs.getAttribute(AbstractDocument.ElementNameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            Object o = (elementName != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                null : attrs.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            if (o instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                HTML.Tag kind = (HTML.Tag) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                if (kind == HTML.Tag.CONTENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    return new InlineView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                } else if (kind == HTML.Tag.IMPLIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                    String ws = (String) elem.getAttributes().getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                        CSS.Attribute.WHITE_SPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    if ((ws != null) && ws.equals("pre")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                        return new LineView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    return new javax.swing.text.html.ParagraphView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                } else if ((kind == HTML.Tag.P) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                           (kind == HTML.Tag.H1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                           (kind == HTML.Tag.H2) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                           (kind == HTML.Tag.H3) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                           (kind == HTML.Tag.H4) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                           (kind == HTML.Tag.H5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                           (kind == HTML.Tag.H6) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                           (kind == HTML.Tag.DT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    // paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    return new javax.swing.text.html.ParagraphView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                } else if ((kind == HTML.Tag.MENU) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                           (kind == HTML.Tag.DIR) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                           (kind == HTML.Tag.UL)   ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                           (kind == HTML.Tag.OL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    return new ListView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                } else if (kind == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    return new BodyBlockView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                } else if (kind == HTML.Tag.HTML) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    return new BlockView(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                } else if ((kind == HTML.Tag.LI) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                           (kind == HTML.Tag.CENTER) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                           (kind == HTML.Tag.DL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                           (kind == HTML.Tag.DD) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                           (kind == HTML.Tag.DIV) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                           (kind == HTML.Tag.BLOCKQUOTE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                           (kind == HTML.Tag.PRE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                           (kind == HTML.Tag.FORM)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                    // vertical box
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                    return new BlockView(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                } else if (kind == HTML.Tag.NOFRAMES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                    return new NoFramesView(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                } else if (kind==HTML.Tag.IMG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    return new ImageView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                } else if (kind == HTML.Tag.ISINDEX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    return new IsindexView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                } else if (kind == HTML.Tag.HR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                    return new HRuleView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                } else if (kind == HTML.Tag.BR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                    return new BRView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                } else if (kind == HTML.Tag.TABLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                    return new javax.swing.text.html.TableView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                } else if ((kind == HTML.Tag.INPUT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                           (kind == HTML.Tag.SELECT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                           (kind == HTML.Tag.TEXTAREA)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    return new FormView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                } else if (kind == HTML.Tag.OBJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    return new ObjectView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                } else if (kind == HTML.Tag.FRAMESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                     if (elem.getAttributes().isDefined(HTML.Attribute.ROWS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                         return new FrameSetView(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                     } else if (elem.getAttributes().isDefined(HTML.Attribute.COLS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                         return new FrameSetView(elem, View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                     throw new RuntimeException("Can't build a"  + kind + ", " + elem + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                                     "no ROWS or COLS defined.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                } else if (kind == HTML.Tag.FRAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    return new FrameView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                } else if (kind instanceof HTML.UnknownTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                    return new HiddenTagView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                } else if (kind == HTML.Tag.COMMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    return new CommentView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                } else if (kind == HTML.Tag.HEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    // Make the head never visible, and never load its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    // children. For Cursor positioning,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    // getNextVisualPositionFrom is overriden to always return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    // the end offset of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                    return new BlockView(elem, View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                        public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                        public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                        protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                        public Shape modelToView(int pos, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                               Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                        public int getNextVisualPositionFrom(int pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                                     Position.Bias b, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                                     int direction, Position.Bias[] biasRet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                            return getElement().getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                } else if ((kind == HTML.Tag.TITLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                           (kind == HTML.Tag.META) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                           (kind == HTML.Tag.LINK) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                           (kind == HTML.Tag.STYLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                           (kind == HTML.Tag.SCRIPT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                           (kind == HTML.Tag.AREA) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                           (kind == HTML.Tag.MAP) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                           (kind == HTML.Tag.PARAM) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                           (kind == HTML.Tag.APPLET)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                    return new HiddenTagView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            // If we get here, it's either an element we don't know about
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            // or something from StyledDocument that doesn't have a mapping to HTML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            String nm = (elementName != null) ? (String)elementName :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                                                elem.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            if (nm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                if (nm.equals(AbstractDocument.ContentElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    return new LabelView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                } else if (nm.equals(AbstractDocument.ParagraphElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    return new ParagraphView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                } else if (nm.equals(AbstractDocument.SectionElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    return new BoxView(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                } else if (nm.equals(StyleConstants.ComponentElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                    return new ComponentView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                } else if (nm.equals(StyleConstants.IconElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    return new IconView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            // default to text display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            return new LabelView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        static class BodyBlockView extends BlockView implements ComponentListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            public BodyBlockView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                super(elem,View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            // reimplement major axis requirements to indicate that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            // block is flexible for the body element... so that it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            // be stretched to fill the background properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            protected SizeRequirements calculateMajorAxisRequirements(int axis, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                r = super.calculateMajorAxisRequirements(axis, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                r.maximum = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                Container container = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                Container parentContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                if (container != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    && (container instanceof javax.swing.JEditorPane)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                    && (parentContainer = container.getParent()) != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                    && (parentContainer instanceof javax.swing.JViewport)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    JViewport viewPort = (JViewport)parentContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                    if (cachedViewPort != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1366
                        JViewport cachedObject = cachedViewPort.get();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1367
                        if (cachedObject != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                            if (cachedObject != viewPort) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1369
                                cachedObject.removeComponentListener(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                            cachedViewPort = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                    if (cachedViewPort == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                        viewPort.addComponentListener(this);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1377
                        cachedViewPort = new WeakReference<JViewport>(viewPort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    componentVisibleWidth = viewPort.getExtentSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    if (componentVisibleWidth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    Insets insets = container.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                    viewVisibleWidth = componentVisibleWidth - insets.left - getLeftInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    //try to use viewVisibleWidth if it is smaller than targetSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    targetSpan = Math.min(targetSpan, viewVisibleWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    if (cachedViewPort != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1389
                        JViewport cachedObject = cachedViewPort.get();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1390
                        if (cachedObject != null) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1391
                            cachedObject.removeComponentListener(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                        cachedViewPort = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                super.layoutMinorAxis(targetSpan, axis, offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                //if parent == null unregister component listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    if (cachedViewPort != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                        Object cachedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                        if ((cachedObject = cachedViewPort.get()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                            ((JComponent)cachedObject).removeComponentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                        cachedViewPort = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            public void componentResized(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                if ( !(e.getSource() instanceof JViewport) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                JViewport viewPort = (JViewport)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                if (componentVisibleWidth != viewPort.getExtentSize().width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                    Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                    if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                        AbstractDocument document = (AbstractDocument)getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                        document.readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                            layoutChanged(X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                            preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                            document.readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            public void componentHidden(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            public void componentMoved(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            public void componentShown(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
             * we keep weak reference to viewPort if and only if BodyBoxView is listening for ComponentEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
             * only in that case cachedViewPort is not equal to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
             * we need to keep this reference in order to remove BodyBoxView from viewPort listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
             */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1445
            private Reference<JViewport> cachedViewPort = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            private boolean isListening = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            private int viewVisibleWidth = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            private int componentVisibleWidth = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    // --- Action implementations ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
/** The bold action identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    public static final String  BOLD_ACTION = "html-bold-action";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
/** The italic action identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    public static final String  ITALIC_ACTION = "html-italic-action";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
/** The paragraph left indent action identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    public static final String  PARA_INDENT_LEFT = "html-para-indent-left";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
/** The paragraph right indent action identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    public static final String  PARA_INDENT_RIGHT = "html-para-indent-right";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
/** The  font size increase to next value action identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    public static final String  FONT_CHANGE_BIGGER = "html-font-bigger";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
/** The font size decrease to next value action identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    public static final String  FONT_CHANGE_SMALLER = "html-font-smaller";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
/** The Color choice action identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     The color is passed as an argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    public static final String  COLOR_ACTION = "html-color-action";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
/** The logical style choice action identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     The logical style is passed in as an argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    public static final String  LOGICAL_STYLE_ACTION = "html-logical-style-action";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * Align images at the top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    public static final String  IMG_ALIGN_TOP = "html-image-align-top";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * Align images in the middle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    public static final String  IMG_ALIGN_MIDDLE = "html-image-align-middle";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * Align images at the bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    public static final String  IMG_ALIGN_BOTTOM = "html-image-align-bottom";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * Align images at the border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    public static final String  IMG_BORDER = "html-image-border";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    /** HTML used when inserting tables. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    private static final String INSERT_TABLE_HTML = "<table border=1><tr><td></td></tr></table>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    /** HTML used when inserting unordered lists. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    private static final String INSERT_UL_HTML = "<ul><li></li></ul>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    /** HTML used when inserting ordered lists. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    private static final String INSERT_OL_HTML = "<ol><li></li></ol>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    /** HTML used when inserting hr. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    private static final String INSERT_HR_HTML = "<hr>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    /** HTML used when inserting pre. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    private static final String INSERT_PRE_HTML = "<pre></pre>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    private static final NavigateLinkAction nextLinkAction =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        new NavigateLinkAction("next-link-action");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    private static final NavigateLinkAction previousLinkAction =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        new NavigateLinkAction("previous-link-action");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    private static final ActivateLinkAction activateLinkAction =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        new ActivateLinkAction("activate-link-action");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    private static final Action[] defaultActions = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        new InsertHTMLTextAction("InsertTable", INSERT_TABLE_HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                                 HTML.Tag.BODY, HTML.Tag.TABLE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        new InsertHTMLTextAction("InsertTableRow", INSERT_TABLE_HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                                 HTML.Tag.TABLE, HTML.Tag.TR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                                 HTML.Tag.BODY, HTML.Tag.TABLE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        new InsertHTMLTextAction("InsertTableDataCell", INSERT_TABLE_HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                                 HTML.Tag.TR, HTML.Tag.TD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                                 HTML.Tag.BODY, HTML.Tag.TABLE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        new InsertHTMLTextAction("InsertUnorderedList", INSERT_UL_HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                                 HTML.Tag.BODY, HTML.Tag.UL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        new InsertHTMLTextAction("InsertUnorderedListItem", INSERT_UL_HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                                 HTML.Tag.UL, HTML.Tag.LI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                                 HTML.Tag.BODY, HTML.Tag.UL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        new InsertHTMLTextAction("InsertOrderedList", INSERT_OL_HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                 HTML.Tag.BODY, HTML.Tag.OL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        new InsertHTMLTextAction("InsertOrderedListItem", INSERT_OL_HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                                 HTML.Tag.OL, HTML.Tag.LI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                                 HTML.Tag.BODY, HTML.Tag.OL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        new InsertHRAction(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        new InsertHTMLTextAction("InsertPre", INSERT_PRE_HTML,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                                 HTML.Tag.BODY, HTML.Tag.PRE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        nextLinkAction, previousLinkAction, activateLinkAction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        new BeginAction(beginAction, false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        new BeginAction(selectionBeginAction, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    // link navigation support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    private boolean foundLink = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    private int prevHypertextOffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    private Object linkNavigationTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * An abstract Action providing some convenience methods that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * be useful in inserting HTML into an existing document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * <p>NOTE: None of the convenience methods obtain a lock on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * document. If you have another thread modifying the text these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * methods may have inconsistent behavior, or return the wrong thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  1567
    @SuppressWarnings("serial") // Superclass is not serializable across versions
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
  1568
    public abstract static class HTMLTextAction extends StyledTextAction {
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1569
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1570
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1571
         * Creates a new HTMLTextAction from a string action name.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1572
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1573
         * @param name the name of the action
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1574
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        public HTMLTextAction(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        /**
24269
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1580
         * @param e the JEditorPane
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
         * @return HTMLDocument of <code>e</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        protected HTMLDocument getHTMLDocument(JEditorPane e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            Document d = e.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            if (d instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                return (HTMLDocument) d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            throw new IllegalArgumentException("document must be HTMLDocument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        /**
24269
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1592
         * @param e the JEditorPane
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
         * @return HTMLEditorKit for <code>e</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        protected HTMLEditorKit getHTMLEditorKit(JEditorPane e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            EditorKit k = e.getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            if (k instanceof HTMLEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                return (HTMLEditorKit) k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            throw new IllegalArgumentException("EditorKit must be HTMLEditorKit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
         * Returns an array of the Elements that contain <code>offset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
         * The first elements corresponds to the root.
24269
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1606
         *
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1607
         * @param doc an instance of HTMLDocument
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1608
         * @param offset value of offset
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1609
         * @return an array of the Elements that contain <code>offset</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        protected Element[] getElementsAt(HTMLDocument doc, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            return getElementsAt(doc.getDefaultRootElement(), offset, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
         * Recursive method used by getElementsAt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        private Element[] getElementsAt(Element parent, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                                        int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            if (parent.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                Element[] retValue = new Element[depth + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                retValue[depth] = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            Element[] retValue = getElementsAt(parent.getElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                          (parent.getElementIndex(offset)), offset, depth + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            retValue[depth] = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
         * Returns number of elements, starting at the deepest leaf, needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
         * to get to an element representing <code>tag</code>. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
         * return -1 if no elements is found representing <code>tag</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
         * or 0 if the parent of the leaf at <code>offset</code> represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
         * <code>tag</code>.
24269
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1637
         *
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1638
         * @param doc an instance of HTMLDocument
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1639
         * @param offset an offset to start from
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1640
         * @param tag tag to represent
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 24157
diff changeset
  1641
         * @return number of elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        protected int elementCountToTag(HTMLDocument doc, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                                        HTML.Tag tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            int depth = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            Element e = doc.getCharacterElement(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            while (e != null && e.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                   (StyleConstants.NameAttribute) != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                e = e.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                depth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            return depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         * Returns the deepest element at <code>offset</code> matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         * <code>tag</code>.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1661
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1662
         * @param doc an instance of HTMLDocument
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1663
         * @param offset the specified offset &gt;= 0
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1664
         * @param tag an instance of HTML.Tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1665
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1666
         * @return the deepest element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        protected Element findElementMatchingTag(HTMLDocument doc, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                                                 HTML.Tag tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            Element e = doc.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            Element lastMatch = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                if (e.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                   (StyleConstants.NameAttribute) == tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                    lastMatch = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            return lastMatch;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * InsertHTMLTextAction can be used to insert an arbitrary string of HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * into an existing HTML document. At least two HTML.Tags need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * supplied. The first Tag, parentTag, identifies the parent in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * the document to add the elements to. The second tag, addTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * identifies the first tag that should be added to the document as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * seen in the HTML string. One important thing to remember, is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * the parser is going to generate all the appropriate tags, even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * they aren't in the HTML string passed in.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * For example, lets say you wanted to create an action to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * a table into the body. The parentTag would be HTML.Tag.BODY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * addTag would be HTML.Tag.TABLE, and the string could be something
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * like &lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * <p>There is also an option to supply an alternate parentTag and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * addTag. These will be checked for if there is no parentTag at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  1701
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    public static class InsertHTMLTextAction extends HTMLTextAction {
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1703
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1704
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1705
         * Creates a new InsertHTMLTextAction.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1706
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1707
         * @param name a name of the action
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1708
         * @param html an HTML string
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1709
         * @param parentTag a parent tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1710
         * @param addTag the first tag to start inserting into document
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1711
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        public InsertHTMLTextAction(String name, String html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                                    HTML.Tag parentTag, HTML.Tag addTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            this(name, html, parentTag, addTag, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1717
        /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1718
         * Creates a new InsertHTMLTextAction.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1719
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1720
         * @param name a name of the action
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1721
         * @param html an HTML string
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1722
         * @param parentTag a parent tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1723
         * @param addTag the first tag to start inserting into document
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1724
         * @param alternateParentTag an alternative parent tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1725
         * @param alternateAddTag an alternative tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1726
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        public InsertHTMLTextAction(String name, String html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                                    HTML.Tag parentTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                                    HTML.Tag addTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                                    HTML.Tag alternateParentTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                                    HTML.Tag alternateAddTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            this(name, html, parentTag, addTag, alternateParentTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                 alternateAddTag, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        /* public */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        InsertHTMLTextAction(String name, String html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                                    HTML.Tag parentTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                                    HTML.Tag addTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                                    HTML.Tag alternateParentTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                                    HTML.Tag alternateAddTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                                    boolean adjustSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            this.html = html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            this.parentTag = parentTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            this.addTag = addTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            this.alternateParentTag = alternateParentTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            this.alternateAddTag = alternateAddTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            this.adjustSelection = adjustSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
         * A cover for HTMLEditorKit.insertHTML. If an exception it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
         * thrown it is wrapped in a RuntimeException and thrown.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1755
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1756
         * @param editor an instance of JEditorPane
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1757
         * @param doc the document to insert into
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1758
         * @param offset the offset to insert HTML at
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1759
         * @param html an HTML string
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1760
         * @param popDepth the number of ElementSpec.EndTagTypes to generate
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1761
         *                  before inserting
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1762
         * @param pushDepth the number of ElementSpec.StartTagTypes with a direction
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1763
         *                  of ElementSpec.JoinNextDirection that should be generated
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1764
         *                  before inserting, but after the end tags have been generated
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1765
         * @param addTag the first tag to start inserting into document
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        protected void insertHTML(JEditorPane editor, HTMLDocument doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                                  int offset, String html, int popDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                                  int pushDepth, HTML.Tag addTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                getHTMLEditorKit(editor).insertHTML(doc, offset, html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                                                    popDepth, pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                                                    addTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                throw new RuntimeException("Unable to insert: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                throw new RuntimeException("Unable to insert: " + ble);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
         * This is invoked when inserting at a boundary. It determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
         * the number of pops, and then the number of pushes that need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
         * to be performed, and then invokes insertHTML.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1785
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1786
         * @param editor an instance of JEditorPane
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1787
         * @param doc an instance of HTMLDocument
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1788
         * @param offset an offset to start from
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1789
         * @param insertElement an instance of Element
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1790
         * @param html an HTML string
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1791
         * @param parentTag a parent tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1792
         * @param addTag the first tag to start inserting into document
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1793
         *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
         * @since 1.3
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1795
         *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        protected void insertAtBoundary(JEditorPane editor, HTMLDocument doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                                        int offset, Element insertElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                                        String html, HTML.Tag parentTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                                        HTML.Tag addTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            insertAtBoundry(editor, doc, offset, insertElement, html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                            parentTag, addTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
         * This is invoked when inserting at a boundary. It determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
         * the number of pops, and then the number of pushes that need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
         * to be performed, and then invokes insertHTML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
         * @deprecated As of Java 2 platform v1.3, use insertAtBoundary
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1810
         *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1811
         * @param editor an instance of JEditorPane
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1812
         * @param doc an instance of HTMLDocument
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1813
         * @param offset an offset to start from
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1814
         * @param insertElement an instance of Element
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1815
         * @param html an HTML string
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1816
         * @param parentTag a parent tag
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  1817
         * @param addTag the first tag to start inserting into document
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        protected void insertAtBoundry(JEditorPane editor, HTMLDocument doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                                       int offset, Element insertElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                                       String html, HTML.Tag parentTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                                       HTML.Tag addTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            // Find the common parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            Element e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            Element commonParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            boolean isFirst = (offset == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            if (offset > 0 || insertElement == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                e = doc.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                while (e != null && e.getStartOffset() != offset &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                       !e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                    e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                commonParent = (e != null) ? e.getParentElement() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                // If inserting at the origin, the common parent is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                // insertElement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                commonParent = insertElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            if (commonParent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                // Determine how many pops to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                int pops = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                int pushes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                if (isFirst && insertElement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                    e = commonParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                    while (e != null && !e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                        e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                        pops++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                    e = commonParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                    offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                    while (e != null && !e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                        e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                        pops++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                    // And how many pushes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                    e = commonParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                    offset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                    while (e != null && e != insertElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                        e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                        pushes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                pops = Math.max(0, pops - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                // And insert!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                insertHTML(editor, doc, offset, html, pops, pushes, addTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         * If there is an Element with name <code>tag</code> at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
         * <code>offset</code>, this will invoke either insertAtBoundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
         * or <code>insertHTML</code>. This returns true if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
         * a match, and one of the inserts is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        /*protected*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        boolean insertIntoTag(JEditorPane editor, HTMLDocument doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                              int offset, HTML.Tag tag, HTML.Tag addTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            Element e = findElementMatchingTag(doc, offset, tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            if (e != null && e.getStartOffset() == offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                insertAtBoundary(editor, doc, offset, e, html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                                 tag, addTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            else if (offset > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                int depth = elementCountToTag(doc, offset - 1, tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                if (depth != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                    insertHTML(editor, doc, offset, html, depth, 0, addTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
         * Called after an insertion to adjust the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        /* protected */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        void adjustSelection(JEditorPane pane, HTMLDocument doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                             int startOffset, int oldLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            int newLength = doc.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            if (newLength != oldLength && startOffset < newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                if (startOffset > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                    String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                        text = doc.getText(startOffset - 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                    } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                        text = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                    if (text != null && text.length() > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                        text.charAt(0) == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                        pane.select(startOffset, startOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                        pane.select(startOffset + 1, startOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                    pane.select(1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
         * Inserts the HTML into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
         * @param ae the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            JEditorPane editor = getEditor(ae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                HTMLDocument doc = getHTMLDocument(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                int offset = editor.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                int length = doc.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                boolean inserted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                // Try first choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                if (!insertIntoTag(editor, doc, offset, parentTag, addTag) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                    alternateParentTag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                    // Then alternate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                    inserted = insertIntoTag(editor, doc, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                                             alternateParentTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                                             alternateAddTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                    inserted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                if (adjustSelection && inserted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                    adjustSelection(editor, doc, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        /** HTML to insert. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        protected String html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        /** Tag to check for in the document. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        protected HTML.Tag parentTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        /** Tag in HTML to start adding tags from. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        protected HTML.Tag addTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        /** Alternate Tag to check for in the document if parentTag is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
         * not found. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        protected HTML.Tag alternateParentTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        /** Alternate tag in HTML to start adding tags from if parentTag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
         * is not found and alternateParentTag is found. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        protected HTML.Tag alternateAddTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        /** True indicates the selection should be adjusted after an insert. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        boolean adjustSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * InsertHRAction is special, at actionPerformed time it will determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * the parent HTML.Tag based on the paragraph element at the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  1981
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    static class InsertHRAction extends InsertHTMLTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        InsertHRAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            super("InsertHR", "<hr>", null, HTML.Tag.IMPLIED, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                  false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
         * Inserts the HTML into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
         * @param ae the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            JEditorPane editor = getEditor(ae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                HTMLDocument doc = getHTMLDocument(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                int offset = editor.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                Element paragraph = doc.getParagraphElement(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                if (paragraph.getParentElement() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                    parentTag = (HTML.Tag)paragraph.getParentElement().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                                  getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                                  (StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                    super.actionPerformed(ae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * Returns the object in an AttributeSet matching a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
  2013
    private static Object getAttrValue(AttributeSet attr, HTML.Attribute key) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
  2014
        Enumeration<?> names = attr.getAttributeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            Object nextKey = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            Object nextVal = attr.getAttribute(nextKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            if (nextVal instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                Object value = getAttrValue((AttributeSet)nextVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            } else if (nextKey == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                return nextVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * Action to move the focus on the next or previous hypertext link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * or object. TODO: This method relies on support from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * javax.accessibility package.  The text package should support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * keyboard navigation of text elements directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  2036
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    static class NavigateLinkAction extends TextAction implements CaretListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        private static final FocusHighlightPainter focusPainter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            new FocusHighlightPainter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        private final boolean focusBack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
         * Create this action with the appropriate identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        public NavigateLinkAction(String actionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            super(actionName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            focusBack = "previous-link-action".equals(actionName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
         * Called when the caret position is updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
         * @param e the caret event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        public void caretUpdate(CaretEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            Object src = e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            if (src instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                JTextComponent comp = (JTextComponent) src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                HTMLEditorKit kit = getHTMLEditorKit(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                if (kit != null && kit.foundLink) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                    kit.foundLink = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                    // TODO: The AccessibleContext for the editor should register
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                    // as a listener for CaretEvents and forward the events to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                    // assistive technologies listening for such events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                    comp.getAccessibleContext().firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                        AccessibleContext.ACCESSIBLE_HYPERTEXT_OFFSET,
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  2068
                        Integer.valueOf(kit.prevHypertextOffset),
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  2069
                        Integer.valueOf(e.getDot()));
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
         * The operation to perform when this action is triggered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            JTextComponent comp = getTextComponent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            if (comp == null || comp.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            Document doc = comp.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            HTMLEditorKit kit = getHTMLEditorKit(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            if (doc == null || kit == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            // TODO: Should start successive iterations from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            // current caret position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            ElementIterator ei = new ElementIterator(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            int currentOffset = comp.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            int prevStartOffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            int prevEndOffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
            // highlight the next link or object after the current caret position
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2097
            Element nextElement;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            while ((nextElement = ei.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                String name = nextElement.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                AttributeSet attr = nextElement.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                Object href = getAttrValue(attr, HTML.Attribute.HREF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                if (!(name.equals(HTML.Tag.OBJECT.toString())) && href == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                int elementOffset = nextElement.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                if (focusBack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                    if (elementOffset >= currentOffset &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        prevStartOffset >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                        kit.foundLink = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                        comp.setCaretPosition(prevStartOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                        moveCaretPosition(comp, kit, prevStartOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                                          prevEndOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                        kit.prevHypertextOffset = prevStartOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                } else { // focus forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                    if (elementOffset > currentOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                        kit.foundLink = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                        comp.setCaretPosition(elementOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                        moveCaretPosition(comp, kit, elementOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                                          nextElement.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                        kit.prevHypertextOffset = elementOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                prevStartOffset = nextElement.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                prevEndOffset = nextElement.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            if (focusBack && prevStartOffset >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                kit.foundLink = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                comp.setCaretPosition(prevStartOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                moveCaretPosition(comp, kit, prevStartOffset, prevEndOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                kit.prevHypertextOffset = prevStartOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            }
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
         * Moves the caret from mark to dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        private void moveCaretPosition(JTextComponent comp, HTMLEditorKit kit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                                       int mark, int dot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            Highlighter h = comp.getHighlighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            if (h != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                int p0 = Math.min(dot, mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                int p1 = Math.max(dot, mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                    if (kit.linkNavigationTag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                        h.changeHighlight(kit.linkNavigationTag, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                        kit.linkNavigationTag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                                h.addHighlight(p0, p1, focusPainter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        private HTMLEditorKit getHTMLEditorKit(JTextComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            if (comp instanceof JEditorPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                EditorKit kit = ((JEditorPane) comp).getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                if (kit instanceof HTMLEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                    return (HTMLEditorKit) kit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
         * A highlight painter that draws a one-pixel border around
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
         * the highlighted area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        static class FocusHighlightPainter extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            DefaultHighlighter.DefaultHighlightPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
            FocusHighlightPainter(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                super(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
             * Paints a portion of a highlight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
             * @param g the graphics context
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
  2187
             * @param offs0 the starting model offset &ge; 0
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
  2188
             * @param offs1 the ending model offset &ge; offs1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
             * @param bounds the bounding box of the view, which is not
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24269
diff changeset
  2190
             *               necessarily the region to paint.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
             * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
             * @param view View painting for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
             * @return region in which drawing occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            public Shape paintLayer(Graphics g, int offs0, int offs1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                                    Shape bounds, JTextComponent c, View view) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                Color color = getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                    g.setColor(c.getSelectionColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                    g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                if (offs0 == view.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                    offs1 == view.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                    // Contained in view, can just use bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                    Rectangle alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                    if (bounds instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                        alloc = (Rectangle)bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                        alloc = bounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                    g.drawRect(alloc.x, alloc.y, alloc.width - 1, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                    return alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                    // Should only render part of View.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                        // --- determine locations ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                        Shape shape = view.modelToView(offs0, Position.Bias.Forward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                                                       offs1,Position.Bias.Backward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                                                       bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                        Rectangle r = (shape instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                            (Rectangle)shape : shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                        g.drawRect(r.x, r.y, r.width - 1, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                    } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                        // can't render
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                // Only if exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * Action to activate the hypertext link that has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * TODO: This method relies on support from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * javax.accessibility package.  The text package should support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * keyboard navigation of text elements directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  2246
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
    static class ActivateLinkAction extends TextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
         * Create this action with the appropriate identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        public ActivateLinkAction(String actionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            super(actionName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
         * activates the hyperlink at offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        private void activateLink(String href, HTMLDocument doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                                  JEditorPane editor, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                URL page =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                    (URL)doc.getProperty(Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                URL url = new URL(page, href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                HyperlinkEvent linkEvent = new HyperlinkEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                    (editor, HyperlinkEvent.EventType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                     ACTIVATED, url, url.toExternalForm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                     doc.getCharacterElement(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                editor.fireHyperlinkUpdate(linkEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            } catch (MalformedURLException m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
         * Invokes default action on the object in an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        private void doObjectAction(JEditorPane editor, Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            View view = getView(editor, elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            if (view != null && view instanceof ObjectView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                Component comp = ((ObjectView)view).getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                if (comp != null && comp instanceof Accessible) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2282
                    AccessibleContext ac = comp.getAccessibleContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                    if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                        AccessibleAction aa = ac.getAccessibleAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                        if (aa != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                            aa.doAccessibleAction(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
         * Returns the root view for a document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        private View getRootView(JEditorPane editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            return editor.getUI().getRootView(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
         * Returns a view associated with an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        private View getView(JEditorPane editor, Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            Object lock = lock(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                View rootView = getRootView(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                int start = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                if (rootView != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                    return getView(rootView, elem, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                unlock(lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        private View getView(View parent, Element elem, int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
            if (parent.getElement() == elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            int index = parent.getViewIndex(start, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            if (index != -1 && index < parent.getViewCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                return getView(parent.getView(index), elem, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
         * If possible acquires a lock on the Document.  If a lock has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
         * obtained a key will be retured that should be passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
         * <code>unlock</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        private Object lock(JEditorPane editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            Document document = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            if (document instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                ((AbstractDocument)document).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                return document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
         * Releases a lock previously obtained via <code>lock</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        private void unlock(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                ((AbstractDocument)key).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
         * The operation to perform when this action is triggered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
            JTextComponent c = getTextComponent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            if (c.isEditable() || !(c instanceof JEditorPane)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            JEditorPane editor = (JEditorPane)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            Document d = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            if (d == null || !(d instanceof HTMLDocument)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            HTMLDocument doc = (HTMLDocument)d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            ElementIterator ei = new ElementIterator(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            int currentOffset = editor.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            // invoke the next link or object action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            String urlString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            String objString = null;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2376
            Element currentElement;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            while ((currentElement = ei.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                String name = currentElement.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                AttributeSet attr = currentElement.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                Object href = getAttrValue(attr, HTML.Attribute.HREF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                if (href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                    if (currentOffset >= currentElement.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                        currentOffset <= currentElement.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                        activateLink((String)href, doc, editor, currentOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                } else if (name.equals(HTML.Tag.OBJECT.toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                    Object obj = getAttrValue(attr, HTML.Attribute.CLASSID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                    if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                        if (currentOffset >= currentElement.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                            currentOffset <= currentElement.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                            doObjectAction(editor, currentElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
    private static int getBodyElementStart(JTextComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        Element rootElement = comp.getDocument().getRootElements()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        for (int i = 0; i < rootElement.getElementCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            Element currElement = rootElement.getElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            if("body".equals(currElement.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                return currElement.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * Move the caret to the beginning of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     * @see DefaultEditorKit#beginAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     * @see HTMLEditorKit#getActions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  2420
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    static class BeginAction extends TextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        /* Create this object with the appropriate identifier. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        BeginAction(String nm, boolean select) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
            super(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
            this.select = select;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        /** The operation to perform when this action is triggered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            JTextComponent target = getTextComponent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
            int bodyStart = getBodyElementStart(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
            if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                if (select) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                    target.moveCaretPosition(bodyStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                    target.setCaretPosition(bodyStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        private boolean select;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
}