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