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