001    /* Box.java --
002       Copyright (C) 2002, 2004 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    
039    package javax.swing;
040    
041    import java.awt.AWTError;
042    import java.awt.Component;
043    import java.awt.Container;
044    import java.awt.Dimension;
045    import java.awt.LayoutManager;
046    
047    import javax.accessibility.Accessible;
048    import javax.accessibility.AccessibleContext;
049    import javax.accessibility.AccessibleRole;
050    
051    /**
052     * A component that uses a {@link BoxLayout} as Layout Manager.
053     *
054     * In addition to that, this class provides a set of static methods for
055     * creating some filler components ('struts' and 'glue') for use in
056     * containers that are laid out using BoxLayout.
057     *
058     * @author Ronald Veldema (rveldema@cs.vu.nl)
059     */
060    public class Box extends JComponent implements Accessible
061    {
062      private static final long serialVersionUID = 1525417495883046342L;
063    
064      /**
065       * Provides accessibility support for <code>Box</code>es.
066       */
067      protected class AccessibleBox extends Container.AccessibleAWTContainer
068      {
069        private static final long serialVersionUID = -7775079816389931944L;
070    
071        protected AccessibleBox()
072        {
073          // Nothing to do here.
074        }
075    
076        public AccessibleRole getAccessibleRole()
077        {
078          return null;
079        }
080      }
081    
082      /**
083       * A component that servers as a filler in BoxLayout controlled containers.
084       */
085      public static class Filler extends JComponent implements Accessible
086      {
087        private static final long serialVersionUID = -1204263191910183998L;
088    
089        /**
090         * Provides accessibility support for <code>Box.Filler</code>.
091         */
092        protected class AccessibleBoxFiller
093          extends Component.AccessibleAWTComponent
094        {
095          private static final long serialVersionUID = 164963348357479321L;
096    
097          protected AccessibleBoxFiller()
098          {
099            // Nothing to do here.
100          }
101    
102          public AccessibleRole getAccessibleRole()
103          {
104            return null;
105          }
106        }
107    
108        private transient Dimension min, pref, max;
109    
110        /**
111         * Creates a new instance of Filler.
112         *
113         * @param min the minimum size of the filler.
114         * @param pref the preferred size of the filler.
115         * @param max the maximum size of the filler.
116         */
117        public Filler(Dimension min, Dimension pref, Dimension max)
118        {
119          changeShape(min, pref, max);
120        }
121    
122        /**
123         * Changes the dimensions of this Filler.
124         *
125         * @param min the new minimum size of the filler.
126         * @param pref the new preferred size of the filler.
127         * @param max the new maximum size of the filler.
128         */
129        public void changeShape(Dimension min, Dimension pref, Dimension max)
130        {
131          this.min = min;
132          this.pref = pref;
133          this.max = max;
134        }
135    
136        public AccessibleContext getAccessibleContext()
137        {
138          if (accessibleContext == null)
139            accessibleContext = new AccessibleBoxFiller();
140          return accessibleContext;
141        }
142    
143        /**
144         * Returns the maximum size of this Filler.
145         *
146         * @return the maximum size of this Filler.
147         */
148        public Dimension getMaximumSize()
149        {
150          return max;
151        }
152    
153        /**
154         * Returns the minimum size of this Filler.
155         *
156         * @return the minimum size of this Filler.
157         */
158        public Dimension getMinimumSize()
159        {
160          return min;
161        }
162    
163        /**
164         * Returns the preferred size of this Filler.
165         *
166         * @return the preferred size of this Filler.
167         */
168        public Dimension getPreferredSize()
169        {
170          return pref;
171        }
172      }
173    
174      /**
175       * Creates a new Box component, that lays out its children according
176       * to the <code>axis</code> parameter.
177       *
178       * @param axis the orientation of the BoxLayout.
179       *
180       * @see BoxLayout#X_AXIS
181       * @see BoxLayout#Y_AXIS
182       * @see BoxLayout#LINE_AXIS
183       * @see BoxLayout#PAGE_AXIS
184       */
185      public Box(int axis)
186      {
187        super.setLayout(new BoxLayout(this, axis));
188      }
189    
190      /**
191       * Creates a filler component which acts as glue between components.
192       * It does not take space unless some extra space is available. If extra
193       * space is available, this component can expand in both X and Y directions.
194       *
195       * @return a glue-like filler component.
196       */
197      public static Component createGlue()
198      {
199        Filler glue = new Filler(new Dimension(0, 0), new Dimension(0, 0),
200                                 new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
201        return glue;
202      }
203    
204      public static Box createHorizontalBox()
205      {
206        return new Box(BoxLayout.X_AXIS);
207      }
208    
209      /**
210       * Creates a filler component which acts as glue between components.
211       * It does not take space unless some extra space is available. If extra
212       * space is available, this component can expand in the X direction.
213       *
214       * @return a glue-like filler component.
215       */
216      public static Component createHorizontalGlue()
217      {
218        Filler glue = new Filler(new Dimension(0, 0), new Dimension(0, 0),
219                                 new Dimension(Short.MAX_VALUE, 0));
220        return glue;
221      }
222    
223      /**
224       * Creates a filler component which acts as strut between components.
225       * It will fill exactly the specified horizontal size.
226       *
227       * @param width the width of this strut in pixels.
228       *
229       * @return a strut-like filler component.
230       */
231      public static Component createHorizontalStrut(int width)
232      {
233        Filler strut = new Filler(new Dimension(width, 0),
234                                  new Dimension(width, 0),
235                                  new Dimension(width, Integer.MAX_VALUE));
236        return strut;
237      }
238    
239      public static Component createRigidArea(Dimension d)
240      {
241        return new Filler(d, d, d);
242      }
243    
244      public static Box createVerticalBox()
245      {
246        return new Box(BoxLayout.Y_AXIS);
247      }
248    
249      /**
250       * Creates a filler component which acts as glue between components.
251       * It does not take space unless some extra space is available. If extra
252       * space is available, this component can expand in the Y direction.
253       *
254       * @return a glue-like filler component.
255       */
256      public static Component createVerticalGlue()
257      {
258        return createGlue();
259      }
260    
261      /**
262       * Creates a filler component which acts as strut between components.
263       * It will fill exactly the specified vertical size.
264       *
265       * @param height the height of this strut in pixels.
266       *
267       * @return a strut-like filler component.
268       */
269      public static Component createVerticalStrut(int height)
270      {
271        Filler strut = new Filler(new Dimension(0, height),
272                                  new Dimension(0, height),
273                                  new Dimension(Integer.MAX_VALUE, height));
274        return strut;
275      }
276    
277      public void setLayout(LayoutManager l)
278      {
279        throw new AWTError("Not allowed to set layout managers for boxes.");
280      }
281    
282      public AccessibleContext getAccessibleContext()
283      {
284        if (accessibleContext == null)
285          accessibleContext = new AccessibleBox();
286        return accessibleContext;
287      }
288    
289    
290    }