Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/sun/lwawt/LWTextAreaPeer.java
38827 views
/*1* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.lwawt;2627import java.awt.Component;28import java.awt.Cursor;29import java.awt.Dimension;30import java.awt.Insets;31import java.awt.Point;32import java.awt.TextArea;33import java.awt.event.TextEvent;34import java.awt.peer.TextAreaPeer;3536import javax.swing.JScrollBar;37import javax.swing.JScrollPane;38import javax.swing.JTextArea;39import javax.swing.ScrollPaneConstants;40import javax.swing.text.Document;4142/**43* Lightweight implementation of {@link TextAreaPeer}. Delegates most of the44* work to the {@link JTextArea} inside {@link JScrollPane}.45*/46final class LWTextAreaPeer47extends LWTextComponentPeer<TextArea, LWTextAreaPeer.ScrollableJTextArea>48implements TextAreaPeer {4950/**51* The default number of visible columns.52*/53private static final int DEFAULT_COLUMNS = 60;5455/**56* The default number of visible rows.57*/58private static final int DEFAULT_ROWS = 10;5960LWTextAreaPeer(final TextArea target,61final PlatformComponent platformComponent) {62super(target, platformComponent);63}6465@Override66ScrollableJTextArea createDelegate() {67return new ScrollableJTextArea();68}6970@Override71void initializeImpl() {72super.initializeImpl();73final int visibility = getTarget().getScrollbarVisibility();74synchronized (getDelegateLock()) {75getTextComponent().setWrapStyleWord(true);76setScrollBarVisibility(visibility);77}78}7980@Override81JTextArea getTextComponent() {82return getDelegate().getView();83}8485@Override86Cursor getCursor(final Point p) {87final boolean isContains;88synchronized (getDelegateLock()) {89isContains = getDelegate().getViewport().getBounds().contains(p);90}91return isContains ? super.getCursor(p) : null;92}9394@Override95Component getDelegateFocusOwner() {96return getTextComponent();97}9899@Override100public Dimension getPreferredSize() {101return getMinimumSize();102}103104@Override105public Dimension getMinimumSize() {106return getMinimumSize(DEFAULT_ROWS, DEFAULT_COLUMNS);107}108109@Override110public Dimension getPreferredSize(final int rows, final int columns) {111return getMinimumSize(rows, columns);112}113114@Override115public Dimension getMinimumSize(final int rows, final int columns) {116final Dimension size = super.getMinimumSize(rows, columns);117synchronized (getDelegateLock()) {118// JScrollPane insets119final Insets pi = getDelegate().getInsets();120size.width += pi.left + pi.right;121size.height += pi.top + pi.bottom;122// Take scrollbars into account.123final int vsbPolicy = getDelegate().getVerticalScrollBarPolicy();124if (vsbPolicy == ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) {125final JScrollBar vbar = getDelegate().getVerticalScrollBar();126size.width += vbar != null ? vbar.getMinimumSize().width : 0;127}128final int hsbPolicy = getDelegate().getHorizontalScrollBarPolicy();129if (hsbPolicy == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) {130final JScrollBar hbar = getDelegate().getHorizontalScrollBar();131size.height += hbar != null ? hbar.getMinimumSize().height : 0;132}133}134return size;135}136137@Override138public void insert(final String text, final int pos) {139final ScrollableJTextArea pane = getDelegate();140synchronized (getDelegateLock()) {141final JTextArea area = pane.getView();142final boolean doScroll = pos >= area.getDocument().getLength()143&& area.getDocument().getLength() != 0;144area.insert(text, pos);145revalidate();146if (doScroll) {147final JScrollBar vbar = pane.getVerticalScrollBar();148if (vbar != null) {149vbar.setValue(vbar.getMaximum() - vbar.getVisibleAmount());150}151}152}153repaintPeer();154}155156@Override157public void replaceRange(final String text, final int start,158final int end) {159synchronized (getDelegateLock()) {160// JTextArea.replaceRange() posts two different events.161// Since we make no differences between text events,162// the document listener has to be disabled while163// JTextArea.replaceRange() is called.164final Document document = getTextComponent().getDocument();165document.removeDocumentListener(this);166getTextComponent().replaceRange(text, start, end);167revalidate();168postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));169document.addDocumentListener(this);170}171repaintPeer();172}173174private void setScrollBarVisibility(final int visibility) {175final ScrollableJTextArea pane = getDelegate();176final JTextArea view = pane.getView();177view.setLineWrap(false);178179switch (visibility) {180case TextArea.SCROLLBARS_NONE:181pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);182pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);183view.setLineWrap(true);184break;185case TextArea.SCROLLBARS_VERTICAL_ONLY:186pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);187pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);188view.setLineWrap(true);189break;190case TextArea.SCROLLBARS_HORIZONTAL_ONLY:191pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);192pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);193break;194default:195pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);196pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);197break;198}199}200201@SuppressWarnings("serial")// Safe: outer class is non-serializable.202final class ScrollableJTextArea extends JScrollPane {203204ScrollableJTextArea() {205super();206getViewport().setView(new JTextAreaDelegate());207}208209public JTextArea getView() {210return (JTextArea) getViewport().getView();211}212213@Override214public void setEnabled(final boolean enabled) {215getViewport().getView().setEnabled(enabled);216super.setEnabled(enabled);217}218219private final class JTextAreaDelegate extends JTextArea {220221// Empty non private constructor was added because access to this222// class shouldn't be emulated by a synthetic accessor method.223JTextAreaDelegate() {224super();225}226227@Override228public void replaceSelection(String content) {229getDocument().removeDocumentListener(LWTextAreaPeer.this);230super.replaceSelection(content);231// post only one text event in this case232postTextEvent();233getDocument().addDocumentListener(LWTextAreaPeer.this);234}235236@Override237public boolean hasFocus() {238return getTarget().hasFocus();239}240241@Override242public Point getLocationOnScreen() {243return LWTextAreaPeer.this.getLocationOnScreen();244}245}246}247}248249250