001    /* ViewportLayout.java --
002       Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package javax.swing;
039    
040    import java.awt.Component;
041    import java.awt.Container;
042    import java.awt.Dimension;
043    import java.awt.LayoutManager;
044    import java.awt.Point;
045    import java.awt.Rectangle;
046    import java.io.Serializable;
047    
048    /**
049     * The default layout for {@link JViewport}. The viewport makes its view the 
050     * same size as itself, but not smaller than its minimum size. 
051     * 
052     * If the port extends extends into space <em>past</em> the edge of the view,
053     * this layout manager moves the port up or to the left, in view space, by the
054     * amount of empty space (keep the lower and right edges lined up).
055     * 
056     * @author  Andrew Selkirk
057     * @author  Graydon Hoare
058     * @author  Audrius Meskauskas (audriusa@Bioinformatics.org)
059     */
060    public class ViewportLayout implements LayoutManager, Serializable
061    {
062      private static final long serialVersionUID = -788225906076097229L;
063    
064      public ViewportLayout() 
065      {
066        // Nothing to do here.
067      }
068      
069      /**
070       * The method is not used with this manager.
071       */
072      public void addLayoutComponent(String name, Component c) 
073      {
074        // Nothing to do here.
075      }
076    
077      /**
078       * The method is not used with this manager.
079       */
080      public void removeLayoutComponent(Component c) 
081      {
082        // Nothing to do here.
083      }
084      
085      /**
086       * Get the preferred layout size. If the view implements
087       * {@link Scrollable}, this method returns 
088       * {@link Scrollable#getPreferredScrollableViewportSize}.
089       * Otherwise, it returns {@link Component#getPreferredSize()}.
090       * 
091       * @return the preferred layout size, as described about.
092       */
093      public Dimension preferredLayoutSize(Container parent) 
094      {
095        JViewport vp = (JViewport)parent;
096        Component view = vp.getView();
097        if (view != null)
098          {
099            if (view instanceof Scrollable)
100              return ((Scrollable)view).getPreferredScrollableViewportSize();
101            return view.getPreferredSize();
102          }
103        else
104          return new Dimension();
105      }
106    
107      /**
108       * Get the minimum layout size. Normally this method returns the value,
109       * returned by the view method {@link Component#getMinimumSize()}.
110       * 
111       * If the view is not set, the zero size is returned. 
112       *
113       * @param parent the viewport
114       * @return the minimum layout size.
115       */
116      public Dimension minimumLayoutSize(Container parent) 
117      {
118        // These values have been determined by the Mauve test for this method.
119        return new Dimension(4, 4);
120      }
121    
122      /**
123       * Layout the view and viewport to respect the following rules. These are not
124       * precisely the rules described in sun's javadocs, but they are the rules
125       * which sun's swing implementation follows, if you watch its behavior:
126       * <ol>
127       * <li>If the port is smaller than the view, leave the view at its current
128       * size.</li>
129       * <li>If the view is smaller than the port, the view is top aligned.</li>
130       * <li>If the view tracks the port size, the view position is always zero and
131       * the size equal to the viewport size</li>
132       * <li>In {@link JViewport#setViewSize(Dimension)}, the view size is never
133       * set smaller that its minimum size.</li>
134       * </ol>
135       * 
136       * @see JViewport#getViewSize
137       * @see JViewport#setViewSize
138       * @see JViewport#getViewPosition
139       * @see JViewport#setViewPosition
140       */
141      public void layoutContainer(Container parent)
142      {
143        // The way to interpret this function is basically to ignore the names
144        // of methods it calls, and focus on the variable names here. getViewRect
145        // doesn't, for example, return the view; it returns the port bounds in
146        // view space. Likwise setViewPosition doesn't reposition the view; it
147        // positions the port, in view coordinates.
148    
149        JViewport port = (JViewport) parent;
150        Component view = port.getView();
151    
152        if (view == null)
153          return;
154    
155        // These dimensions and positions are in *view space*. Do not mix
156        // variables in here from port space (eg. parent.getBounds()). This
157        // function should be entirely in view space, because the methods on
158        // the viewport require inputs in view space.
159    
160        Rectangle portBounds = port.getViewRect();
161        Dimension viewPref = new Dimension(view.getPreferredSize());
162    
163        Point portLowerRight = new Point(portBounds.x + portBounds.width,
164                                         portBounds.y + portBounds.height);
165    
166        // vertical implementation of the above rules
167        if (view instanceof Scrollable)
168          {
169            Scrollable sView = (Scrollable) view;
170    
171            // If the view size matches viewport size, the port offset can
172            // only be zero.
173            if (sView.getScrollableTracksViewportWidth())
174              {
175                viewPref.width = portBounds.width;
176                portBounds.x = 0;
177              }
178            if (sView.getScrollableTracksViewportHeight())
179              {
180                viewPref.height = portBounds.height;
181                portBounds.y = 0;
182              }
183          }
184    
185         if (viewPref.width < portBounds.width)
186           viewPref.width = portBounds.width;
187         if (viewPref.height < portBounds.height)
188           viewPref.height = portBounds.height;
189    
190        // If the view is larger than the port, the port is top and right
191        // aligned.
192        if (portLowerRight.x > viewPref.width)
193          portBounds.x = 0;
194    
195        if (portLowerRight.y > viewPref.height)
196          portBounds.y = 0;
197    
198        port.setViewSize(viewPref);
199        port.setViewPosition(portBounds.getLocation());
200      }
201    
202    }