4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A
4632N/Apackage sun.lwawt;
4632N/A
4632N/Aimport java.awt.Component;
5137N/Aimport java.awt.Cursor;
4632N/Aimport java.awt.Dimension;
4632N/Aimport java.awt.Point;
4632N/Aimport java.awt.TextArea;
4632N/Aimport java.awt.event.TextEvent;
4632N/Aimport java.awt.peer.TextAreaPeer;
4632N/A
4632N/Aimport javax.swing.JScrollBar;
4632N/Aimport javax.swing.JScrollPane;
4632N/Aimport javax.swing.JTextArea;
4632N/Aimport javax.swing.ScrollPaneConstants;
4632N/Aimport javax.swing.text.Document;
4632N/Aimport javax.swing.text.JTextComponent;
4632N/A
4632N/Afinal class LWTextAreaPeer
4632N/A extends LWTextComponentPeer<TextArea, LWTextAreaPeer.ScrollableJTextArea>
4632N/A implements TextAreaPeer {
4632N/A
4632N/A private static final int DEFAULT_COLUMNS = 60;
4632N/A private static final int DEFAULT_ROWS = 10;
4632N/A
4632N/A LWTextAreaPeer(final TextArea target,
4632N/A final PlatformComponent platformComponent) {
4632N/A super(target, platformComponent);
4632N/A }
4632N/A
4632N/A @Override
4632N/A protected ScrollableJTextArea createDelegate() {
4632N/A return new ScrollableJTextArea();
4632N/A }
4632N/A
4632N/A @Override
5166N/A void initializeImpl() {
5166N/A super.initializeImpl();
4632N/A final int visibility = getTarget().getScrollbarVisibility();
4632N/A synchronized (getDelegateLock()) {
4632N/A setScrollBarVisibility(visibility);
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A JTextComponent getTextComponent() {
4632N/A return getDelegate().getView();
4632N/A }
4632N/A
4632N/A @Override
5137N/A protected Cursor getCursor(final Point p) {
5137N/A final boolean isContains;
5137N/A synchronized (getDelegateLock()) {
5137N/A isContains = getDelegate().getViewport().getBounds().contains(p);
5137N/A }
5137N/A return isContains ? super.getCursor(p) : null;
5137N/A }
5137N/A
5137N/A @Override
4632N/A protected Component getDelegateFocusOwner() {
4632N/A return getTextComponent();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public Dimension getMinimumSize() {
4632N/A return getMinimumSize(DEFAULT_ROWS, DEFAULT_COLUMNS);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public Dimension getMinimumSize(final int rows, final int columns) {
4632N/A return getPreferredSize(rows, columns);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public Dimension getPreferredSize(final int rows, final int columns) {
4632N/A final Dimension size = super.getPreferredSize(rows, columns);
4632N/A synchronized (getDelegateLock()) {
4632N/A final JScrollBar vbar = getDelegate().getVerticalScrollBar();
4632N/A final JScrollBar hbar = getDelegate().getHorizontalScrollBar();
4632N/A final int scrollbarW = vbar != null ? vbar.getWidth() : 0;
4632N/A final int scrollbarH = hbar != null ? hbar.getHeight() : 0;
4632N/A return new Dimension(size.width + scrollbarW,
4632N/A size.height + scrollbarH);
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void insert(final String text, final int pos) {
4639N/A final ScrollableJTextArea pane = getDelegate();
4632N/A synchronized (getDelegateLock()) {
4639N/A final JTextArea area = pane.getView();
4632N/A final boolean doScroll = pos >= area.getDocument().getLength()
4632N/A && area.getDocument().getLength() != 0;
4632N/A area.insert(text, pos);
4694N/A revalidate();
4632N/A if (doScroll) {
4639N/A final JScrollBar vbar = pane.getVerticalScrollBar();
4639N/A if (vbar != null) {
4639N/A vbar.setValue(vbar.getMaximum() - vbar.getVisibleAmount());
4632N/A }
4632N/A }
4632N/A }
4632N/A repaintPeer();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void replaceRange(final String text, final int start,
4632N/A final int end) {
4632N/A synchronized (getDelegateLock()) {
4632N/A // JTextArea.replaceRange() posts two different events.
4632N/A // Since we make no differences between text events,
4632N/A // the document listener has to be disabled while
4632N/A // JTextArea.replaceRange() is called.
4632N/A final Document document = getTextComponent().getDocument();
4632N/A document.removeDocumentListener(this);
4632N/A getDelegate().getView().replaceRange(text, start, end);
4694N/A revalidate();
4632N/A postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));
4632N/A document.addDocumentListener(this);
4632N/A }
4632N/A repaintPeer();
4632N/A }
4632N/A
4632N/A private void setScrollBarVisibility(final int visibility) {
4632N/A final ScrollableJTextArea pane = getDelegate();
4632N/A final JTextArea view = pane.getView();
4632N/A view.setLineWrap(false);
4632N/A
4632N/A switch (visibility) {
4632N/A case TextArea.SCROLLBARS_NONE:
4632N/A pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
4632N/A pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
4632N/A view.setLineWrap(true);
4632N/A break;
4632N/A case TextArea.SCROLLBARS_VERTICAL_ONLY:
4632N/A pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
4632N/A pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
4632N/A view.setLineWrap(true);
4632N/A break;
4632N/A case TextArea.SCROLLBARS_HORIZONTAL_ONLY:
4632N/A pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
4632N/A pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
4632N/A break;
4632N/A default:
4632N/A pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
4632N/A pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
4632N/A break;
4632N/A }
4632N/A }
4632N/A
4632N/A @SuppressWarnings("serial")
4632N/A final class ScrollableJTextArea extends JScrollPane {
4632N/A
4632N/A ScrollableJTextArea() {
4632N/A super();
4632N/A getViewport().setView(new JTextAreaDelegate());
4632N/A }
4632N/A
4632N/A public JTextArea getView() {
4632N/A return (JTextArea) getViewport().getView();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setEnabled(final boolean enabled) {
4632N/A getViewport().getView().setEnabled(enabled);
4632N/A super.setEnabled(enabled);
4632N/A }
4632N/A
4632N/A @SuppressWarnings("serial")
4632N/A private final class JTextAreaDelegate extends JTextArea {
4632N/A
4632N/A // Empty non private constructor was added because access to this
4632N/A // class shouldn't be emulated by a synthetic accessor method.
4632N/A JTextAreaDelegate() {
4632N/A super();
4632N/A }
4632N/A
4632N/A @Override
4990N/A public void replaceSelection(String content) {
4990N/A getDocument().removeDocumentListener(LWTextAreaPeer.this);
4990N/A super.replaceSelection(content);
4990N/A // post only one text event in this case
4990N/A postTextEvent();
4990N/A getDocument().addDocumentListener(LWTextAreaPeer.this);
4990N/A }
4990N/A
4990N/A @Override
4632N/A public boolean hasFocus() {
4632N/A return getTarget().hasFocus();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public Point getLocationOnScreen() {
4632N/A return LWTextAreaPeer.this.getLocationOnScreen();
4632N/A }
4632N/A }
4632N/A }
4632N/A}