summaryrefslogtreecommitdiff
path: root/java/awt/CardLayout.java
blob: 2e3feece8d2f638e2fa0e5017b87d88600a710e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/* CardLayout.java -- Card-based layout engine
   Copyright (C) 1999, 2000, 2002, 2003, 2004  Free Software Foundation

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.awt;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * This class implements a card-based layout scheme.  Each included
 * component is treated as a card.  Only one card can be shown at a
 * time.  This class includes methods for changing which card is
 * shown.
 *
 * @author Tom Tromey (tromey@redhat.com)
 * @author Aaron M. Renn (arenn@urbanophile.com)
 */
public class CardLayout implements LayoutManager2, Serializable
{
  private static final long serialVersionUID = -4328196481005934313L;

  /**
   * Initializes a new instance of <code>CardLayout</code> with horizontal
   * and vertical gaps of 0.
   */
  public CardLayout ()
  {
    this (0, 0);
  }

  /**
   * Create a new <code>CardLayout</code> object with the specified
   * horizontal and vertical gaps.
   *
   * @param hgap The horizontal gap
   * @param vgap The vertical gap
   */
  public CardLayout (int hgap, int vgap)
  {
    this.hgap = hgap;
    this.vgap = vgap;
    this.tab = new Hashtable ();
  }

  /**
   * Add a new component to the layout.  The constraint must be a
   * string which is used to name the component.  This string can
   * later be used to refer to the particular component.
   *
   * @param comp The component to add
   * @param constraints The name by which the component can later be called
   * 
   * @exception IllegalArgumentException If `constraints' is not a
   * <code>String</code>
   */
  public void addLayoutComponent (Component comp, Object constraints)
  {
    if (! (constraints instanceof String))
      throw new IllegalArgumentException ("Object " + constraints
					  + " is not a string");
    addLayoutComponent ((String) constraints, comp);
  }

  /**
   * Add a new component to the layout.  The name can be used later
   * to refer to the component.
   * 
   * @param name The name by which the component can later be called
   * @param comp The component to add
   * 
   * @deprecated This method is deprecated in favor of
   * <code>addLayoutComponent(Component, Object)</code>.
   */
  public void addLayoutComponent (String name, Component comp)
  {
    tab.put (name, comp);
    // First component added is the default component.
    comp.setVisible(tab.size() == 1);
  }

  /**
   * Cause the first component in the container to be displayed.
   *
   * @param parent The parent container, not <code>null</code>.
   */
  public void first (Container parent)
  {
    gotoComponent (parent, FIRST);
  }

  /**
   * Return this layout manager's horizontal gap.
   *
   * @return the horizontal gap
   */
  public int getHgap ()
  {
    return hgap;
  }

  /**
   * Return this layout manager's x alignment.  This method always
   * returns Component.CENTER_ALIGNMENT.
   * 
   * @param parent Container using this layout manager instance
   *
   * @return the x-axis alignment
   */
  public float getLayoutAlignmentX (Container parent)
  {
    return Component.CENTER_ALIGNMENT;
  }

  /**
   * Returns this layout manager's y alignment.  This method always
   * returns Component.CENTER_ALIGNMENT.
   * 
   * @param parent Container using this layout manager instance
   *
   * @return the y-axis alignment
   */
  public float getLayoutAlignmentY (Container parent)
  {
    return Component.CENTER_ALIGNMENT;
  }

  /**
   * Return this layout manager's vertical gap.
   *
   * @return the vertical gap
   */
  public int getVgap ()
  {
    return vgap;
  }

  /**
   * Invalidate this layout manager's state.
   */
  public void invalidateLayout (Container target)
  {
    // Do nothing.
  }

  /**
   * Cause the last component in the container to be displayed.
   * 
   * @param parent The parent container, not <code>null</code>.
   */
  public void last (Container parent)
  {
    gotoComponent (parent, LAST);
  }

  /**
   * Lays out the container.  This is done by resizing the child components
   * to be the same size as the parent, less insets and gaps.
   *
   * @param parent The parent container.
   */ 
  public void layoutContainer (Container parent)
  {
    synchronized (parent.getTreeLock ())
      {
	int width = parent.width;
	int height = parent.height;

	Insets ins = parent.getInsets ();

	int num = parent.ncomponents;
	Component[] comps = parent.component;

	int x = ins.left + hgap;
	int y = ins.top + vgap;
	width = width - 2 * hgap - ins.left - ins.right;
	height = height - 2 * vgap - ins.top - ins.bottom;

	for (int i = 0; i < num; ++i)
	  comps[i].setBounds (x, y, width, height);
      }
  }

  /**
   * Get the maximum layout size of the container.
   * 
   * @param target The parent container
   *
   * @return the maximum layout size
   */
  public Dimension maximumLayoutSize (Container target)
  {
    if (target == null)
      return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
    // The JCL says that this returns Integer.MAX_VALUE for both
    // dimensions.  But that just seems wrong to me.
    return getSize (target, MAX);
  }

  /**
   * Get the minimum layout size of the container.
   * 
   * @param target The parent container
   *
   * @return the minimum layout size
   */
  public Dimension minimumLayoutSize (Container target)
  {
    return getSize (target, MIN);
  }

  /**
   * Cause the next component in the container to be displayed.  If
   * this current card is the  last one in the deck, the first
   * component is displayed.
   * 
   * @param parent The parent container, not <code>null</code>.
   */
  public void next (Container parent)
  {
    gotoComponent (parent, NEXT);
  }

  /**
   * Get the preferred layout size of the container.
   * 
   * @param parent The parent container
   *
   * @return the preferred layout size
   */
  public Dimension preferredLayoutSize (Container parent)
  {
    return getSize (parent, PREF);
  }

  /**
   * Cause the previous component in the container to be displayed.
   * If this current card is the first one in the deck, the last
   * component is displayed.
   * 
   * @param parent The parent container, not <code>null</code>.
   */
  public void previous (Container parent)
  {
    gotoComponent (parent, PREV);
  }

  /**
   * Remove the indicated component from this layout manager.
   * 
   * @param comp The component to remove
   */
  public void removeLayoutComponent (Component comp)
  {
    Enumeration e = tab.keys ();
    while (e.hasMoreElements ())
      {
	Object key = e.nextElement ();
	if (tab.get (key) == comp)
	  {
	    tab.remove (key);
	    Container parent = comp.getParent();
	    next(parent);
	    break;
	  }
      }
  }

  /**
   * Set this layout manager's horizontal gap.
   * 
   * @param hgap The new gap
   */
  public void setHgap (int hgap)
  {
    this.hgap = hgap;
  }

  /**
   * Set this layout manager's vertical gap.
   * 
   * @param vgap The new gap
   */
  public void setVgap (int vgap)
  {
    this.vgap = vgap;
  }

  /**
   * Cause the named component to be shown.  If the component name is
   * unknown or <code>null</code>, this method does nothing.
   * 
   * @param parent The parent container, not <code>null</code>.
   * @param name The name of the component to show 
   */
  public void show (Container parent, String name)
  {
    if (name == null)
      return;
   
    if (parent.getLayout() != this)
      throw new IllegalArgumentException("parent's layout is not this CardLayout");
    
    Object target = tab.get (name);
    if (target != null)
      {
	int num = parent.ncomponents;
	// This is more efficient than calling getComponents().
	Component[] comps = parent.component;
	for (int i = 0; i < num; ++i)
	  {
	    if (comps[i].isVisible())
	      {
		if (target == comps[i])
		  return;
		comps[i].setVisible (false);
	      }
	  }
	((Component) target).setVisible (true);
        parent.validate();
      }
  }

  /**
   * Returns a string representation of this layout manager.
   *
   * @return A string representation of this object.
   */
  public String toString ()
  {
    return getClass ().getName () + "[hgap=" + hgap + ",vgap=" + vgap + "]";
  }

  /**
   * This implements first(), last(), next(), and previous().
   * 
   * @param parent The parent container
   * @param what The type of goto: FIRST, LAST, NEXT or PREV
   * 
   * @throws IllegalArgumentException if parent has not this 
   * CardLayout set as its layout.
   */
  private void gotoComponent (Container parent, int what)
  {
    if (parent.getLayout() != this)
      throw new IllegalArgumentException("parent's layout is not this CardLayout");
    
    synchronized (parent.getTreeLock ())
      {
	int num = parent.ncomponents;
	// This is more efficient than calling getComponents().
	Component[] comps = parent.component;

	if (num == 1)
	  {
	    comps[0].setVisible(true);
	    return;
	  }

	int choice = -1;

	if (what == FIRST)
	  choice = 0;
	else if (what == LAST)
	  choice = num - 1;

	for (int i = 0; i < num; ++i)
	  {
	    if (comps[i].isVisible ())
	      {
		if (choice == i)
		  {
		    // Do nothing if we're already looking at the right
                    // component.
                    return;
		  }
		else if (what == PREV)
		  {
		    choice = i - 1;
		    if (choice < 0)
		      choice = num - 1;
		  }
		else if (what == NEXT)
		  {
                    choice = i + 1;
                    if (choice == num)
                      choice = 0;
		  }
		comps[i].setVisible (false);
 
		if (choice >= 0)
		  break;
	      } else 
                {
                  comps[i].setVisible(true);
                }
	  }

	if (choice >= 0 && choice < num)
	  comps[choice].setVisible (true);
      }
  }

  // Compute the size according to WHAT.
  private Dimension getSize (Container parent, int what)
  {
    synchronized (parent.getTreeLock ())
      {
	int w = 0, h = 0, num = parent.ncomponents;
	Component[] comps = parent.component;

	for (int i = 0; i < num; ++i)
	  {
	    Dimension d;

	    if (what == MIN)
	      d = comps[i].getMinimumSize ();
	    else if (what == MAX)
	      d = comps[i].getMaximumSize ();
	    else
	      d = comps[i].getPreferredSize ();

	    w = Math.max (d.width, w);
	    h = Math.max (d.height, h);
	  }

	Insets i = parent.getInsets ();
	w += 2 * hgap + i.right + i.left;
	h += 2 * vgap + i.bottom + i.top;

	// Handle overflow.
	if (w < 0)
	  w = Integer.MAX_VALUE;
	if (h < 0)
	  h = Integer.MAX_VALUE;

	return new Dimension (w, h);
      }
  }

  /**
   * @serial Horizontal gap value.
   */
  private int hgap;

  /**
   * @serial Vertical gap value.
   */
  private int vgap;

  /**
   * @serial Table of named components.
   */
  private Hashtable tab;

  // These constants are used by the private gotoComponent method.
  private static final int FIRST = 0;
  private static final int LAST = 1;
  private static final int NEXT = 2;
  private static final int PREV = 3;

  // These constants are used by the private getSize method.
  private static final int MIN = 0;
  private static final int MAX = 1;
  private static final int PREF = 2;
}