src/macosx/classes/sun/lwawt/LWTextAreaPeer.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
  * 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

@@ -27,10 +27,11 @@
 package sun.lwawt;
 
 import java.awt.Component;
 import java.awt.Cursor;
 import java.awt.Dimension;
+import java.awt.Insets;
 import java.awt.Point;
 import java.awt.TextArea;
 import java.awt.event.TextEvent;
 import java.awt.peer.TextAreaPeer;
 

@@ -39,15 +40,26 @@
 import javax.swing.JTextArea;
 import javax.swing.ScrollPaneConstants;
 import javax.swing.text.Document;
 import javax.swing.text.JTextComponent;
 
+/**
+ * Lightweight implementation of {@link TextAreaPeer}. Delegates most of the
+ * work to the {@link JTextArea} inside JScrollPane.
+ */
 final class LWTextAreaPeer
         extends LWTextComponentPeer<TextArea, LWTextAreaPeer.ScrollableJTextArea>
         implements TextAreaPeer {
 
+    /**
+     * The default number of visible columns.
+     */
     private static final int DEFAULT_COLUMNS = 60;
+
+    /**
+     * The default number of visible rows.
+     */
     private static final int DEFAULT_ROWS = 10;
 
     LWTextAreaPeer(final TextArea target,
                    final PlatformComponent platformComponent) {
         super(target, platformComponent);

@@ -85,30 +97,45 @@
     protected Component getDelegateFocusOwner() {
         return getTextComponent();
     }
 
     @Override
+    public Dimension getPreferredSize() {
+        return getMinimumSize();
+    }
+
+    @Override
     public Dimension getMinimumSize() {
         return getMinimumSize(DEFAULT_ROWS, DEFAULT_COLUMNS);
     }
 
     @Override
-    public Dimension getMinimumSize(final int rows, final int columns) {
-        return getPreferredSize(rows, columns);
+    public Dimension getPreferredSize(final int rows, final int columns) {
+        return getMinimumSize(rows, columns);
     }
 
     @Override
-    public Dimension getPreferredSize(final int rows, final int columns) {
-        final Dimension size = super.getPreferredSize(rows, columns);
+    public Dimension getMinimumSize(final int rows, final int columns) {
+        final Dimension size = super.getMinimumSize(rows, columns);
         synchronized (getDelegateLock()) {
+            // 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();
-            final int scrollbarW = vbar != null ? vbar.getWidth() : 0;
-            final int scrollbarH = hbar != null ? hbar.getHeight() : 0;
-            return new Dimension(size.width + scrollbarW,
-                                 size.height + scrollbarH);
+                size.height += hbar != null ? hbar.getMinimumSize().height : 0;
+            }
         }
+        return size;
     }
 
     @Override
     public void insert(final String text, final int pos) {
         final ScrollableJTextArea pane = getDelegate();