jdk/src/share/classes/javax/swing/ViewportLayout.java
author cl
Mon, 07 Oct 2013 11:34:44 -0700
changeset 20458 f2423fb3fd19
parent 20169 d7fa6d7586c9
child 22574 7f8ce0c8c20a
permissions -rw-r--r--
8025840: Fix all the doclint warnings about trademark Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2003, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class ViewportLayout implements LayoutManager, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Single instance used by JViewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static ViewportLayout SHARED_INSTANCE = new ViewportLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Adds the specified component to the layout. Not used by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param name the name of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param c the the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void addLayoutComponent(String name, Component c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Removes the specified component from the layout. Not used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param c the component to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void removeLayoutComponent(Component c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Returns the preferred dimensions for this layout given the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param parent the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return a <code>Dimension</code> object containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *          preferred dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @see #minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        Component view = ((JViewport)parent).getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        else if (view instanceof Scrollable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return ((Scrollable)view).getPreferredScrollableViewportSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return view.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
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
     * Returns the minimum dimensions needed to layout the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * contained in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param parent the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @return a <code>Dimension</code> object containing the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *          dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @see #preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return new Dimension(4, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Called by the AWT when the specified container needs to be laid out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param parent  the container to lay out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   120
     * @throws AWTError if the target isn't the container specified to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *                      <code>BoxLayout</code> constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void layoutContainer(Container parent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        JViewport vp = (JViewport)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Component view = vp.getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Scrollable scrollableView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        else if (view instanceof Scrollable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            scrollableView = (Scrollable) view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        /* All of the dimensions below are in view coordinates, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * vpSize which we're converting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Insets insets = vp.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Dimension viewPrefSize = view.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Dimension vpSize = vp.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Dimension extentSize = vp.toViewCoordinates(vpSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Dimension viewSize = new Dimension(viewPrefSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (scrollableView != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (scrollableView.getScrollableTracksViewportWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                viewSize.width = vpSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (scrollableView.getScrollableTracksViewportHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                viewSize.height = vpSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Point viewPosition = vp.getViewPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        /* If the new viewport size would leave empty space to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * right of the view, right justify the view or left justify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * the view when the width of the view is smaller than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (scrollableView == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            vp.getParent() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            vp.getParent().getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if ((viewPosition.x + extentSize.width) > viewSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                viewPosition.x = Math.max(0, viewSize.width - extentSize.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (extentSize.width > viewSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                viewPosition.x = viewSize.width - extentSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                viewPosition.x = Math.max(0, Math.min(viewSize.width - extentSize.width, viewPosition.x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        /* If the new viewport size would leave empty space below the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * view, bottom justify the view or top justify the view when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * the height of the view is smaller than the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if ((viewPosition.y + extentSize.height) > viewSize.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            viewPosition.y = Math.max(0, viewSize.height - extentSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        /* If we haven't been advised about how the viewports size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * should change wrt to the viewport, i.e. if the view isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         * an instance of Scrollable, then adjust the views size as follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * If the origin of the view is showing and the viewport is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * bigger than the views preferred size, then make the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * the same size as the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (scrollableView == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                viewSize.width = vpSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                viewSize.height = vpSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        vp.setViewPosition(viewPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        vp.setViewSize(viewSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}