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