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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
/* CardLayout.java -- Layout manager for a card stack
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of the non-peer AWT libraries of GNU Classpath.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published
by the Free Software Foundation, either version 2 of the License, or
(at your option) any later verion.
This library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA. */
package java.awt;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* This class is a layout manager that treats components as cards in
* a stack. Only one is visible at a time.
*
* @author Aaron M. Renn (arenn@urbanophile.com)
*/
public class CardLayout implements LayoutManager2, java.io.Serializable
{
/*
* Static Variables
*/
// Layout size constants
private static final int PREF = 0;
private static final int MAX = 1;
private static final int MIN = 2;
// Serialization constant
private static final long serialVersionUID = -4328196481005934313L;
/*************************************************************************/
/*
* Instance Variables
*/
/**
* @serial Horitzontal gap value.
*/
private int hgap;
/**
* @serial Vertical gap value.
*/
private int vgap;
/**
* @serial Table of named components.
*/
private Hashtable tab = new Hashtable();
/*************************************************************************/
/*
* Constructors
*/
/**
* Initializes a new instance of <code>CardLayout</code> with horizontal
* and vertical gaps of 0.
*/
public
CardLayout()
{
this(0,0);
}
/*************************************************************************/
/**
* Initializes a new instance of <code>CardLayout</code> 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;
}
/*************************************************************************/
/*
* Instance Variables
*/
/**
* Returns the horitzontal gap value.
*
* @return The horitzontal gap value.
*/
public int
getHgap()
{
return(hgap);
}
/*************************************************************************/
/**
* Sets the horizontal gap to the specified value.
*
* @param hgap The new horizontal gap.
*/
public void
setHgap(int hgap)
{
this.hgap = hgap;
}
/*************************************************************************/
/**
* Returns the vertical gap value.
*
* @return The vertical gap value.
*/
public int
getVgap()
{
return(vgap);
}
/*************************************************************************/
/**
* Sets the vertical gap to the specified value.
*
* @param vgap The new vertical gap value.
*/
public void
setVgap(int vgap)
{
this.vgap = vgap;
}
/*************************************************************************/
/**
* Adds the specified component to this object's table of components.
* Doing this allows random access by name.
*
* @param component The component to add.
* @param name The name of the component, which must be a <code>String</code>.
*
* @exception IllegalArgumentException If the name object is not a
* <code>String</code>.
*/
public void
addLayoutComponent(Component component, Object name)
{
if (!(name instanceof String))
throw new IllegalArgumentException("Name must a string");
tab.put(name, component);
}
/*************************************************************************/
/**
* Adds the specified component to this object's table of components.
* Doing this allows random access by name.
*
* @param name The name of the component.
* @param component The component to add.
*
* @deprecated This method is deprecated in favor of
* <code>addLayoutComponent(Component, Object)</code>.
*/
public void
addLayoutComponent(String name, Component component)
{
addLayoutComponent(component, name);
}
/*************************************************************************/
/**
* Removes the specified component from the table of internal names.
*
* @param Component The component to remove.
*/
public void
removeLayoutComponent(Component component)
{
Enumeration keys = tab.keys();
while (keys.hasMoreElements())
{
String name = (String)keys.nextElement();
Object obj = tab.get(name);
if (obj == component)
{
tab.remove(name);
return;
}
}
}
/*************************************************************************/
// Internal layout size calculator
private Dimension
layoutSize(Container parent, int type)
{
int width = 0, height = 0;
Component[] clist = parent.getComponents();
if (clist.length > 0)
for (int i = 0; i < clist.length; i++)
{
Component comp = clist[i];
Dimension dim = null;
if (type == PREF)
dim = comp.getPreferredSize();
else if (type == MAX)
dim = comp.getMaximumSize();
else if (type == MIN)
dim = comp.getMinimumSize();
if (dim.width > width)
width = dim.width;
if (dim.height > height)
width = dim.height;
}
Insets ins = parent.getInsets();
width += (ins.left + ins.right);
height += (ins.top + ins.bottom);
return(new Dimension(width, height));
}
/*************************************************************************/
/**
* Returns the preferred size of the container for supporting this
* layout.
*
* @param parent The parent container.
*/
public Dimension
preferredLayoutSize(Container parent)
{
return(layoutSize(parent, PREF));
}
/*************************************************************************/
/**
* Returns the minimum size of the container for supporting this
* layout.
*
* @param parent The parent container.
*/
public Dimension
minimumLayoutSize(Container parent)
{
return(layoutSize(parent, MIN));
}
/*************************************************************************/
/**
* Returns the maximum size of the container for supporting this
* layout.
*
* @param parent The parent container.
*/
public Dimension
maximumLayoutSize(Container parent)
{
return(layoutSize(parent, MAX));
}
/*************************************************************************/
/**
* Returns the X axis alignment, which is a <code>float</code> indicating
* where along the X axis this container wishs to position its layout.
* 0 indicates align to the left, 1 indicates align to the right, and 0.5
* indicates align to the center.
*
* @param parent The parent container.
*
* @return The X alignment value.
*/
public float
getLayoutAlignmentX(Container parent)
{
return(parent.getAlignmentX());
}
/*************************************************************************/
/**
* Returns the Y axis alignment, which is a <code>float</code> indicating
* where along the Y axis this container wishs to position its layout.
* 0 indicates align to the top, 1 indicates align to the bottom, and 0.5
* indicates align to the center.
*
* @param parent The parent container.
*
* @return The Y alignment value.
*/
public float
getLayoutAlignmentY(Container parent)
{
return(parent.getAlignmentY());
}
/*************************************************************************/
/**
* Instructs this object to discard any layout information it might
* have cached.
*
* @param parent The parent container.
*/
public void
invalidateLayout(Container parent)
{
}
/*************************************************************************/
/**
* Goes to the first card in the container.
*
* @param parent The parent container.
*/
public void
first(Container parent)
{
Component[] clist = parent.getComponents();
if (clist.length > 0)
{
for (int i = 0; i < clist.length; i++)
clist[i].setVisible(false);
clist[0].setVisible(true);
}
}
/*************************************************************************/
/**
* Goes to the last card in the container.
*
* @param parent The parent container.
*/
public void
last(Container parent)
{
Component[] clist = parent.getComponents();
if (clist.length > 0)
{
for (int i = 0; i < clist.length; i++)
clist[i].setVisible(false);
clist[clist.length-1].setVisible(true);
}
}
/*************************************************************************/
/**
* Goes to the next card in the container. If this current card is the
* last one in the deck, the first component is displayed.
*
* @param parent The parent container.
*/
public void
next(Container parent)
{
Component[] clist = parent.getComponents();
if (clist.length > 0)
{
for (int i = 0; i < clist.length; i++)
{
if (clist[i].isVisible())
{
clist[i].setVisible(false);
if ((i + 1) == clist.length)
clist[0].setVisible(true);
else
clist[i+1].setVisible(true);
break;
}
}
}
}
/*************************************************************************/
/**
* Goes to the next card in the container. If this current card is the
* first one in the deck, the last component is displayed.
*
* @param parent The parent container.
*/
public void
previous(Container parent)
{
Component[] clist = parent.getComponents();
if (clist.length > 0)
{
for (int i = 0; i < clist.length; i++)
{
if (clist[i].isVisible())
{
clist[i].setVisible(false);
if (i == 0)
clist[clist.length-1].setVisible(true);
else
clist[i-1].setVisible(true);
break;
}
}
}
}
/*************************************************************************/
/**
* Displays the specified component that was previous added by name
* using the <code>addLayoutComponent()</code> method. If the named
* component doesn't exist, this method returns silently.
*
* @param parent The parent container.
* @param name The name of the component to display.
*/
public void
show(Container parent, String name)
{
Component comp = (Component)tab.get(name);
if (comp == null)
return;
Component[] clist = parent.getComponents();
if (clist.length > 0)
{
for (int i = 0; i < clist.length; i++)
{
if (clist[i] == comp)
continue;
if (clist[i].isVisible())
clist[i].setVisible(false);
}
}
}
/*************************************************************************/
/**
* 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)
{
Insets ins = parent.getInsets();
Dimension dim = parent.getSize();
Component[] clist = parent.getComponents();
if (clist.length > 0)
for (int i = 0; i < clist.length; i++)
{
int x = ins.left + hgap;
int y = ins.top + vgap;
int width = dim.width - (ins.left + ins.right + hgap);
int height = dim.height - (ins.top + ins.bottom + vgap);
clist[i].setLocation(x, y);
clist[i].setSize(width, height);
}
}
/*************************************************************************/
/**
* Returns a string representation of this layout manager.
*
* @return A string representation of this object.
*/
public String
toString()
{
return(getClass().getName());
}
} // class CardLayout
|