01: /*
02: * CardLayoutMapping.java
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.awt;
10:
11: import java.awt.*;
12:
13: /**
14: * CardLayout mapping of <a href="/pnuts/doc/hierarchicalLayout.html">Hierarchical Layout</a>.
15: *
16: * @author Toyokazu Tomatsu
17: * @author Nathan Sweet (misc@n4te.com)
18: */
19: public class CardLayoutMapping extends Layout {
20:
21: public Container createContainer(Container container,
22: Object[] format) {
23: if (!(format[1] instanceof Object[])) {
24: throw new LayoutException(
25: "Element after the CardLayout class must be an array of constructor arguments.");
26: }
27: Object args[] = (Object[]) format[1];
28: CardLayout lm = null;
29: if (args.length == 0) {
30: lm = new CardLayout();
31: } else if (args.length == 2) {
32: lm = new CardLayout(((Integer) args[0]).intValue(),
33: ((Integer) args[1]).intValue());
34: }
35: container.setLayout(lm);
36: for (int i = 2; i < format.length; i++) {
37: if (!(format[i] instanceof Object[]))
38: throw new LayoutException(
39: "CardLayout requires all elements to be an array.");
40: Object[] a = (Object[]) format[i];
41: if (!(a[0] instanceof String))
42: throw new LayoutException(
43: "CardLayout requires the first element to be a String.");
44: Component compToAdd;
45: if (a[1] == null) {
46: compToAdd = makePanel(container);
47: } else if (isArray(a[1])) {
48: compToAdd = Layout.layout(makePanel(container),
49: (Object[]) a[1]);
50: } else if (a[1] instanceof Component) {
51: compToAdd = (Component) a[1];
52: } else {
53: throw new LayoutException(
54: "CardLayout requires the second element to be a Component, array, or null.");
55: }
56: container.add((String) a[0], compToAdd);
57: if (a.length > 2) {
58: if (!(compToAdd instanceof Container))
59: throw new LayoutException(
60: "Component must be a Container when a third element is used: "
61: + a[0]);
62: if (!(a[2] instanceof Object[]))
63: throw new LayoutException(
64: "Third element must be an array: " + a[2]);
65: Layout.layout((Container) compToAdd, (Object[]) a[2]);
66: }
67: }
68: return container;
69: }
70: }
|