jdk/src/java.desktop/share/classes/javax/swing/ViewportLayout.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 28059 e576535359cc
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
    28
import java.awt.AWTError;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.LayoutManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The default layout manager for <code>JViewport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <code>ViewportLayout</code> defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a policy for layout that should be useful for most applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The viewport makes its view the same size as the viewport,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * however it will not make the view smaller than its minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * As the viewport grows the view is kept bottom justified until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the entire view is visible, subsequently the view is kept top
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20169
diff changeset
    53
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Hans Muller
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 22574
diff changeset
    58
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    60
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class ViewportLayout implements LayoutManager, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Single instance used by JViewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static ViewportLayout SHARED_INSTANCE = new ViewportLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Adds the specified component to the layout. Not used by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param name the name of the component
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    69
     * @param c the component to be added
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public void addLayoutComponent(String name, Component c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Removes the specified component from the layout. Not used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param c the component to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void removeLayoutComponent(Component c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Returns the preferred dimensions for this layout given the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param parent the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return a <code>Dimension</code> object containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *          preferred dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see #minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Component view = ((JViewport)parent).getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        else if (view instanceof Scrollable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return ((Scrollable)view).getPreferredScrollableViewportSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return view.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Returns the minimum dimensions needed to layout the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * contained in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param parent the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return a <code>Dimension</code> object containing the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *          dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see #preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return new Dimension(4, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Called by the AWT when the specified container needs to be laid out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param parent  the container to lay out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   122
     * @throws AWTError if the target isn't the container specified to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *                      <code>BoxLayout</code> constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void layoutContainer(Container parent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        JViewport vp = (JViewport)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Component view = vp.getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Scrollable scrollableView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        else if (view instanceof Scrollable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            scrollableView = (Scrollable) view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        /* All of the dimensions below are in view coordinates, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         * vpSize which we're converting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Insets insets = vp.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Dimension viewPrefSize = view.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Dimension vpSize = vp.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Dimension extentSize = vp.toViewCoordinates(vpSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Dimension viewSize = new Dimension(viewPrefSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (scrollableView != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (scrollableView.getScrollableTracksViewportWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                viewSize.width = vpSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (scrollableView.getScrollableTracksViewportHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                viewSize.height = vpSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Point viewPosition = vp.getViewPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /* If the new viewport size would leave empty space to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * right of the view, right justify the view or left justify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * the view when the width of the view is smaller than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (scrollableView == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            vp.getParent() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            vp.getParent().getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if ((viewPosition.x + extentSize.width) > viewSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                viewPosition.x = Math.max(0, viewSize.width - extentSize.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (extentSize.width > viewSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                viewPosition.x = viewSize.width - extentSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                viewPosition.x = Math.max(0, Math.min(viewSize.width - extentSize.width, viewPosition.x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        /* If the new viewport size would leave empty space below the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * view, bottom justify the view or top justify the view when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * the height of the view is smaller than the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if ((viewPosition.y + extentSize.height) > viewSize.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            viewPosition.y = Math.max(0, viewSize.height - extentSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        /* If we haven't been advised about how the viewports size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         * should change wrt to the viewport, i.e. if the view isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * an instance of Scrollable, then adjust the views size as follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * If the origin of the view is showing and the viewport is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * bigger than the views preferred size, then make the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         * the same size as the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (scrollableView == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                viewSize.width = vpSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                viewSize.height = vpSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        vp.setViewPosition(viewPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        vp.setViewSize(viewSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}