jdk/src/share/classes/javax/swing/ViewportLayout.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.LayoutManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The default layout manager for <code>JViewport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>ViewportLayout</code> defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * a policy for layout that should be useful for most applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The viewport makes its view the same size as the viewport,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * however it will not make the view smaller than its minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * As the viewport grows the view is kept bottom justified until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the entire view is visible, subsequently the view is kept top
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class ViewportLayout implements LayoutManager, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Single instance used by JViewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static ViewportLayout SHARED_INSTANCE = new ViewportLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Adds the specified component to the layout. Not used by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param name the name of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param c the the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void addLayoutComponent(String name, Component c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Removes the specified component from the layout. Not used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param c the component to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public void removeLayoutComponent(Component c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Returns the preferred dimensions for this layout given the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param parent the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @return a <code>Dimension</code> object containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *          preferred dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @see #minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Component view = ((JViewport)parent).getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        else if (view instanceof Scrollable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return ((Scrollable)view).getPreferredScrollableViewportSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return view.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
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
     * Returns the minimum dimensions needed to layout the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * contained in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param parent the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return a <code>Dimension</code> object containing the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *          dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see #preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return new Dimension(4, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
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
     * Called by the AWT when the specified container needs to be laid out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param parent  the container to lay out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @exception AWTError  if the target isn't the container specified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *                      <code>BoxLayout</code> constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void layoutContainer(Container parent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        JViewport vp = (JViewport)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Component view = vp.getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Scrollable scrollableView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        else if (view instanceof Scrollable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            scrollableView = (Scrollable) view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /* All of the dimensions below are in view coordinates, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * vpSize which we're converting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Insets insets = vp.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Dimension viewPrefSize = view.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Dimension vpSize = vp.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Dimension extentSize = vp.toViewCoordinates(vpSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Dimension viewSize = new Dimension(viewPrefSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (scrollableView != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (scrollableView.getScrollableTracksViewportWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                viewSize.width = vpSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (scrollableView.getScrollableTracksViewportHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                viewSize.height = vpSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Point viewPosition = vp.getViewPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        /* If the new viewport size would leave empty space to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * right of the view, right justify the view or left justify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * the view when the width of the view is smaller than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (scrollableView == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            vp.getParent() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            vp.getParent().getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if ((viewPosition.x + extentSize.width) > viewSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                viewPosition.x = Math.max(0, viewSize.width - extentSize.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (extentSize.width > viewSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                viewPosition.x = viewSize.width - extentSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                viewPosition.x = Math.max(0, Math.min(viewSize.width - extentSize.width, viewPosition.x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        /* If the new viewport size would leave empty space below the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * view, bottom justify the view or top justify the view when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * the height of the view is smaller than the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if ((viewPosition.y + extentSize.height) > viewSize.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            viewPosition.y = Math.max(0, viewSize.height - extentSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        /* If we haven't been advised about how the viewports size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         * should change wrt to the viewport, i.e. if the view isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * an instance of Scrollable, then adjust the views size as follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         * If the origin of the view is showing and the viewport is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * bigger than the views preferred size, then make the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * the same size as the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (scrollableView == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                viewSize.width = vpSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                viewSize.height = vpSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        vp.setViewPosition(viewPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        vp.setViewSize(viewSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}