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