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