1 // CardLayout.java - Card-based layout engine
3 /* Copyright (C) 2000, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15 import java.io.Serializable;
17 /** This class implements a card-based layout scheme. Each included
18 * component is treated as a card. Only one card can be shown at a
19 * time. This class includes methods for changing which card is
23 * @author Tom Tromey <tromey@redhat.com>
24 * @date December 2, 2000
26 public class CardLayout implements LayoutManager2, Serializable
28 /** Create a new CardLayout object with both gaps zero. */
34 /** Create a new CardLayout object with the specified horizontal and
36 * @param hgap The horizontal gap
37 * @param vgap The vertical gap
39 public CardLayout (int hgap, int vgap)
43 this.map = new Hashtable ();
46 /** Add a new component to the layout. The constraint must be a
47 * string which is used to name the component. This string can
48 * later be used to refer to the particular component.
49 * @param comp The component to add
50 * @param constraints The name by which the component can later be called
51 * @exception IllegalArgumentException If `constraints' is not a string
53 public void addLayoutComponent (Component comp, Object constraints)
55 if (! (constraints instanceof String))
56 throw new IllegalArgumentException ("Object " + constraints
57 + " is not a string");
58 map.put (constraints, comp);
61 /** Add a new component to the layout. The name can be used later
62 * to refer to the component.
63 * @param name The name by which the component can later be called
64 * @param comp The component to add
67 public void addLayoutComponent (String name, Component comp)
69 addLayoutComponent (comp, name);
72 /** Cause the first component in the container to be displayed.
73 * @param parent The parent container
75 public void first (Container parent)
77 gotoComponent (parent, FIRST, null);
80 /** Return this layout manager's horizontal gap. */
86 /** Return this layout manager's x alignment. This method always
87 * returns Component.CENTER_ALIGNMENT.
88 * @param parent Container using this layout manager instance
90 public float getLayoutAlignmentX (Container parent)
92 return Component.CENTER_ALIGNMENT;
95 /** Returns this layout manager's y alignment. This method always
96 * returns Component.CENTER_ALIGNMENT.
97 * @param parent Container using this layout manager instance
99 public float getLayoutAlignmentY (Container parent)
101 return Component.CENTER_ALIGNMENT;
104 /** Return this layout manager's vertical gap. */
105 public int getVgap ()
110 /** Invalidate this layout manager's state. */
111 public void invalidateLayout (Container target)
116 /** Cause the last component in the container to be displayed.
117 * @param parent The parent container
119 public void last (Container parent)
121 gotoComponent (parent, LAST, null);
124 /** Lay out the container's components based on the current
126 * @param parent The parent container
128 public void layoutContainer (Container parent)
130 int width = parent.width;
131 int height = parent.height;
133 Insets ins = parent.getInsets ();
135 int num = parent.ncomponents;
136 Component[] comps = parent.component;
138 for (int i = 0; i < num; ++i)
140 comps[i].setBounds (hgap + ins.left, vgap + ins.top,
141 width - 2 * hgap - ins.left - ins.right,
142 height - 2 * vgap - ins.top - ins.bottom);
146 /** Get the maximum layout size of the container.
147 * @param target The parent container
149 public Dimension maximumLayoutSize (Container target)
151 // The JCL says that this returns Integer.MAX_VALUE for both
152 // dimensions. But that just seems wrong to me.
153 return getSize (target, MAX);
156 /** Get the minimum layout size of the container.
157 * @param target The parent container
159 public Dimension minimumLayoutSize (Container target)
161 return getSize (target, MIN);
164 /** Cause the next component in the container to be displayed.
165 * @param parent The parent container
167 public void next (Container parent)
169 gotoComponent (parent, NEXT, null);
172 /** Get the preferred layout size of the container.
173 * @param target The parent container
175 public Dimension preferredLayoutSize (Container parent)
177 return getSize (parent, PREF);
180 /** Cause the previous component in the container to be displayed.
181 * @param parent The parent container
183 public void previous (Container parent)
185 gotoComponent (parent, PREV, null);
188 /** Remove the indicated component from this layout manager.
189 * @param comp The component to remove
191 public void removeLayoutComponent (Component comp)
193 Enumeration e = map.keys ();
194 while (e.hasMoreElements ())
196 Object key = e.nextElement ();
197 if (map.get (key) == comp)
205 /** Set this layout manager's horizontal gap.
206 * @param hgap The new gap
208 public void setHgap (int hgap)
213 /** Set this layout manager's vertical gap.
214 * @param vgap The new gap
216 public void setVgap (int vgap)
221 /** Cause the named component to be shown. If the component name is
222 * unknown, this method does nothing.
223 * @param parent The parent container
224 * @param name The name of the component to show
226 public void show (Container parent, String name)
228 Object target = map.get (name);
230 gotoComponent (parent, NONE, (Component) target);
233 public String toString ()
235 return getClass ().getName () + "[" + hgap + "," + vgap + "]";
238 // This implements first(), last(), next(), and previous().
239 private void gotoComponent (Container parent, int what,
242 int num = parent.getComponentCount ();
243 // This is more efficient than calling getComponents().
244 Component[] comps = parent.component;
249 else if (what == LAST)
254 for (int i = 0; i < num; ++i)
256 // If TARGET is set then we are looking for a specific
260 if (target == comps[i])
264 if (comps[i].isVisible ())
272 else if (what == PREV)
280 // Do nothing if we're already looking at the right
285 comps[i].setVisible (false);
292 comps[choice].setVisible (true);
295 // Compute the size according to WHAT.
296 private Dimension getSize (Container parent, int what)
298 int w = 0, h = 0, num = parent.ncomponents;
299 Component[] comps = parent.component;
301 for (int i = 0; i < num; ++i)
306 d = comps[i].getMinimumSize ();
307 else if (what == MAX)
308 d = comps[i].getMaximumSize ();
310 d = comps[i].getPreferredSize ();
312 w = Math.max (d.width, w);
313 h = Math.max (d.height, h);
316 Insets i = parent.getInsets ();
317 w += 2 * hgap + i.right + i.left;
318 h += 2 * vgap + i.bottom + i.top;
322 w = Integer.MAX_VALUE;
324 h = Integer.MAX_VALUE;
326 return new Dimension (w, h);
333 // This hashtable maps a name to a component.
334 private Hashtable map;
336 // These constants are used by the private gotoComponent method.
337 private int FIRST = 0;
338 private int LAST = 1;
339 private int NEXT = 2;
340 private int PREV = 3;
341 private int NONE = 4;
343 // These constants are used by the private getSize method.
346 private int PREF = 2;