CardLayout.java 10.8 KB
Newer Older
Tom Tromey committed
1 2
// CardLayout.java - Card-based layout engine

3
/* Copyright (C) 1999, 2000, 2002, 2003  Free Software Foundation
Tom Tromey committed
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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. */
Tom Tromey committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51


package java.awt;

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

/** 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>
52
 * @author Aaron M. Renn (arenn@urbanophile.com)
Tom Tromey committed
53 54 55
 */
public class CardLayout implements LayoutManager2, Serializable
{
56 57
  static final long serialVersionUID = -4328196481005934313L;

58 59 60 61
  /**
   * Initializes a new instance of <code>CardLayout</code> with horizontal
   * and vertical gaps of 0.
   */
Tom Tromey committed
62 63 64 65 66
  public CardLayout ()
  {
    this (0, 0);
  }

67 68 69
  /**
   * Create a new <code>CardLayout</code> object with the specified
   * horizontal and vertical gaps.
Tom Tromey committed
70 71 72 73 74 75 76
   * @param hgap The horizontal gap
   * @param vgap The vertical gap
   */
  public CardLayout (int hgap, int vgap)
  {
    this.hgap = hgap;
    this.vgap = vgap;
77
    this.tab = new Hashtable ();
Tom Tromey committed
78 79 80 81 82 83 84
  }

  /** 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
85 86
   * @exception IllegalArgumentException If `constraints' is not a
   * <code>String</code>
Tom Tromey committed
87 88 89 90 91 92
   */
  public void addLayoutComponent (Component comp, Object constraints)
  {
    if (! (constraints instanceof String))
      throw new IllegalArgumentException ("Object " + constraints
					  + " is not a string");
93
    tab.put (constraints, comp);
Tom Tromey committed
94 95 96 97 98 99
  }

  /** 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
100 101
   * @deprecated This method is deprecated in favor of
   * <code>addLayoutComponent(Component, Object)</code>.
Tom Tromey committed
102 103 104 105 106 107 108 109 110 111 112
   */
  public void addLayoutComponent (String name, Component comp)
  {
    addLayoutComponent (comp, name);
  }

  /** Cause the first component in the container to be displayed.
   * @param parent The parent container
   */
  public void first (Container parent)
  {
113
    gotoComponent (parent, FIRST);
Tom Tromey committed
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
  }

  /** Return this layout manager's 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
   */
  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
   */
  public float getLayoutAlignmentY (Container parent)
  {
    return Component.CENTER_ALIGNMENT;
  }

  /** Return this layout manager's 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
   */
  public void last (Container parent)
  {
157
    gotoComponent (parent, LAST);
Tom Tromey committed
158 159
  }

160 161 162 163 164 165
  /**
   * 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.
   */ 
Tom Tromey committed
166 167
  public void layoutContainer (Container parent)
  {
168 169 170 171
    synchronized (parent.getTreeLock ())
      {
	int width = parent.width;
	int height = parent.height;
Tom Tromey committed
172

173
	Insets ins = parent.getInsets ();
Tom Tromey committed
174

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

178 179 180 181
	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;
182

183 184 185
	for (int i = 0; i < num; ++i)
	  comps[i].setBounds (x, y, width, height);
      }
Tom Tromey committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  }

  /** Get the maximum layout size of the container.
   * @param target The parent container
   */
  public Dimension maximumLayoutSize (Container target)
  {
    // 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
   */
  public Dimension minimumLayoutSize (Container target)
  {
    return getSize (target, MIN);
  }

206 207 208
  /** 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.
Tom Tromey committed
209 210 211 212
   * @param parent The parent container
   */
  public void next (Container parent)
  {
213
    gotoComponent (parent, NEXT);
Tom Tromey committed
214 215 216 217 218 219 220 221 222 223 224
  }

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

  /** Cause the previous component in the container to be displayed.
225 226
   * If this current card is the first one in the deck, the last
   * component is displayed.
Tom Tromey committed
227 228 229 230
   * @param parent The parent container
   */
  public void previous (Container parent)
  {
231
    gotoComponent (parent, PREV);
Tom Tromey committed
232 233 234 235 236 237 238
  }

  /** Remove the indicated component from this layout manager.
   * @param comp The component to remove
   */
  public void removeLayoutComponent (Component comp)
  {
239
    Enumeration e = tab.keys ();
Tom Tromey committed
240 241 242
    while (e.hasMoreElements ())
      {
	Object key = e.nextElement ();
243
	if (tab.get (key) == comp)
Tom Tromey committed
244
	  {
245
	    tab.remove (key);
Tom Tromey committed
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
	    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, this method does nothing.
   * @param parent The parent container
   * @param name The name of the component to show
   */
  public void show (Container parent, String name)
  {
274
    Object target = tab.get (name);
Tom Tromey committed
275
    if (target != null)
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
      {
	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);
      }
Tom Tromey committed
291 292
  }

293 294 295 296 297
  /**
   * Returns a string representation of this layout manager.
   *
   * @return A string representation of this object.
   */
Tom Tromey committed
298 299 300 301 302
  public String toString ()
  {
    return getClass ().getName () + "[" + hgap + "," + vgap + "]";
  }

303 304 305 306 307
  /** This implements first(), last(), next(), and previous().
   * @param parent The parent container
   * @param what The type of goto: FIRST, LAST, NEXT or PREV
   */
  private void gotoComponent (Container parent, int what)
Tom Tromey committed
308
  {
309
    synchronized (parent.getTreeLock ())
Tom Tromey committed
310
      {
311 312 313 314 315 316 317 318 319 320 321
	int num = parent.ncomponents;
	// This is more efficient than calling getComponents().
	Component[] comps = parent.component;
	int choice = -1;

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

	for (int i = 0; i < num; ++i)
Tom Tromey committed
322
	  {
323
	    if (comps[i].isVisible ())
Tom Tromey committed
324
	      {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		if (what == NEXT)
		  {
		    choice = i + 1;
		    if (choice == num)
		      choice = 0;
		  }
		else if (what == PREV)
		  {
		    choice = i - 1;
		    if (choice < 0)
		      choice = num - 1;
		  }
		else if (choice == i)
		  {
		    // Do nothing if we're already looking at the right
		    // component.
		    return;
		  }
		comps[i].setVisible (false);
344
 
345 346
		if (choice >= 0)
		  break;
Tom Tromey committed
347 348 349
	      }
	  }

350 351 352
	if (choice >= 0 && choice < num)
	  comps[choice].setVisible (true);
      }
Tom Tromey committed
353 354 355 356 357
  }

  // Compute the size according to WHAT.
  private Dimension getSize (Container parent, int what)
  {
358
    synchronized (parent.getTreeLock ())
Tom Tromey committed
359
      {
360 361 362 363 364 365
	int w = 0, h = 0, num = parent.ncomponents;
	Component[] comps = parent.component;

	for (int i = 0; i < num; ++i)
	  {
	    Dimension d;
Tom Tromey committed
366

367 368 369 370 371 372
	    if (what == MIN)
	      d = comps[i].getMinimumSize ();
	    else if (what == MAX)
	      d = comps[i].getMaximumSize ();
	    else
	      d = comps[i].getPreferredSize ();
Tom Tromey committed
373

374 375 376
	    w = Math.max (d.width, w);
	    h = Math.max (d.height, h);
	  }
Tom Tromey committed
377

378 379 380
	Insets i = parent.getInsets ();
	w += 2 * hgap + i.right + i.left;
	h += 2 * vgap + i.bottom + i.top;
Tom Tromey committed
381

382 383 384 385 386
	// Handle overflow.
	if (w < 0)
	  w = Integer.MAX_VALUE;
	if (h < 0)
	  h = Integer.MAX_VALUE;
Tom Tromey committed
387

388 389
	return new Dimension (w, h);
      }
Tom Tromey committed
390 391
  }

392 393 394
  /**
   * @serial Horizontal gap value.
   */
Tom Tromey committed
395
  private int hgap;
396 397 398 399

  /**
   * @serial Vertical gap value.
   */
Tom Tromey committed
400 401
  private int vgap;

402 403 404 405
  /**
   * @serial Table of named components.
   */
  private Hashtable tab;
Tom Tromey committed
406 407 408 409 410 411 412 413 414 415 416 417

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

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