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