KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > ViewportLayout


1 /*
2  * @(#)ViewportLayout.java 1.38 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.awt.LayoutManager JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Container JavaDoc;
13 import java.awt.Rectangle JavaDoc;
14 import java.awt.Point JavaDoc;
15 import java.awt.Dimension JavaDoc;
16 import java.awt.Insets JavaDoc;
17 import java.io.Serializable JavaDoc;
18
19 /**
20  * The default layout manager for <code>JViewport</code>.
21  * <code>ViewportLayout</code> defines
22  * a policy for layout that should be useful for most applications.
23  * The viewport makes its view the same size as the viewport,
24  * however it will not make the view smaller than its minimum size.
25  * As the viewport grows the view is kept bottom justified until
26  * the entire view is visible, subsequently the view is kept top
27  * justified.
28  * <p>
29  * <strong>Warning:</strong>
30  * Serialized objects of this class will not be compatible with
31  * future Swing releases. The current serialization support is
32  * appropriate for short term storage or RMI between applications running
33  * the same version of Swing. As of 1.4, support for long term storage
34  * of all JavaBeans<sup><font size="-2">TM</font></sup>
35  * has been added to the <code>java.beans</code> package.
36  * Please see {@link java.beans.XMLEncoder}.
37  *
38  * @version 1.38 12/19/03
39  * @author Hans Muller
40  */

41 public class ViewportLayout implements LayoutManager JavaDoc, Serializable JavaDoc
42 {
43     // Single instance used by JViewport.
44
static ViewportLayout JavaDoc SHARED_INSTANCE = new ViewportLayout JavaDoc();
45
46     /**
47      * Adds the specified component to the layout. Not used by this class.
48      * @param name the name of the component
49      * @param c the the component to be added
50      */

51     public void addLayoutComponent(String JavaDoc name, Component JavaDoc c) { }
52
53     /**
54      * Removes the specified component from the layout. Not used by
55      * this class.
56      * @param c the component to remove
57      */

58     public void removeLayoutComponent(Component JavaDoc c) { }
59
60
61     /**
62      * Returns the preferred dimensions for this layout given the components
63      * in the specified target container.
64      * @param parent the component which needs to be laid out
65      * @return a <code>Dimension</code> object containing the
66      * preferred dimensions
67      * @see #minimumLayoutSize
68      */

69     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc parent) {
70     Component JavaDoc view = ((JViewport JavaDoc)parent).getView();
71     if (view == null) {
72         return new Dimension JavaDoc(0, 0);
73     }
74     else if (view instanceof Scrollable JavaDoc) {
75         return ((Scrollable JavaDoc)view).getPreferredScrollableViewportSize();
76     }
77     else {
78         return view.getPreferredSize();
79     }
80     }
81
82
83     /**
84      * Returns the minimum dimensions needed to layout the components
85      * contained in the specified target container.
86      *
87      * @param parent the component which needs to be laid out
88      * @return a <code>Dimension</code> object containing the minimum
89      * dimensions
90      * @see #preferredLayoutSize
91      */

92     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc parent) {
93     return new Dimension JavaDoc(4, 4);
94     }
95
96
97     /**
98      * Called by the AWT when the specified container needs to be laid out.
99      *
100      * @param parent the container to lay out
101      *
102      * @exception AWTError if the target isn't the container specified to the
103      * <code>BoxLayout</code> constructor
104      */

105     public void layoutContainer(Container JavaDoc parent)
106     {
107     JViewport JavaDoc vp = (JViewport JavaDoc)parent;
108     Component JavaDoc view = vp.getView();
109     Scrollable JavaDoc scrollableView = null;
110
111     if (view == null) {
112         return;
113     }
114     else if (view instanceof Scrollable JavaDoc) {
115         scrollableView = (Scrollable JavaDoc) view;
116     }
117
118     /* All of the dimensions below are in view coordinates, except
119      * vpSize which we're converting.
120      */

121
122     Insets JavaDoc insets = vp.getInsets();
123     Dimension JavaDoc viewPrefSize = view.getPreferredSize();
124     Dimension JavaDoc vpSize = vp.getSize();
125     Dimension JavaDoc extentSize = vp.toViewCoordinates(vpSize);
126     Dimension JavaDoc viewSize = new Dimension JavaDoc(viewPrefSize);
127
128     if (scrollableView != null) {
129         if (scrollableView.getScrollableTracksViewportWidth()) {
130         viewSize.width = vpSize.width;
131         }
132         if (scrollableView.getScrollableTracksViewportHeight()) {
133         viewSize.height = vpSize.height;
134         }
135     }
136
137     Point JavaDoc viewPosition = vp.getViewPosition();
138
139     /* If the new viewport size would leave empty space to the
140      * right of the view, right justify the view or left justify
141      * the view when the width of the view is smaller than the
142      * container.
143      */

144     if (scrollableView == null ||
145         vp.getParent() == null ||
146         vp.getParent().getComponentOrientation().isLeftToRight()) {
147         if ((viewPosition.x + extentSize.width) > viewSize.width) {
148         viewPosition.x = Math.max(0, viewSize.width - extentSize.width);
149         }
150     } else {
151         if (extentSize.width > viewSize.width) {
152         viewPosition.x = viewSize.width - extentSize.width;
153         } else {
154         viewPosition.x = Math.max(0, Math.min(viewSize.width - extentSize.width, viewPosition.x));
155         }
156     }
157
158     /* If the new viewport size would leave empty space below the
159      * view, bottom justify the view or top justify the view when
160      * the height of the view is smaller than the container.
161      */

162     if ((viewPosition.y + extentSize.height) > viewSize.height) {
163         viewPosition.y = Math.max(0, viewSize.height - extentSize.height);
164     }
165
166     /* If we haven't been advised about how the viewports size
167      * should change wrt to the viewport, i.e. if the view isn't
168      * an instance of Scrollable, then adjust the views size as follows.
169      *
170      * If the origin of the view is showing and the viewport is
171      * bigger than the views preferred size, then make the view
172      * the same size as the viewport.
173      */

174     if (scrollableView == null) {
175             if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
176             viewSize.width = vpSize.width;
177             }
178             if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
179             viewSize.height = vpSize.height;
180             }
181         }
182     vp.setViewPosition(viewPosition);
183     vp.setViewSize(viewSize);
184     }
185 }
186
187
Popular Tags