0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport javax.swing.text.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.accessibility.*;
0N/A
0N/Aimport java.util.Collections;
0N/Aimport java.util.Set;
0N/Aimport java.util.StringTokenizer;
0N/A
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * A <code>JTextArea</code> is a multi-line area that displays plain text.
0N/A * It is intended to be a lightweight component that provides source
0N/A * compatibility with the <code>java.awt.TextArea</code> class where it can
0N/A * reasonably do so.
0N/A * You can find information and examples of using all the text components in
0N/A * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/text.html">Using Text Components</a>,
0N/A * a section in <em>The Java Tutorial.</em>
0N/A *
0N/A * <p>
0N/A * This component has capabilities not found in the
0N/A * <code>java.awt.TextArea</code> class. The superclass should be
0N/A * consulted for additional capabilities.
0N/A * Alternative multi-line text classes with
0N/A * more capabilities are <code>JTextPane</code> and <code>JEditorPane</code>.
0N/A * <p>
0N/A * The <code>java.awt.TextArea</code> internally handles scrolling.
0N/A * <code>JTextArea</code> is different in that it doesn't manage scrolling,
0N/A * but implements the swing <code>Scrollable</code> interface. This allows it
0N/A * to be placed inside a <code>JScrollPane</code> if scrolling
0N/A * behavior is desired, and used directly if scrolling is not desired.
0N/A * <p>
0N/A * The <code>java.awt.TextArea</code> has the ability to do line wrapping.
0N/A * This was controlled by the horizontal scrolling policy. Since
0N/A * scrolling is not done by <code>JTextArea</code> directly, backward
0N/A * compatibility must be provided another way. <code>JTextArea</code> has
0N/A * a bound property for line wrapping that controls whether or
0N/A * not it will wrap lines. By default, the line wrapping property
0N/A * is set to false (not wrapped).
0N/A * <p>
0N/A * <code>java.awt.TextArea</code> has two properties <code>rows</code>
0N/A * and <code>columns</code> that are used to determine the preferred size.
0N/A * <code>JTextArea</code> uses these properties to indicate the
0N/A * preferred size of the viewport when placed inside a <code>JScrollPane</code>
0N/A * to match the functionality provided by <code>java.awt.TextArea</code>.
0N/A * <code>JTextArea</code> has a preferred size of what is needed to
0N/A * display all of the text, so that it functions properly inside of
0N/A * a <code>JScrollPane</code>. If the value for <code>rows</code>
0N/A * or <code>columns</code> is equal to zero,
0N/A * the preferred size along that axis is used for
0N/A * the viewport preferred size along the same axis.
0N/A * <p>
0N/A * The <code>java.awt.TextArea</code> could be monitored for changes by adding
0N/A * a <code>TextListener</code> for <code>TextEvent</code>s.
0N/A * In the <code>JTextComponent</code> based
0N/A * components, changes are broadcasted from the model via a
0N/A * <code>DocumentEvent</code> to <code>DocumentListeners</code>.
0N/A * The <code>DocumentEvent</code> gives
0N/A * the location of the change and the kind of change if desired.
0N/A * The code fragment might look something like:
0N/A * <pre>
0N/A * DocumentListener myListener = ??;
0N/A * JTextArea myArea = ??;
0N/A * myArea.getDocument().addDocumentListener(myListener);
0N/A * </pre>
0N/A * <p>
0N/A * <dl>
0N/A * <dt><b><font size=+1>Newlines</font></b>
0N/A * <dd>
0N/A * For a discussion on how newlines are handled, see
0N/A * <a href="text/DefaultEditorKit.html">DefaultEditorKit</a>.
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: A multi-line area that displays plain text.
0N/A *
0N/A * @author Timothy Prinzing
0N/A * @see JTextPane
0N/A * @see JEditorPane
0N/A */
0N/Apublic class JTextArea extends JTextComponent {
0N/A
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "TextAreaUI";
0N/A
0N/A /**
0N/A * Constructs a new TextArea. A default model is set, the initial string
0N/A * is null, and rows/columns are set to 0.
0N/A */
0N/A public JTextArea() {
0N/A this(null, null, 0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new TextArea with the specified text displayed.
0N/A * A default model is created and rows/columns are set to 0.
0N/A *
0N/A * @param text the text to be displayed, or null
0N/A */
0N/A public JTextArea(String text) {
0N/A this(null, text, 0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new empty TextArea with the specified number of
0N/A * rows and columns. A default model is created, and the initial
0N/A * string is null.
0N/A *
0N/A * @param rows the number of rows >= 0
0N/A * @param columns the number of columns >= 0
0N/A * @exception IllegalArgumentException if the rows or columns
0N/A * arguments are negative.
0N/A */
0N/A public JTextArea(int rows, int columns) {
0N/A this(null, null, rows, columns);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new TextArea with the specified text and number
0N/A * of rows and columns. A default model is created.
0N/A *
0N/A * @param text the text to be displayed, or null
0N/A * @param rows the number of rows >= 0
0N/A * @param columns the number of columns >= 0
0N/A * @exception IllegalArgumentException if the rows or columns
0N/A * arguments are negative.
0N/A */
0N/A public JTextArea(String text, int rows, int columns) {
0N/A this(null, text, rows, columns);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new JTextArea with the given document model, and defaults
0N/A * for all of the other arguments (null, 0, 0).
0N/A *
0N/A * @param doc the model to use
0N/A */
0N/A public JTextArea(Document doc) {
0N/A this(doc, null, 0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new JTextArea with the specified number of rows
0N/A * and columns, and the given model. All of the constructors
0N/A * feed through this constructor.
0N/A *
0N/A * @param doc the model to use, or create a default one if null
0N/A * @param text the text to be displayed, null if none
0N/A * @param rows the number of rows >= 0
0N/A * @param columns the number of columns >= 0
0N/A * @exception IllegalArgumentException if the rows or columns
0N/A * arguments are negative.
0N/A */
0N/A public JTextArea(Document doc, String text, int rows, int columns) {
0N/A super();
0N/A this.rows = rows;
0N/A this.columns = columns;
0N/A if (doc == null) {
0N/A doc = createDefaultModel();
0N/A }
0N/A setDocument(doc);
0N/A if (text != null) {
0N/A setText(text);
0N/A select(0, 0);
0N/A }
0N/A if (rows < 0) {
0N/A throw new IllegalArgumentException("rows: " + rows);
0N/A }
0N/A if (columns < 0) {
0N/A throw new IllegalArgumentException("columns: " + columns);
0N/A }
0N/A LookAndFeel.installProperty(this,
0N/A "focusTraversalKeysForward",
0N/A JComponent.
0N/A getManagingFocusForwardTraversalKeys());
0N/A LookAndFeel.installProperty(this,
0N/A "focusTraversalKeysBackward",
0N/A JComponent.
0N/A getManagingFocusBackwardTraversalKeys());
0N/A }
0N/A
0N/A /**
0N/A * Returns the class ID for the UI.
0N/A *
0N/A * @return the ID ("TextAreaUI")
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A /**
0N/A * Creates the default implementation of the model
0N/A * to be used at construction if one isn't explicitly
0N/A * given. A new instance of PlainDocument is returned.
0N/A *
0N/A * @return the default document model
0N/A */
0N/A protected Document createDefaultModel() {
0N/A return new PlainDocument();
0N/A }
0N/A
0N/A /**
0N/A * Sets the number of characters to expand tabs to.
0N/A * This will be multiplied by the maximum advance for
0N/A * variable width fonts. A PropertyChange event ("tabSize") is fired
0N/A * when the tab size changes.
0N/A *
0N/A * @param size number of characters to expand to
0N/A * @see #getTabSize
0N/A * @beaninfo
0N/A * preferred: true
0N/A * bound: true
0N/A * description: the number of characters to expand tabs to
0N/A */
0N/A public void setTabSize(int size) {
0N/A Document doc = getDocument();
0N/A if (doc != null) {
0N/A int old = getTabSize();
215N/A doc.putProperty(PlainDocument.tabSizeAttribute, Integer.valueOf(size));
0N/A firePropertyChange("tabSize", old, size);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the number of characters used to expand tabs. If the document is
0N/A * null or doesn't have a tab setting, return a default of 8.
0N/A *
0N/A * @return the number of characters
0N/A */
0N/A public int getTabSize() {
0N/A int size = 8;
0N/A Document doc = getDocument();
0N/A if (doc != null) {
0N/A Integer i = (Integer) doc.getProperty(PlainDocument.tabSizeAttribute);
0N/A if (i != null) {
0N/A size = i.intValue();
0N/A }
0N/A }
0N/A return size;
0N/A }
0N/A
0N/A /**
0N/A * Sets the line-wrapping policy of the text area. If set
0N/A * to true the lines will be wrapped if they are too long
0N/A * to fit within the allocated width. If set to false,
0N/A * the lines will always be unwrapped. A <code>PropertyChange</code>
0N/A * event ("lineWrap") is fired when the policy is changed.
0N/A * By default this property is false.
0N/A *
0N/A * @param wrap indicates if lines should be wrapped
0N/A * @see #getLineWrap
0N/A * @beaninfo
0N/A * preferred: true
0N/A * bound: true
0N/A * description: should lines be wrapped
0N/A */
0N/A public void setLineWrap(boolean wrap) {
0N/A boolean old = this.wrap;
0N/A this.wrap = wrap;
0N/A firePropertyChange("lineWrap", old, wrap);
0N/A }
0N/A
0N/A /**
0N/A * Gets the line-wrapping policy of the text area. If set
0N/A * to true the lines will be wrapped if they are too long
0N/A * to fit within the allocated width. If set to false,
0N/A * the lines will always be unwrapped.
0N/A *
0N/A * @return if lines will be wrapped
0N/A */
0N/A public boolean getLineWrap() {
0N/A return wrap;
0N/A }
0N/A
0N/A /**
0N/A * Sets the style of wrapping used if the text area is wrapping
0N/A * lines. If set to true the lines will be wrapped at word
0N/A * boundaries (whitespace) if they are too long
0N/A * to fit within the allocated width. If set to false,
0N/A * the lines will be wrapped at character boundaries.
0N/A * By default this property is false.
0N/A *
0N/A * @param word indicates if word boundaries should be used
0N/A * for line wrapping
0N/A * @see #getWrapStyleWord
0N/A * @beaninfo
0N/A * preferred: false
0N/A * bound: true
0N/A * description: should wrapping occur at word boundaries
0N/A */
0N/A public void setWrapStyleWord(boolean word) {
0N/A boolean old = this.word;
0N/A this.word = word;
0N/A firePropertyChange("wrapStyleWord", old, word);
0N/A }
0N/A
0N/A /**
0N/A * Gets the style of wrapping used if the text area is wrapping
0N/A * lines. If set to true the lines will be wrapped at word
0N/A * boundaries (ie whitespace) if they are too long
0N/A * to fit within the allocated width. If set to false,
0N/A * the lines will be wrapped at character boundaries.
0N/A *
0N/A * @return if the wrap style should be word boundaries
0N/A * instead of character boundaries
0N/A * @see #setWrapStyleWord
0N/A */
0N/A public boolean getWrapStyleWord() {
0N/A return word;
0N/A }
0N/A
0N/A /**
0N/A * Translates an offset into the components text to a
0N/A * line number.
0N/A *
0N/A * @param offset the offset >= 0
0N/A * @return the line number >= 0
0N/A * @exception BadLocationException thrown if the offset is
0N/A * less than zero or greater than the document length.
0N/A */
0N/A public int getLineOfOffset(int offset) throws BadLocationException {
0N/A Document doc = getDocument();
0N/A if (offset < 0) {
0N/A throw new BadLocationException("Can't translate offset to line", -1);
0N/A } else if (offset > doc.getLength()) {
0N/A throw new BadLocationException("Can't translate offset to line", doc.getLength()+1);
0N/A } else {
0N/A Element map = getDocument().getDefaultRootElement();
0N/A return map.getElementIndex(offset);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Determines the number of lines contained in the area.
0N/A *
0N/A * @return the number of lines > 0
0N/A */
0N/A public int getLineCount() {
0N/A Element map = getDocument().getDefaultRootElement();
0N/A return map.getElementCount();
0N/A }
0N/A
0N/A /**
0N/A * Determines the offset of the start of the given line.
0N/A *
0N/A * @param line the line number to translate >= 0
0N/A * @return the offset >= 0
0N/A * @exception BadLocationException thrown if the line is
0N/A * less than zero or greater or equal to the number of
0N/A * lines contained in the document (as reported by
0N/A * getLineCount).
0N/A */
0N/A public int getLineStartOffset(int line) throws BadLocationException {
0N/A int lineCount = getLineCount();
0N/A if (line < 0) {
0N/A throw new BadLocationException("Negative line", -1);
0N/A } else if (line >= lineCount) {
0N/A throw new BadLocationException("No such line", getDocument().getLength()+1);
0N/A } else {
0N/A Element map = getDocument().getDefaultRootElement();
0N/A Element lineElem = map.getElement(line);
0N/A return lineElem.getStartOffset();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Determines the offset of the end of the given line.
0N/A *
0N/A * @param line the line >= 0
0N/A * @return the offset >= 0
0N/A * @exception BadLocationException Thrown if the line is
0N/A * less than zero or greater or equal to the number of
0N/A * lines contained in the document (as reported by
0N/A * getLineCount).
0N/A */
0N/A public int getLineEndOffset(int line) throws BadLocationException {
0N/A int lineCount = getLineCount();
0N/A if (line < 0) {
0N/A throw new BadLocationException("Negative line", -1);
0N/A } else if (line >= lineCount) {
0N/A throw new BadLocationException("No such line", getDocument().getLength()+1);
0N/A } else {
0N/A Element map = getDocument().getDefaultRootElement();
0N/A Element lineElem = map.getElement(line);
0N/A int endOffset = lineElem.getEndOffset();
0N/A // hide the implicit break at the end of the document
0N/A return ((line == lineCount - 1) ? (endOffset - 1) : endOffset);
0N/A }
0N/A }
0N/A
0N/A // --- java.awt.TextArea methods ---------------------------------
0N/A
0N/A /**
0N/A * Inserts the specified text at the specified position. Does nothing
0N/A * if the model is null or if the text is null or empty.
0N/A *
0N/A * @param str the text to insert
0N/A * @param pos the position at which to insert >= 0
0N/A * @exception IllegalArgumentException if pos is an
0N/A * invalid position in the model
0N/A * @see TextComponent#setText
0N/A * @see #replaceRange
0N/A */
0N/A public void insert(String str, int pos) {
0N/A Document doc = getDocument();
0N/A if (doc != null) {
0N/A try {
0N/A doc.insertString(pos, str, null);
0N/A } catch (BadLocationException e) {
0N/A throw new IllegalArgumentException(e.getMessage());
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Appends the given text to the end of the document. Does nothing if
0N/A * the model is null or the string is null or empty.
0N/A *
0N/A * @param str the text to insert
0N/A * @see #insert
0N/A */
0N/A public void append(String str) {
0N/A Document doc = getDocument();
0N/A if (doc != null) {
0N/A try {
0N/A doc.insertString(doc.getLength(), str, null);
0N/A } catch (BadLocationException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Replaces text from the indicated start to end position with the
0N/A * new text specified. Does nothing if the model is null. Simply
0N/A * does a delete if the new string is null or empty.
0N/A *
0N/A * @param str the text to use as the replacement
0N/A * @param start the start position >= 0
0N/A * @param end the end position >= start
0N/A * @exception IllegalArgumentException if part of the range is an
0N/A * invalid position in the model
0N/A * @see #insert
0N/A * @see #replaceRange
0N/A */
0N/A public void replaceRange(String str, int start, int end) {
0N/A if (end < start) {
0N/A throw new IllegalArgumentException("end before start");
0N/A }
0N/A Document doc = getDocument();
0N/A if (doc != null) {
0N/A try {
0N/A if (doc instanceof AbstractDocument) {
0N/A ((AbstractDocument)doc).replace(start, end - start, str,
0N/A null);
0N/A }
0N/A else {
0N/A doc.remove(start, end - start);
0N/A doc.insertString(start, str, null);
0N/A }
0N/A } catch (BadLocationException e) {
0N/A throw new IllegalArgumentException(e.getMessage());
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of rows in the TextArea.
0N/A *
0N/A * @return the number of rows >= 0
0N/A */
0N/A public int getRows() {
0N/A return rows;
0N/A }
0N/A
0N/A /**
0N/A * Sets the number of rows for this TextArea. Calls invalidate() after
0N/A * setting the new value.
0N/A *
0N/A * @param rows the number of rows >= 0
0N/A * @exception IllegalArgumentException if rows is less than 0
0N/A * @see #getRows
0N/A * @beaninfo
0N/A * description: the number of rows preferred for display
0N/A */
0N/A public void setRows(int rows) {
0N/A int oldVal = this.rows;
0N/A if (rows < 0) {
0N/A throw new IllegalArgumentException("rows less than zero.");
0N/A }
0N/A if (rows != oldVal) {
0N/A this.rows = rows;
0N/A invalidate();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Defines the meaning of the height of a row. This defaults to
0N/A * the height of the font.
0N/A *
0N/A * @return the height >= 1
0N/A */
0N/A protected int getRowHeight() {
0N/A if (rowHeight == 0) {
0N/A FontMetrics metrics = getFontMetrics(getFont());
0N/A rowHeight = metrics.getHeight();
0N/A }
0N/A return rowHeight;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of columns in the TextArea.
0N/A *
0N/A * @return number of columns >= 0
0N/A */
0N/A public int getColumns() {
0N/A return columns;
0N/A }
0N/A
0N/A /**
0N/A * Sets the number of columns for this TextArea. Does an invalidate()
0N/A * after setting the new value.
0N/A *
0N/A * @param columns the number of columns >= 0
0N/A * @exception IllegalArgumentException if columns is less than 0
0N/A * @see #getColumns
0N/A * @beaninfo
0N/A * description: the number of columns preferred for display
0N/A */
0N/A public void setColumns(int columns) {
0N/A int oldVal = this.columns;
0N/A if (columns < 0) {
0N/A throw new IllegalArgumentException("columns less than zero.");
0N/A }
0N/A if (columns != oldVal) {
0N/A this.columns = columns;
0N/A invalidate();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets column width.
0N/A * The meaning of what a column is can be considered a fairly weak
0N/A * notion for some fonts. This method is used to define the width
0N/A * of a column. By default this is defined to be the width of the
0N/A * character <em>m</em> for the font used. This method can be
0N/A * redefined to be some alternative amount.
0N/A *
0N/A * @return the column width >= 1
0N/A */
0N/A protected int getColumnWidth() {
0N/A if (columnWidth == 0) {
0N/A FontMetrics metrics = getFontMetrics(getFont());
0N/A columnWidth = metrics.charWidth('m');
0N/A }
0N/A return columnWidth;
0N/A }
0N/A
0N/A // --- Component methods -----------------------------------------
0N/A
0N/A /**
0N/A * Returns the preferred size of the TextArea. This is the
0N/A * maximum of the size needed to display the text and the
0N/A * size requested for the viewport.
0N/A *
0N/A * @return the size
0N/A */
0N/A public Dimension getPreferredSize() {
0N/A Dimension d = super.getPreferredSize();
0N/A d = (d == null) ? new Dimension(400,400) : d;
0N/A Insets insets = getInsets();
0N/A
0N/A if (columns != 0) {
0N/A d.width = Math.max(d.width, columns * getColumnWidth() +
0N/A insets.left + insets.right);
0N/A }
0N/A if (rows != 0) {
0N/A d.height = Math.max(d.height, rows * getRowHeight() +
0N/A insets.top + insets.bottom);
0N/A }
0N/A return d;
0N/A }
0N/A
0N/A /**
0N/A * Sets the current font. This removes cached row height and column
0N/A * width so the new font will be reflected, and calls revalidate().
0N/A *
0N/A * @param f the font to use as the current font
0N/A */
0N/A public void setFont(Font f) {
0N/A super.setFont(f);
0N/A rowHeight = 0;
0N/A columnWidth = 0;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this JTextArea. This method
0N/A * is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this JTextArea.
0N/A */
0N/A protected String paramString() {
0N/A String wrapString = (wrap ?
0N/A "true" : "false");
0N/A String wordString = (word ?
0N/A "true" : "false");
0N/A
0N/A return super.paramString() +
0N/A ",colums=" + columns +
0N/A ",columWidth=" + columnWidth +
0N/A ",rows=" + rows +
0N/A ",rowHeight=" + rowHeight +
0N/A ",word=" + wordString +
0N/A ",wrap=" + wrapString;
0N/A }
0N/A
0N/A // --- Scrollable methods ----------------------------------------
0N/A
0N/A /**
0N/A * Returns true if a viewport should always force the width of this
0N/A * Scrollable to match the width of the viewport. This is implemented
0N/A * to return true if the line wrapping policy is true, and false
0N/A * if lines are not being wrapped.
0N/A *
0N/A * @return true if a viewport should force the Scrollables width
0N/A * to match its own.
0N/A */
0N/A public boolean getScrollableTracksViewportWidth() {
0N/A return (wrap) ? true : super.getScrollableTracksViewportWidth();
0N/A }
0N/A
0N/A /**
0N/A * Returns the preferred size of the viewport if this component
0N/A * is embedded in a JScrollPane. This uses the desired column
0N/A * and row settings if they have been set, otherwise the superclass
0N/A * behavior is used.
0N/A *
0N/A * @return The preferredSize of a JViewport whose view is this Scrollable.
0N/A * @see JViewport#getPreferredSize
0N/A */
0N/A public Dimension getPreferredScrollableViewportSize() {
0N/A Dimension size = super.getPreferredScrollableViewportSize();
0N/A size = (size == null) ? new Dimension(400,400) : size;
0N/A Insets insets = getInsets();
0N/A
0N/A size.width = (columns == 0) ? size.width :
0N/A columns * getColumnWidth() + insets.left + insets.right;
0N/A size.height = (rows == 0) ? size.height :
0N/A rows * getRowHeight() + insets.top + insets.bottom;
0N/A return size;
0N/A }
0N/A
0N/A /**
0N/A * Components that display logical rows or columns should compute
0N/A * the scroll increment that will completely expose one new row
0N/A * or column, depending on the value of orientation. This is implemented
0N/A * to use the values returned by the <code>getRowHeight</code> and
0N/A * <code>getColumnWidth</code> methods.
0N/A * <p>
0N/A * Scrolling containers, like JScrollPane, will use this method
0N/A * each time the user requests a unit scroll.
0N/A *
0N/A * @param visibleRect the view area visible within the viewport
0N/A * @param orientation Either SwingConstants.VERTICAL or
0N/A * SwingConstants.HORIZONTAL.
0N/A * @param direction Less than zero to scroll up/left,
0N/A * greater than zero for down/right.
0N/A * @return The "unit" increment for scrolling in the specified direction
0N/A * @exception IllegalArgumentException for an invalid orientation
0N/A * @see JScrollBar#setUnitIncrement
0N/A * @see #getRowHeight
0N/A * @see #getColumnWidth
0N/A */
0N/A public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
0N/A switch (orientation) {
0N/A case SwingConstants.VERTICAL:
0N/A return getRowHeight();
0N/A case SwingConstants.HORIZONTAL:
0N/A return getColumnWidth();
0N/A default:
0N/A throw new IllegalArgumentException("Invalid orientation: " + orientation);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * See readObject() and writeObject() in JComponent for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JTextArea.
0N/A * For JTextAreas, the AccessibleContext takes the form of an
0N/A * AccessibleJTextArea.
0N/A * A new AccessibleJTextArea instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJTextArea that serves as the
0N/A * AccessibleContext of this JTextArea
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJTextArea();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JTextArea</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to text area user-interface
0N/A * elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJTextArea extends AccessibleJTextComponent {
0N/A
0N/A /**
0N/A * Gets the state set of this object.
0N/A *
0N/A * @return an instance of AccessibleStateSet describing the states
0N/A * of the object
0N/A * @see AccessibleStateSet
0N/A */
0N/A public AccessibleStateSet getAccessibleStateSet() {
0N/A AccessibleStateSet states = super.getAccessibleStateSet();
0N/A states.add(AccessibleState.MULTI_LINE);
0N/A return states;
0N/A }
0N/A }
0N/A
0N/A // --- variables -------------------------------------------------
0N/A
0N/A private int rows;
0N/A private int columns;
0N/A private int columnWidth;
0N/A private int rowHeight;
0N/A private boolean wrap;
0N/A private boolean word;
0N/A
0N/A}