source: trunk/src/gcc/libjava/java/awt/CardLayout.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.5 KB
Line 
1// CardLayout.java - Card-based layout engine
2
3/* Copyright (C) 1999, 2000, 2002 Free Software Foundation
4
5This file is part of GNU Classpath.
6
7GNU Classpath is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Classpath is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Classpath; see the file COPYING. If not, write to the
19Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
2002111-1307 USA.
21
22Linking this library statically or dynamically with other modules is
23making a combined work based on this library. Thus, the terms and
24conditions of the GNU General Public License cover the whole
25combination.
26
27As a special exception, the copyright holders of this library give you
28permission to link this library with independent modules to produce an
29executable, regardless of the license terms of these independent
30modules, and to copy and distribute the resulting executable under
31terms of your choice, provided that you also meet, for each linked
32independent module, the terms and conditions of the license of that
33module. An independent module is a module which is not derived from
34or based on this library. If you modify this library, you may extend
35this exception to your version of the library, but you are not
36obligated to do so. If you do not wish to do so, delete this
37exception statement from your version. */
38
39
40package java.awt;
41
42import java.util.Enumeration;
43import java.util.Hashtable;
44import java.io.Serializable;
45
46/** This class implements a card-based layout scheme. Each included
47 * component is treated as a card. Only one card can be shown at a
48 * time. This class includes methods for changing which card is
49 * shown.
50 *
51 * @author Tom Tromey <[email protected]>
52 * @author Aaron M. Renn ([email protected])
53 */
54public class CardLayout implements LayoutManager2, Serializable
55{
56 /**
57 * Initializes a new instance of <code>CardLayout</code> with horizontal
58 * and vertical gaps of 0.
59 */
60 public CardLayout ()
61 {
62 this (0, 0);
63 }
64
65 /**
66 * Create a new <code>CardLayout</code> object with the specified
67 * horizontal and vertical gaps.
68 * @param hgap The horizontal gap
69 * @param vgap The vertical gap
70 */
71 public CardLayout (int hgap, int vgap)
72 {
73 this.hgap = hgap;
74 this.vgap = vgap;
75 this.tab = new Hashtable ();
76 }
77
78 /** Add a new component to the layout. The constraint must be a
79 * string which is used to name the component. This string can
80 * later be used to refer to the particular component.
81 * @param comp The component to add
82 * @param constraints The name by which the component can later be called
83 * @exception IllegalArgumentException If `constraints' is not a
84 * <code>String</code>
85 */
86 public void addLayoutComponent (Component comp, Object constraints)
87 {
88 if (! (constraints instanceof String))
89 throw new IllegalArgumentException ("Object " + constraints
90 + " is not a string");
91 tab.put (constraints, comp);
92 }
93
94 /** Add a new component to the layout. The name can be used later
95 * to refer to the component.
96 * @param name The name by which the component can later be called
97 * @param comp The component to add
98 * @deprecated This method is deprecated in favor of
99 * <code>addLayoutComponent(Component, Object)</code>.
100 */
101 public void addLayoutComponent (String name, Component comp)
102 {
103 addLayoutComponent (comp, name);
104 }
105
106 /** Cause the first component in the container to be displayed.
107 * @param parent The parent container
108 */
109 public void first (Container parent)
110 {
111 gotoComponent (parent, FIRST, null);
112 }
113
114 /** Return this layout manager's horizontal gap. */
115 public int getHgap ()
116 {
117 return hgap;
118 }
119
120 /** Return this layout manager's x alignment. This method always
121 * returns Component.CENTER_ALIGNMENT.
122 * @param parent Container using this layout manager instance
123 */
124 public float getLayoutAlignmentX (Container parent)
125 {
126 return Component.CENTER_ALIGNMENT;
127 }
128
129 /** Returns this layout manager's y alignment. This method always
130 * returns Component.CENTER_ALIGNMENT.
131 * @param parent Container using this layout manager instance
132 */
133 public float getLayoutAlignmentY (Container parent)
134 {
135 return Component.CENTER_ALIGNMENT;
136 }
137
138 /** Return this layout manager's vertical gap. */
139 public int getVgap ()
140 {
141 return vgap;
142 }
143
144 /** Invalidate this layout manager's state. */
145 public void invalidateLayout (Container target)
146 {
147 // Do nothing.
148 }
149
150 /** Cause the last component in the container to be displayed.
151 * @param parent The parent container
152 */
153 public void last (Container parent)
154 {
155 gotoComponent (parent, LAST, null);
156 }
157
158 /**
159 * Lays out the container. This is done by resizing the child components
160 * to be the same size as the parent, less insets and gaps.
161 *
162 * @param parent The parent container.
163 */
164 public void layoutContainer (Container parent)
165 {
166 int width = parent.width;
167 int height = parent.height;
168
169 Insets ins = parent.getInsets ();
170
171 int num = parent.ncomponents;
172 Component[] comps = parent.component;
173
174 int x = ins.left + hgap;
175 int y = ins.top + vgap;
176 width = width - 2 * hgap - ins.left - ins.right;
177 height = height - 2 * vgap - ins.top - ins.bottom;
178
179 for (int i = 0; i < num; ++i)
180 comps[i].setBounds (x, y, width, height);
181 }
182
183 /** Get the maximum layout size of the container.
184 * @param target The parent container
185 */
186 public Dimension maximumLayoutSize (Container target)
187 {
188 // The JCL says that this returns Integer.MAX_VALUE for both
189 // dimensions. But that just seems wrong to me.
190 return getSize (target, MAX);
191 }
192
193 /** Get the minimum layout size of the container.
194 * @param target The parent container
195 */
196 public Dimension minimumLayoutSize (Container target)
197 {
198 return getSize (target, MIN);
199 }
200
201 /** Cause the next component in the container to be displayed. If
202 * this current card is the last one in the deck, the first
203 * component is displayed.
204 * @param parent The parent container
205 */
206 public void next (Container parent)
207 {
208 gotoComponent (parent, NEXT, null);
209 }
210
211 /** Get the preferred layout size of the container.
212 * @param target The parent container
213 */
214 public Dimension preferredLayoutSize (Container parent)
215 {
216 return getSize (parent, PREF);
217 }
218
219 /** Cause the previous component in the container to be displayed.
220 * If this current card is the first one in the deck, the last
221 * component is displayed.
222 * @param parent The parent container
223 */
224 public void previous (Container parent)
225 {
226 gotoComponent (parent, PREV, null);
227 }
228
229 /** Remove the indicated component from this layout manager.
230 * @param comp The component to remove
231 */
232 public void removeLayoutComponent (Component comp)
233 {
234 Enumeration e = tab.keys ();
235 while (e.hasMoreElements ())
236 {
237 Object key = e.nextElement ();
238 if (tab.get (key) == comp)
239 {
240 tab.remove (key);
241 break;
242 }
243 }
244 }
245
246 /** Set this layout manager's horizontal gap.
247 * @param hgap The new gap
248 */
249 public void setHgap (int hgap)
250 {
251 this.hgap = hgap;
252 }
253
254 /** Set this layout manager's vertical gap.
255 * @param vgap The new gap
256 */
257 public void setVgap (int vgap)
258 {
259 this.vgap = vgap;
260 }
261
262 /** Cause the named component to be shown. If the component name is
263 * unknown, this method does nothing.
264 * @param parent The parent container
265 * @param name The name of the component to show
266 */
267 public void show (Container parent, String name)
268 {
269 Object target = tab.get (name);
270 if (target != null)
271 gotoComponent (parent, NONE, (Component) target);
272 }
273
274 /**
275 * Returns a string representation of this layout manager.
276 *
277 * @return A string representation of this object.
278 */
279 public String toString ()
280 {
281 return getClass ().getName () + "[" + hgap + "," + vgap + "]";
282 }
283
284 // This implements first(), last(), next(), and previous().
285 private void gotoComponent (Container parent, int what,
286 Component target)
287 {
288 int num = parent.ncomponents;
289 // This is more efficient than calling getComponents().
290 Component[] comps = parent.component;
291 int choice = -1;
292
293 if (what == FIRST)
294 choice = 0;
295 else if (what == LAST)
296 choice = num - 1;
297 else if (what >= 0)
298 choice = what;
299
300 for (int i = 0; i < num; ++i)
301 {
302 // If TARGET is set then we are looking for a specific
303 // component.
304 if (target != null)
305 {
306 if (target == comps[i])
307 choice = i;
308 }
309
310 if (comps[i].isVisible ())
311 {
312 if (what == NEXT)
313 {
314 choice = i + 1;
315 if (choice == num)
316 choice = 0;
317 }
318 else if (what == PREV)
319 {
320 choice = i - 1;
321 if (choice < 0)
322 choice = num - 1;
323 }
324 else if (choice == i)
325 {
326 // Do nothing if we're already looking at the right
327 // component.
328 return;
329 }
330 comps[i].setVisible (false);
331
332 if (choice >= 0)
333 break;
334 }
335 }
336
337 if (choice >= 0 && choice < num)
338 comps[choice].setVisible (true);
339 }
340
341 // Compute the size according to WHAT.
342 private Dimension getSize (Container parent, int what)
343 {
344 int w = 0, h = 0, num = parent.ncomponents;
345 Component[] comps = parent.component;
346
347 for (int i = 0; i < num; ++i)
348 {
349 Dimension d;
350
351 if (what == MIN)
352 d = comps[i].getMinimumSize ();
353 else if (what == MAX)
354 d = comps[i].getMaximumSize ();
355 else
356 d = comps[i].getPreferredSize ();
357
358 w = Math.max (d.width, w);
359 h = Math.max (d.height, h);
360 }
361
362 Insets i = parent.getInsets ();
363 w += 2 * hgap + i.right + i.left;
364 h += 2 * vgap + i.bottom + i.top;
365
366 // Handle overflow.
367 if (w < 0)
368 w = Integer.MAX_VALUE;
369 if (h < 0)
370 h = Integer.MAX_VALUE;
371
372 return new Dimension (w, h);
373 }
374
375 /**
376 * @serial Horizontal gap value.
377 */
378 private int hgap;
379
380 /**
381 * @serial Vertical gap value.
382 */
383 private int vgap;
384
385 /**
386 * @serial Table of named components.
387 */
388 private Hashtable tab;
389
390 // These constants are used by the private gotoComponent method.
391 private int FIRST = 0;
392 private int LAST = 1;
393 private int NEXT = 2;
394 private int PREV = 3;
395 private int NONE = 4;
396
397 // These constants are used by the private getSize method.
398 private int MIN = 0;
399 private int MAX = 1;
400 private int PREF = 2;
401}
Note: See TracBrowser for help on using the repository browser.