jdk/src/macosx/classes/sun/lwawt/LWTextAreaPeer.java
changeset 13993 8b3fe3d8badb
parent 13357 6a75209a8aeb
child 20153 d5bf90bfcb6d
equal deleted inserted replaced
13992:d1b65c4e924c 13993:8b3fe3d8badb
     1 /*
     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    27 package sun.lwawt;
    27 package sun.lwawt;
    28 
    28 
    29 import java.awt.Component;
    29 import java.awt.Component;
    30 import java.awt.Cursor;
    30 import java.awt.Cursor;
    31 import java.awt.Dimension;
    31 import java.awt.Dimension;
       
    32 import java.awt.Insets;
    32 import java.awt.Point;
    33 import java.awt.Point;
    33 import java.awt.TextArea;
    34 import java.awt.TextArea;
    34 import java.awt.event.TextEvent;
    35 import java.awt.event.TextEvent;
    35 import java.awt.peer.TextAreaPeer;
    36 import java.awt.peer.TextAreaPeer;
    36 
    37 
    39 import javax.swing.JTextArea;
    40 import javax.swing.JTextArea;
    40 import javax.swing.ScrollPaneConstants;
    41 import javax.swing.ScrollPaneConstants;
    41 import javax.swing.text.Document;
    42 import javax.swing.text.Document;
    42 import javax.swing.text.JTextComponent;
    43 import javax.swing.text.JTextComponent;
    43 
    44 
       
    45 /**
       
    46  * Lightweight implementation of {@link TextAreaPeer}. Delegates most of the
       
    47  * work to the {@link JTextArea} inside JScrollPane.
       
    48  */
    44 final class LWTextAreaPeer
    49 final class LWTextAreaPeer
    45         extends LWTextComponentPeer<TextArea, LWTextAreaPeer.ScrollableJTextArea>
    50         extends LWTextComponentPeer<TextArea, LWTextAreaPeer.ScrollableJTextArea>
    46         implements TextAreaPeer {
    51         implements TextAreaPeer {
    47 
    52 
       
    53     /**
       
    54      * The default number of visible columns.
       
    55      */
    48     private static final int DEFAULT_COLUMNS = 60;
    56     private static final int DEFAULT_COLUMNS = 60;
       
    57 
       
    58     /**
       
    59      * The default number of visible rows.
       
    60      */
    49     private static final int DEFAULT_ROWS = 10;
    61     private static final int DEFAULT_ROWS = 10;
    50 
    62 
    51     LWTextAreaPeer(final TextArea target,
    63     LWTextAreaPeer(final TextArea target,
    52                    final PlatformComponent platformComponent) {
    64                    final PlatformComponent platformComponent) {
    53         super(target, platformComponent);
    65         super(target, platformComponent);
    85     protected Component getDelegateFocusOwner() {
    97     protected Component getDelegateFocusOwner() {
    86         return getTextComponent();
    98         return getTextComponent();
    87     }
    99     }
    88 
   100 
    89     @Override
   101     @Override
       
   102     public Dimension getPreferredSize() {
       
   103         return getMinimumSize();
       
   104     }
       
   105 
       
   106     @Override
    90     public Dimension getMinimumSize() {
   107     public Dimension getMinimumSize() {
    91         return getMinimumSize(DEFAULT_ROWS, DEFAULT_COLUMNS);
   108         return getMinimumSize(DEFAULT_ROWS, DEFAULT_COLUMNS);
    92     }
   109     }
    93 
   110 
    94     @Override
   111     @Override
       
   112     public Dimension getPreferredSize(final int rows, final int columns) {
       
   113         return getMinimumSize(rows, columns);
       
   114     }
       
   115 
       
   116     @Override
    95     public Dimension getMinimumSize(final int rows, final int columns) {
   117     public Dimension getMinimumSize(final int rows, final int columns) {
    96         return getPreferredSize(rows, columns);
   118         final Dimension size = super.getMinimumSize(rows, columns);
    97     }
   119         synchronized (getDelegateLock()) {
    98 
   120             // JScrollPane insets
    99     @Override
   121             final Insets pi = getDelegate().getInsets();
   100     public Dimension getPreferredSize(final int rows, final int columns) {
   122             size.width += pi.left + pi.right;
   101         final Dimension size = super.getPreferredSize(rows, columns);
   123             size.height += pi.top + pi.bottom;
   102         synchronized (getDelegateLock()) {
   124             // Take scrollbars into account.
   103             final JScrollBar vbar = getDelegate().getVerticalScrollBar();
   125             final int vsbPolicy = getDelegate().getVerticalScrollBarPolicy();
   104             final JScrollBar hbar = getDelegate().getHorizontalScrollBar();
   126             if (vsbPolicy == ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) {
   105             final int scrollbarW = vbar != null ? vbar.getWidth() : 0;
   127                 final JScrollBar vbar = getDelegate().getVerticalScrollBar();
   106             final int scrollbarH = hbar != null ? hbar.getHeight() : 0;
   128                 size.width += vbar != null ? vbar.getMinimumSize().width : 0;
   107             return new Dimension(size.width + scrollbarW,
   129             }
   108                                  size.height + scrollbarH);
   130             final int hsbPolicy = getDelegate().getHorizontalScrollBarPolicy();
   109         }
   131             if (hsbPolicy == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) {
       
   132                 final JScrollBar hbar = getDelegate().getHorizontalScrollBar();
       
   133                 size.height += hbar != null ? hbar.getMinimumSize().height : 0;
       
   134             }
       
   135         }
       
   136         return size;
   110     }
   137     }
   111 
   138 
   112     @Override
   139     @Override
   113     public void insert(final String text, final int pos) {
   140     public void insert(final String text, final int pos) {
   114         final ScrollableJTextArea pane = getDelegate();
   141         final ScrollableJTextArea pane = getDelegate();