jdk/src/share/classes/javax/swing/text/html/HTMLDocument.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5763 9a942d6fb0aa
child 10100 c525c5fbb86c
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5763
diff changeset
     2
 * Copyright (c) 1997, 2010, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.font.TextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.undo.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.swing.SwingUtilities2;
5763
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5506
diff changeset
    37
import static sun.swing.SwingUtilities2.IMPLIED_CR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A document that models HTML.  The purpose of this model is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * support both browsing and editing.  As a result, the structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * described by an HTML document is not exactly replicated by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The element structure that is modeled by default, is built by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * class <code>HTMLDocument.HTMLReader</code>, which implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>HTMLEditorKit.ParserCallback</code> protocol that the parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * expects.  To change the structure one can subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <code>HTMLReader</code>, and reimplement the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * #getReader(int)} to return the new reader implementation.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * documentation for <code>HTMLReader</code> should be consulted for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * the details of the default structure created.  The intent is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the document be non-lossy (although reproducing the HTML format may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * result in a different format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>The document models only HTML, and makes no attempt to store
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * view attributes in it.  The elements are identified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <code>StyleContext.NameAttribute</code> attribute, which should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * always have a value of type <code>HTML.Tag</code> that identifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the kind of element.  Some of the elements (such as comments) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * synthesized.  The <code>HTMLFactory</code> uses this attribute to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * determine what kind of view to build.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>This document supports incremental loading.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <code>TokenThreshold</code> property controls how much of the parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * is buffered before trying to update the element structure of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * document.  This property is set by the <code>EditorKit</code> so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * that subclasses can disable it.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>The <code>Base</code> property determines the URL against which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * relative URLs are resolved.  By default, this will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <code>Document.StreamDescriptionProperty</code> if the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * property is a URL.  If a &lt;BASE&gt; tag is encountered, the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * will become the URL specified by that tag.  Because the base URL is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * a property, it can of course be set directly.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>The default content storage mechanism for this document is a gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * buffer (<code>GapContent</code>).  Alternatives can be supplied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * using the constructor that takes a <code>Content</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * implementation.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <h2>Modifying HTMLDocument</h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>In addition to the methods provided by Document and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * StyledDocument for mutating an HTMLDocument, HTMLDocument provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * a number of convenience methods.  The following methods can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * to insert HTML content into an existing document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   <li>{@link #setInnerHTML(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   <li>{@link #setOuterHTML(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   <li>{@link #insertBeforeStart(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   <li>{@link #insertAfterStart(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   <li>{@link #insertBeforeEnd(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   <li>{@link #insertAfterEnd(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>The following examples illustrate using these methods.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * example assumes the HTML document is initialized in the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * way:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * JEditorPane p = new JEditorPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * p.setContentType("text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * p.setText("..."); // Document text is provided below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * HTMLDocument d = (HTMLDocument) p.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p>With the following HTML content:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * &lt;html>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *   &lt;head>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *     &lt;title>An example HTMLDocument&lt;/title>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *     &lt;style type="text/css">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *       div { background-color: silver; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *       ul { color: red; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *     &lt;/style>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *   &lt;/head>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *   &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *     &lt;div id="BOX">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *       &lt;p>Paragraph 1&lt;/p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *       &lt;p>Paragraph 2&lt;/p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *     &lt;/div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *   &lt;/body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * &lt;/html>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p>All the methods for modifying an HTML document require an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Element}.  Elements can be obtained from an HTML document by using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * the method {@link #getElement(Element e, Object attribute, Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * value)}.  It returns the first descendant element that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * specified attribute with the given value, in depth-first order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * For example, <code>d.getElement(d.getDefaultRootElement(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * StyleConstants.NameAttribute, HTML.Tag.P)</code> returns the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * paragraph element.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <p>A convenient shortcut for locating elements is the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * #getElement(String)}; returns an element whose <code>ID</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * attribute matches the specified value.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <code>d.getElement("BOX")</code> returns the <code>DIV</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * element.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p>The {@link #getIterator(HTML.Tag t)} method can also be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * finding all occurrences of the specified HTML tag in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <h3>Inserting elements</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <p>Elements can be inserted before or after the existing children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * of any non-leaf element by using the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <code>insertAfterStart</code> and <code>insertBeforeEnd</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * For example, if <code>e</code> is the <code>DIV</code> element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <code>d.insertAfterStart(e, "&lt;ul>&lt;li>List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * Item&lt;/li>&lt;/ul>")</code> inserts the list before the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * paragraph, and <code>d.insertBeforeEnd(e, "&lt;ul>&lt;li>List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * Item&lt;/li>&lt;/ul>")</code> inserts the list after the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * paragraph.  The <code>DIV</code> block becomes the parent of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * newly inserted elements.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <p>Sibling elements can be inserted before or after any element by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * using the methods <code>insertBeforeStart</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <code>insertAfterEnd</code>.  For example, if <code>e</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <code>DIV</code> element, <code>d.insertBeforeStart(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * "&lt;ul>&lt;li>List Item&lt;/li>&lt;/ul>")</code> inserts the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * before the <code>DIV</code> element, and <code>d.insertAfterEnd(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * "&lt;ul>&lt;li>List Item&lt;/li>&lt;/ul>")</code> inserts the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * after the <code>DIV</code> element.  The newly inserted elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * become siblings of the <code>DIV</code> element.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <h3>Replacing elements</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <p>Elements and all their descendants can be replaced by using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * methods <code>setInnerHTML</code> and <code>setOuterHTML</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * For example, if <code>e</code> is the <code>DIV</code> element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * <code>d.setInnerHTML(e, "&lt;ul>&lt;li>List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * Item&lt;/li>&lt;/ul>")</code> replaces all children paragraphs with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * the list, and <code>d.setOuterHTML(e, "&lt;ul>&lt;li>List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * Item&lt;/li>&lt;/ul>")</code> replaces the <code>DIV</code> element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * itself.  In latter case the parent of the list is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * <code>BODY</code> element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * <h3>Summary</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <p>The following table shows the example document and the results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * of various methods described above.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * <table border=1 cellspacing=0>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *   <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *     <th>Example</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *     <th><code>insertAfterStart</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *     <th><code>insertBeforeEnd</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *     <th><code>insertBeforeStart</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *     <th><code>insertAfterEnd</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *     <th><code>setInnerHTML</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *     <th><code>setOuterHTML</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *   </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *   <tr valign="top">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *     <td nowrap="nowrap">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * <!--insertAfterStart-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *     <td nowrap="nowrap">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *         <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *           <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *         </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * <!--insertBeforeEnd-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *     <td nowrap="nowrap">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *         <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *           <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *         </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * <!--insertBeforeStart-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *     <td nowrap="nowrap">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *       <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *         <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *       </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * <!--insertAfterEnd-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *     <td nowrap="nowrap">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *       <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *         <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *       </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * <!--setInnerHTML-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *     <td nowrap="nowrap">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *         <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *           <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *         </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * <!--setOuterHTML-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *     <td nowrap="nowrap">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *       <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *         <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *       </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *   </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * <p><strong>Warning:</strong> Serialized objects of this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * not be compatible with future Swing releases. The current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * serialization support is appropriate for short term storage or RMI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * between applications running the same version of Swing.  As of 1.4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * support for long term storage of all JavaBeans<sup><font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * size="-2">TM</font></sup> has been added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * <code>java.beans</code> package.  Please see {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * java.beans.XMLEncoder}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 * @author  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * @author  Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
public class HTMLDocument extends DefaultStyledDocument {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Constructs an HTML document using the default buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * and a default <code>StyleSheet</code>.  This is a convenience
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * method for the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <code>HTMLDocument(Content, StyleSheet)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public HTMLDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        this(new GapContent(BUFFER_SIZE_DEFAULT), new StyleSheet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Constructs an HTML document with the default content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * storage implementation and the specified style/attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * storage mechanism.  This is a convenience method for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <code>HTMLDocument(Content, StyleSheet)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param styles  the styles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public HTMLDocument(StyleSheet styles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        this(new GapContent(BUFFER_SIZE_DEFAULT), styles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Constructs an HTML document with the given content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * storage implementation and the given style/attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * storage mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param c  the container for the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param styles the styles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public HTMLDocument(Content c, StyleSheet styles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        super(c, styles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Fetches the reader for the parser to use when loading the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * with HTML.  This is implemented to return an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <code>HTMLDocument.HTMLReader</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Subclasses can reimplement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * method to change how the document gets structured if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * (For example, to handle custom tags, or structurally represent character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * style elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param pos the starting position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @return the reader used by the parser to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public HTMLEditorKit.ParserCallback getReader(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        Object desc = getProperty(Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (desc instanceof URL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            setBase((URL)desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        HTMLReader reader = new HTMLReader(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Returns the reader for the parser to use to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * with HTML.  This is implemented to return an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <code>HTMLDocument.HTMLReader</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Subclasses can reimplement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * method to change how the document gets structured if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * (For example, to handle custom tags, or structurally represent character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * style elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <code>getReader(int, int, int, HTML.Tag, TRUE)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param popDepth   the number of <code>ElementSpec.EndTagTypes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *          to generate before inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param pushDepth  the number of <code>ElementSpec.StartTagTypes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *          with a direction of <code>ElementSpec.JoinNextDirection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *          that should be generated before inserting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *          but after the end tags have been generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param insertTag  the first tag to start inserting into document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the reader used by the parser to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public HTMLEditorKit.ParserCallback getReader(int pos, int popDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                                  int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                                  HTML.Tag insertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return getReader(pos, popDepth, pushDepth, insertTag, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Fetches the reader for the parser to use to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * with HTML.  This is implemented to return an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * HTMLDocument.HTMLReader.  Subclasses can reimplement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * method to change how the document get structured if desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * (e.g. to handle custom tags, structurally represent character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * style elements, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param popDepth   the number of <code>ElementSpec.EndTagTypes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *          to generate before inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param pushDepth  the number of <code>ElementSpec.StartTagTypes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *          with a direction of <code>ElementSpec.JoinNextDirection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *          that should be generated before inserting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *          but after the end tags have been generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param insertTag  the first tag to start inserting into document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param insertInsertTag  false if all the Elements after insertTag should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *        be inserted; otherwise insertTag will be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return the reader used by the parser to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    HTMLEditorKit.ParserCallback getReader(int pos, int popDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                           int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                           HTML.Tag insertTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                           boolean insertInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        Object desc = getProperty(Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (desc instanceof URL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            setBase((URL)desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        HTMLReader reader = new HTMLReader(pos, popDepth, pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                           insertTag, insertInsertTag, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                           true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Returns the location to resolve relative URLs against.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * default this will be the document's URL if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * was loaded from a URL.  If a base tag is found and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * can be parsed, it will be used as the base location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return the base location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public URL getBase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Sets the location to resolve relative URLs against.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * default this will be the document's URL if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * was loaded from a URL.  If a base tag is found and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * can be parsed, it will be used as the base location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>This also sets the base of the <code>StyleSheet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * to be <code>u</code> as well as the base of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param u  the desired base URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public void setBase(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        base = u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        getStyleSheet().setBase(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Inserts new elements in bulk.  This is how elements get created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * in the document.  The parsing determines what structure is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * and creates the specification as a set of tokens that describe the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * edit while leaving the document free of a write-lock.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * can then be called in bursts by the reader to acquire a write-lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * for a shorter duration (i.e. while the document is actually being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * altered).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param offset the starting offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param data the element data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *   represent a valid location in the associated document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    protected void insert(int offset, ElementSpec[] data) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        super.insert(offset, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Updates document structure as a result of text insertion.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * will happen within a write lock.  This implementation simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * parses the inserted content for line breaks and builds up a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * of instructions for the element buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param chng a description of the document change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param attr the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if(attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            attr = contentAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // If this is the composed text element, merge the content attribute to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        else if (attr.isDefined(StyleConstants.ComposedTextAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            ((MutableAttributeSet)attr).addAttributes(contentAttributeSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (attr.isDefined(IMPLIED_CR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            ((MutableAttributeSet)attr).removeAttribute(IMPLIED_CR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        super.insertUpdate(chng, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Replaces the contents of the document with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * element specifications.  This is called before insert if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * the loading is done in bursts.  This is the only method called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * if loading the document entirely in one burst.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param data  the new contents of the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    protected void create(ElementSpec[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        super.create(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Sets attributes for a paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * are not. Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param offset the offset into the paragraph (must be at least 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param length the number of characters affected (must be at least 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param s the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param replace whether to replace existing attributes, or merge them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void setParagraphAttributes(int offset, int length, AttributeSet s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                       boolean replace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // Make sure we send out a change for the length of the paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            int end = Math.min(offset + length, getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            Element e = getParagraphElement(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            offset = e.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            e = getParagraphElement(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            length = Math.max(0, e.getEndOffset() - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            DefaultDocumentEvent changes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                new DefaultDocumentEvent(offset, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                         DocumentEvent.EventType.CHANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            AttributeSet sCopy = s.copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            int lastEnd = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            for (int pos = offset; pos <= end; pos = lastEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                Element paragraph = getParagraphElement(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (lastEnd == paragraph.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    lastEnd++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    lastEnd = paragraph.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                MutableAttributeSet attr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    (MutableAttributeSet) paragraph.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                changes.addEdit(new AttributeUndoableEdit(paragraph, sCopy, replace));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                if (replace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    attr.removeAttributes(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                attr.addAttributes(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            changes.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            fireChangedUpdate(changes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Fetches the <code>StyleSheet</code> with the document-specific display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * rules (CSS) that were specified in the HTML document itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return the <code>StyleSheet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public StyleSheet getStyleSheet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return (StyleSheet) getAttributeContext();
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
     * Fetches an iterator for the specified HTML tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * This can be used for things like iterating over the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * set of anchors contained, or iterating over the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param t the requested <code>HTML.Tag</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @return the <code>Iterator</code> for the given HTML tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @see javax.swing.text.html.HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public Iterator getIterator(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (t.isBlock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            // TBD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        return new LeafIterator(t, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Creates a document leaf element that directly represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * text (doesn't have any children).  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * to return an element of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <code>HTMLDocument.RunElement</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param parent the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param a the attributes for the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param p0 the beginning of the range (must be at least 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param p1 the end of the range (must be at least p0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @return the new element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return new RunElement(parent, a, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Creates a document branch element, that can contain other elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * This is implemented to return an element of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <code>HTMLDocument.BlockElement</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param parent the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param a the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @return the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    protected Element createBranchElement(Element parent, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return new BlockElement(parent, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Creates the root element to be used to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * default document structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @return the element base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    protected AbstractElement createDefaultRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // grabs a write-lock for this initialization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // abandon it during initialization so in normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // operation we can detect an illegitimate attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        // to mutate attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        MutableAttributeSet a = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.HTML);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        BlockElement html = new BlockElement(null, a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        a.removeAttributes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.BODY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        BlockElement body = new BlockElement(html, a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        a.removeAttributes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.P);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        getStyleSheet().addCSSAttributeFromHTML(a, CSS.Attribute.MARGIN_TOP, "0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        BlockElement paragraph = new BlockElement(body, a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        a.removeAttributes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        RunElement brk = new RunElement(paragraph, a, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        Element[] buff = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        buff[0] = brk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        paragraph.replace(0, 0, buff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        buff[0] = paragraph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        body.replace(0, 0, buff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        buff[0] = body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        html.replace(0, 0, buff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Sets the number of tokens to buffer before trying to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * the documents element structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @param n  the number of tokens to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public void setTokenThreshold(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        putProperty(TokenThreshold, new Integer(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Gets the number of tokens to buffer before trying to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * the documents element structure.  The default value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <code>Integer.MAX_VALUE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @return the number of tokens to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public int getTokenThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        Integer i = (Integer) getProperty(TokenThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (i != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return i.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * Determines how unknown tags are handled by the parser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * If set to true, unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * tags are put in the model, otherwise they are dropped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param preservesTags  true if unknown tags should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *          saved in the model, otherwise tags are dropped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @see javax.swing.text.html.HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public void setPreservesUnknownTags(boolean preservesTags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        preservesUnknownTags = preservesTags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Returns the behavior the parser observes when encountering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * unknown tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @see javax.swing.text.html.HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @return true if unknown tags are to be preserved when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public boolean getPreservesUnknownTags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return preservesUnknownTags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * Processes <code>HyperlinkEvents</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * are generated by documents in an HTML frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * The <code>HyperlinkEvent</code> type, as the parameter suggests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * is <code>HTMLFrameHyperlinkEvent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * In addition to the typical information contained in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <code>HyperlinkEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * this event contains the element that corresponds to the frame in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * which the click happened (the source element) and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * target name.  The target name has 4 possible values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * <li>  _self
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * <li>  _parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <li>  _top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <li>  a named frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * If target is _self, the action is to change the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <code>HTML.Attribute.SRC</code> attribute and fires a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <code>ChangedUpdate</code> event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * If the target is _parent, then it deletes the parent element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * which is a &lt;FRAMESET&gt; element, and inserts a new &lt;FRAME&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * element, and sets its <code>HTML.Attribute.SRC</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * to have a value equal to the destination URL and fire a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * <code>RemovedUpdate</code> and <code>InsertUpdate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * If the target is _top, this method does nothing. In the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * of the view for a frame, namely the <code>FrameView</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * the processing of _top is handled.  Given that _top implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * replacing the entire document, it made sense to handle this outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * of the document that it will replace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * If the target is a named frame, then the element hierarchy is searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * for an element with a name equal to the target, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * <code>HTML.Attribute.SRC</code> attribute is updated and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * <code>ChangedUpdate</code> event is fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        String frameName = e.getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        Element element = e.getSourceElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        String urlStr = e.getURL().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (frameName.equals("_self")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
              The source and destination elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
              are the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            updateFrame(element, urlStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        } else if (frameName.equals("_parent")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
              The destination is the parent of the frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            updateFrameSet(element.getParentElement(), urlStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
              locate a named frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            Element targetElement = findFrame(frameName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (targetElement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                updateFrame(targetElement, urlStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Searches the element hierarchy for an FRAME element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * that has its name attribute equal to the <code>frameName</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @param frameName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @return the element whose NAME attribute has a value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *          <code>frameName</code>; returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *          if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    private Element findFrame(String frameName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        ElementIterator it = new ElementIterator(this);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   745
        Element next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        while ((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            AttributeSet attr = next.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            if (matchNameAttribute(attr, HTML.Tag.FRAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                String frameTarget = (String)attr.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                if (frameTarget != null && frameTarget.equals(frameName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Returns true if <code>StyleConstants.NameAttribute</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * equal to the tag that is passed in as a parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @param attr the attributes to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param tag the value to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return true if there is a match, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @see javax.swing.text.html.HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    static boolean matchNameAttribute(AttributeSet attr, HTML.Tag tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        Object o = attr.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (o instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            HTML.Tag name = (HTML.Tag) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            if (name == tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Replaces a frameset branch Element with a frame leaf element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @param element the frameset element to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @param url     the value for the SRC attribute for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *                new frame that will replace the frameset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    private void updateFrameSet(Element element, String url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            int startOffset = element.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            int endOffset = Math.min(getLength(), element.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            String html = "<frame";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                html += " src=\"" + url + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            html += ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            installParserIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            setOuterHTML(element, html);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        } catch (BadLocationException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            // Should handle this better
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // Should handle this better
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Updates the Frame elements <code>HTML.Attribute.SRC attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * and fires a <code>ChangedUpdate</code> event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @param element a FRAME element whose SRC attribute will be updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param url     a string specifying the new value for the SRC attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    private void updateFrame(Element element, String url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            DefaultDocumentEvent changes = new DefaultDocumentEvent(element.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                                                                    1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                                                                    DocumentEvent.EventType.CHANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            AttributeSet sCopy = element.getAttributes().copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            MutableAttributeSet attr = (MutableAttributeSet) element.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            changes.addEdit(new AttributeUndoableEdit(element, sCopy, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            attr.removeAttribute(HTML.Attribute.SRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            attr.addAttribute(HTML.Attribute.SRC, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            changes.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            fireChangedUpdate(changes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Returns true if the document will be viewed in a frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @return true if document will be viewed in a frame, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    boolean isFrameDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return frameDocument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * Sets a boolean state about whether the document will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * viewed in a frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @param frameDoc  true if the document will be viewed in a frame,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *          otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    void setFrameDocumentState(boolean frameDoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        this.frameDocument = frameDoc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * Adds the specified map, this will remove a Map that has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * previously registered with the same name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param map  the <code>Map</code> to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    void addMap(Map map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        String     name = map.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            Object     maps = getProperty(MAP_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            if (maps == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                maps = new Hashtable(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                putProperty(MAP_PROPERTY, maps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            if (maps instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                ((Hashtable)maps).put("#" + name, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Removes a previously registered map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param map the <code>Map</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    void removeMap(Map map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        String     name = map.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            Object     maps = getProperty(MAP_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            if (maps instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                ((Hashtable)maps).remove("#" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * Returns the Map associated with the given name.
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   891
     * @param name the name of the desired <code>Map</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @return the <code>Map</code> or <code>null</code> if it can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *          be found, or if <code>name</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    Map getMap(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            Object     maps = getProperty(MAP_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            if (maps != null && (maps instanceof Hashtable)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                return (Map)((Hashtable)maps).get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Returns an <code>Enumeration</code> of the possible Maps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @return the enumerated list of maps, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *          if the maps are not an instance of <code>Hashtable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    Enumeration getMaps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        Object     maps = getProperty(MAP_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (maps instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return ((Hashtable)maps).elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Sets the content type language used for style sheets that do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * explicitly specify the type. The default is text/css.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @param contentType  the content type language for the style sheets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    /* public */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    void setDefaultStyleSheetType(String contentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        putProperty(StyleType, contentType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * Returns the content type language used for style sheets. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * is text/css.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @return the content type language used for the style sheets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /* public */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    String getDefaultStyleSheetType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        String retValue = (String)getProperty(StyleType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        if (retValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            return "text/css";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * Sets the parser that is used by the methods that insert html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * into the existing document, such as <code>setInnerHTML</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * and <code>setOuterHTML</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * <code>HTMLEditorKit.createDefaultDocument</code> will set the parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * for you. If you create an <code>HTMLDocument</code> by hand,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * be sure and set the parser accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @param parser the parser to be used for text insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    public void setParser(HTMLEditorKit.Parser parser) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        this.parser = parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        putProperty("__PARSER__", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * Returns the parser that is used when inserting HTML into the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @return the parser used for text insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    public HTMLEditorKit.Parser getParser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        Object p = getProperty("__PARSER__");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (p instanceof HTMLEditorKit.Parser) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            return (HTMLEditorKit.Parser)p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        return parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Replaces the children of the given element with the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * specified as an HTML string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <p>This will be seen as at least two events, n inserts followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * a remove.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *     &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *       |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *     <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *      /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *    &lt;p>   &lt;p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * <p>Invoking <code>setInnerHTML(elem, "&lt;ul>&lt;li>")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * results in the following structure (new elements are <font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * color="red">in red</font>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *     &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *       |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *     <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *         <font color="red">&lt;ul></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *           <font color="red">&lt;li></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * <p>Parameter <code>elem</code> must not be a leaf element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * otherwise an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * If either <code>elem</code> or <code>htmlText</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * is <code>null</code>, no changes are made to the document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * <p>For this to work correcty, the document must have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @param elem the branch element whose children will be replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @param htmlText the string to be parsed and assigned to <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @throws IllegalArgumentException if <code>elem</code> is a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @throws IllegalStateException if an <code>HTMLEditorKit.Parser</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *         has not been defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    public void setInnerHTML(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                             BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (elem != null && elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                ("Can not set inner HTML of a leaf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        if (elem != null && htmlText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            int oldCount = elem.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            int insertPosition = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            insertHTML(elem, elem.getStartOffset(), htmlText, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            if (elem.getElementCount() > oldCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                // Elements were inserted, do the cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                removeElements(elem, elem.getElementCount() - oldCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                               oldCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * Replaces the given element in the parent with the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * specified as an HTML string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * <p>This will be seen as at least two events, n inserts followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * a remove.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * <p>When replacing a leaf this will attempt to make sure there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * a newline present if one is needed. This may result in an additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * element being inserted. Consider, if you were to replace a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * element that contained a newline with &lt;img&gt; this would create
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * two elements, one for the image, ane one for the newline.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * <p>If you try to replace the element at length you will most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * likely end up with two elements, eg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * <code>setOuterHTML(getCharacterElement (getLength()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * "blah")</code> will result in two leaf elements at the end, one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * representing 'blah', and the other representing the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * element.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *     &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     *       |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *     <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *      /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *    &lt;p>   &lt;p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * <p>Invoking <code>setOuterHTML(elem, "&lt;ul>&lt;li>")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * results in the following structure (new elements are <font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * color="red">in red</font>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *    &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *      |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *     <font color="red">&lt;ul></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *       <font color="red">&lt;li></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * <p>If either <code>elem</code> or <code>htmlText</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * parameter is <code>null</code>, no changes are made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * <p>For this to work correcty, the document must have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * HTMLEditorKit.Parser set. This will be the case if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * @param elem the element to replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @param htmlText the string to be parsed and inserted in place of <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *         been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    public void setOuterHTML(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                            BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (elem != null && elem.getParentElement() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            htmlText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            int start = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            int end = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            int startLength = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            // We don't want a newline if elem is a leaf, and doesn't contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            // a newline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            boolean wantsNewline = !elem.isLeaf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (!wantsNewline && (end > startLength ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                                 getText(end - 1, 1).charAt(0) == NEWLINE[0])){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                wantsNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            Element parent = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            int oldCount = parent.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            insertHTML(parent, start, htmlText, wantsNewline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            // Remove old.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            int newLength = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            if (oldCount != parent.getElementCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                int removeIndex = parent.getElementIndex(start + newLength -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                                                         startLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                removeElements(parent, removeIndex, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Inserts the HTML specified as a string at the start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *     &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *       |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *     <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *      /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *    &lt;p>   &lt;p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * <p>Invoking <code>insertAfterStart(elem,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * "&lt;ul>&lt;li>")</code> results in the following structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * (new elements are <font color="red">in red</font>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *        &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     *          |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *        <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     *       /  |  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     *    <font color="red">&lt;ul></font> &lt;p> &lt;p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *     /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *  <font color="red">&lt;li></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * <p>Unlike the <code>insertBeforeStart</code> method, new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *  elements become <em>children</em> of the specified element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *  not siblings.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <p>Parameter <code>elem</code> must not be a leaf element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * otherwise an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * If either <code>elem</code> or <code>htmlText</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * is <code>null</code>, no changes are made to the document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * <p>For this to work correcty, the document must have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @param elem the branch element to be the root for the new text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @param htmlText the string to be parsed and assigned to <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @throws IllegalArgumentException if <code>elem</code> is a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     *         been set on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    public void insertAfterStart(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                                 BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if (elem != null && elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                ("Can not insert HTML after start of a leaf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        insertHTML(elem, elem.getStartOffset(), htmlText, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * Inserts the HTML specified as a string at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * <p> If <code>elem</code>'s children are leaves, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * character at a <code>elem.getEndOffset() - 1</code> is a newline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * this will insert before the newline so that there isn't text after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * the newline.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *     &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     *       |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *     <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *      /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *    &lt;p>   &lt;p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * <p>Invoking <code>insertBeforeEnd(elem, "&lt;ul>&lt;li>")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * results in the following structure (new elements are <font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * color="red">in red</font>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     *        &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *          |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *        <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *       /  |  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     *     &lt;p> &lt;p> <font color="red">&lt;ul></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *               <font color="red">&lt;li></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * <p>Unlike the <code>insertAfterEnd</code> method, new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * become <em>children</em> of the specified element, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * siblings.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * <p>Parameter <code>elem</code> must not be a leaf element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * otherwise an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * If either <code>elem</code> or <code>htmlText</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * is <code>null</code>, no changes are made to the document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * <p>For this to work correcty, the document must have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @param elem the element to be the root for the new text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * @param htmlText the string to be parsed and assigned to <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @throws IllegalArgumentException if <code>elem</code> is a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *         been set on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    public void insertBeforeEnd(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                                BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        if (elem != null && elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                ("Can not set inner HTML before end of leaf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            int offset = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            if (elem.getElement(elem.getElementIndex(offset - 1)).isLeaf() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                getText(offset - 1, 1).charAt(0) == NEWLINE[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            insertHTML(elem, offset, htmlText, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * Inserts the HTML specified as a string before the start of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *     &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *       |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *     <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *      /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *    &lt;p>   &lt;p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * <p>Invoking <code>insertBeforeStart(elem,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * "&lt;ul>&lt;li>")</code> results in the following structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * (new elements are <font color="red">in red</font>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *        &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *         /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *      <font color="red">&lt;ul></font> <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *       /    /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *     <font color="red">&lt;li></font> &lt;p>  &lt;p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * <p>Unlike the <code>insertAfterStart</code> method, new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * elements become <em>siblings</em> of the specified element, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * children.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * <p>If either <code>elem</code> or <code>htmlText</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * parameter is <code>null</code>, no changes are made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * <p>For this to work correcty, the document must have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * @param elem the element the content is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * @param htmlText the string to be parsed and inserted before <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *         been set on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    public void insertBeforeStart(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                                  BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            Element parent = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                insertHTML(parent, elem.getStartOffset(), htmlText, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * Inserts the HTML specified as a string after the the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *     &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *       |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     *     <b>&lt;div></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *      /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *    &lt;p>   &lt;p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * <p>Invoking <code>insertAfterEnd(elem, "&lt;ul>&lt;li>")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * results in the following structure (new elements are <font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * color="red">in red</font>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     *        &lt;body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     *         /  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     *      <b>&lt;div></b> <font color="red">&lt;ul></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     *       / \    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     *     &lt;p> &lt;p>  <font color="red">&lt;li></font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * <p>Unlike the <code>insertBeforeEnd</code> method, new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * become <em>siblings</em> of the specified element, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * children.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * <p>If either <code>elem</code> or <code>htmlText</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * parameter is <code>null</code>, no changes are made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * <p>For this to work correcty, the document must have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * @param elem the element the content is inserted after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * @param htmlText the string to be parsed and inserted after <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *         been set on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    public void insertAfterEnd(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                               BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            Element parent = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                int offset = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                if (offset > getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                    offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                else if (elem.isLeaf() && getText(offset - 1, 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                    charAt(0) == NEWLINE[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                insertHTML(parent, offset, htmlText, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * Returns the element that has the given id <code>Attribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * If the element can't be found, <code>null</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * Note that this method works on an <code>Attribute</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * <i>not</i> a character tag.  In the following HTML snippet:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * <code>&lt;a id="HelloThere"&gt;</code> the attribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * 'id' and the character tag is 'a'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * <code>getElement(RootElement, HTML.Attribute.id, id)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * This is not thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @param id  the string representing the desired <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @return the element with the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *          or <code>null</code> if it can't be found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *          or <code>null</code> if <code>id</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @see javax.swing.text.html.HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    public Element getElement(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        if (id == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        return getElement(getDefaultRootElement(), HTML.Attribute.ID, id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                          true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * Returns the child element of <code>e</code> that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * attribute, <code>attribute</code> with value <code>value</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * <code>null</code> if one isn't found. This is not thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * @param e the root element where the search begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * @param attribute the desired <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * @param value the values for the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * @return the element with the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *          and the specified <code>value</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *          if it can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @see javax.swing.text.html.HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    public Element getElement(Element e, Object attribute, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        return getElement(e, attribute, value, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * Returns the child element of <code>e</code> that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * attribute, <code>attribute</code> with value <code>value</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * <code>null</code> if one isn't found. This is not thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * If <code>searchLeafAttributes</code> is true, and <code>e</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * a leaf, any attributes that are instances of <code>HTML.Tag</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * with a value that is an <code>AttributeSet</code> will also be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * @param e the root element where the search begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * @param attribute the desired <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * @param value the values for the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * @return the element with the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     *          and the specified <code>value</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     *          if it can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * @see javax.swing.text.html.HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    private Element getElement(Element e, Object attribute, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                               boolean searchLeafAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        AttributeSet attr = e.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        if (attr != null && attr.isDefined(attribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            if (value.equals(attr.getAttribute(attribute))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        if (!e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            for (int counter = 0, maxCounter = e.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                 counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                Element retValue = getElement(e.getElement(counter), attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                                              value, searchLeafAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                if (retValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                    return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        else if (searchLeafAttributes && attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            // For some leaf elements we store the actual attributes inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            // the AttributeSet of the Element (such as anchors).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            Enumeration names = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                    Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                    if ((name instanceof HTML.Tag) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                        (attr.getAttribute(name) instanceof AttributeSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                        AttributeSet check = (AttributeSet)attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                                             getAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                        if (check.isDefined(attribute) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                            value.equals(check.getAttribute(attribute))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        return null;
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
     * Verifies the document has an <code>HTMLEditorKit.Parser</code> set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * If <code>getParser</code> returns <code>null</code>, this will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * IllegalStateException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @throws IllegalStateException if the document does not have a Parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    private void verifyParser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        if (getParser() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            throw new IllegalStateException("No HTMLEditorKit.Parser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * Installs a default Parser if one has not been installed yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    private void installParserIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        if (getParser() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            setParser(new HTMLEditorKit().getParser());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * Inserts a string of HTML into the document at the given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * <code>parent</code> is used to identify the location to insert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * <code>html</code>. If <code>parent</code> is a leaf this can have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * unexpected results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    private void insertHTML(Element parent, int offset, String html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                            boolean wantsTrailingNewline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                 throws BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        if (parent != null && html != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            HTMLEditorKit.Parser parser = getParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            if (parser != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                int lastOffset = Math.max(0, offset - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                Element charElement = getCharacterElement(lastOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                Element commonParent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                int pop = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                int push = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                if (parent.getStartOffset() > lastOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                    while (commonParent != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                           commonParent.getStartOffset() > lastOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                        commonParent = commonParent.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                        push++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    if (commonParent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                        throw new BadLocationException("No common parent",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                                                       offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                while (charElement != null && charElement != commonParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    pop++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    charElement = charElement.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                if (charElement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    // Found it, do the insert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    HTMLReader reader = new HTMLReader(offset, pop - 1, push,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                                                       null, false, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                                                       wantsTrailingNewline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    parser.parse(new StringReader(html), reader, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                    reader.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * Removes child Elements of the passed in Element <code>e</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * will do the necessary cleanup to ensure the element representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * end character is correctly created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * <p>This is not a general purpose method, it assumes that <code>e</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * will still have at least one child after the remove, and it assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * the character at <code>e.getStartOffset() - 1</code> is a newline and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * is of length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    private void removeElements(Element e, int index, int count) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            int start = e.getElement(index).getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            int end = e.getElement(index + count - 1).getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            if (end > getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                removeElementsAtEnd(e, index, count, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                removeElements(e, index, count, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * Called to remove child elements of <code>e</code> when one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * elements to remove is representing the end character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * <p>Since the Content will not allow a removal to the end character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * this will do a remove from <code>start - 1</code> to <code>end</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * The end Element(s) will be removed, and the element representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * <code>start - 1</code> to <code>start</code> will be recreated. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * Element has to be recreated as after the content removal its offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * become <code>start - 1</code> to <code>start - 1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    private void removeElementsAtEnd(Element e, int index, int count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                         int start, int end) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        // index must be > 0 otherwise no insert would have happened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        boolean isLeaf = (e.getElement(index - 1).isLeaf());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        DefaultDocumentEvent dde = new DefaultDocumentEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                       start - 1, end - start + 1, DocumentEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                       EventType.REMOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        if (isLeaf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            Element endE = getCharacterElement(getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            // e.getElement(index - 1) should represent the newline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            if (endE.getParentElement() != e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                // The hiearchies don't match, we'll have to manually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                // recreate the leaf at e.getElement(index - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                replace(dde, e, index, ++count, start, end, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                // The hierarchies for the end Element and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                // e.getElement(index - 1), match, we can safely remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                // the Elements and the end content will be aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                // appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                replace(dde, e, index, count, start, end, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            // Not a leaf, descend until we find the leaf representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            // start - 1 and remove it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            Element newLineE = e.getElement(index - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            while (!newLineE.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                newLineE = newLineE.getElement(newLineE.getElementCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            newLineE = newLineE.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            replace(dde, e, index, count, start, end, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            replace(dde, newLineE, newLineE.getElementCount() - 1, 1, start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                    end, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        postRemoveUpdate(dde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        dde.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        fireRemoveUpdate(dde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        fireUndoableEditUpdate(new UndoableEditEvent(this, dde));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * This is used by <code>removeElementsAtEnd</code>, it removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * <code>count</code> elements starting at <code>start</code> from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * <code>e</code>.  If <code>remove</code> is true text of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * <code>start - 1</code> to <code>end - 1</code> is removed.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * <code>create</code> is true a new leaf is created of length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    private void replace(DefaultDocumentEvent dde, Element e, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                         int count, int start, int end, boolean remove,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                         boolean create) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        Element[] added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        AttributeSet attrs = e.getElement(index).getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        Element[] removed = new Element[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        for (int counter = 0; counter < count; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            removed[counter] = e.getElement(counter + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        if (remove) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            UndoableEdit u = getContent().remove(start - 1, end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                dde.addEdit(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            added = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            added[0] = createLeafElement(e, attrs, start - 1, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            added = new Element[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        dde.addEdit(new ElementEdit(e, index, removed, added));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        ((AbstractDocument.BranchElement)e).replace(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                                             index, removed.length, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * Called to remove child Elements when the end is not touched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    private void removeElements(Element e, int index, int count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                             int start, int end) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        Element[] removed = new Element[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        Element[] added = new Element[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        for (int counter = 0; counter < count; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            removed[counter] = e.getElement(counter + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        DefaultDocumentEvent dde = new DefaultDocumentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                (start, end - start, DocumentEvent.EventType.REMOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        ((AbstractDocument.BranchElement)e).replace(index, removed.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                                                    added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        dde.addEdit(new ElementEdit(e, index, removed, added));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        UndoableEdit u = getContent().remove(start, end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            dde.addEdit(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        postRemoveUpdate(dde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        dde.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        fireRemoveUpdate(dde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            fireUndoableEditUpdate(new UndoableEditEvent(this, dde));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    // These two are provided for inner class access. The are named different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    // than the super class as the super class implementations are final.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    void obtainLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    void releaseLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    // Provided for inner class access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    protected void fireChangedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        super.fireChangedUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
    protected void fireUndoableEditUpdate(UndoableEditEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        super.fireUndoableEditUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    boolean hasBaseTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        return hasBaseTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    String getBaseTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        return baseTarget;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * state defines whether the document is a frame document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    private boolean frameDocument = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    private boolean preservesUnknownTags = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * Used to store button groups for radio buttons in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * a form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1759
    private HashMap<String, ButtonGroup> radioButtonGroupsMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * Document property for the number of tokens to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * before building an element subtree to represent them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    static final String TokenThreshold = "token threshold";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    private static final int MaxThreshold = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    private static final int StepThreshold = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * Document property key value. The value for the key will be a Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * of Strings that are comments not found in the body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    public static final String AdditionalComments = "AdditionalComments";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * Document property key value. The value for the key will be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * String indicating the default type of stylesheet links.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    /* public */ static final String StyleType = "StyleType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * The location to resolve relative URLs against.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * default this will be the document's URL if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * was loaded from a URL.  If a base tag is found and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * can be parsed, it will be used as the base location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    URL base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * does the document have base tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    boolean hasBaseTag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * BASE tag's TARGET attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    private String baseTarget = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * The parser that is used when inserting html into the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    private HTMLEditorKit.Parser parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * Used for inserts when a null AttributeSet is supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    private static AttributeSet contentAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * Property Maps are registered under, will be a Hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    static String MAP_PROPERTY = "__MAP__";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    private static char[] NEWLINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * I18N property key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     *
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1823
     * @see AbstractDocument#I18NProperty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    private static final String I18NProperty = "i18n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        contentAttributeSet = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        ((MutableAttributeSet)contentAttributeSet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                        addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                                     HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        NEWLINE = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        NEWLINE[0] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * An iterator to iterate over a particular type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * tag.  The iterator is not thread safe.  If reliable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * access to the document is not already ensured by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * the context under which the iterator is being used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * its use should be performed under the protection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * Document.render.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    public static abstract class Iterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
         * Return the attributes for this tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
         * @return the <code>AttributeSet</code> for this tag, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
         *      <code>null</code> if none can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        public abstract AttributeSet getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
         * Returns the start of the range for which the current occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
         * the tag is defined and has the same attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
         * @return the start of the range, or -1 if it can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        public abstract int getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
         * Returns the end of the range for which the current occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
         * the tag is defined and has the same attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
         * @return the end of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        public abstract int getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
         * Move the iterator forward to the next occurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
         * of the tag it represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        public abstract void next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         * Indicates if the iterator is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
         * representing an occurrence of a tag.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
         * false there are no more tags for this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
         * @return true if the iterator is currently representing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         *              occurrence of a tag, otherwise returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        public abstract boolean isValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
         * Type of tag this iterator represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        public abstract HTML.Tag getTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * An iterator to iterate over a particular type of tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    static class LeafIterator extends Iterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        LeafIterator(HTML.Tag t, Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            tag = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            pos = new ElementIterator(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            endOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
         * Returns the attributes for this tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
         * @return the <code>AttributeSet</code> for this tag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
         *              or <code>null</code> if none can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            Element elem = pos.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                AttributeSet a = (AttributeSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                    elem.getAttributes().getAttribute(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                if (a == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1914
                    a = elem.getAttributes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         * Returns the start of the range for which the current occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
         * the tag is defined and has the same attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
         * @return the start of the range, or -1 if it can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            Element elem = pos.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                return elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
         * Returns the end of the range for which the current occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
         * the tag is defined and has the same attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
         * @return the end of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            return endOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
         * Moves the iterator forward to the next occurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
         * of the tag it represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        public void next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            for (nextLeaf(pos); isValid(); nextLeaf(pos)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                Element elem = pos.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                if (elem.getStartOffset() >= endOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                    AttributeSet a = pos.current().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                    if (a.isDefined(tag) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                        a.getAttribute(StyleConstants.NameAttribute) == tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                        // we found the next one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                        setEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
         * Returns the type of tag this iterator represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
         * @return the <code>HTML.Tag</code> that this iterator represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
         * @see javax.swing.text.html.HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        public HTML.Tag getTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            return tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
         * Returns true if the current position is not <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
         * @return true if current position is not <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
         *              otherwise returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        public boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            return (pos.current() != null);
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 given iterator to the next leaf element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
         * @param iter  the iterator to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        void nextLeaf(ElementIterator iter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            for (iter.next(); iter.current() != null; iter.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                Element e = iter.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                if (e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
         * Marches a cloned iterator forward to locate the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
         * of the run.  This sets the value of <code>endOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        void setEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            AttributeSet a0 = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            endOffset = pos.current().getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            ElementIterator fwd = (ElementIterator) pos.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            for (nextLeaf(fwd); fwd.current() != null; nextLeaf(fwd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                Element e = fwd.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                AttributeSet a1 = (AttributeSet) e.getAttributes().getAttribute(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                if ((a1 == null) || (! a1.equals(a0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                endOffset = e.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        private int endOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        private HTML.Tag tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        private ElementIterator pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * An HTML reader to load an HTML document with an HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     * element structure.  This is a set of callbacks from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * the parser, implemented to create a set of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * tagged with attributes.  The parse builds up tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * (ElementSpec) that describe the element subtree desired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * and burst it into the document under the protection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * a write lock using the insert method on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * outer class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * The reader can be configured by registering actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * (of type <code>HTMLDocument.HTMLReader.TagAction</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * that describe how to handle the action.  The idea behind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * the actions provided is that the most natural text editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * operations can be provided if the element structure boils
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * down to paragraphs with runs of some kind of style
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * in them.  Some things are more naturally specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * structurally, so arbitrary structure should be allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * above the paragraphs, but will need to be edited with structural
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * actions.  The implication of this is that some of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * HTML elements specified in the stream being parsed will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * be collapsed into attributes, and in some cases paragraphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * will be synthesized.  When HTML elements have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * converted to attributes, the attribute key will be of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * type HTML.Tag, and the value will be of type AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * so that no information is lost.  This enables many of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * existing actions to work so that the user can type input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * hit the return key, backspace, delete, etc and have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     * reasonable result.  Selections can be created, and attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * applied or removed, etc.  With this in mind, the work done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * by the reader can be categorized into the following kinds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * of tasks:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * <dt>Block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * <dd>Build the structure like it's specified in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * This produces elements that contain other elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * <dt>Paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * <dd>Like block except that it's expected that the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * will be used with a paragraph view so a paragraph element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * won't need to be synthesized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * <dt>Character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * <dd>Contribute the element as an attribute that will start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * and stop at arbitrary text locations.  This will ultimately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * be mixed into a run of text, with all of the currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * flattened HTML character elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * <dt>Special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * <dd>Produce an embedded graphical element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * <dt>Form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * <dd>Produce an element that is like the embedded graphical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * element, except that it also has a component model associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * <dt>Hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * <dd>Create an element that is hidden from view when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * document is being viewed read-only, and visible when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * document is being edited.  This is useful to keep the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * model from losing information, and used to store things
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * like comments and unrecognized tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * Currently, &lt;APPLET&gt;, &lt;PARAM&gt;, &lt;MAP&gt;, &lt;AREA&gt;, &lt;LINK&gt;,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * &lt;SCRIPT&gt; and &lt;STYLE&gt; are unsupported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * The assignment of the actions described is shown in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * following table for the tags defined in <code>HTML.Tag</code>.<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * <table border=1 summary="HTML tags and assigned actions">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * <tr><th>Tag</th><th>Action</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * <tr><td><code>HTML.Tag.A</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * <tr><td><code>HTML.Tag.ADDRESS</code>   <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * <tr><td><code>HTML.Tag.APPLET</code>    <td>HiddenAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * <tr><td><code>HTML.Tag.AREA</code>      <td>AreaAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * <tr><td><code>HTML.Tag.B</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * <tr><td><code>HTML.Tag.BASE</code>      <td>BaseAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * <tr><td><code>HTML.Tag.BASEFONT</code>  <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * <tr><td><code>HTML.Tag.BIG</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * <tr><td><code>HTML.Tag.BLOCKQUOTE</code><td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * <tr><td><code>HTML.Tag.BODY</code>      <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * <tr><td><code>HTML.Tag.BR</code>        <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * <tr><td><code>HTML.Tag.CAPTION</code>   <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * <tr><td><code>HTML.Tag.CENTER</code>    <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * <tr><td><code>HTML.Tag.CITE</code>      <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * <tr><td><code>HTML.Tag.CODE</code>      <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * <tr><td><code>HTML.Tag.DD</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * <tr><td><code>HTML.Tag.DFN</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * <tr><td><code>HTML.Tag.DIR</code>       <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     * <tr><td><code>HTML.Tag.DIV</code>       <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     * <tr><td><code>HTML.Tag.DL</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     * <tr><td><code>HTML.Tag.DT</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * <tr><td><code>HTML.Tag.EM</code>        <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * <tr><td><code>HTML.Tag.FONT</code>      <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * <tr><td><code>HTML.Tag.FORM</code>      <td>As of 1.4 a BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * <tr><td><code>HTML.Tag.FRAME</code>     <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * <tr><td><code>HTML.Tag.FRAMESET</code>  <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * <tr><td><code>HTML.Tag.H1</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * <tr><td><code>HTML.Tag.H2</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * <tr><td><code>HTML.Tag.H3</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * <tr><td><code>HTML.Tag.H4</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * <tr><td><code>HTML.Tag.H5</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * <tr><td><code>HTML.Tag.H6</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * <tr><td><code>HTML.Tag.HEAD</code>      <td>HeadAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * <tr><td><code>HTML.Tag.HR</code>        <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * <tr><td><code>HTML.Tag.HTML</code>      <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * <tr><td><code>HTML.Tag.I</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * <tr><td><code>HTML.Tag.IMG</code>       <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * <tr><td><code>HTML.Tag.INPUT</code>     <td>FormAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * <tr><td><code>HTML.Tag.ISINDEX</code>   <td>IsndexAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * <tr><td><code>HTML.Tag.KBD</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * <tr><td><code>HTML.Tag.LI</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * <tr><td><code>HTML.Tag.LINK</code>      <td>LinkAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * <tr><td><code>HTML.Tag.MAP</code>       <td>MapAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     * <tr><td><code>HTML.Tag.MENU</code>      <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * <tr><td><code>HTML.Tag.META</code>      <td>MetaAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * <tr><td><code>HTML.Tag.NOFRAMES</code>  <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * <tr><td><code>HTML.Tag.OBJECT</code>    <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * <tr><td><code>HTML.Tag.OL</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * <tr><td><code>HTML.Tag.OPTION</code>    <td>FormAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * <tr><td><code>HTML.Tag.P</code>         <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * <tr><td><code>HTML.Tag.PARAM</code>     <td>HiddenAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * <tr><td><code>HTML.Tag.PRE</code>       <td>PreAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * <tr><td><code>HTML.Tag.SAMP</code>      <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * <tr><td><code>HTML.Tag.SCRIPT</code>    <td>HiddenAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     * <tr><td><code>HTML.Tag.SELECT</code>    <td>FormAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * <tr><td><code>HTML.Tag.SMALL</code>     <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * <tr><td><code>HTML.Tag.STRIKE</code>    <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * <tr><td><code>HTML.Tag.S</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * <tr><td><code>HTML.Tag.STRONG</code>    <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * <tr><td><code>HTML.Tag.STYLE</code>     <td>StyleAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * <tr><td><code>HTML.Tag.SUB</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * <tr><td><code>HTML.Tag.SUP</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * <tr><td><code>HTML.Tag.TABLE</code>     <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * <tr><td><code>HTML.Tag.TD</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * <tr><td><code>HTML.Tag.TEXTAREA</code>  <td>FormAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     * <tr><td><code>HTML.Tag.TH</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * <tr><td><code>HTML.Tag.TITLE</code>     <td>TitleAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * <tr><td><code>HTML.Tag.TR</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * <tr><td><code>HTML.Tag.TT</code>        <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * <tr><td><code>HTML.Tag.U</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * <tr><td><code>HTML.Tag.UL</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * <tr><td><code>HTML.Tag.VAR</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     * Once &lt;/html> is encountered, the Actions are no longer notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    public class HTMLReader extends HTMLEditorKit.ParserCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        public HTMLReader(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            this(offset, 0, 0, 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
        public HTMLReader(int offset, int popDepth, int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                          HTML.Tag insertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            this(offset, popDepth, pushDepth, insertTag, true, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
         * Generates a RuntimeException (will eventually generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
         * a BadLocationException when API changes are alloced) if inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
         * into non empty document, <code>insertTag</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
         * non-<code>null</code>, and <code>offset</code> is not in the body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        // PENDING(sky): Add throws BadLocationException and remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        // RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        HTMLReader(int offset, int popDepth, int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                   HTML.Tag insertTag, boolean insertInsertTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                   boolean insertAfterImplied, boolean wantsTrailingNewline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
            emptyDocument = (getLength() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            isStyleCSS = "text/css".equals(getDefaultStyleSheetType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
            threshold = HTMLDocument.this.getTokenThreshold();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2192
            tagMap = new Hashtable<HTML.Tag, TagAction>(57);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            TagAction na = new TagAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            TagAction ba = new BlockAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            TagAction pa = new ParagraphAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            TagAction ca = new CharacterAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
            TagAction sa = new SpecialAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            TagAction fa = new FormAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            TagAction ha = new HiddenAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
            TagAction conv = new ConvertAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            // register handlers for the well known tags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            tagMap.put(HTML.Tag.A, new AnchorAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            tagMap.put(HTML.Tag.ADDRESS, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            tagMap.put(HTML.Tag.APPLET, ha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            tagMap.put(HTML.Tag.AREA, new AreaAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
            tagMap.put(HTML.Tag.B, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            tagMap.put(HTML.Tag.BASE, new BaseAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            tagMap.put(HTML.Tag.BASEFONT, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            tagMap.put(HTML.Tag.BIG, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            tagMap.put(HTML.Tag.BLOCKQUOTE, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            tagMap.put(HTML.Tag.BODY, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            tagMap.put(HTML.Tag.BR, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            tagMap.put(HTML.Tag.CAPTION, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            tagMap.put(HTML.Tag.CENTER, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            tagMap.put(HTML.Tag.CITE, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            tagMap.put(HTML.Tag.CODE, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            tagMap.put(HTML.Tag.DD, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            tagMap.put(HTML.Tag.DFN, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            tagMap.put(HTML.Tag.DIR, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
            tagMap.put(HTML.Tag.DIV, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            tagMap.put(HTML.Tag.DL, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            tagMap.put(HTML.Tag.DT, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            tagMap.put(HTML.Tag.EM, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
            tagMap.put(HTML.Tag.FONT, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            tagMap.put(HTML.Tag.FORM, new FormTagAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            tagMap.put(HTML.Tag.FRAME, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
            tagMap.put(HTML.Tag.FRAMESET, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            tagMap.put(HTML.Tag.H1, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            tagMap.put(HTML.Tag.H2, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            tagMap.put(HTML.Tag.H3, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            tagMap.put(HTML.Tag.H4, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            tagMap.put(HTML.Tag.H5, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            tagMap.put(HTML.Tag.H6, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            tagMap.put(HTML.Tag.HEAD, new HeadAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            tagMap.put(HTML.Tag.HR, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            tagMap.put(HTML.Tag.HTML, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            tagMap.put(HTML.Tag.I, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            tagMap.put(HTML.Tag.IMG, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            tagMap.put(HTML.Tag.INPUT, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            tagMap.put(HTML.Tag.ISINDEX, new IsindexAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            tagMap.put(HTML.Tag.KBD, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            tagMap.put(HTML.Tag.LI, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            tagMap.put(HTML.Tag.LINK, new LinkAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            tagMap.put(HTML.Tag.MAP, new MapAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            tagMap.put(HTML.Tag.MENU, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            tagMap.put(HTML.Tag.META, new MetaAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            tagMap.put(HTML.Tag.NOBR, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            tagMap.put(HTML.Tag.NOFRAMES, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            tagMap.put(HTML.Tag.OBJECT, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            tagMap.put(HTML.Tag.OL, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            tagMap.put(HTML.Tag.OPTION, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            tagMap.put(HTML.Tag.P, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            tagMap.put(HTML.Tag.PARAM, new ObjectAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            tagMap.put(HTML.Tag.PRE, new PreAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            tagMap.put(HTML.Tag.SAMP, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            tagMap.put(HTML.Tag.SCRIPT, ha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            tagMap.put(HTML.Tag.SELECT, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            tagMap.put(HTML.Tag.SMALL, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            tagMap.put(HTML.Tag.SPAN, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            tagMap.put(HTML.Tag.STRIKE, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
            tagMap.put(HTML.Tag.S, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            tagMap.put(HTML.Tag.STRONG, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            tagMap.put(HTML.Tag.STYLE, new StyleAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            tagMap.put(HTML.Tag.SUB, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            tagMap.put(HTML.Tag.SUP, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            tagMap.put(HTML.Tag.TABLE, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            tagMap.put(HTML.Tag.TD, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            tagMap.put(HTML.Tag.TEXTAREA, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            tagMap.put(HTML.Tag.TH, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            tagMap.put(HTML.Tag.TITLE, new TitleAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            tagMap.put(HTML.Tag.TR, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            tagMap.put(HTML.Tag.TT, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            tagMap.put(HTML.Tag.U, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            tagMap.put(HTML.Tag.UL, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            tagMap.put(HTML.Tag.VAR, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            if (insertTag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                this.insertTag = insertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                this.popDepth = popDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                this.pushDepth = pushDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                this.insertInsertTag = insertInsertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                foundInsertTag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                foundInsertTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            if (insertAfterImplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                this.popDepth = popDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                this.pushDepth = pushDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                this.insertAfterImplied = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                foundInsertTag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                midInsert = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                this.insertInsertTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                this.wantsTrailingNewline = wantsTrailingNewline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                midInsert = (!emptyDocument && insertTag == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                if (midInsert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                    generateEndsSpecsForMidInsert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
             * This block initializes the <code>inParagraph</code> flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
             * It is left in <code>false</code> value automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
             * if the target document is empty or future inserts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
             * were positioned into the 'body' tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
            if (!emptyDocument && !midInsert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                int targetOffset = Math.max(this.offset - 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                Element elem =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                        HTMLDocument.this.getCharacterElement(targetOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                /* Going up by the left document structure path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                for (int i = 0; i <= this.popDepth; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                    elem = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                /* Going down by the right document structure path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                for (int i = 0; i < this.pushDepth; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                    int index = elem.getElementIndex(this.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                    elem = elem.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                AttributeSet attrs = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                if (attrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                    HTML.Tag tagToInsertInto =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                            (HTML.Tag) attrs.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                    if (tagToInsertInto != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                        this.inParagraph = tagToInsertInto.isParagraph();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
         * Generates an initial batch of end <code>ElementSpecs</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
         * in parseBuffer to position future inserts into the body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        private void generateEndsSpecsForMidInsert() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            int           count = heightToElementWithName(HTML.Tag.BODY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                                                   Math.max(0, offset - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            boolean       joinNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            if (count == -1 && offset > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                count = heightToElementWithName(HTML.Tag.BODY, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                if (count != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                    // Previous isn't in body, but current is. Have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                    // do some end specs, followed by join next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                    count = depthTo(offset - 1) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                    joinNext = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            if (count == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                throw new RuntimeException("Must insert new content into body element-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
            if (count != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                // Insert a newline, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                    if (!joinNext && offset > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                        !getText(offset - 1, 1).equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                        SimpleAttributeSet newAttrs = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                        newAttrs.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                                              HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                        ElementSpec spec = new ElementSpec(newAttrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                                    ElementSpec.ContentType, NEWLINE, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                        parseBuffer.addElement(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                    // Should never throw, but will catch anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                while (count-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                    parseBuffer.addElement(new ElementSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                                           (null, ElementSpec.EndTagType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                if (joinNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                    ElementSpec spec = new ElementSpec(null, ElementSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                                                       StartTagType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                    spec.setDirection(ElementSpec.JoinNextDirection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                    parseBuffer.addElement(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            // We should probably throw an exception if (count == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            // Or look for the body and reset the offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
         * @return number of parents to reach the child at offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        private int depthTo(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            Element       e = getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            int           count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            while (!e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
         * @return number of parents of the leaf at <code>offset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
         *         until a parent with name, <code>name</code> has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
         *         found. -1 indicates no matching parent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
         *         <code>name</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        private int heightToElementWithName(Object name, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            Element       e = getCharacterElement(offset).getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            int           count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            while (e != null && e.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                   (StyleConstants.NameAttribute) != name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                e = e.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            return (e == null) ? -1 : count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
         * This will make sure there aren't two BODYs (the second is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
         * typically created when you do a remove all, and then an insert).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        private void adjustEndElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
            int length = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
            obtainLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                Element[] pPath = getPathTo(length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                int pLength = pPath.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                if (pLength > 1 && pPath[1].getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                         (StyleConstants.NameAttribute) == HTML.Tag.BODY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                         pPath[1].getEndOffset() == length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                    String lastText = getText(length - 1, 1);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2434
                    DefaultDocumentEvent event;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                    Element[] added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                    Element[] removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                    // Remove the fake second body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                    added = new Element[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                    removed = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                    index = pPath[0].getElementIndex(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                    removed[0] = pPath[0].getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
                    ((BranchElement)pPath[0]).replace(index, 1, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                    ElementEdit firstEdit = new ElementEdit(pPath[0], index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                                                            removed, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                    // Insert a new element to represent the end that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                    // second body was representing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
                    SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
                    sas.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
                                         HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                    sas.addAttribute(IMPLIED_CR, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                    added = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                    added[0] = createLeafElement(pPath[pLength - 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                                                     sas, length, length + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                    index = pPath[pLength - 1].getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                    ((BranchElement)pPath[pLength - 1]).replace(index, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                                                                added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                    event = new DefaultDocumentEvent(length, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
                                            DocumentEvent.EventType.CHANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                    event.addEdit(new ElementEdit(pPath[pLength - 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                                         index, new Element[0], added));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                    event.addEdit(firstEdit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
                    event.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                    fireChangedUpdate(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                    fireUndoableEditUpdate(new UndoableEditEvent(this, event));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                    if (lastText.equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                        // We now have two \n's, one part of the Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                        // We need to remove one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                        event = new DefaultDocumentEvent(length - 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                                           DocumentEvent.EventType.REMOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                        removeUpdate(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                        UndoableEdit u = getContent().remove(length - 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                        if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                            event.addEdit(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                        postRemoveUpdate(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                        // Mark the edit as done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                        event.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                        fireRemoveUpdate(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                        fireUndoableEditUpdate(new UndoableEditEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                                               this, event));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
            catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                releaseLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        private Element[] getPathTo(int offset) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2495
            Stack<Element> elements = new Stack<Element>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            Element e = getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            while (!e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                elements.push(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            Element[] retValue = new Element[elements.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
            elements.copyInto(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        // -- HTMLEditorKit.ParserCallback methods --------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
         * The last method called on the reader.  It allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
         * any pending changes to be flushed into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
         * Since this is currently loading synchronously, the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
         * set of changes are pushed in at this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        public void flush() throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            if (emptyDocument && !insertAfterImplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                if (HTMLDocument.this.getLength() > 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                                      parseBuffer.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                    flushBuffer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                    adjustEndElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                // We won't insert when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                flushBuffer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
         * Called by the parser to indicate a block of text was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
         * encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        public void handleText(char[] data, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
            if (receivedEndHTML || (midInsert && !inBody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
            // see if complex glyph layout support is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            if(HTMLDocument.this.getProperty(I18NProperty).equals( Boolean.FALSE ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                // if a default direction of right-to-left has been specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                // we want complex layout even if the text is all left to right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                Object d = getProperty(TextAttribute.RUN_DIRECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                if ((d != null) && (d.equals(TextAttribute.RUN_DIRECTION_RTL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
                    HTMLDocument.this.putProperty( I18NProperty, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                    if (SwingUtilities2.isComplexLayout(data, 0, data.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                        HTMLDocument.this.putProperty( I18NProperty, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            if (inTextArea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                textAreaContent(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
            } else if (inPre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                preContent(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
            } else if (inTitle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                putProperty(Document.TitleProperty, new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
            } else if (option != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                option.setLabel(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            } else if (inStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                if (styles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                    styles.addElement(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            } else if (inBlock > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                if (!foundInsertTag && insertAfterImplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                    // Assume content should be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                    foundInsertTag(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                    foundInsertTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                    inParagraph = impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                if (data.length >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                    addContent(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
         * Callback from the parser.  Route to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
         * handler for the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            if (receivedEndHTML) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
            if (midInsert && !inBody) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                if (t == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                    inBody = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                    // Increment inBlock since we know we are in the body,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                    // this is needed incase an implied-p is needed. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                    // inBlock isn't incremented, and an implied-p is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                    // encountered, addContent won't be called!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                    inBlock++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
            if (!inBody && t == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                inBody = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            if (isStyleCSS && a.isDefined(HTML.Attribute.STYLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                // Map the style attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                String decl = (String)a.getAttribute(HTML.Attribute.STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                a.removeAttribute(HTML.Attribute.STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                styleAttributes = getStyleSheet().getDeclaration(decl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                a.addAttributes(styleAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                styleAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2609
            TagAction action = tagMap.get(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                action.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        public void handleComment(char[] data, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            if (receivedEndHTML) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                addExternalComment(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
            if (inStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                if (styles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                    styles.addElement(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            else if (getPreservesUnknownTags()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                if (inBlock == 0 && (foundInsertTag ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                                     insertTag != HTML.Tag.COMMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                    // Comment outside of body, will not be able to show it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
                    // but can add it as a property on the Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
                    addExternalComment(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                sas.addAttribute(HTML.Attribute.COMMENT, new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                addSpecialElement(HTML.Tag.COMMENT, sas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2639
            TagAction action = tagMap.get(HTML.Tag.COMMENT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
                action.start(HTML.Tag.COMMENT, new SimpleAttributeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                action.end(HTML.Tag.COMMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
         * Adds the comment <code>comment</code> to the set of comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
         * maintained outside of the scope of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        private void addExternalComment(String comment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
            Object comments = getProperty(AdditionalComments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
            if (comments != null && !(comments instanceof Vector)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                // No place to put comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            if (comments == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                comments = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                putProperty(AdditionalComments, comments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            ((Vector)comments).addElement(comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
         * Callback from the parser.  Route to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
         * handler for the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
        public void handleEndTag(HTML.Tag t, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
            if (receivedEndHTML || (midInsert && !inBody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
            if (t == HTML.Tag.HTML) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                receivedEndHTML = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            if (t == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                inBody = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                if (midInsert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                    inBlock--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2680
            TagAction action = tagMap.get(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                action.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
         * Callback from the parser.  Route to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
         * handler for the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
        public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            if (receivedEndHTML || (midInsert && !inBody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
            if (isStyleCSS && a.isDefined(HTML.Attribute.STYLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                // Map the style attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                String decl = (String)a.getAttribute(HTML.Attribute.STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                a.removeAttribute(HTML.Attribute.STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                styleAttributes = getStyleSheet().getDeclaration(decl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                a.addAttributes(styleAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                styleAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2706
            TagAction action = tagMap.get(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
                action.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
                action.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
            else if (getPreservesUnknownTags()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                // unknown tag, only add if should preserve it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
         * This is invoked after the stream has been parsed, but before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
         * <code>flush</code>. <code>eol</code> will be one of \n, \r
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
         * or \r\n, which ever is encountered the most in parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
         * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
        public void handleEndOfLineString(String eol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
            if (emptyDocument && eol != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                putProperty(DefaultEditorKit.EndOfLineStringProperty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                            eol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        // ---- tag handling support ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
         * Registers a handler for the given tag.  By default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
         * all of the well-known tags will have been registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
         * This can be used to change the handling of a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
         * tag or to add support for custom tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
        protected void registerTag(HTML.Tag t, TagAction a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
            tagMap.put(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
         * An action to be performed in response
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
         * to parsing a tag.  This allows customization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
         * of how each tag is handled and avoids a large
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
         * switch statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
        public class TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
             * Called when a start tag is seen for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
             * type of tag this action was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
             * to.  The tag argument indicates the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
             * tag for those actions that are shared across
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
             * many tags.  By default this does nothing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
             * completely ignores the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
             * Called when an end tag is seen for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
             * type of tag this action was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
             * to.  The tag argument indicates the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
             * tag for those actions that are shared across
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
             * many tags.  By default this does nothing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
             * completely ignores the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
        public class BlockAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                blockOpen(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                blockClose(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
         * Action used for the actual element form tag. This is named such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
         * as there was already a public class named FormAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        private class FormTagAction extends BlockAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                super.start(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                // initialize a ButtonGroupsMap when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                // FORM tag is encountered.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                // be used for any radio buttons that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                // might be defined in the FORM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                // for new group new ButtonGroup will be created (fix for 4529702)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                // group name is a key in radioButtonGroupsMap
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2801
                radioButtonGroupsMap = new HashMap<String, ButtonGroup>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                // reset the button group to null since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                // the form has ended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
                radioButtonGroupsMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
        public class ParagraphAction extends BlockAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                inParagraph = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        public class SpecialAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        public class IsindexAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
        public class HiddenAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
                addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
                if (!isEmpty(t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                    MutableAttributeSet a = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
                    a.addAttribute(HTML.Attribute.ENDTAG, "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
                    addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                if (t == HTML.Tag.APPLET ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                    t == HTML.Tag.SCRIPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
         * Subclass of HiddenAction to set the content type for style sheets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
         * and to set the name of the default style sheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        class MetaAction extends HiddenAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                Object equiv = a.getAttribute(HTML.Attribute.HTTPEQUIV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                if (equiv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                    equiv = ((String)equiv).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                    if (equiv.equals("content-style-type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
                        String value = (String)a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
                                       (HTML.Attribute.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                        setDefaultStyleSheetType(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                        isStyleCSS = "text/css".equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                                      (getDefaultStyleSheetType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                    else if (equiv.equals("default-style")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
                        defaultStyle = (String)a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
                                       (HTML.Attribute.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
         * End if overridden to create the necessary stylesheets that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
         * are referenced via the link tag. It is done in this manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
         * as the meta tag can be used to specify an alternate style sheet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
         * and is not guaranteed to come before the link tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
        class HeadAction extends BlockAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
                inHead = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                // This check of the insertTag is put in to avoid considering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                // the implied-p that is generated for the head. This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                // inserts for HR to work correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                if ((insertTag == null && !insertAfterImplied) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                    (insertTag == HTML.Tag.HEAD) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                    (insertAfterImplied &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                     (foundInsertTag || !a.isDefined(IMPLIED)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                    super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                inHead = inStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                // See if there is a StyleSheet to link to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                if (styles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                    boolean isDefaultCSS = isStyleCSS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                    for (int counter = 0, maxCounter = styles.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                         counter < maxCounter;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                        Object value = styles.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                        if (value == HTML.Tag.LINK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                            handleLink((AttributeSet)styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                                       elementAt(++counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                            counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                            // Rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                            // First element gives type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                            String type = (String)styles.elementAt(++counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                            boolean isCSS = (type == null) ? isDefaultCSS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                                            type.equals("text/css");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                            while (++counter < maxCounter &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                                   (styles.elementAt(counter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                                    instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                                if (isCSS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                                    addCSSRules((String)styles.elementAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
                                                (counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                if ((insertTag == null && !insertAfterImplied) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                    insertTag == HTML.Tag.HEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                    (insertAfterImplied && foundInsertTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                    super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
            private void handleLink(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
                // Link.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
                String type = (String)attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
                if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
                    type = getDefaultStyleSheetType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                // Only choose if type==text/css
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                // Select link if rel==stylesheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                // Otherwise if rel==alternate stylesheet and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                //   title matches default style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                if (type.equals("text/css")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                    String rel = (String)attr.getAttribute(HTML.Attribute.REL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                    String title = (String)attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                                               (HTML.Attribute.TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                    String media = (String)attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                                                   (HTML.Attribute.MEDIA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                    if (media == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
                        media = "all";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                        media = media.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
                    if (rel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                        rel = rel.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
                        if ((media.indexOf("all") != -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                             media.indexOf("screen") != -1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
                            (rel.equals("stylesheet") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
                             (rel.equals("alternate stylesheet") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
                              title.equals(defaultStyle)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                            linkCSSStyleSheet((String)attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
                                              (HTML.Attribute.HREF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
         * A subclass to add the AttributeSet to styles if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
         * attributes contains an attribute for 'rel' with value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
         * 'stylesheet' or 'alternate stylesheet'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
        class LinkAction extends HiddenAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                String rel = (String)a.getAttribute(HTML.Attribute.REL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                if (rel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                    rel = rel.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                    if (rel.equals("stylesheet") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
                        rel.equals("alternate stylesheet")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                        if (styles == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3014
                            styles = new Vector<Object>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                        styles.addElement(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                        styles.addElement(a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
        class MapAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                lastMap = new Map((String)a.getAttribute(HTML.Attribute.NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                addMap(lastMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
        class AreaAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
                if (lastMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
                    lastMap.addArea(a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
        class StyleAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
                if (inHead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
                    if (styles == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3054
                        styles = new Vector<Object>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
                    styles.addElement(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
                    styles.addElement(a.getAttribute(HTML.Attribute.TYPE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                    inStyle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                inStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
        public class PreAction extends BlockAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
                inPre = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
                blockOpen(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
                attr.addAttribute(CSS.Attribute.WHITE_SPACE, "pre");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
                blockOpen(HTML.Tag.IMPLIED, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
                // set inPre to false after closing, so that if a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
                // is added it won't generate a blockOpen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
                inPre = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
                blockClose(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
        public class CharacterAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
                pushCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                    // Note that the third argument should really be based off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                    // inParagraph and impliedP. If we're wrong (that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
                    // insertTagDepthDelta shouldn't be changed), we'll end up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
                    // removing an extra EndSpec, which won't matter anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
                    boolean insert = canInsertTag(t, attr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                    if (foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                        if (!inParagraph) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                            inParagraph = impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
                    if (!insert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
                if (attr.isDefined(IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
                    attr.removeAttribute(IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
                charAttr.addAttribute(t, attr.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
                if (styleAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
                    charAttr.addAttributes(styleAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
                popCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
         * Provides conversion of HTML tag/attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
         * mappings that have a corresponding StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
         * and CSS mapping.  The conversion is to CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
        class ConvertAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
                pushCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
                if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
                    // Note that the third argument should really be based off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
                    // inParagraph and impliedP. If we're wrong (that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
                    // insertTagDepthDelta shouldn't be changed), we'll end up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
                    // removing an extra EndSpec, which won't matter anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
                    boolean insert = canInsertTag(t, attr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
                    if (foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
                        if (!inParagraph) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                            inParagraph = impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
                    if (!insert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
                if (attr.isDefined(IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
                    attr.removeAttribute(IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
                if (styleAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
                    charAttr.addAttributes(styleAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
                // We also need to add attr, otherwise we lose custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
                // attributes, including class/id for style lookups, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
                // further confuse style lookup (doesn't have tag).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
                charAttr.addAttribute(t, attr.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
                StyleSheet sheet = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
                if (t == HTML.Tag.B) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_WEIGHT, "bold");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
                } else if (t == HTML.Tag.I) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_STYLE, "italic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
                } else if (t == HTML.Tag.U) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
                    Object v = charAttr.getAttribute(CSS.Attribute.TEXT_DECORATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
                    String value = "underline";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
                    value = (v != null) ? value + "," + v.toString() : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.TEXT_DECORATION, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
                } else if (t == HTML.Tag.STRIKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
                    Object v = charAttr.getAttribute(CSS.Attribute.TEXT_DECORATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
                    String value = "line-through";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
                    value = (v != null) ? value + "," + v.toString() : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.TEXT_DECORATION, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                } else if (t == HTML.Tag.SUP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
                    Object v = charAttr.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
                    String value = "sup";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
                    value = (v != null) ? value + "," + v.toString() : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.VERTICAL_ALIGN, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
                } else if (t == HTML.Tag.SUB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
                    Object v = charAttr.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
                    String value = "sub";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
                    value = (v != null) ? value + "," + v.toString() : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.VERTICAL_ALIGN, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
                } else if (t == HTML.Tag.FONT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
                    String color = (String) attr.getAttribute(HTML.Attribute.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
                    if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
                        sheet.addCSSAttribute(charAttr, CSS.Attribute.COLOR, color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
                    String face = (String) attr.getAttribute(HTML.Attribute.FACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
                    if (face != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
                        sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_FAMILY, face);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
                    String size = (String) attr.getAttribute(HTML.Attribute.SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
                    if (size != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
                        sheet.addCSSAttributeFromHTML(charAttr, CSS.Attribute.FONT_SIZE, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
                popCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
        class AnchorAction extends CharacterAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
                // set flag to catch empty anchors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
                emptyAnchor = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
                super.start(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
                if (emptyAnchor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
                    // if the anchor was empty it was probably a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                    // named anchor point and we don't want to throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
                    // it away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
                    char[] one = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
                    one[0] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
                    addContent(one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
        class TitleAction extends HiddenAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
                inTitle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
                super.start(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                inTitle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
                super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
        class BaseAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
                String href = (String) attr.getAttribute(HTML.Attribute.HREF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
                if (href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
                        URL newBase = new URL(base, href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
                        setBase(newBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
                        hasBaseTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
                    } catch (MalformedURLException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
                baseTarget = (String) attr.getAttribute(HTML.Attribute.TARGET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
        class ObjectAction extends SpecialAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
                if (t == HTML.Tag.PARAM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
                    addParameter(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
                    super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
                if (t != HTML.Tag.PARAM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
                    super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
            void addParameter(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
                String name = (String) a.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
                String value = (String) a.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
                if ((name != null) && (value != null)) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3279
                    ElementSpec objSpec = parseBuffer.lastElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
                    MutableAttributeSet objAttr = (MutableAttributeSet) objSpec.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
                    objAttr.addAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
         * Action to support forms by building all of the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
         * used to represent form controls.  This will process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
         * the &lt;INPUT&gt;, &lt;TEXTAREA&gt;, &lt;SELECT&gt;,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
         * and &lt;OPTION&gt; tags.  The element created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
         * this action is expected to have the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
         * <code>StyleConstants.ModelAttribute</code> set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
         * the model that holds the state for the form control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
         * This enables multiple views, and allows document to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
         * be iterated over picking up the data of the form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
         * The following are the model assignments for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
         * various type of form elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
         * <table summary="model assignments for the various types of form elements">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
         *   <th>Element Type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
         *   <th>Model Type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
         *   <td>input, type button
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
         *   <td>{@link DefaultButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
         *   <td>input, type checkbox
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
         *   <td>{@link javax.swing.JToggleButton.ToggleButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
         *   <td>input, type image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
         *   <td>{@link DefaultButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
         *   <td>input, type password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
         *   <td>{@link PlainDocument}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
         *   <td>input, type radio
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
         *   <td>{@link javax.swing.JToggleButton.ToggleButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
         *   <td>input, type reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
         *   <td>{@link DefaultButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
         *   <td>input, type submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
         *   <td>{@link DefaultButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
         *   <td>input, type text or type is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
         *   <td>{@link PlainDocument}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
         *   <td>select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
         *   <td>{@link DefaultComboBoxModel} or an {@link DefaultListModel}, with an item type of Option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
         *   <td>textarea
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
         *   <td>{@link PlainDocument}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
         * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
        public class FormAction extends SpecialAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
                if (t == HTML.Tag.INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                    String type = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
                        attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                     * if type is not defined teh default is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                     * assumed to be text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                    if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                        type = "text";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
                        attr.addAttribute(HTML.Attribute.TYPE, "text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
                    setModel(type, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
                } else if (t == HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
                    inTextArea = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
                    textAreaDocument = new TextAreaDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                    attr.addAttribute(StyleConstants.ModelAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
                                      textAreaDocument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
                } else if (t == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
                    int size = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
                                                             HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
                                                             1);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3359
                    boolean multiple = attr.getAttribute(HTML.Attribute.MULTIPLE) != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
                    if ((size > 1) || multiple) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
                        OptionListModel m = new OptionListModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
                        if (multiple) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
                            m.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
                        selectModel = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
                        selectModel = new OptionComboBoxModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
                    attr.addAttribute(StyleConstants.ModelAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
                                      selectModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
                // build the element, unless this is an option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
                if (t == HTML.Tag.OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
                    option = new Option(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                    if (selectModel instanceof OptionListModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
                        OptionListModel m = (OptionListModel)selectModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                        m.addElement(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                        if (option.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
                            m.addSelectionInterval(optionCount, optionCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                            m.setInitialSelection(optionCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
                    } else if (selectModel instanceof OptionComboBoxModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
                        OptionComboBoxModel m = (OptionComboBoxModel)selectModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
                        m.addElement(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
                        if (option.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
                            m.setSelectedItem(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
                            m.setInitialSelection(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
                    optionCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
                    super.start(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
                if (t == HTML.Tag.OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
                    option = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
                    if (t == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
                        selectModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
                        optionCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
                    } else if (t == HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
                        inTextArea = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
                        /* Now that the textarea has ended,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
                         * store the entire initial text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
                         * of the text area.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
                         * enable us to restore the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
                         * state if a reset is requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
                        textAreaDocument.storeInitialText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
                    super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
            void setModel(String type, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
                if (type.equals("submit") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
                    type.equals("reset") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
                    type.equals("image")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                    // button model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
                    attr.addAttribute(StyleConstants.ModelAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
                                      new DefaultButtonModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                } else if (type.equals("text") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
                           type.equals("password")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
                    // plain text model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
                    int maxLength = HTML.getIntegerAttributeValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
                                       attr, HTML.Attribute.MAXLENGTH, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
                    Document doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
                    if (maxLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
                        doc = new FixedLengthDocument(maxLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
                        doc = new PlainDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
                    String value = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
                        attr.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
                        doc.insertString(0, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
                    } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
                    attr.addAttribute(StyleConstants.ModelAttribute, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
                } else if (type.equals("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                    // plain text model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                    attr.addAttribute(StyleConstants.ModelAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                                      new PlainDocument());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                } else if (type.equals("checkbox") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                           type.equals("radio")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
                    JToggleButton.ToggleButtonModel model = new JToggleButton.ToggleButtonModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                    if (type.equals("radio")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                        String name = (String) attr.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                        if ( radioButtonGroupsMap == null ) { //fix for 4772743
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3459
                           radioButtonGroupsMap = new HashMap<String, ButtonGroup>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3461
                        ButtonGroup radioButtonGroup = radioButtonGroupsMap.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                        if (radioButtonGroup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                            radioButtonGroup = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
                            radioButtonGroupsMap.put(name,radioButtonGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                        model.setGroup(radioButtonGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                    boolean checked = (attr.getAttribute(HTML.Attribute.CHECKED) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
                    model.setSelected(checked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
                    attr.addAttribute(StyleConstants.ModelAttribute, model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
             * If a &lt;SELECT&gt; tag is being processed, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
             * model will be a reference to the model being filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
             * with the &lt;OPTION&gt; elements (which produce
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
             * objects of type <code>Option</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
            Object selectModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
            int optionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
        // --- utility methods used by the reader ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
         * Pushes the current character style on a stack in preparation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
         * for forming a new nested character style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
        protected void pushCharacterStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
            charAttrStack.push(charAttr.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
         * Pops a previously pushed character style off the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
         * to return to a previous style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
        protected void popCharacterStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
            if (!charAttrStack.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
                charAttr = (MutableAttributeSet) charAttrStack.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
                charAttrStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
         * Adds the given content to the textarea document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
         * This method gets called when we are in a textarea
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
         * context.  Therefore all text that is seen belongs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
         * to the text area and is hence added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
         * TextAreaDocument associated with the text area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
        protected void textAreaContent(char[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
                textAreaDocument.insertString(textAreaDocument.getLength(), new String(data), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
                // Should do something reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
         * Adds the given content that was encountered in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
         * PRE element.  This synthesizes lines to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
         * runs of text, and makes calls to addContent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
         * actually add the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
        protected void preContent(char[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
            int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
            for (int i = 0; i < data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                if (data[i] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                    addContent(data, last, i - last + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
                    blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
                    MutableAttributeSet a = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
                    a.addAttribute(CSS.Attribute.WHITE_SPACE, "pre");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
                    blockOpen(HTML.Tag.IMPLIED, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                    last = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
            if (last < data.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
                addContent(data, last, data.length - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
         * Adds an instruction to the parse buffer to create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
         * block element with the given attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
        protected void blockOpen(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
            if (impliedP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
                blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
            inBlock++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            if (!canInsertTag(t, attr, true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
            if (attr.isDefined(IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
                attr.removeAttribute(IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
            lastWasNewline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
            attr.addAttribute(StyleConstants.NameAttribute, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
            ElementSpec es = new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
                attr.copyAttributes(), ElementSpec.StartTagType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
         * Adds an instruction to the parse buffer to close out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
         * a block element of the given type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
        protected void blockClose(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
            inBlock--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
            if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
            // Add a new line, if the last character wasn't one. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
            // needed for proper positioning of the cursor. addContent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
            // with true will force an implied paragraph to be generated if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
            // there isn't one. This may result in a rather bogus structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
            // (perhaps a table with a child pargraph), but the paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
            // is needed for proper positioning and display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
            if(!lastWasNewline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
                pushCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
                charAttr.addAttribute(IMPLIED_CR, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
                addContent(NEWLINE, 0, 1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
                popCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
                lastWasNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
            if (impliedP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
                impliedP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
                inParagraph = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
                if (t != HTML.Tag.IMPLIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
                    blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
            // an open/close with no content will be removed, so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
            // add a space of content to keep the element being formed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
            ElementSpec prev = (parseBuffer.size() > 0) ?
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3603
                parseBuffer.lastElement() : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
            if (prev != null && prev.getType() == ElementSpec.StartTagType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
                char[] one = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
                one[0] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
                addContent(one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
            ElementSpec es = new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                null, ElementSpec.EndTagType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
            parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
         * Adds some text with the current character attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
         * @param data the content to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
         * @param offs the initial offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
         * @param length the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
        protected void addContent(char[] data, int offs, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
            addContent(data, offs, length, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
         * Adds some text with the current character attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
         * @param data the content to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
         * @param offs the initial offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
         * @param length the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
         * @param generateImpliedPIfNecessary whether to generate implied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
         * paragraphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
        protected void addContent(char[] data, int offs, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
                                  boolean generateImpliedPIfNecessary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
            if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
            if (generateImpliedPIfNecessary && (! inParagraph) && (! inPre)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
                blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
                inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
                impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
            emptyAnchor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
            charAttr.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
            AttributeSet a = charAttr.copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
            ElementSpec es = new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
                a, ElementSpec.ContentType, data, offs, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
            parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
            if (parseBuffer.size() > threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
                if ( threshold <= MaxThreshold ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
                    threshold *= StepThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
                    flushBuffer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
                } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
            if(length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
                lastWasNewline = (data[offs + length - 1] == '\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
         * Adds content that is basically specified entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
         * in the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
        protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
            if ((t != HTML.Tag.FRAME) && (! inParagraph) && (! inPre)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
                nextTagAfterPImplied = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
                blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
                nextTagAfterPImplied = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
                inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
                impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
            if (!canInsertTag(t, a, t.isBlock())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
            if (a.isDefined(IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
                a.removeAttribute(IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
            emptyAnchor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
            a.addAttributes(charAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
            a.addAttribute(StyleConstants.NameAttribute, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
            char[] one = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
            one[0] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
            ElementSpec es = new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
                a.copyAttributes(), ElementSpec.ContentType, one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
            parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
            // Set this to avoid generating a newline for frames, frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
            // shouldn't have any content, and shouldn't need a newline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
            if (t == HTML.Tag.FRAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
                lastWasNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
         * Flushes the current parse buffer into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
         * @param endOfStream true if there is no more content to parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
        void flushBuffer(boolean endOfStream) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
            int oldLength = HTMLDocument.this.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
            int size = parseBuffer.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
            if (endOfStream && (insertTag != null || insertAfterImplied) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
                size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
                adjustEndSpecsForPartialInsert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
                size = parseBuffer.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
            ElementSpec[] spec = new ElementSpec[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
            parseBuffer.copyInto(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
            if (oldLength == 0 && (insertTag == null && !insertAfterImplied)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
                create(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
                insert(offset, spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            parseBuffer.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
            offset += HTMLDocument.this.getLength() - oldLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
            flushCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
         * This will be invoked for the last flush, if <code>insertTag</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
         * is non null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
        private void adjustEndSpecsForPartialInsert() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
            int size = parseBuffer.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
            if (insertTagDepthDelta < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
                // When inserting via an insertTag, the depths (of the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
                // being read in, and existing hiearchy) may not match up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
                // This attemps to clean it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
                int removeCounter = insertTagDepthDelta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
                while (removeCounter < 0 && size >= 0 &&
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3736
                        parseBuffer.elementAt(size - 1).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
                       getType() == ElementSpec.EndTagType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
                    parseBuffer.removeElementAt(--size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                    removeCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
            if (flushCount == 0 && (!insertAfterImplied ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                                    !wantsTrailingNewline)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
                // If this starts with content (or popDepth > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
                // pushDepth > 0) and ends with EndTagTypes, make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
                // the last content isn't a \n, otherwise will end up with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                // an extra \n in the middle of content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
                int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
                if (pushDepth > 0) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3750
                    if (parseBuffer.elementAt(0).getType() ==
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                        ElementSpec.ContentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
                index += (popDepth + pushDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                int cCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                int cStart = index;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3758
                while (index < size && parseBuffer.elementAt
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3759
                        (index).getType() == ElementSpec.ContentType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
                    cCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
                if (cCount > 1) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3764
                    while (index < size && parseBuffer.elementAt
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3765
                            (index).getType() == ElementSpec.EndTagType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
                    if (index == size) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3769
                        char[] lastText = parseBuffer.elementAt
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3770
                                (cStart + cCount - 1).getArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
                        if (lastText.length == 1 && lastText[0] == NEWLINE[0]){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
                            index = cStart + cCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
                            while (size > index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
                                parseBuffer.removeElementAt(--size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
            if (wantsTrailingNewline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
                // Make sure there is in fact a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
                for (int counter = parseBuffer.size() - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
                                   counter--) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3784
                    ElementSpec spec = parseBuffer.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
                    if (spec.getType() == ElementSpec.ContentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
                        if (spec.getArray()[spec.getLength() - 1] != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
                            SimpleAttributeSet attrs =new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
                            attrs.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
                                               HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
                            parseBuffer.insertElementAt(new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
                                    attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
                                    ElementSpec.ContentType, NEWLINE, 0, 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
                                    counter + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
         * Adds the CSS rules in <code>rules</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
        void addCSSRules(String rules) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
            StyleSheet ss = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
            ss.addRule(rules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
         * Adds the CSS stylesheet at <code>href</code> to the known list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
         * of stylesheets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
        void linkCSSStyleSheet(String href) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3815
            URL url;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
                url = new URL(base, href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
            } catch (MalformedURLException mfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
                    url = new URL(href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
                } catch (MalformedURLException mfe2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
                    url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
                getStyleSheet().importStyleSheet(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
         * Returns true if can insert starting at <code>t</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
         * will return false if the insert tag is set, and hasn't been found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
         * yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
        private boolean canInsertTag(HTML.Tag t, AttributeSet attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
                                     boolean isBlockTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
            if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
                boolean needPImplied = ((t == HTML.Tag.IMPLIED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
                                                          && (!inParagraph)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
                                                          && (!inPre));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
                if (needPImplied && (nextTagAfterPImplied != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
                     * If insertTag == null then just proceed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
                     * foundInsertTag() call below and return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
                    if (insertTag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
                        boolean nextTagIsInsertTag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
                                isInsertTag(nextTagAfterPImplied);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
                        if ( (! nextTagIsInsertTag) || (! insertInsertTag) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
                     *  Proceed to foundInsertTag() call...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
                 } else if ((insertTag != null && !isInsertTag(t))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
                               || (insertAfterImplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
                                    && (attr == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
                                        || attr.isDefined(IMPLIED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
                                        || t == HTML.Tag.IMPLIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
                                       )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
                                   )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
                           ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
                // Allow the insert if t matches the insert tag, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
                // insertAfterImplied is true and the element is implied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
                foundInsertTag(isBlockTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
                if (!insertInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
        private boolean isInsertTag(HTML.Tag tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
            return (insertTag == tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
        private void foundInsertTag(boolean isBlockTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
            foundInsertTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
            if (!insertAfterImplied && (popDepth > 0 || pushDepth > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
                    if (offset == 0 || !getText(offset - 1, 1).equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
                        // Need to insert a newline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
                        AttributeSet newAttrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
                        boolean joinP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
                        if (offset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
                            // Determine if we can use JoinPrevious, we can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
                            // if the Element has some attributes that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
                            // not meant to be duplicated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
                            Element charElement = getCharacterElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
                                                    (offset - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
                            AttributeSet attrs = charElement.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
                            if (attrs.isDefined(StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
                                                ComposedTextAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
                                joinP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
                                Object name = attrs.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
                                              (StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
                                if (name instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
                                    HTML.Tag tag = (HTML.Tag)name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
                                    if (tag == HTML.Tag.IMG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
                                        tag == HTML.Tag.HR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
                                        tag == HTML.Tag.COMMENT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
                                        (tag instanceof HTML.UnknownTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
                                        joinP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
                        if (!joinP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
                            // If not joining with the previous element, be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
                            // sure and set the name (otherwise it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
                            // inherited).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
                            newAttrs = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
                            ((SimpleAttributeSet)newAttrs).addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
                                              (StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
                                               HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
                        ElementSpec es = new ElementSpec(newAttrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
                                     ElementSpec.ContentType, NEWLINE, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
                                     NEWLINE.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
                        if (joinP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
                            es.setDirection(ElementSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
                                            JoinPreviousDirection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
                        parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
                } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
            // pops
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
            for (int counter = 0; counter < popDepth; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
                parseBuffer.addElement(new ElementSpec(null, ElementSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
                                                       EndTagType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
            // pushes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
            for (int counter = 0; counter < pushDepth; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
                ElementSpec es = new ElementSpec(null, ElementSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
                                                 StartTagType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
                es.setDirection(ElementSpec.JoinNextDirection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
                parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
            insertTagDepthDelta = depthTo(Math.max(0, offset - 1)) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
                                  popDepth + pushDepth - inBlock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
            if (isBlockTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
                // A start spec will be added (for this tag), so we account
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
                // for it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
                insertTagDepthDelta++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
                // An implied paragraph close (end spec) is going to be added,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
                // so we account for it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
                insertTagDepthDelta--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
                inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
                lastWasNewline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
         * This is set to true when and end is invoked for <html>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
        private boolean receivedEndHTML;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
        /** Number of times <code>flushBuffer</code> has been invoked. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
        private int flushCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
        /** If true, behavior is similiar to insertTag, but instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
         * waiting for insertTag will wait for first Element without
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
         * an 'implied' attribute and begin inserting then. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
        private boolean insertAfterImplied;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
        /** This is only used if insertAfterImplied is true. If false, only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
         * inserting content, and there is a trailing newline it is removed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
        private boolean wantsTrailingNewline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
        int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
        int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
        boolean inParagraph = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
        boolean impliedP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
        boolean inPre = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
        boolean inTextArea = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
        TextAreaDocument textAreaDocument = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
        boolean inTitle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
        boolean lastWasNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
        boolean emptyAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
        /** True if (!emptyDocument && insertTag == null), this is used so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
         * much it is cached. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
        boolean midInsert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
        /** True when the body has been encountered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
        boolean inBody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
        /** If non null, gives parent Tag that insert is to happen at. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
        HTML.Tag insertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
        /** If true, the insertTag is inserted, otherwise elements after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
         * the insertTag is found are inserted. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
        boolean insertInsertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
        /** Set to true when insertTag has been found. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
        boolean foundInsertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
        /** When foundInsertTag is set to true, this will be updated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
         * reflect the delta between the two structures. That is, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
         * will be the depth the inserts are happening at minus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
         * depth of the tags being passed in. A value of 0 (the common
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
         * case) indicates the structures match, a value greater than 0 indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
         * the insert is happening at a deeper depth than the stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
         * parsing, and a value less than 0 indicates the insert is happening earlier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
         * in the tree that the parser thinks and that we will need to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
         * EndTagType specs in the flushBuffer method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
        int insertTagDepthDelta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
        /** How many parents to ascend before insert new elements. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
        int popDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
        /** How many parents to descend (relative to popDepth) before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
         * inserting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
        int pushDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
        /** Last Map that was encountered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
        Map lastMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
        /** Set to true when a style element is encountered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
        boolean inStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
        /** Name of style to use. Obtained from Meta tag. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
        String defaultStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
        /** Vector describing styles that should be include. Will consist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
         * of a bunch of HTML.Tags, which will either be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
         * <p>LINK: in which case it is followed by an AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
         * <p>STYLE: in which case the following element is a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
         * indicating the type (may be null), and the elements following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
         * it until the next HTML.Tag are the rules as Strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
         */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  4029
        Vector<Object> styles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
        /** True if inside the head tag. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
        boolean inHead = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
        /** Set to true if the style language is text/css. Since this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
         * used alot, it is cached. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
        boolean isStyleCSS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
        /** True if inserting into an empty document. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
        boolean emptyDocument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
        /** Attributes from a style Attribute. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
        AttributeSet styleAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
         * Current option, if in an option element (needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
         * load the label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
        Option option;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  4046
        protected Vector<ElementSpec> parseBuffer = new Vector<ElementSpec>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
        protected MutableAttributeSet charAttr = new TaggedAttributeSet();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  4048
        Stack<AttributeSet> charAttrStack = new Stack<AttributeSet>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  4049
        Hashtable<HTML.Tag, TagAction> tagMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
        int inBlock = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
         * This attribute is sometimes used to refer to next tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
         * to be handled after p-implied when the latter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
         * the current tag which is being handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
        private HTML.Tag nextTagAfterPImplied = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
     * Used by StyleSheet to determine when to avoid removing HTML.Tags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
     * matching StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
    static class TaggedAttributeSet extends SimpleAttributeSet {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
        TaggedAttributeSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
     * An element that represents a chunk of text that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
     * a set of HTML character level attributes assigned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
    public class RunElement extends LeafElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
         * Constructs an element that represents content within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
         * document (has no children).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
         * @param parent  the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
         * @param a       the element attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
         * @param offs0   the start offset (must be at least 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
         * @param offs1   the end offset (must be at least offs0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
        public RunElement(Element parent, AttributeSet a, int offs0, int offs1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
            super(parent, a, offs0, offs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
         * Gets the name of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
         * @return the name, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
            Object o = getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
            if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
                return o.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
            return super.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
         * Gets the resolving parent.  HTML attributes are not inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
         * at the model level so we override this to return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
         * @return null, there are none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
     * An element that represents a structural <em>block</em> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
     * HTML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
    public class BlockElement extends BranchElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
         * Constructs a composite element that initially contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
         * no children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
         * @param parent  the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
         * @param a       the attributes for the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
        public BlockElement(Element parent, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
            super(parent, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
         * Gets the name of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
         * @return the name, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
            Object o = getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
            if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
                return o.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
            return super.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
         * Gets the resolving parent.  HTML attributes are not inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
         * at the model level so we override this to return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
         * @return null, there are none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
     * Document that allows you to set the maximum length of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
    private static class FixedLengthDocument extends PlainDocument {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
        private int maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
        public FixedLengthDocument(int maxLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
            this.maxLength = maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
        public void insertString(int offset, String str, AttributeSet a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
            throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
            if (str != null && str.length() + getLength() <= maxLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
                super.insertString(offset, str, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
}