1 // CardLayout.java - Card-based layout engine
3 /* Copyright (C) 2000 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 // FIXME: can we just use the width and height fields of parent?
131 // Or will that break with subclassing?
132 Dimension d = parent.getSize ();
134 Insets ins = parent.getInsets ();
136 int num = parent.getComponentCount ();
137 // This is more efficient than calling getComponents().
138 Component[] comps = parent.component;
140 for (int i = 0; i < num; ++i)
142 if (comps[i].isVisible ())
144 // Only resize the one we care about.
145 comps[i].setBounds (hgap + ins.left, vgap + ins.top,
146 d.width - 2 * hgap - ins.left - ins.right,
147 d.height - 2 * vgap - ins.top - ins.bottom);
153 /** Get the maximum layout size of the container.
154 * @param target The parent container
156 public Dimension maximumLayoutSize (Container target)
158 // The JCL says that this returns Integer.MAX_VALUE for both
159 // dimensions. But that just seems wrong to me.
160 return getSize (target, MAX);
163 /** Get the minimum layout size of the container.
164 * @param target The parent container
166 public Dimension minimumLayoutSize (Container target)
168 return getSize (target, MIN);
171 /** Cause the next component in the container to be displayed.
172 * @param parent The parent container
174 public void next (Container parent)
176 gotoComponent (parent, NEXT, null);
179 /** Get the preferred layout size of the container.
180 * @param target The parent container
182 public Dimension preferredLayoutSize (Container parent)
184 return getSize (parent, PREF);
187 /** Cause the previous component in the container to be displayed.
188 * @param parent The parent container
190 public void previous (Container parent)
192 gotoComponent (parent, PREV, null);
195 /** Remove the indicated component from this layout manager.
196 * @param comp The component to remove
198 public void removeLayoutComponent (Component comp)
200 Enumeration e = map.keys ();
201 while (e.hasMoreElements ())
203 Object key = e.nextElement ();
204 if (map.get (key) == comp)
212 /** Set this layout manager's horizontal gap.
213 * @param hgap The new gap
215 public void setHgap (int hgap)
220 /** Set this layout manager's vertical gap.
221 * @param vgap The new gap
223 public void setVgap (int vgap)
228 /** Cause the named component to be shown. If the component name is
229 * unknown, this method does nothing.
230 * @param parent The parent container
231 * @param name The name of the component to show
233 public void show (Container parent, String name)
235 Object target = map.get (name);
237 gotoComponent (parent, NONE, (Component) target);
240 public String toString ()
242 return getClass ().getName () + "[" + hgap + "," + vgap + "]";
245 // This implements first(), last(), next(), and previous().
246 private void gotoComponent (Container parent, int what,
249 int num = parent.getComponentCount ();
250 // This is more efficient than calling getComponents().
251 Component[] comps = parent.component;
256 else if (what == LAST)
261 for (int i = 0; i < num; ++i)
263 // If TARGET is set then we are looking for a specific
267 if (target == comps[i])
271 if (comps[i].isVisible ())
279 else if (what == PREV)
287 // Do nothing if we're already looking at the right
292 comps[i].setVisible (false);
299 comps[choice].setVisible (true);
302 // Compute the size according to WHAT.
303 private Dimension getSize (Container parent, int what)
305 int w = 0, h = 0, num = parent.getComponentCount ();
306 // This is more efficient than calling getComponents().
307 Component[] comps = parent.component;
309 for (int i = 0; i < num; ++i)
311 // FIXME: can we just directly read the fields in Component?
312 // Or will that not work with subclassing?
316 d = comps[i].getMinimumSize ();
317 else if (what == MAX)
318 d = comps[i].getMaximumSize ();
320 d = comps[i].getPreferredSize ();
322 w = Math.max (d.width, w);
323 h = Math.max (d.height, h);
326 Insets i = parent.getInsets ();
327 w += 2 * hgap + i.right + i.left;
328 h += 2 * vgap + i.bottom + i.top;
332 w = Integer.MAX_VALUE;
334 h = Integer.MAX_VALUE;
336 return new Dimension (w, h);
343 // This hashtable maps a name to a component.
344 private Hashtable map;
346 // These constants are used by the private gotoComponent method.
347 private int FIRST = 0;
348 private int LAST = 1;
349 private int NEXT = 2;
350 private int PREV = 3;
351 private int NONE = 4;
353 // These constants are used by the private getSize method.
356 private int PREF = 2;