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