jdk/src/share/classes/javax/swing/JTextArea.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 715 f16baef3a20e
child 20157 cafca01a8e28
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A <code>JTextArea</code> is a multi-line area that displays plain text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * It is intended to be a lightweight component that provides source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * compatibility with the <code>java.awt.TextArea</code> class where it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * reasonably do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * You can find information and examples of using all the text components in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/text.html">Using Text Components</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This component has capabilities not found in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>java.awt.TextArea</code> class.  The superclass should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * consulted for additional capabilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Alternative multi-line text classes with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * more capabilities are <code>JTextPane</code> and <code>JEditorPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The <code>java.awt.TextArea</code> internally handles scrolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>JTextArea</code> is different in that it doesn't manage scrolling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * but implements the swing <code>Scrollable</code> interface.  This allows it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * to be placed inside a <code>JScrollPane</code> if scrolling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * behavior is desired, and used directly if scrolling is not desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * The <code>java.awt.TextArea</code> has the ability to do line wrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * This was controlled by the horizontal scrolling policy.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * scrolling is not done by <code>JTextArea</code> directly, backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * compatibility must be provided another way.  <code>JTextArea</code> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * a bound property for line wrapping that controls whether or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * not it will wrap lines.  By default, the line wrapping property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * is set to false (not wrapped).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <code>java.awt.TextArea</code> has two properties <code>rows</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * and <code>columns</code> that are used to determine the preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <code>JTextArea</code> uses these properties to indicate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * preferred size of the viewport when placed inside a <code>JScrollPane</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * to match the functionality provided by <code>java.awt.TextArea</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <code>JTextArea</code> has a preferred size of what is needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * display all of the text, so that it functions properly inside of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * a <code>JScrollPane</code>.  If the value for <code>rows</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * or <code>columns</code> is equal to zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * the preferred size along that axis is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * the viewport preferred size along the same axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * The <code>java.awt.TextArea</code> could be monitored for changes by adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * a <code>TextListener</code> for <code>TextEvent</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * In the <code>JTextComponent</code> based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * components, changes are broadcasted from the model via a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <code>DocumentEvent</code> to <code>DocumentListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * The <code>DocumentEvent</code> gives
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the location of the change and the kind of change if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The code fragment might look something like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *    DocumentListener myListener = ??;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *    JTextArea myArea = ??;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *    myArea.getDocument().addDocumentListener(myListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <dt><b><font size=+1>Newlines</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * For a discussion on how newlines are handled, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <a href="text/DefaultEditorKit.html">DefaultEditorKit</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *   attribute: isContainer false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * description: A multi-line area that displays plain text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see JTextPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
public class JTextArea extends JTextComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static final String uiClassID = "TextAreaUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Constructs a new TextArea.  A default model is set, the initial string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * is null, and rows/columns are set to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public JTextArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this(null, null, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Constructs a new TextArea with the specified text displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * A default model is created and rows/columns are set to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param text the text to be displayed, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public JTextArea(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this(null, text, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Constructs a new empty TextArea with the specified number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * rows and columns.  A default model is created, and the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * string is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param rows the number of rows >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param columns the number of columns >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception IllegalArgumentException if the rows or columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *  arguments are negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public JTextArea(int rows, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this(null, null, rows, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Constructs a new TextArea with the specified text and number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * of rows and columns.  A default model is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param text the text to be displayed, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param rows the number of rows >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param columns the number of columns >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception IllegalArgumentException if the rows or columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *  arguments are negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public JTextArea(String text, int rows, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this(null, text, rows, columns);
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
     * Constructs a new JTextArea with the given document model, and defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * for all of the other arguments (null, 0, 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param doc  the model to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public JTextArea(Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this(doc, null, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Constructs a new JTextArea with the specified number of rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * and columns, and the given model.  All of the constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * feed through this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param doc the model to use, or create a default one if null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param text the text to be displayed, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param rows the number of rows >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param columns the number of columns >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @exception IllegalArgumentException if the rows or columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *  arguments are negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public JTextArea(Document doc, String text, int rows, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this.rows = rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        this.columns = columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (doc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            doc = createDefaultModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        setDocument(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            select(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (rows < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new IllegalArgumentException("rows: " + rows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (columns < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new IllegalArgumentException("columns: " + columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        LookAndFeel.installProperty(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                    "focusTraversalKeysForward",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                    JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                    getManagingFocusForwardTraversalKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        LookAndFeel.installProperty(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                    "focusTraversalKeysBackward",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                    JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                    getManagingFocusBackwardTraversalKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Returns the class ID for the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return the ID ("TextAreaUI")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Creates the default implementation of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * to be used at construction if one isn't explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * given.  A new instance of PlainDocument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return the default document model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    protected Document createDefaultModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return new PlainDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Sets the number of characters to expand tabs to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * This will be multiplied by the maximum advance for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * variable width fonts.  A PropertyChange event ("tabSize") is fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * when the tab size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param size number of characters to expand to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see #getTabSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *   preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * description: the number of characters to expand tabs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public void setTabSize(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            int old = getTabSize();
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   270
            doc.putProperty(PlainDocument.tabSizeAttribute, Integer.valueOf(size));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            firePropertyChange("tabSize", old, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Gets the number of characters used to expand tabs.  If the document is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * null or doesn't have a tab setting, return a default of 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return the number of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public int getTabSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        int size = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Integer i = (Integer) doc.getProperty(PlainDocument.tabSizeAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (i != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                size = i.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Sets the line-wrapping policy of the text area.  If set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * to true the lines will be wrapped if they are too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * to fit within the allocated width.  If set to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * the lines will always be unwrapped.  A <code>PropertyChange</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * event ("lineWrap") is fired when the policy is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * By default this property is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param wrap indicates if lines should be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see #getLineWrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *   preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * description: should lines be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void setLineWrap(boolean wrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        boolean old = this.wrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        this.wrap = wrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        firePropertyChange("lineWrap", old, wrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Gets the line-wrapping policy of the text area.  If set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * to true the lines will be wrapped if they are too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * to fit within the allocated width.  If set to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * the lines will always be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return if lines will be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public boolean getLineWrap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return wrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Sets the style of wrapping used if the text area is wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * lines.  If set to true the lines will be wrapped at word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * boundaries (whitespace) if they are too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * to fit within the allocated width.  If set to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * the lines will be wrapped at character boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * By default this property is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param word indicates if word boundaries should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *   for line wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see #getWrapStyleWord
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *   preferred: false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * description: should wrapping occur at word boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public void setWrapStyleWord(boolean word) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        boolean old = this.word;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        this.word = word;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        firePropertyChange("wrapStyleWord", old, word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Gets the style of wrapping used if the text area is wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * lines.  If set to true the lines will be wrapped at word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * boundaries (ie whitespace) if they are too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * to fit within the allocated width.  If set to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * the lines will be wrapped at character boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return if the wrap style should be word boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *  instead of character boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @see #setWrapStyleWord
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public boolean getWrapStyleWord() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return word;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Translates an offset into the components text to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param offset the offset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @return the line number >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @exception BadLocationException thrown if the offset is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *   less than zero or greater than the document length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public int getLineOfOffset(int offset) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (offset < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            throw new BadLocationException("Can't translate offset to line", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else if (offset > doc.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw new BadLocationException("Can't translate offset to line", doc.getLength()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            Element map = getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return map.getElementIndex(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Determines the number of lines contained in the area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @return the number of lines > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public int getLineCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        Element map = getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return map.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Determines the offset of the start of the given line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param line  the line number to translate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @return the offset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @exception BadLocationException thrown if the line is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * less than zero or greater or equal to the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * lines contained in the document (as reported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * getLineCount).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public int getLineStartOffset(int line) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        int lineCount = getLineCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (line < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            throw new BadLocationException("Negative line", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        } else if (line >= lineCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new BadLocationException("No such line", getDocument().getLength()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            Element map = getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            Element lineElem = map.getElement(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return lineElem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Determines the offset of the end of the given line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param line  the line >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @return the offset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @exception BadLocationException Thrown if the line is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * less than zero or greater or equal to the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * lines contained in the document (as reported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * getLineCount).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public int getLineEndOffset(int line) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        int lineCount = getLineCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (line < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throw new BadLocationException("Negative line", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } else if (line >= lineCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            throw new BadLocationException("No such line", getDocument().getLength()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            Element map = getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            Element lineElem = map.getElement(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            int endOffset = lineElem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            // hide the implicit break at the end of the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return ((line == lineCount - 1) ? (endOffset - 1) : endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    // --- java.awt.TextArea methods ---------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Inserts the specified text at the specified position.  Does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * if the model is null or if the text is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param str the text to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @param pos the position at which to insert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @exception IllegalArgumentException  if pos is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *  invalid position in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see TextComponent#setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see #replaceRange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public void insert(String str, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                doc.insertString(pos, str, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Appends the given text to the end of the document.  Does nothing if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * the model is null or the string is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param str the text to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @see #insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public void append(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                doc.insertString(doc.getLength(), str, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Replaces text from the indicated start to end position with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * new text specified.  Does nothing if the model is null.  Simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * does a delete if the new string is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param str the text to use as the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param start the start position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param end the end position >= start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @exception IllegalArgumentException  if part of the range is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *  invalid position in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @see #insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @see #replaceRange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public void replaceRange(String str, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (end < start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            throw new IllegalArgumentException("end before start");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    ((AbstractDocument)doc).replace(start, end - start, str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                                    null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    doc.remove(start, end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    doc.insertString(start, str, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Returns the number of rows in the TextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @return the number of rows >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public int getRows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Sets the number of rows for this TextArea.  Calls invalidate() after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * setting the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @param rows the number of rows >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @exception IllegalArgumentException if rows is less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @see #getRows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * description: the number of rows preferred for display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    public void setRows(int rows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        int oldVal = this.rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (rows < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            throw new IllegalArgumentException("rows less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (rows != oldVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            this.rows = rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Defines the meaning of the height of a row.  This defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * the height of the font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return the height >= 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    protected int getRowHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (rowHeight == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            FontMetrics metrics = getFontMetrics(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            rowHeight = metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        return rowHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * Returns the number of columns in the TextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @return number of columns >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public int getColumns() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Sets the number of columns for this TextArea.  Does an invalidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * after setting the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param columns the number of columns >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @exception IllegalArgumentException if columns is less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @see #getColumns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * description: the number of columns preferred for display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    public void setColumns(int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        int oldVal = this.columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (columns < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            throw new IllegalArgumentException("columns less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (columns != oldVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            this.columns = columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Gets column width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * The meaning of what a column is can be considered a fairly weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * notion for some fonts.  This method is used to define the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * of a column.  By default this is defined to be the width of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * character <em>m</em> for the font used.  This method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * redefined to be some alternative amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @return the column width >= 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    protected int getColumnWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (columnWidth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            FontMetrics metrics = getFontMetrics(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            columnWidth = metrics.charWidth('m');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    // --- Component methods -----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Returns the preferred size of the TextArea.  This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * maximum of the size needed to display the text and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * size requested for the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @return the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        Dimension d = super.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        d = (d == null) ? new Dimension(400,400) : d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (columns != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            d.width = Math.max(d.width, columns * getColumnWidth() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        if (rows != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            d.height = Math.max(d.height, rows * getRowHeight() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Sets the current font.  This removes cached row height and column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * width so the new font will be reflected, and calls revalidate().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @param f the font to use as the current font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public void setFont(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        super.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        rowHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        columnWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Returns a string representation of this JTextArea. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @return  a string representation of this JTextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        String wrapString = (wrap ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                             "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        String wordString = (word ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                             "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        ",colums=" + columns +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        ",columWidth=" + columnWidth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        ",rows=" + rows +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        ",rowHeight=" + rowHeight +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        ",word=" + wordString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        ",wrap=" + wrapString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    // --- Scrollable methods ----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Returns true if a viewport should always force the width of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Scrollable to match the width of the viewport.  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * to return true if the line wrapping policy is true, and false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * if lines are not being wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @return true if a viewport should force the Scrollables width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * to match its own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public boolean getScrollableTracksViewportWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return (wrap) ? true : super.getScrollableTracksViewportWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Returns the preferred size of the viewport if this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * is embedded in a JScrollPane.  This uses the desired column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * and row settings if they have been set, otherwise the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * behavior is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @return The preferredSize of a JViewport whose view is this Scrollable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @see JViewport#getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    public Dimension getPreferredScrollableViewportSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        Dimension size = super.getPreferredScrollableViewportSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        size = (size == null) ? new Dimension(400,400) : size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        size.width = (columns == 0) ? size.width :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                columns * getColumnWidth() + insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        size.height = (rows == 0) ? size.height :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                rows * getRowHeight() + insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Components that display logical rows or columns should compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * the scroll increment that will completely expose one new row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * or column, depending on the value of orientation.  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * to use the values returned by the <code>getRowHeight</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * <code>getColumnWidth</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Scrolling containers, like JScrollPane, will use this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * each time the user requests a unit scroll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @param visibleRect the view area visible within the viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @param orientation Either SwingConstants.VERTICAL or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *   SwingConstants.HORIZONTAL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @param direction Less than zero to scroll up/left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *   greater than zero for down/right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @return The "unit" increment for scrolling in the specified direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @exception IllegalArgumentException for an invalid orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @see JScrollBar#setUnitIncrement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @see #getRowHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @see #getColumnWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        switch (orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        case SwingConstants.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        case SwingConstants.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            return getColumnWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            throw new IllegalArgumentException("Invalid orientation: " + orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * See readObject() and writeObject() in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Gets the AccessibleContext associated with this JTextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * For JTextAreas, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * AccessibleJTextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * A new AccessibleJTextArea instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return an AccessibleJTextArea that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *         AccessibleContext of this JTextArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            accessibleContext = new AccessibleJTextArea();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return accessibleContext;
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
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <code>JTextArea</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Java Accessibility API appropriate to text area user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    protected class AccessibleJTextArea extends AccessibleJTextComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         * Gets the state set of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * @return an instance of AccessibleStateSet describing the states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         * @see AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            states.add(AccessibleState.MULTI_LINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    // --- variables -------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    private int rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    private int columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    private int columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    private int rowHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    private boolean wrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    private boolean word;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
}