LWTextAreaPeer.java 8.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


package sun.lwawt;

import java.awt.Component;
30
import java.awt.Cursor;
31
import java.awt.Dimension;
32
import java.awt.Insets;
33 34 35 36 37 38 39 40 41 42 43 44
import java.awt.Point;
import java.awt.TextArea;
import java.awt.event.TextEvent;
import java.awt.peer.TextAreaPeer;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

45 46
/**
 * Lightweight implementation of {@link TextAreaPeer}. Delegates most of the
47
 * work to the {@link JTextArea} inside {@link JScrollPane}.
48
 */
49 50 51 52
final class LWTextAreaPeer
        extends LWTextComponentPeer<TextArea, LWTextAreaPeer.ScrollableJTextArea>
        implements TextAreaPeer {

53 54 55
    /**
     * The default number of visible columns.
     */
56
    private static final int DEFAULT_COLUMNS = 60;
57 58 59 60

    /**
     * The default number of visible rows.
     */
61 62 63 64 65 66 67 68
    private static final int DEFAULT_ROWS = 10;

    LWTextAreaPeer(final TextArea target,
                   final PlatformComponent platformComponent) {
        super(target, platformComponent);
    }

    @Override
69
    ScrollableJTextArea createDelegate() {
70 71 72 73
        return new ScrollableJTextArea();
    }

    @Override
74 75
    void initializeImpl() {
        super.initializeImpl();
76 77 78 79 80 81 82 83 84 85 86
        final int visibility = getTarget().getScrollbarVisibility();
        synchronized (getDelegateLock()) {
            setScrollBarVisibility(visibility);
        }
    }

    @Override
    JTextComponent getTextComponent() {
        return getDelegate().getView();
    }

87
    @Override
88
    Cursor getCursor(final Point p) {
89 90 91 92 93 94 95
        final boolean isContains;
        synchronized (getDelegateLock()) {
            isContains = getDelegate().getViewport().getBounds().contains(p);
        }
        return isContains ? super.getCursor(p) : null;
    }

96
    @Override
97
    Component getDelegateFocusOwner() {
98 99 100
        return getTextComponent();
    }

101 102 103 104 105
    @Override
    public Dimension getPreferredSize() {
        return getMinimumSize();
    }

106 107 108 109 110 111
    @Override
    public Dimension getMinimumSize() {
        return getMinimumSize(DEFAULT_ROWS, DEFAULT_COLUMNS);
    }

    @Override
112 113
    public Dimension getPreferredSize(final int rows, final int columns) {
        return getMinimumSize(rows, columns);
114 115 116
    }

    @Override
117 118
    public Dimension getMinimumSize(final int rows, final int columns) {
        final Dimension size = super.getMinimumSize(rows, columns);
119
        synchronized (getDelegateLock()) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            // JScrollPane insets
            final Insets pi = getDelegate().getInsets();
            size.width += pi.left + pi.right;
            size.height += pi.top + pi.bottom;
            // Take scrollbars into account.
            final int vsbPolicy = getDelegate().getVerticalScrollBarPolicy();
            if (vsbPolicy == ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) {
                final JScrollBar vbar = getDelegate().getVerticalScrollBar();
                size.width += vbar != null ? vbar.getMinimumSize().width : 0;
            }
            final int hsbPolicy = getDelegate().getHorizontalScrollBarPolicy();
            if (hsbPolicy == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) {
                final JScrollBar hbar = getDelegate().getHorizontalScrollBar();
                size.height += hbar != null ? hbar.getMinimumSize().height : 0;
            }
135
        }
136
        return size;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    }

    @Override
    public void insert(final String text, final int pos) {
        final ScrollableJTextArea pane = getDelegate();
        synchronized (getDelegateLock()) {
            final JTextArea area = pane.getView();
            final boolean doScroll = pos >= area.getDocument().getLength()
                                     && area.getDocument().getLength() != 0;
            area.insert(text, pos);
            revalidate();
            if (doScroll) {
                final JScrollBar vbar = pane.getVerticalScrollBar();
                if (vbar != null) {
                    vbar.setValue(vbar.getMaximum() - vbar.getVisibleAmount());
                }
            }
        }
        repaintPeer();
    }

    @Override
    public void replaceRange(final String text, final int start,
                             final int end) {
        synchronized (getDelegateLock()) {
            // JTextArea.replaceRange() posts two different events.
            // Since we make no differences between text events,
            // the document listener has to be disabled while
            // JTextArea.replaceRange() is called.
            final Document document = getTextComponent().getDocument();
            document.removeDocumentListener(this);
            getDelegate().getView().replaceRange(text, start, end);
            revalidate();
            postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));
            document.addDocumentListener(this);
        }
        repaintPeer();
    }

    private void setScrollBarVisibility(final int visibility) {
        final ScrollableJTextArea pane = getDelegate();
        final JTextArea view = pane.getView();
        view.setLineWrap(false);

        switch (visibility) {
            case TextArea.SCROLLBARS_NONE:
                pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
                view.setLineWrap(true);
                break;
            case TextArea.SCROLLBARS_VERTICAL_ONLY:
                pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                view.setLineWrap(true);
                break;
            case TextArea.SCROLLBARS_HORIZONTAL_ONLY:
                pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
                pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                break;
            default:
                pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                break;
        }
    }

203
    @SuppressWarnings("serial")// Safe: outer class is non-serializable.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    final class ScrollableJTextArea extends JScrollPane {

        ScrollableJTextArea() {
            super();
            getViewport().setView(new JTextAreaDelegate());
        }

        public JTextArea getView() {
            return (JTextArea) getViewport().getView();
        }

        @Override
        public void setEnabled(final boolean enabled) {
            getViewport().getView().setEnabled(enabled);
            super.setEnabled(enabled);
        }

        private final class JTextAreaDelegate extends JTextArea {

            // Empty non private constructor was added because access to this
            // class shouldn't be emulated by a synthetic accessor method.
            JTextAreaDelegate() {
                super();
            }

229 230 231 232 233 234 235 236 237
            @Override
            public void replaceSelection(String content) {
                getDocument().removeDocumentListener(LWTextAreaPeer.this);
                super.replaceSelection(content);
                // post only one text event in this case
                postTextEvent();
                getDocument().addDocumentListener(LWTextAreaPeer.this);
            }

238 239 240 241 242 243 244 245 246 247 248 249
            @Override
            public boolean hasFocus() {
                return getTarget().hasFocus();
            }

            @Override
            public Point getLocationOnScreen() {
                return LWTextAreaPeer.this.getLocationOnScreen();
            }
        }
    }
}