jdk/src/share/classes/javax/swing/JTextArea.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 22574 7f8ce0c8c20a
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
     2
 * Copyright (c) 1997, 2013, 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
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20157
diff changeset
    47
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/text.html">Using Text Components</a>,
2
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>
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 20458
diff changeset
    96
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <dl>
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
    98
 * <dt><b>Newlines</b>
2
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
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   115
 * of all JavaBeans&trade;
2
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
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22260
diff changeset
   127
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
public class JTextArea extends JTextComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final String uiClassID = "TextAreaUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Constructs a new TextArea.  A default model is set, the initial string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * is null, and rows/columns are set to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public JTextArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this(null, null, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Constructs a new TextArea with the specified text displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * A default model is created and rows/columns are set to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param text the text to be displayed, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public JTextArea(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this(null, text, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Constructs a new empty TextArea with the specified number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * rows and columns.  A default model is created, and the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * string is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   159
     * @param rows the number of rows &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   160
     * @param columns the number of columns &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @exception IllegalArgumentException if the rows or columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *  arguments are negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public JTextArea(int rows, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this(null, null, rows, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Constructs a new TextArea with the specified text and number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * of rows and columns.  A default model is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param text the text to be displayed, or null
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   173
     * @param rows the number of rows &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   174
     * @param columns the number of columns &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @exception IllegalArgumentException if the rows or columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *  arguments are negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public JTextArea(String text, int rows, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        this(null, text, rows, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Constructs a new JTextArea with the given document model, and defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * for all of the other arguments (null, 0, 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param doc  the model to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public JTextArea(Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this(doc, null, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Constructs a new JTextArea with the specified number of rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * and columns, and the given model.  All of the constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * feed through this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param doc the model to use, or create a default one if null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param text the text to be displayed, null if none
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   199
     * @param rows the number of rows &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   200
     * @param columns the number of columns &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @exception IllegalArgumentException if the rows or columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *  arguments are negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public JTextArea(Document doc, String text, int rows, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        this.rows = rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        this.columns = columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (doc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            doc = createDefaultModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        setDocument(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            select(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (rows < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new IllegalArgumentException("rows: " + rows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (columns < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new IllegalArgumentException("columns: " + columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        LookAndFeel.installProperty(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                    "focusTraversalKeysForward",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                    JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                    getManagingFocusForwardTraversalKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        LookAndFeel.installProperty(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                    "focusTraversalKeysBackward",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                    JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                    getManagingFocusBackwardTraversalKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Returns the class ID for the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return the ID ("TextAreaUI")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Creates the default implementation of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * to be used at construction if one isn't explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * given.  A new instance of PlainDocument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return the default document model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    protected Document createDefaultModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return new PlainDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Sets the number of characters to expand tabs to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * This will be multiplied by the maximum advance for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * variable width fonts.  A PropertyChange event ("tabSize") is fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * when the tab size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param size number of characters to expand to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @see #getTabSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *   preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * description: the number of characters to expand tabs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public void setTabSize(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            int old = getTabSize();
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   271
            doc.putProperty(PlainDocument.tabSizeAttribute, Integer.valueOf(size));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            firePropertyChange("tabSize", old, size);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Gets the number of characters used to expand tabs.  If the document is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * null or doesn't have a tab setting, return a default of 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return the number of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public int getTabSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int size = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            Integer i = (Integer) doc.getProperty(PlainDocument.tabSizeAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (i != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                size = i.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Sets the line-wrapping policy of the text area.  If set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * to true the lines will be wrapped if they are too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * to fit within the allocated width.  If set to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * the lines will always be unwrapped.  A <code>PropertyChange</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * event ("lineWrap") is fired when the policy is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * By default this property is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param wrap indicates if lines should be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see #getLineWrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *   preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * description: should lines be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void setLineWrap(boolean wrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        boolean old = this.wrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        this.wrap = wrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        firePropertyChange("lineWrap", old, wrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Gets the line-wrapping policy of the text area.  If set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * to true the lines will be wrapped if they are too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * to fit within the allocated width.  If set to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * the lines will always be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return if lines will be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public boolean getLineWrap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return wrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Sets the style of wrapping used if the text area is wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * lines.  If set to true the lines will be wrapped at word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * boundaries (whitespace) if they are too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * to fit within the allocated width.  If set to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * the lines will be wrapped at character boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * By default this property is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param word indicates if word boundaries should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *   for line wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @see #getWrapStyleWord
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *   preferred: false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * description: should wrapping occur at word boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public void setWrapStyleWord(boolean word) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        boolean old = this.word;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        this.word = word;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        firePropertyChange("wrapStyleWord", old, word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Gets the style of wrapping used if the text area is wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * lines.  If set to true the lines will be wrapped at word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * boundaries (ie whitespace) if they are too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * to fit within the allocated width.  If set to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * the lines will be wrapped at character boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return if the wrap style should be word boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *  instead of character boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @see #setWrapStyleWord
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public boolean getWrapStyleWord() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return word;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Translates an offset into the components text to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   368
     * @param offset the offset &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   369
     * @return the line number &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @exception BadLocationException thrown if the offset is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *   less than zero or greater than the document length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public int getLineOfOffset(int offset) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (offset < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            throw new BadLocationException("Can't translate offset to line", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } else if (offset > doc.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw new BadLocationException("Can't translate offset to line", doc.getLength()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            Element map = getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return map.getElementIndex(offset);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Determines the number of lines contained in the area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   388
     * @return the number of lines &gt; 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public int getLineCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        Element map = getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return map.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Determines the offset of the start of the given line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   398
     * @param line  the line number to translate &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   399
     * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @exception BadLocationException thrown if the line is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * less than zero or greater or equal to the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * lines contained in the document (as reported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * getLineCount).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public int getLineStartOffset(int line) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        int lineCount = getLineCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (line < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new BadLocationException("Negative line", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        } else if (line >= lineCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throw new BadLocationException("No such line", getDocument().getLength()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            Element map = getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            Element lineElem = map.getElement(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return lineElem.getStartOffset();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Determines the offset of the end of the given line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   421
     * @param line  the line &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   422
     * @return the offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @exception BadLocationException Thrown if the line is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * less than zero or greater or equal to the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * lines contained in the document (as reported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * getLineCount).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public int getLineEndOffset(int line) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        int lineCount = getLineCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (line < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new BadLocationException("Negative line", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        } else if (line >= lineCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new BadLocationException("No such line", getDocument().getLength()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            Element map = getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            Element lineElem = map.getElement(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            int endOffset = lineElem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            // hide the implicit break at the end of the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return ((line == lineCount - 1) ? (endOffset - 1) : endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    // --- java.awt.TextArea methods ---------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Inserts the specified text at the specified position.  Does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * if the model is null or if the text is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @param str the text to insert
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   450
     * @param pos the position at which to insert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @exception IllegalArgumentException  if pos is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *  invalid position in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see TextComponent#setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @see #replaceRange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public void insert(String str, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                doc.insertString(pos, str, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                throw new IllegalArgumentException(e.getMessage());
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Appends the given text to the end of the document.  Does nothing if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * the model is null or the string is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param str the text to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @see #insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public void append(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                doc.insertString(doc.getLength(), str, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            } catch (BadLocationException e) {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Replaces text from the indicated start to end position with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * new text specified.  Does nothing if the model is null.  Simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * does a delete if the new string is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param str the text to use as the replacement
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   490
     * @param start the start position &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   491
     * @param end the end position &gt;= start
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @exception IllegalArgumentException  if part of the range is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *  invalid position in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @see #insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @see #replaceRange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public void replaceRange(String str, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (end < start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            throw new IllegalArgumentException("end before start");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    ((AbstractDocument)doc).replace(start, end - start, str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                                    null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    doc.remove(start, end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    doc.insertString(start, str, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                throw new IllegalArgumentException(e.getMessage());
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Returns the number of rows in the TextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   521
     * @return the number of rows &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public int getRows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Sets the number of rows for this TextArea.  Calls invalidate() after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * setting the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   531
     * @param rows the number of rows &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @exception IllegalArgumentException if rows is less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @see #getRows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * description: the number of rows preferred for display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public void setRows(int rows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        int oldVal = this.rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (rows < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            throw new IllegalArgumentException("rows less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (rows != oldVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            this.rows = rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            invalidate();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Defines the meaning of the height of a row.  This defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * the height of the font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   552
     * @return the height &gt;= 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    protected int getRowHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (rowHeight == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            FontMetrics metrics = getFontMetrics(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            rowHeight = metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return rowHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Returns the number of columns in the TextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   565
     * @return number of columns &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public int getColumns() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Sets the number of columns for this TextArea.  Does an invalidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * after setting the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   575
     * @param columns the number of columns &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @exception IllegalArgumentException if columns is less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @see #getColumns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * description: the number of columns preferred for display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    public void setColumns(int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        int oldVal = this.columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (columns < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            throw new IllegalArgumentException("columns less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (columns != oldVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            this.columns = columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            invalidate();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Gets column width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * The meaning of what a column is can be considered a fairly weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * notion for some fonts.  This method is used to define the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * of a column.  By default this is defined to be the width of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * character <em>m</em> for the font used.  This method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * redefined to be some alternative amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   600
     * @return the column width &gt;= 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    protected int getColumnWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (columnWidth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            FontMetrics metrics = getFontMetrics(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            columnWidth = metrics.charWidth('m');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    // --- Component methods -----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Returns the preferred size of the TextArea.  This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * maximum of the size needed to display the text and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * size requested for the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @return the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        Dimension d = super.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        d = (d == null) ? new Dimension(400,400) : d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (columns != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            d.width = Math.max(d.width, columns * getColumnWidth() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (rows != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            d.height = Math.max(d.height, rows * getRowHeight() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Sets the current font.  This removes cached row height and column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * width so the new font will be reflected, and calls revalidate().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param f the font to use as the current font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public void setFont(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        super.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        rowHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        columnWidth = 0;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Returns a string representation of this JTextArea. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return  a string representation of this JTextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        String wrapString = (wrap ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                             "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        String wordString = (word ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                             "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        ",colums=" + columns +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        ",columWidth=" + columnWidth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        ",rows=" + rows +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        ",rowHeight=" + rowHeight +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        ",word=" + wordString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        ",wrap=" + wrapString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    // --- Scrollable methods ----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Returns true if a viewport should always force the width of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Scrollable to match the width of the viewport.  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * to return true if the line wrapping policy is true, and false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * if lines are not being wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @return true if a viewport should force the Scrollables width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * to match its own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public boolean getScrollableTracksViewportWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return (wrap) ? true : super.getScrollableTracksViewportWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Returns the preferred size of the viewport if this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * is embedded in a JScrollPane.  This uses the desired column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * and row settings if they have been set, otherwise the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * behavior is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @return The preferredSize of a JViewport whose view is this Scrollable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @see JViewport#getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public Dimension getPreferredScrollableViewportSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        Dimension size = super.getPreferredScrollableViewportSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        size = (size == null) ? new Dimension(400,400) : size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        size.width = (columns == 0) ? size.width :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                columns * getColumnWidth() + insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        size.height = (rows == 0) ? size.height :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                rows * getRowHeight() + insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Components that display logical rows or columns should compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * the scroll increment that will completely expose one new row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * or column, depending on the value of orientation.  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * to use the values returned by the <code>getRowHeight</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <code>getColumnWidth</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Scrolling containers, like JScrollPane, will use this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * each time the user requests a unit scroll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @param visibleRect the view area visible within the viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @param orientation Either SwingConstants.VERTICAL or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *   SwingConstants.HORIZONTAL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @param direction Less than zero to scroll up/left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *   greater than zero for down/right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @return The "unit" increment for scrolling in the specified direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @exception IllegalArgumentException for an invalid orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @see JScrollBar#setUnitIncrement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @see #getRowHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @see #getColumnWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        switch (orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        case SwingConstants.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            return getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        case SwingConstants.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            return getColumnWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            throw new IllegalArgumentException("Invalid orientation: " + orientation);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * See readObject() and writeObject() in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                ui.installUI(this);
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
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
// Accessibility support
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Gets the AccessibleContext associated with this JTextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * For JTextAreas, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * AccessibleJTextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * A new AccessibleJTextArea instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @return an AccessibleJTextArea that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *         AccessibleContext of this JTextArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            accessibleContext = new AccessibleJTextArea();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * <code>JTextArea</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Java Accessibility API appropriate to text area user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * 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
   787
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22260
diff changeset
   791
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    protected class AccessibleJTextArea extends AccessibleJTextComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * Gets the state set of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         * @return an instance of AccessibleStateSet describing the states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         * of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         * @see AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            states.add(AccessibleState.MULTI_LINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    // --- variables -------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    private int rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    private int columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    private int columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    private int rowHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    private boolean wrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    private boolean word;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
}