jdk/src/java.desktop/share/classes/javax/swing/text/AbstractDocument.java
author ddehaven
Tue, 19 Aug 2014 10:32:16 -0700
changeset 26037 508779ce6619
parent 26027 jdk/src/share/classes/javax/swing/text/AbstractDocument.java@f42f02a8666d
parent 25859 jdk/src/share/classes/javax/swing/text/AbstractDocument.java@3317bb8137f4
child 30462 507bcb03c954
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.font.TextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.text.Bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.UIManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.undo.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.tree.TreeNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.font.BidiUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * An implementation of the document interface to serve as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * basis for implementing various kinds of documents.  At this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * level there is very little policy, so there is a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * increase in difficulty of use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This class implements a locking mechanism for the document.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * allows multiple readers or one writer, and writers must wait until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * all observers of the document have been notified of a previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * change before beginning another mutation to the document.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * read lock is acquired and released using the <code>render</code>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
    51
 * method.  A write lock is acquired by the methods that mutate the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * document, and are held for the duration of the method call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Notification is done on the thread that produced the mutation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * and the thread has full read access to the document for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * duration of the notification, but other readers are kept out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * until the notification has finished.  The notification is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * beans event notification which does not allow any further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * mutations until all listeners have been notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Any models subclassed from this class and used in conjunction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * with a text component that has a look and feel implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * that is derived from BasicTextUI may be safely updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * asynchronously, because all access to the View hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * is serialized by BasicTextUI if the document is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>AbstractDocument</code>.  The locking assumes that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * independent thread will access the View hierarchy only from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * the DocumentListener methods, and that there will be only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * one event thread active at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * If concurrency support is desired, there are the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * additional implications.  The code path for any DocumentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * implementation and any UndoListener implementation must be threadsafe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * and not access the component lock if trying to be safe from deadlocks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The <code>repaint</code> and <code>revalidate</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * on JComponent are safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * AbstractDocument models an implied break at the end of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Among other things this allows you to position the caret after the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * character. As a result of this, <code>getLength</code> returns one less
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * than the length of the Content. If you create your own Content, be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * sure and initialize it to have an additional character. Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * StringContent and GapContent for examples of this. Another implication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * of this is that Elements that model the implied end character will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * an endOffset == (getLength() + 1). For example, in DefaultStyledDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code>getParagraphElement(getLength()).getEndOffset() == getLength() + 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    93
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
    99
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
public abstract class AbstractDocument implements Document, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Constructs a new <code>AbstractDocument</code>, wrapped around some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * specified content storage mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param data the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected AbstractDocument(Content data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this(data, StyleContext.getDefaultStyleContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Constructs a new <code>AbstractDocument</code>, wrapped around some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * specified content storage mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param data the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param context the attribute context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    protected AbstractDocument(Content data, AttributeContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        bidiRoot = new BidiRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (defaultI18NProperty == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // determine default setting for i18n support
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   126
            String o = java.security.AccessController.doPrivileged(
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   127
                new java.security.PrivilegedAction<String>() {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   128
                    public String run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        return System.getProperty(I18NProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (o != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   134
                defaultI18NProperty = Boolean.valueOf(o);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                defaultI18NProperty = Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        putProperty( I18NProperty, defaultI18NProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        //REMIND(bcb) This creates an initial bidi element to account for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        //the \n that exists by default in the content.  Doing it this way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        //seems to expose a little too much knowledge of the content given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        //to us by the sub-class.  Consider having the sub-class' constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        //make an initial call to insertUpdate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Element[] p = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            p[0] = new BidiElement( bidiRoot, 0, 1, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            bidiRoot.replace(0,0,p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Supports managing a set of properties. Callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * can use the <code>documentProperties</code> dictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * to annotate the document with document-wide properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return a non-<code>null</code> <code>Dictionary</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see #setDocumentProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public Dictionary<Object,Object> getDocumentProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (documentProperties == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   166
            documentProperties = new Hashtable<Object, Object>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return documentProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Replaces the document properties dictionary for this document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param x the new dictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see #getDocumentProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void setDocumentProperties(Dictionary<Object,Object> x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        documentProperties = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected void fireInsertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        notifyingListeners = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (listeners[i]==DocumentListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    // if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    // e = new ListSelectionEvent(this, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    ((DocumentListener)listeners[i+1]).insertUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            notifyingListeners = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected void fireChangedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        notifyingListeners = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (listeners[i]==DocumentListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    // if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    // e = new ListSelectionEvent(this, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    ((DocumentListener)listeners[i+1]).changedUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            notifyingListeners = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    protected void fireRemoveUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        notifyingListeners = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (listeners[i]==DocumentListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    // if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    // e = new ListSelectionEvent(this, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    ((DocumentListener)listeners[i+1]).removeUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            notifyingListeners = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    protected void fireUndoableEditUpdate(UndoableEditEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (listeners[i]==UndoableEditListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                // if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                // e = new ListSelectionEvent(this, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                ((UndoableEditListener)listeners[i+1]).undoableEditHappened(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * upon this document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * with a class literal, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * document <code>d</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * for its document listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <pre>DocumentListener[] mls = (DocumentListener[])(d.getListeners(DocumentListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *          should specify an interface that descends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *          <code><em>Foo</em>Listener</code>s on this component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see #getDocumentListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @see #getUndoableEditListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Gets the asynchronous loading priority.  If less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the document should not be loaded asynchronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return the asynchronous loading priority, or <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *   if the document should not be loaded asynchronously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public int getAsynchronousLoadPriority() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        Integer loadPriority = (Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            getProperty(AbstractDocument.AsyncLoadPriority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (loadPriority != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            return loadPriority.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Sets the asynchronous loading priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param p the new asynchronous loading priority; a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *   less than zero indicates that the document should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *   loaded asynchronously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void setAsynchronousLoadPriority(int p) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   354
        Integer loadPriority = (p >= 0) ? Integer.valueOf(p) : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        putProperty(AbstractDocument.AsyncLoadPriority, loadPriority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Sets the <code>DocumentFilter</code>. The <code>DocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * is passed <code>insert</code> and <code>remove</code> to conditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * allow inserting/deleting of the text.  A <code>null</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * indicates that no filtering will occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param filter the <code>DocumentFilter</code> used to constrain text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @see #getDocumentFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public void setDocumentFilter(DocumentFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        documentFilter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Returns the <code>DocumentFilter</code> that is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * filtering of insertion/removal. A <code>null</code> return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * implies no filtering is to occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @see #setDocumentFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return the DocumentFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public DocumentFilter getDocumentFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return documentFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    // --- Document methods -----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * This allows the model to be safely rendered in the presence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * of currency, if the model supports being updated asynchronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * The given runnable will be executed in a way that allows it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * to safely read the model with no changes while the runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * is being executed.  The runnable itself may <em>not</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * make any mutations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   395
     * This is implemented to acquire a read lock for the duration
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * of the runnables execution.  There may be multiple runnables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * executing at the same time, and all writers will be blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * while there are active rendering runnables.  If the runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * throws an exception, its lock will be safely released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * There is no protection against a runnable that never exits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * which will effectively leave the document locked for it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * lifetime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * If the given runnable attempts to make any mutations in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * this implementation, a deadlock will occur.  There is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * no tracking of individual rendering threads to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * detecting this situation, but a subclass could incur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * the overhead of tracking them and throwing an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * are not. Please see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20428
diff changeset
   412
     * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   413
     * in Swing</A> for more information.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param r the renderer to execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public void render(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            r.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Returns the length of the data.  This is the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * characters of content that represents the users data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   430
     * @return the length &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @see Document#getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public int getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return data.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Adds a document listener for notification of any changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param listener the <code>DocumentListener</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @see Document#addDocumentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public void addDocumentListener(DocumentListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        listenerList.add(DocumentListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Removes a document listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param listener the <code>DocumentListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see Document#removeDocumentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public void removeDocumentListener(DocumentListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        listenerList.remove(DocumentListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Returns an array of all the document listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * registered on this document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return all of this document's <code>DocumentListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *         or an empty array if no document listeners are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *         currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @see #addDocumentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @see #removeDocumentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public DocumentListener[] getDocumentListeners() {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   470
        return listenerList.getListeners(DocumentListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Adds an undo listener for notification of any changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Undo/Redo operations performed on the <code>UndoableEdit</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * will cause the appropriate DocumentEvent to be fired to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * the view(s) in sync with the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param listener the <code>UndoableEditListener</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see Document#addUndoableEditListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public void addUndoableEditListener(UndoableEditListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        listenerList.add(UndoableEditListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Removes an undo listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param listener the <code>UndoableEditListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @see Document#removeDocumentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public void removeUndoableEditListener(UndoableEditListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        listenerList.remove(UndoableEditListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Returns an array of all the undoable edit listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * registered on this document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @return all of this document's <code>UndoableEditListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *         or an empty array if no undoable edit listeners are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *         currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @see #addUndoableEditListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @see #removeUndoableEditListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public UndoableEditListener[] getUndoableEditListeners() {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   510
        return listenerList.getListeners(UndoableEditListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * A convenience method for looking up a property value. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * getDocumentProperties().get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param key the non-<code>null</code> property key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return the value of this property or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @see #getDocumentProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public final Object getProperty(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        return getDocumentProperties().get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * A convenience method for storing up a property value.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * getDocumentProperties().put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * If <code>value</code> is <code>null</code> this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * remove the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param key the non-<code>null</code> key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param value the property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @see #getDocumentProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public final void putProperty(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            getDocumentProperties().put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            getDocumentProperties().remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if( key == TextAttribute.RUN_DIRECTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            && Boolean.TRUE.equals(getProperty(I18NProperty)) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            //REMIND - this needs to flip on the i18n property if run dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            //is rtl and the i18n property is not already on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                DefaultDocumentEvent e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    = new DefaultDocumentEvent(0, getLength(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                               DocumentEvent.EventType.INSERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                updateBidi( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Removes some content from the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Removing content causes a write lock to be held while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * actual changes are taking place.  Observers are notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * of the change on the thread that called this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * are not. Please see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20428
diff changeset
   573
     * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   574
     * in Swing</A> for more information.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   576
     * @param offs the starting offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   577
     * @param len the number of characters to remove &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @exception BadLocationException  the given remove position is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *   position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see Document#remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public void remove(int offs, int len) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        DocumentFilter filter = getDocumentFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            if (filter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                filter.remove(getFilterBypass(), offs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                handleRemove(offs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Performs the actual work of the remove. It is assumed the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * will have obtained a <code>writeLock</code> before invoking this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    void handleRemove(int offs, int len) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (offs < 0 || (offs + len) > getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                throw new BadLocationException("Invalid remove",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                               getLength() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            DefaultDocumentEvent chng =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    new DefaultDocumentEvent(offs, len, DocumentEvent.EventType.REMOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   611
            boolean isComposedTextElement;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            // Check whether the position of interest is the composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            isComposedTextElement = Utilities.isComposedTextElement(this, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            removeUpdate(chng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            UndoableEdit u = data.remove(offs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                chng.addEdit(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            postRemoveUpdate(chng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            // Mark the edit as done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            chng.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            fireRemoveUpdate(chng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            // only fire undo if Content implementation supports it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            // undo for the composed text is not supported for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if ((u != null) && !isComposedTextElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                fireUndoableEditUpdate(new UndoableEditEvent(this, chng));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Deletes the region of text from <code>offset</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <code>offset + length</code>, and replaces it with <code>text</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * It is up to the implementation as to how this is implemented, some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * implementations may treat this as two distinct operations: a remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * followed by an insert, others may treat the replace as one atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @param offset index of child element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @param length length of text to delete, may be 0 indicating don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *               delete anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param text text to insert, <code>null</code> indicates no text to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @param attrs AttributeSet indicating attributes of inserted text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *              <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *              is legal, and typically treated as an empty attributeset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *              but exact interpretation is left to the subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @exception BadLocationException the given position is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *            position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    public void replace(int offset, int length, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        AttributeSet attrs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (length == 0 && (text == null || text.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        DocumentFilter filter = getDocumentFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if (filter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                filter.replace(getFilterBypass(), offset, length, text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                               attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    remove(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                if (text != null && text.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    insertString(offset, text, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Inserts some content into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Inserting content causes a write lock to be held while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * actual changes are taking place, followed by notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * to the observers on the thread that grabbed the write lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * are not. Please see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20428
diff changeset
   686
     * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   687
     * in Swing</A> for more information.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   689
     * @param offs the starting offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @param str the string to insert; does nothing with null/empty strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @param a the attributes for the inserted content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @exception BadLocationException  the given insert position is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *   position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @see Document#insertString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if ((str == null) || (str.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        DocumentFilter filter = getDocumentFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        writeLock();
12404
de0c1d3ed1c5 7146146: Deadlock between subclass of AbstractDocument and UndoManager
rupashka
parents: 10316
diff changeset
   703
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            if (filter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                filter.insertString(getFilterBypass(), offs, str, a);
12404
de0c1d3ed1c5 7146146: Deadlock between subclass of AbstractDocument and UndoManager
rupashka
parents: 10316
diff changeset
   707
            } else {
23249
cc477ea79442 8030118: Document listeners fired outside document lock
malenkov
parents: 21982
diff changeset
   708
                handleInsertString(offs, str, a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Performs the actual work of inserting the text; it is assumed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * caller has obtained a write lock before invoking this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
23249
cc477ea79442 8030118: Document listeners fired outside document lock
malenkov
parents: 21982
diff changeset
   719
    private void handleInsertString(int offs, String str, AttributeSet a)
12404
de0c1d3ed1c5 7146146: Deadlock between subclass of AbstractDocument and UndoManager
rupashka
parents: 10316
diff changeset
   720
            throws BadLocationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if ((str == null) || (str.length() == 0)) {
23249
cc477ea79442 8030118: Document listeners fired outside document lock
malenkov
parents: 21982
diff changeset
   722
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        UndoableEdit u = data.insertString(offs, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        DefaultDocumentEvent e =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            new DefaultDocumentEvent(offs, str.length(), DocumentEvent.EventType.INSERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            e.addEdit(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        // see if complex glyph layout support is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if( getProperty(I18NProperty).equals( Boolean.FALSE ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            // if a default direction of right-to-left has been specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            // we want complex layout even if the text is all left to right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            Object d = getProperty(TextAttribute.RUN_DIRECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            if ((d != null) && (d.equals(TextAttribute.RUN_DIRECTION_RTL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                putProperty( I18NProperty, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                char[] chars = str.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if (SwingUtilities2.isComplexLayout(chars, 0, chars.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    putProperty( I18NProperty, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        insertUpdate(e, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        // Mark the edit as done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        e.end();
23249
cc477ea79442 8030118: Document listeners fired outside document lock
malenkov
parents: 21982
diff changeset
   749
        fireInsertUpdate(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        // only fire undo if Content implementation supports it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        // undo for the composed text is not supported for now
12404
de0c1d3ed1c5 7146146: Deadlock between subclass of AbstractDocument and UndoManager
rupashka
parents: 10316
diff changeset
   752
        if (u != null && (a == null || !a.isDefined(StyleConstants.ComposedTextAttribute))) {
23249
cc477ea79442 8030118: Document listeners fired outside document lock
malenkov
parents: 21982
diff changeset
   753
            fireUndoableEditUpdate(new UndoableEditEvent(this, e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Gets a sequence of text from the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   760
     * @param offset the starting offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   761
     * @param length the number of characters to retrieve &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @exception BadLocationException  the range given includes a position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *   that is not a valid position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @see Document#getText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    public String getText(int offset, int length) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            throw new BadLocationException("Length must be positive", length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        String str = data.getString(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Fetches the text contained within the given portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * If the partialReturn property on the txt parameter is false, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * data returned in the Segment will be the entire length requested and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * may or may not be a copy depending upon how the data was stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * If the partialReturn property is true, only the amount of text that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * can be returned without creating a copy is returned.  Using partial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * returns will give better performance for situations where large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * parts of the document are being scanned.  The following is an example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * of using the partial return to access the entire document:
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21591
diff changeset
   787
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * &nbsp; int nleft = doc.getDocumentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * &nbsp; Segment text = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * &nbsp; int offs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * &nbsp; text.setPartialReturn(true);
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   793
     * &nbsp; while (nleft &gt; 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * &nbsp;     doc.getText(offs, nleft, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * &nbsp;     // do something with text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * &nbsp;     nleft -= text.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * &nbsp;     offs += text.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * &nbsp; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   801
     * @param offset the starting offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   802
     * @param length the number of characters to retrieve &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @param txt the Segment object to retrieve the text into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @exception BadLocationException  the range given includes a position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *   that is not a valid position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    public void getText(int offset, int length, Segment txt) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            throw new BadLocationException("Length must be positive", length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        data.getChars(offset, length, txt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Returns a position that will track change as the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * is altered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * are not. Please see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20428
diff changeset
   820
     * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   821
     * in Swing</A> for more information.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   823
     * @param offs the position in the model &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @return the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @see Document#createPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    public synchronized Position createPosition(int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        return data.createPosition(offs);
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 a position that represents the start of the document.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * position returned can be counted on to track change and stay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * located at the beginning of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @return the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public final Position getStartPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        Position p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            p = createPosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Returns a position that represents the end of the document.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * position returned can be counted on to track change and stay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * located at the end of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @return the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public final Position getEndPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        Position p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            p = createPosition(data.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Gets all root elements defined.  Typically, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * will only be one so the default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * is to return the default root element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @return the root element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public Element[] getRootElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        Element[] elems = new Element[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        elems[0] = getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        elems[1] = getBidiRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        return elems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * Returns the root element that views should be based upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * unless some other mechanism for assigning views to element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * structures is provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @return the root element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @see Document#getDefaultRootElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    public abstract Element getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    // ---- local methods -----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * Returns the <code>FilterBypass</code>. This will create one if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * does not yet exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    private DocumentFilter.FilterBypass getFilterBypass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (filterBypass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            filterBypass = new DefaultFilterBypass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return filterBypass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Returns the root element of the bidirectional structure for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * document.  Its children represent character runs with a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Unicode bidi level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    public Element getBidiRootElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        return bidiRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * Returns true if the text in the range <code>p0</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * <code>p1</code> is left to right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     */
20103
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   917
    static boolean isLeftToRight(Document doc, int p0, int p1) {
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   918
        if (Boolean.TRUE.equals(doc.getProperty(I18NProperty))) {
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   919
            if (doc instanceof AbstractDocument) {
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   920
                AbstractDocument adoc = (AbstractDocument) doc;
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   921
                Element bidiRoot = adoc.getBidiRootElement();
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   922
                int index = bidiRoot.getElementIndex(p0);
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   923
                Element bidiElem = bidiRoot.getElement(index);
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   924
                if (bidiElem.getEndOffset() >= p1) {
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   925
                    AttributeSet bidiAttrs = bidiElem.getAttributes();
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   926
                    return ((StyleConstants.getBidiLevel(bidiAttrs) % 2) == 0);
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   927
                }
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 12404
diff changeset
   928
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Get the paragraph element containing the given position.  Sub-classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * must define for themselves what exactly constitutes a paragraph.  They
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * should keep in mind however that a paragraph should at least be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * unit of text over which to run the Unicode bidirectional algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
   939
     * @param pos the starting offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @return the element */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    public abstract Element getParagraphElement(int pos);
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
     * Fetches the context for managing attributes.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * method effectively establishes the strategy used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * for compressing AttributeSet information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @return the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    protected final AttributeContext getAttributeContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Updates document structure as a result of text insertion.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * will happen within a write lock.  If a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * this class reimplements this method, it should delegate to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * superclass as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * @param chng a description of the change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @param attr the attributes for the change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        if( getProperty(I18NProperty).equals( Boolean.TRUE ) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            updateBidi( chng );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        // Check if a multi byte is encountered in the inserted text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        if (chng.type == DocumentEvent.EventType.INSERT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        chng.getLength() > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                        !Boolean.TRUE.equals(getProperty(MultiByteProperty))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            Segment segment = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                getText(chng.getOffset(), chng.getLength(), segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                segment.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    if ((int)segment.current() > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                        putProperty(MultiByteProperty, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                } while (segment.next() != Segment.DONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                // Should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            SegmentCache.releaseSharedSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * Updates any document structure as a result of text removal.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * method is called before the text is actually removed from the Content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * This will happen within a write lock. If a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * of this class reimplements this method, it should delegate to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * superclass as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @param chng a description of the change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    protected void removeUpdate(DefaultDocumentEvent chng) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * Updates any document structure as a result of text removal.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * method is called after the text has been removed from the Content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * This will happen within a write lock. If a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * of this class reimplements this method, it should delegate to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * superclass as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @param chng a description of the change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    protected void postRemoveUpdate(DefaultDocumentEvent chng) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        if( getProperty(I18NProperty).equals( Boolean.TRUE ) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            updateBidi( chng );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * Update the bidi element structure as a result of the given change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * to the document.  The given change will be updated to reflect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * changes made to the bidi structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * This method assumes that every offset in the model is contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * exactly one paragraph.  This method also assumes that it is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * after the change is made to the default element structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    void updateBidi( DefaultDocumentEvent chng ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        // Calculate the range of paragraphs affected by the change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        int firstPStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        int lastPEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if( chng.type == DocumentEvent.EventType.INSERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            || chng.type == DocumentEvent.EventType.CHANGE )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            int chngStart = chng.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            int chngEnd =  chngStart + chng.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            firstPStart = getParagraphElement(chngStart).getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            lastPEnd = getParagraphElement(chngEnd).getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        } else if( chng.type == DocumentEvent.EventType.REMOVE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            Element paragraph = getParagraphElement( chng.getOffset() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            firstPStart = paragraph.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            lastPEnd = paragraph.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            throw new Error("Internal error: unknown event type.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        //System.out.println("updateBidi: firstPStart = " + firstPStart + " lastPEnd = " + lastPEnd );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        // Calculate the bidi levels for the affected range of paragraphs.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        // levels array will contain a bidi level for each character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        // affected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        byte levels[] = calculateBidiLevels( firstPStart, lastPEnd );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1053
        Vector<Element> newElements = new Vector<Element>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        // Calculate the first span of characters in the affected range with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        // the same bidi level.  If this level is the same as the level of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        // previous bidi element (the existing bidi element containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        // firstPStart-1), then merge in the previous element.  If not, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        // the previous element overlaps the affected range, truncate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        // previous element at firstPStart.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        int firstSpanStart = firstPStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        int removeFromIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        if( firstSpanStart > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            int prevElemIndex = bidiRoot.getElementIndex(firstPStart-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            removeFromIndex = prevElemIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            Element prevElem = bidiRoot.getElement(prevElemIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            int prevLevel=StyleConstants.getBidiLevel(prevElem.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            //System.out.println("createbidiElements: prevElem= " + prevElem  + " prevLevel= " + prevLevel + "level[0] = " + levels[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            if( prevLevel==levels[0] ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                firstSpanStart = prevElem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            } else if( prevElem.getEndOffset() > firstPStart ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                newElements.addElement(new BidiElement(bidiRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                                                       prevElem.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                                                       firstPStart, prevLevel));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                removeFromIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        int firstSpanEnd = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        while((firstSpanEnd<levels.length) && (levels[firstSpanEnd]==levels[0]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            firstSpanEnd++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        // Calculate the last span of characters in the affected range with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        // the same bidi level.  If this level is the same as the level of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        // next bidi element (the existing bidi element containing lastPEnd),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        // then merge in the next element.  If not, but the next element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        // overlaps the affected range, adjust the next element to start at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        // lastPEnd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        int lastSpanEnd = lastPEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        Element newNextElem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        int removeToIndex = bidiRoot.getElementCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if( lastSpanEnd <= getLength() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            int nextElemIndex = bidiRoot.getElementIndex( lastPEnd );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            removeToIndex = nextElemIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            Element nextElem = bidiRoot.getElement( nextElemIndex );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            int nextLevel = StyleConstants.getBidiLevel(nextElem.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            if( nextLevel == levels[levels.length-1] ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                lastSpanEnd = nextElem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            } else if( nextElem.getStartOffset() < lastPEnd ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                newNextElem = new BidiElement(bidiRoot, lastPEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                                              nextElem.getEndOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                                              nextLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                removeToIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        int lastSpanStart = levels.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        while( (lastSpanStart>firstSpanEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
               && (levels[lastSpanStart-1]==levels[levels.length-1]) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            lastSpanStart--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        // If the first and last spans are contiguous and have the same level,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        // merge them and create a single new element for the entire span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        // Otherwise, create elements for the first and last spans as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        // any spans in between.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        if((firstSpanEnd==lastSpanStart)&&(levels[0]==levels[levels.length-1])){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            newElements.addElement(new BidiElement(bidiRoot, firstSpanStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                                                   lastSpanEnd, levels[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            // Create an element for the first span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            newElements.addElement(new BidiElement(bidiRoot, firstSpanStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                                                   firstSpanEnd+firstPStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                                                   levels[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            // Create elements for the spans in between the first and last
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            for( int i=firstSpanEnd; i<lastSpanStart; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                //System.out.println("executed line 872");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                for( j=i;  (j<levels.length) && (levels[j] == levels[i]); j++ );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                newElements.addElement(new BidiElement(bidiRoot, firstPStart+i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                                                       firstPStart+j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                                                       (int)levels[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                i=j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            // Create an element for the last span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            newElements.addElement(new BidiElement(bidiRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                                                   lastSpanStart+firstPStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                                                   lastSpanEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                                                   levels[levels.length-1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        if( newNextElem != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            newElements.addElement( newNextElem );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        // Calculate the set of existing bidi elements which must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        // removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        int removedElemCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        if( bidiRoot.getElementCount() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            removedElemCount = removeToIndex - removeFromIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        Element[] removedElems = new Element[removedElemCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        for( int i=0; i<removedElemCount; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            removedElems[i] = bidiRoot.getElement(removeFromIndex+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        Element[] addedElems = new Element[ newElements.size() ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        newElements.copyInto( addedElems );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        // Update the change record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        ElementEdit ee = new ElementEdit( bidiRoot, removeFromIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                                          removedElems, addedElems );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        chng.addEdit( ee );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        // Update the bidi element structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        bidiRoot.replace( removeFromIndex, removedElems.length, addedElems );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * Calculate the levels array for a range of paragraphs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    private byte[] calculateBidiLevels( int firstPStart, int lastPEnd ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        byte levels[] = new byte[ lastPEnd - firstPStart ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        int  levelsEnd = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        Boolean defaultDirection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        Object d = getProperty(TextAttribute.RUN_DIRECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        if (d instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            defaultDirection = (Boolean) d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        // For each paragraph in the given range of paragraphs, get its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        // levels array and add it to the levels array for the entire span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        for(int o=firstPStart; o<lastPEnd; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            Element p = getParagraphElement( o );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            int pStart = p.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            int pEnd = p.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            // default run direction for the paragraph.  This will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            // null if there is no direction override specified (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            // the direction will be determined from the content).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            Boolean direction = defaultDirection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            d = p.getAttributes().getAttribute(TextAttribute.RUN_DIRECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            if (d instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                direction = (Boolean) d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            //System.out.println("updateBidi: paragraph start = " + pStart + " paragraph end = " + pEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            // Create a Bidi over this paragraph then get the level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            // array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                getText(pStart, pEnd-pStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            } catch (BadLocationException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                throw new Error("Internal error: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            // REMIND(bcb) we should really be using a Segment here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            Bidi bidiAnalyzer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            int bidiflag = Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            if (direction != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                if (TextAttribute.RUN_DIRECTION_LTR.equals(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    bidiflag = Bidi.DIRECTION_LEFT_TO_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    bidiflag = Bidi.DIRECTION_RIGHT_TO_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            bidiAnalyzer = new Bidi(seg.array, seg.offset, null, 0, seg.count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                    bidiflag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            BidiUtils.getLevels(bidiAnalyzer, levels, levelsEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            levelsEnd += bidiAnalyzer.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            o =  p.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        // REMIND(bcb) remove this code when debugging is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if( levelsEnd != levels.length )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            throw new Error("levelsEnd assertion failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        return levels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * Gives a diagnostic dump.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    public void dump(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        Element root = getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        if (root instanceof AbstractElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            ((AbstractElement)root).dump(out, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        bidiRoot.dump(out,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * Gets the content for the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @return the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    protected final Content getContent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * Creates a document leaf element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * Hook through which elements are created to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * document structure.  Because this implementation keeps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * structure and content separate, elements grow automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * when content is extended so splits of existing elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * follow.  The document itself gets to decide how to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * elements to give flexibility in the type of elements used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @param parent the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @param a the attributes for the element
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1271
     * @param p0 the beginning of the range &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1272
     * @param p1 the end of the range &gt;= p0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * @return the new element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        return new LeafElement(parent, a, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * Creates a document branch element, that can contain other elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * @param parent the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * @param a the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * @return the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    protected Element createBranchElement(Element parent, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        return new BranchElement(parent, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    // --- Document locking ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * Fetches the current writing thread if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * This can be used to distinguish whether a method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * being called as part of an existing modification or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * if a lock needs to be acquired and a new transaction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @return the thread actively modifying the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *  or <code>null</code> if there are no modifications in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    protected synchronized final Thread getCurrentWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        return currWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * Acquires a lock to begin mutating the document this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * protects.  There can be no writing, notification of changes, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * reading going on in order to gain the lock.  Additionally a thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * allowed to gain more than one <code>writeLock</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * as long as it doesn't attempt to gain additional <code>writeLock</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * from within document notification.  Attempting to gain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * <code>writeLock</code> from within a DocumentListener notification will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * result in an <code>IllegalStateException</code>.  The ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * to obtain more than one <code>writeLock</code> per thread allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * subclasses to gain a writeLock, perform a number of operations, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * release the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * Calls to <code>writeLock</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * must be balanced with calls to <code>writeUnlock</code>, else the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * <code>Document</code> will be left in a locked state so that no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * reading or writing can be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * @exception IllegalStateException thrown on illegal lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *  attempt.  If the document is implemented properly, this can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *  only happen if a document listener attempts to mutate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *  document.  This situation violates the bean event model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *  where order of delivery is not guaranteed and all listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *  should be notified before further mutations are allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    protected synchronized final void writeLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            while ((numReaders > 0) || (currWriter != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                if (Thread.currentThread() == currWriter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    if (notifyingListeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                        // Assuming one doesn't do something wrong in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                        // subclass this should only happen if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                        // DocumentListener tries to mutate the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                        throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                                      "Attempt to mutate in notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                    numWriters++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            currWriter = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            numWriters = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        } catch (InterruptedException e) {
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 21278
diff changeset
  1350
            throw new Error("Interrupted attempt to acquire write lock");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * Releases a write lock previously obtained via <code>writeLock</code>.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1356
     * After decrementing the lock count if there are no outstanding locks
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * this will allow a new writer, or readers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * @see #writeLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    protected synchronized final void writeUnlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        if (--numWriters <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            numWriters = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            currWriter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * Acquires a lock to begin reading some state from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * document.  There can be multiple readers at the same time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * Writing blocks the readers until notification of the change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * to the listeners has been completed.  This method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * be used very carefully to avoid unintended compromise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * of the document.  It should always be balanced with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * <code>readUnlock</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * @see #readUnlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    public synchronized final void readLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            while (currWriter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                if (currWriter == Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    // writer has full read access.... may try to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    // lock in notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            numReaders += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        } catch (InterruptedException e) {
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 21278
diff changeset
  1392
            throw new Error("Interrupted attempt to acquire read lock");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * Does a read unlock.  This signals that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * of the readers is done.  If there are no more readers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * then writing can begin again.  This should be balanced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * with a readLock, and should occur in a finally statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * so that the balance is guaranteed.  The following is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * &nbsp;   readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * &nbsp;   try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * &nbsp;       // do something
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * &nbsp;   } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * &nbsp;       readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * &nbsp;   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * @see #readLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    public synchronized final void readUnlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        if (currWriter == Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            // writer has full read access.... may try to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            // lock in notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        if (numReaders <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            throw new StateInvariantError(BAD_LOCK_STATE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        numReaders -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    // --- serialization ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
26027
f42f02a8666d 8054371: Need to suppress newly added unchecked cast and conversion in Swing code
prr
parents: 26001
diff changeset
  1429
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
      throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    {
26001
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
  1433
        ObjectInputStream.GetField f = s.readFields();
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
  1434
26027
f42f02a8666d 8054371: Need to suppress newly added unchecked cast and conversion in Swing code
prr
parents: 26001
diff changeset
  1435
        documentProperties =
f42f02a8666d 8054371: Need to suppress newly added unchecked cast and conversion in Swing code
prr
parents: 26001
diff changeset
  1436
            (Dictionary<Object, Object>) f.get("documentProperties", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        listenerList = new EventListenerList();
26001
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
  1438
        data = (Content) f.get("data", null);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
  1439
        context = (AttributeContext) f.get("context", null);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 25568
diff changeset
  1440
        documentFilter = (DocumentFilter) f.get("documentFilter", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        // Restore bidi structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        //REMIND(bcb) This creates an initial bidi element to account for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        //the \n that exists by default in the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        bidiRoot = new BidiRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            Element[] p = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            p[0] = new BidiElement( bidiRoot, 0, 1, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            bidiRoot.replace(0,0,p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        // At this point bidi root is only partially correct. To fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        // restore it we need access to getDefaultRootElement. But, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        // is created by the subclass and at this point will be null. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        // thus use registerValidation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        s.registerValidation(new ObjectInputValidation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            public void validateObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                    writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    DefaultDocumentEvent e = new DefaultDocumentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                                   (0, getLength(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                                    DocumentEvent.EventType.INSERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                    updateBidi( e );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                    writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    // ----- member variables ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    private transient int numReaders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    private transient Thread currWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * The number of writers, all obtained from <code>currWriter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    private transient int numWriters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * True will notifying listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    private transient boolean notifyingListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    private static Boolean defaultI18NProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * Storage for document-wide properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    private Dictionary<Object,Object> documentProperties = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * The event listener list for the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * Where the text is actually stored, and a set of marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * that track change as the document is edited are managed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    private Content data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * Factory for the attributes.  This is the strategy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * attribute compression and control of the lifetime of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * a set of attributes as a collection.  This may be shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * with other documents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    private AttributeContext context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * The root of the bidirectional structure for this document.  Its children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * represent character runs with the same Unicode bidi level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    private transient BranchElement bidiRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * Filter for inserting/removing of text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    private DocumentFilter documentFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * Used by DocumentFilter to do actual insert/remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    private transient DocumentFilter.FilterBypass filterBypass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    private static final String BAD_LOCK_STATE = "document lock failure";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * Error message to indicate a bad location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    protected static final String BAD_LOCATION = "document location failure";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * Name of elements used to represent paragraphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    public static final String ParagraphElementName = "paragraph";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * Name of elements used to represent content
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    public static final String ContentElementName = "content";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * Name of elements used to hold sections (lines/paragraphs).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    public static final String SectionElementName = "section";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * Name of elements used to hold a unidirectional run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    public static final String BidiElementName = "bidi level";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * Name of the attribute used to specify element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    public static final String ElementNameAttribute = "$ename";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * Document property that indicates whether internationalization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * functions such as text reordering or reshaping should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * performed. This property should not be publicly exposed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * since it is used for implementation convenience only.  As a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * side effect, copies of this property may be in its subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * that live in different packages (e.g. HTMLDocument as of now),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * so those copies should also be taken care of when this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * needs to be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    static final String I18NProperty = "i18n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * Document property that indicates if a character has been inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * into the document that is more than one byte long.  GlyphView uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * this to determine if it should use BreakIterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    static final Object MultiByteProperty = "multiByte";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * Document property that indicates asynchronous loading is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * desired, with the thread priority given as the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    static final String AsyncLoadPriority = "load priority";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * Interface to describe a sequence of character content that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * can be edited.  Implementations may or may not support a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * history mechanism which will be reflected by whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * mutations return an UndoableEdit implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * @see AbstractDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    public interface Content {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
         * Creates a position within the content that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
         * track change as the content is mutated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1600
         * @param offset the offset in the content &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
         * @return a Position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
         * @exception BadLocationException for an invalid offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        public Position createPosition(int offset) throws BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
         * Current length of the sequence of character content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1609
         * @return the length &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        public int length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
         * Inserts a string of characters into the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1616
         * @param where   offset into the sequence to make the insertion &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
         * @param str     string to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
         * @return  if the implementation supports a history mechanism,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
         *    a reference to an <code>Edit</code> implementation will be returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
         *    otherwise returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
         * @exception BadLocationException  thrown if the area covered by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
         *   the arguments is not contained in the character sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        public UndoableEdit insertString(int where, String str) throws BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
         * Removes some portion of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
         * @param where   The offset into the sequence to make the
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1630
         *   insertion &gt;= 0.
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1631
         * @param nitems  The number of items in the sequence to remove &gt;= 0.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1632
         * @return  If the implementation supports a history mechanism,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
         *    a reference to an Edit implementation will be returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
         *    otherwise null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
         * @exception BadLocationException  Thrown if the area covered by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
         *   the arguments is not contained in the character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        public UndoableEdit remove(int where, int nitems) throws BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
         * Fetches a string of characters contained in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1643
         * @param where   Offset into the sequence to fetch &gt;= 0.
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1644
         * @param len     number of characters to copy &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
         * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
         * @exception BadLocationException  Thrown if the area covered by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
         *   the arguments is not contained in the character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        public String getString(int where, int len) throws BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
         * Gets a sequence of characters and copies them into a Segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1654
         * @param where the starting offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1655
         * @param len the number of characters &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
         * @param txt the target location to copy into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
         * @exception BadLocationException  Thrown if the area covered by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
         *   the arguments is not contained in the character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        public void getChars(int where, int len, Segment txt) throws BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * An interface that can be used to allow MutableAttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * implementations to use pluggable attribute compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * techniques.  Each mutation of the attribute set can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * used to exchange a previous AttributeSet instance with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * another, preserving the possibility of the AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * remaining immutable.  An implementation is provided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * the StyleContext class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * The Element implementations provided by this class use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * this interface to provide their MutableAttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * implementations, so that different AttributeSet compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * techniques can be employed.  The method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * <code>getAttributeContext</code> should be implemented to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * return the object responsible for implementing the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * compression technique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * @see StyleContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    public interface AttributeContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
         * Adds an attribute to the given set, and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
         * the new representative set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
         * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
         * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
         * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        public AttributeSet addAttribute(AttributeSet old, Object name, Object value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
         * Adds a set of attributes to the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
         * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
         * @param attr the attributes to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
         * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
         * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        public AttributeSet addAttributes(AttributeSet old, AttributeSet attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
         * Removes an attribute from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
         * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
         * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
         * @see MutableAttributeSet#removeAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        public AttributeSet removeAttribute(AttributeSet old, Object name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
         * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
         * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
         * @param names the attribute names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
         * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
         * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
         * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
         * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
         * @param attrs the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
         * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
         * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
         * Fetches an empty AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
         * @return the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        public AttributeSet getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
         * Reclaims an attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
         * This is a way for a MutableAttributeSet to mark that it no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
         * longer need a particular immutable set.  This is only necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
         * in 1.1 where there are no weak references.  A 1.1 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
         * would call this in its finalize method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
         * @param a the attribute set to reclaim
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        public void reclaim(AttributeSet a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * Implements the abstract part of an element.  By default elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * support attributes by having a field that represents the immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * part of the current attribute set for the element.  The element itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * implements MutableAttributeSet which can be used to modify the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * by fetching a new immutable set.  The immutable sets are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * by the AttributeContext associated with the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  1768
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
  1772
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    public abstract class AbstractElement implements Element, MutableAttributeSet, Serializable, TreeNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
         * Creates a new AbstractElement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
         * @param parent the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
         * @param a the attributes for the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        public AbstractElement(Element parent, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            attributes = getAttributeContext().getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                addAttributes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        private final void indent(PrintWriter out, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                out.print("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
         * Dumps a debugging representation of the element hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
         * @param psOut the output stream
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1800
         * @param indentAmount the indentation level &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        public void dump(PrintStream psOut, int indentAmount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            PrintWriter out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                out = new PrintWriter(new OutputStreamWriter(psOut,"JavaEsc"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                                      true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            } catch (UnsupportedEncodingException e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                out = new PrintWriter(psOut,true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            indent(out, indentAmount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            if (getName() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                out.print("<??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                out.print("<" + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            if (getAttributeCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                // dump the attributes
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23276
diff changeset
  1819
                Enumeration<?> names = attributes.getAttributeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                    Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                    indent(out, indentAmount + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                    out.println(name + "=" + getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                indent(out, indentAmount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            out.println(">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            if (isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                indent(out, indentAmount+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                out.print("[" + getStartOffset() + "," + getEndOffset() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                Content c = getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                    String contentStr = c.getString(getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                                                    getEndOffset() - getStartOffset())/*.trim()*/;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    if (contentStr.length() > 40) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                        contentStr = contentStr.substring(0, 40) + "...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                    out.println("["+contentStr+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                int n = getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                    AbstractElement e = (AbstractElement) getElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                    e.dump(psOut, indentAmount+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        // --- AttributeSet ----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        // delegated to the immutable field "attributes"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
         * Gets the number of attributes that are defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  1858
         * @return the number of attributes &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
         * @see AttributeSet#getAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        public int getAttributeCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            return attributes.getAttributeCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
         * Checks whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
         * @param attrName the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
         * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
         * @see AttributeSet#isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        public boolean isDefined(Object attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            return attributes.isDefined(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         * Checks whether two attribute sets are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
         * @param attr the attribute set to check against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
         * @return true if the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         * @see AttributeSet#isEqual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        public boolean isEqual(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            return attributes.isEqual(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
         * Copies a set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
         * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
         * @see AttributeSet#copyAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        public AttributeSet copyAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            return attributes.copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
         * Gets the value of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
         * @param attrName the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
         * @return the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
         * @see AttributeSet#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        public Object getAttribute(Object attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            Object value = attributes.getAttribute(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                // The delegate nor it's resolvers had a match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                // so we'll try to resolve through the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                // element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                AttributeSet a = (parent != null) ? parent.getAttributes() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                    value = a.getAttribute(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
         * Gets the names of all attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
         * @return the attribute names as an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         * @see AttributeSet#getAttributeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        public Enumeration<?> getAttributeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            return attributes.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
         * Checks whether a given attribute name/value is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
         * @return true if the name/value is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         * @see AttributeSet#containsAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        public boolean containsAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            return attributes.containsAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
         * Checks whether the element contains all the attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
         * @param attrs the attributes to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
         * @return true if the element contains all the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
         * @see AttributeSet#containsAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        public boolean containsAttributes(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            return attributes.containsAttributes(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         * Gets the resolving parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         * If not overridden, the resolving parent defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         * the parent element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
         * @return the attributes from the parent, <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            AttributeSet a = attributes.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            if ((a == null) && (parent != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                a = parent.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        // --- MutableAttributeSet ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        // should fetch a new immutable record for the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        // "attributes".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         * Adds an attribute to the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
         * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        public void addAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            checkForIllegalCast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            AttributeContext context = getAttributeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            attributes = context.addAttribute(attributes, name, value);
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
         * Adds a set of attributes to the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
         * @param attr the attributes to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
         * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        public void addAttributes(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            checkForIllegalCast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            AttributeContext context = getAttributeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            attributes = context.addAttributes(attributes, attr);
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
         * Removes an attribute from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
         * @see MutableAttributeSet#removeAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        public void removeAttribute(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            checkForIllegalCast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            AttributeContext context = getAttributeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            attributes = context.removeAttribute(attributes, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
         * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
         * @param names the attribute names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
         * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        public void removeAttributes(Enumeration<?> names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            checkForIllegalCast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            AttributeContext context = getAttributeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            attributes = context.removeAttributes(attributes, names);
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
         * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
         * @param attrs the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
         * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        public void removeAttributes(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            checkForIllegalCast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            AttributeContext context = getAttributeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            if (attrs == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                attributes = context.getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                attributes = context.removeAttributes(attributes, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
         * Sets the resolving parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
         * @param parent the parent, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
         * @see MutableAttributeSet#setResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        public void setResolveParent(AttributeSet parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            checkForIllegalCast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            AttributeContext context = getAttributeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                attributes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                    context.addAttribute(attributes, StyleConstants.ResolveAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                                         parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                attributes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                    context.removeAttribute(attributes, StyleConstants.ResolveAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        private final void checkForIllegalCast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            Thread t = getCurrentWriter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            if ((t == null) || (t != Thread.currentThread())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                throw new StateInvariantError("Illegal cast to MutableAttributeSet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        // --- Element methods -------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
         * Retrieves the underlying model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
         * @return the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        public Document getDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            return AbstractDocument.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
         * Gets the parent of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
         * @return the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        public Element getParentElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
         * Gets the attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
         * @return the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
         * Gets the name of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
         * @return the name, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            if (attributes.isDefined(ElementNameAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                return (String) attributes.getAttribute(ElementNameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
         * Gets the starting offset in the model for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2107
         * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        public abstract int getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
         * Gets the ending offset in the model for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2114
         * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        public abstract int getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
         * Gets a child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2121
         * @param index the child index, &gt;= 0 &amp;&amp; &lt; getElementCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
         * @return the child element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        public abstract Element getElement(int index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
         * Gets the number of children for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2129
         * @return the number of children &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        public abstract int getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
         * Gets the child element index closest to the given model offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2136
         * @param offset the offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2137
         * @return the element index &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        public abstract int getElementIndex(int offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
         * Checks whether the element is a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
         * @return true if a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        public abstract boolean isLeaf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        // --- TreeNode methods -------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
         * Returns the child <code>TreeNode</code> at index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
         * <code>childIndex</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        public TreeNode getChildAt(int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            return (TreeNode)getElement(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
         * Returns the number of children <code>TreeNode</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
         * receiver contains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
         * @return the number of children <code>TreeNodews</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
         * receiver contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        public int getChildCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            return getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
         * Returns the parent <code>TreeNode</code> of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
         * @return the parent <code>TreeNode</code> of the receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        public TreeNode getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            return (TreeNode)getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
         * Returns the index of <code>node</code> in the receivers children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
         * If the receiver does not contain <code>node</code>, -1 will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
         * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
         * @param node the location of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
         * @return the index of <code>node</code> in the receiver's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
         * children, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        public int getIndex(TreeNode node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            for(int counter = getChildCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                if(getChildAt(counter) == node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    return counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
         * Returns true if the receiver allows children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
         * @return true if the receiver allows children, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        public abstract boolean getAllowsChildren();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
         * Returns the children of the receiver as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
         * <code>Enumeration</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
         * @return the children of the receiver as an <code>Enumeration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
         */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25193
diff changeset
  2203
        public abstract Enumeration<TreeNode> children();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        // --- serialization ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            StyleContext.writeAttributeSet(s, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            MutableAttributeSet attr = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            StyleContext.readAttributeSet(s, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            AttributeContext context = getAttributeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            attributes = context.addAttributes(SimpleAttributeSet.EMPTY, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        // ---- variables -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        private Element parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        private transient AttributeSet attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     * Implements a composite element that contains other elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  2238
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
  2242
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    public class BranchElement extends AbstractElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
         * Constructs a composite element that initially contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
         * no children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
         * @param parent  The parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
         * @param a the attributes for the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        public BranchElement(Element parent, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            super(parent, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            children = new AbstractElement[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            nchildren = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            lastIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
         * Gets the child element that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
         * the given model position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2264
         * @param pos the position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
         * @return the element, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        public Element positionToElement(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            int index = getElementIndex(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            Element child = children[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            int p0 = child.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            int p1 = child.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            if ((pos >= p0) && (pos < p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
         * Replaces content with a new set of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2281
         * @param offset the starting offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2282
         * @param length the length to replace &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
         * @param elems the new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        public void replace(int offset, int length, Element[] elems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            int delta = elems.length - length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            int src = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            int nmove = nchildren - src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            int dest = src + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            if ((nchildren + delta) >= children.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                // need to grow the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                int newLength = Math.max(2*children.length, nchildren + delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                AbstractElement[] newChildren = new AbstractElement[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                System.arraycopy(children, 0, newChildren, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                System.arraycopy(elems, 0, newChildren, offset, elems.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                System.arraycopy(children, src, newChildren, dest, nmove);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                children = newChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                // patch the existing array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                System.arraycopy(children, src, children, dest, nmove);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                System.arraycopy(elems, 0, children, offset, elems.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            nchildren = nchildren + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
         * Converts the element to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
         * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            return "BranchElement(" + getName() + ") " + getStartOffset() + "," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                getEndOffset() + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        // --- Element methods -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
         * Gets the element name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
         * @return the element name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            String nm = super.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            if (nm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                nm = ParagraphElementName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            return nm;
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
         * Gets the starting offset in the model for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2334
         * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            return children[0].getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
         * Gets the ending offset in the model for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
         * @throws NullPointerException if this element has no children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2344
         * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            Element child =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                (nchildren > 0) ? children[nchildren - 1] : children[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            return child.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
         * Gets a child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2355
         * @param index the child index, &gt;= 0 &amp;&amp; &lt; getElementCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
         * @return the child element, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        public Element getElement(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            if (index < nchildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                return children[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
         * Gets the number of children for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2368
         * @return the number of children &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        public int getElementCount()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            return nchildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
         * Gets the child element index closest to the given model offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2377
         * @param offset the offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2378
         * @return the element index &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        public int getElementIndex(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            int lower = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            int upper = nchildren - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
            int mid = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
            int p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            if (nchildren == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
            if (offset >= getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                return nchildren - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            // see if the last index can be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            if ((lastIndex >= lower) && (lastIndex <= upper)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                Element lastHit = children[lastIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                p0 = lastHit.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                p1 = lastHit.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                if ((offset >= p0) && (offset < p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                    return lastIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
                // last index wasn't a hit, but it does give useful info about
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                // where a hit (if any) would be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                if (offset < p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                    upper = lastIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                    lower = lastIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            while (lower <= upper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                mid = lower + ((upper - lower) / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                Element elem = children[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                p0 = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                p1 = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                if ((offset >= p0) && (offset < p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                    // found the location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                    index = mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                    lastIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                } else if (offset < p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                    upper = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                    lower = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            // didn't find it, but we indicate the index of where it would belong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            if (offset < p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                index = mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                index = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            lastIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
         * Checks whether the element is a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
         * @return true if a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        public boolean isLeaf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        // ------ TreeNode ----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
         * Returns true if the receiver allows children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
         * @return true if the receiver allows children, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
        public boolean getAllowsChildren() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
         * Returns the children of the receiver as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
         * <code>Enumeration</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
         * @return the children of the receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
         */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25193
diff changeset
  2466
        public Enumeration<TreeNode> children() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            if(nchildren == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25193
diff changeset
  2470
            Vector<TreeNode> tempVector = new Vector<>(nchildren);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
            for(int counter = 0; counter < nchildren; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                tempVector.addElement(children[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            return tempVector.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        // ------ members ----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
        private AbstractElement[] children;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
        private int nchildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
        private int lastIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
     * Implements an element that directly represents content of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     * some kind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  2493
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * @see     Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
  2499
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    public class LeafElement extends AbstractElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
         * Constructs an element that represents content within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
         * document (has no children).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
         * @param parent  The parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
         * @param a       The element attributes
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2508
         * @param offs0   The start offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2509
         * @param offs1   The end offset &gt;= offs0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        public LeafElement(Element parent, AttributeSet a, int offs0, int offs1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            super(parent, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                p0 = createPosition(offs0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                p1 = createPosition(offs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                p0 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                p1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                throw new StateInvariantError("Can't create Position references");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
         * Converts the element to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
         * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
            return "LeafElement(" + getName() + ") " + p0 + "," + p1 + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        // --- Element methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
         * Gets the starting offset in the model for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2538
         * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
            return p0.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
         * Gets the ending offset in the model for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2547
         * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            return p1.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
         * Gets the element name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
         * @return the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            String nm = super.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            if (nm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                nm = ContentElementName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
            return nm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
         * Gets the child element index closest to the given model offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2569
         * @param pos the offset &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2570
         * @return the element index &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        public int getElementIndex(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            return -1;
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
         * Gets a child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2579
         * @param index the child index, &gt;= 0 &amp;&amp; &lt; getElementCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
         * @return the child element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        public Element getElement(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
         * Returns the number of child elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2589
         * @return the number of children &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
        public int getElementCount()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
         * Checks whether the element is a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
         * @return true if a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        public boolean isLeaf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        // ------ TreeNode ----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
         * Returns true if the receiver allows children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
         * @return true if the receiver allows children, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        public boolean getAllowsChildren() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        }
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
         * Returns the children of the receiver as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
         * <code>Enumeration</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
         * @return the children of the receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
         */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25193
diff changeset
  2620
        @Override
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25193
diff changeset
  2621
        public Enumeration<TreeNode> children() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        // --- serialization ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
        private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
            s.writeInt(p0.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            s.writeInt(p1.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
        private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
            // set the range with positions that track change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            int off0 = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            int off1 = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                p0 = createPosition(off0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                p1 = createPosition(off1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                p0 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                p1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                throw new IOException("Can't restore Position references");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
        // ---- members -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        private transient Position p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        private transient Position p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
     * Represents the root element of the bidirectional element structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
     * The root element is the only element in the bidi element structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
     * which contains children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
    class BidiRootElement extends BranchElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
        BidiRootElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
            super( null, null );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
         * Gets the name of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
         * @return the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            return "bidi root";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
     * Represents an element of the bidirectional element structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
    class BidiElement extends LeafElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
         * Creates a new BidiElement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        BidiElement(Element parent, int start, int end, int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            super(parent, new SimpleAttributeSet(), start, end);
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  2687
            addAttribute(StyleConstants.BidiLevel, Integer.valueOf(level));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
            //System.out.println("BidiElement: start = " + start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
            //                   + " end = " + end + " level = " + level );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
         * Gets the name of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
         * @return the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
            return BidiElementName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
        int getLevel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
            Integer o = (Integer) getAttribute(StyleConstants.BidiLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                return o.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
            return 0;  // Level 0 is base level (non-embedded) left-to-right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
        boolean isLeftToRight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            return ((getLevel() % 2) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     * Stores document changes as the document is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
     * modified.  Can subsequently be used for change notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
     * when done with the document modification transaction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
     * This is used by the AbstractDocument class and its extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
     * for broadcasting change information to the document listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
    public class DefaultDocumentEvent extends CompoundEdit implements DocumentEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
         * Constructs a change record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2725
         * @param offs the offset into the document of the change &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2726
         * @param len  the length of the change &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
         * @param type the type of event (DocumentEvent.EventType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        public DefaultDocumentEvent(int offs, int len, DocumentEvent.EventType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
            offset = offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
            length = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
         * Returns a string description of the change event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
         * @return a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
            return edits.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        // --- CompoundEdit methods --------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
         * Adds a document edit.  If the number of edits crosses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
         * a threshold, this switches on a hashtable lookup for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
         * ElementChange implementations since access of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
         * needs to be relatively quick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
         * @param anEdit a document edit record
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
         * @return true if the edit was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        public boolean addEdit(UndoableEdit anEdit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
            // if the number of changes gets too great, start using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
            // a hashtable for to locate the change for a given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
            if ((changeLookup == null) && (edits.size() > 10)) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2761
                changeLookup = new Hashtable<Element, ElementChange>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                int n = edits.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                    Object o = edits.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                    if (o instanceof DocumentEvent.ElementChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                        DocumentEvent.ElementChange ec = (DocumentEvent.ElementChange) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                        changeLookup.put(ec.getElement(), ec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
            // if we have a hashtable... add the entry if it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
            // an ElementChange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
            if ((changeLookup != null) && (anEdit instanceof DocumentEvent.ElementChange)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                DocumentEvent.ElementChange ec = (DocumentEvent.ElementChange) anEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                changeLookup.put(ec.getElement(), ec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
            return super.addEdit(anEdit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
         * Redoes a change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
         * @exception CannotRedoException if the change cannot be redone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
        public void redo() throws CannotRedoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
            writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                // change the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                super.redo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                // fire a DocumentEvent to notify the view(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                UndoRedoDocumentEvent ev = new UndoRedoDocumentEvent(this, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                if (type == DocumentEvent.EventType.INSERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                    fireInsertUpdate(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                } else if (type == DocumentEvent.EventType.REMOVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                    fireRemoveUpdate(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                    fireChangedUpdate(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
                writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
         * Undoes a change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
         * @exception CannotUndoException if the change cannot be undone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
        public void undo() throws CannotUndoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
            writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                // change the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                super.undo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                // fire a DocumentEvent to notify the view(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                UndoRedoDocumentEvent ev = new UndoRedoDocumentEvent(this, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                if (type == DocumentEvent.EventType.REMOVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                    fireInsertUpdate(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                } else if (type == DocumentEvent.EventType.INSERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                    fireRemoveUpdate(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                    fireChangedUpdate(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
         * DefaultDocument events are significant.  If you wish to aggregate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
         * DefaultDocumentEvents to present them as a single edit to the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
         * place them into a CompoundEdit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
         * @return whether the event is significant for edit undo purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
        public boolean isSignificant() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
         * Provides a localized, human readable description of this edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
         * suitable for use in, say, a change log.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
         * @return the description
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
        public String getPresentationName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            DocumentEvent.EventType type = getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            if(type == DocumentEvent.EventType.INSERT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
                return UIManager.getString("AbstractDocument.additionText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
            if(type == DocumentEvent.EventType.REMOVE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
                return UIManager.getString("AbstractDocument.deletionText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
            return UIManager.getString("AbstractDocument.styleChangeText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
         * Provides a localized, human readable description of the undoable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
         * form of this edit, e.g. for use as an Undo menu item. Typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
         * derived from getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
         * @return the description
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
        public String getUndoPresentationName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
            return UIManager.getString("AbstractDocument.undoText") + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                getPresentationName();
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
         * Provides a localized, human readable description of the redoable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
         * form of this edit, e.g. for use as a Redo menu item. Typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
         * derived from getPresentationName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
         * @return the description
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
        public String getRedoPresentationName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            return UIManager.getString("AbstractDocument.redoText") + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                getPresentationName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        // --- DocumentEvent methods --------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
         * Returns the type of event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
         * @return the event type as a DocumentEvent.EventType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
         * @see DocumentEvent#getType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
        public DocumentEvent.EventType getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
         * Returns the offset within the document of the start of the change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2895
         * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
         * @see DocumentEvent#getOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
        public int getOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
         * Returns the length of the change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  2905
         * @return the length &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
         * @see DocumentEvent#getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
        public int getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
            return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
         * Gets the document that sourced the change event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
         * @return the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
         * @see DocumentEvent#getDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
        public Document getDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
            return AbstractDocument.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
         * Gets the changes for an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
         * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
         * @return the changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
        public DocumentEvent.ElementChange getChange(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
            if (changeLookup != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2930
                return changeLookup.get(elem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
            int n = edits.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                Object o = edits.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                if (o instanceof DocumentEvent.ElementChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                    DocumentEvent.ElementChange c = (DocumentEvent.ElementChange) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                    if (elem.equals(c.getElement())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
        // --- member variables ------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
        private int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
        private int length;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2949
        private Hashtable<Element, ElementChange> changeLookup;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
        private DocumentEvent.EventType type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
     * This event used when firing document changes while Undo/Redo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
     * operations. It just wraps DefaultDocumentEvent and delegates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
     * all calls to it except getType() which depends on operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
     * (Undo or Redo).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
    class UndoRedoDocumentEvent implements DocumentEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        private DefaultDocumentEvent src = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        private EventType type = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        public UndoRedoDocumentEvent(DefaultDocumentEvent src, boolean isUndo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            this.src = src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
            if(isUndo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                if(src.getType().equals(EventType.INSERT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                    type = EventType.REMOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                } else if(src.getType().equals(EventType.REMOVE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                    type = EventType.INSERT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                    type = src.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                type = src.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
        public DefaultDocumentEvent getSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            return src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
        // DocumentEvent methods delegated to DefaultDocumentEvent source
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        // except getType() which depends on operation (Undo or Redo).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
        public int getOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
            return src.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        public int getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
            return src.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        public Document getDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
            return src.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
        public DocumentEvent.EventType getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
        public DocumentEvent.ElementChange getChange(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
            return src.getChange(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
     * An implementation of ElementChange that can be added to the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
     * event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
    public static class ElementEdit extends AbstractUndoableEdit implements DocumentEvent.ElementChange {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
         * Constructs an edit record.  This does not modify the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
         * so it can safely be used to <em>catch up</em> a view to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
         * current model state for views that just attached to a model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
         * @param e the element
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  3018
         * @param index the index into the model &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
         * @param removed a set of elements that were removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
         * @param added a set of elements that were added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        public ElementEdit(Element e, int index, Element[] removed, Element[] added) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
            this.e = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
            this.removed = removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
            this.added = added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
         * Returns the underlying element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
         * @return the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
        public Element getElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
         * Returns the index into the list of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 20103
diff changeset
  3042
         * @return the index &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
        public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            return index;
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
         * Gets a list of children that were removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
         * @return the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
        public Element[] getChildrenRemoved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
            return removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
         * Gets a list of children that were added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
         * @return the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
        public Element[] getChildrenAdded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
            return added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
         * Redoes a change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
         * @exception CannotRedoException if the change cannot be redone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
        public void redo() throws CannotRedoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
            super.redo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
            // Since this event will be reused, switch around added/removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
            Element[] tmp = removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
            removed = added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
            added = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
            // PENDING(prinz) need MutableElement interface, canRedo() should check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
            ((AbstractDocument.BranchElement)e).replace(index, removed.length, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
         * Undoes a change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
         * @exception CannotUndoException if the change cannot be undone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
        public void undo() throws CannotUndoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
            super.undo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
            // PENDING(prinz) need MutableElement interface, canUndo() should check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
            ((AbstractDocument.BranchElement)e).replace(index, added.length, removed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
            // Since this event will be reused, switch around added/removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
            Element[] tmp = removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
            removed = added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
            added = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        private Element e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        private int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        private Element[] removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        private Element[] added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
    private class DefaultFilterBypass extends DocumentFilter.FilterBypass {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        public Document getDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
            return AbstractDocument.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        public void remove(int offset, int length) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
            BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
            handleRemove(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
        public void insertString(int offset, String string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
                                 AttributeSet attr) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
                                        BadLocationException {
23249
cc477ea79442 8030118: Document listeners fired outside document lock
malenkov
parents: 21982
diff changeset
  3119
            handleInsertString(offset, string, attr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
        public void replace(int offset, int length, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
                            AttributeSet attrs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
            handleRemove(offset, length);
23249
cc477ea79442 8030118: Document listeners fired outside document lock
malenkov
parents: 21982
diff changeset
  3125
            handleInsertString(offset, text, attrs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
}