01: /*
02: * GridLayoutMapping.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: * GridLayout 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 GridLayoutMapping 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 GridLayout class must be an array of constructor arguments.");
26: }
27: Object args[] = (Object[]) format[1];
28: GridLayout lm = null;
29: if (args.length == 0) {
30: lm = new GridLayout();
31: } else if (args.length == 2) {
32: lm = new GridLayout(((Integer) args[0]).intValue(),
33: ((Integer) args[1]).intValue());
34: } else if (args.length == 4) {
35: lm = new GridLayout(((Integer) args[0]).intValue(),
36: ((Integer) args[1]).intValue(), ((Integer) args[2])
37: .intValue(), ((Integer) args[3]).intValue());
38: }
39: container.setLayout(lm);
40: for (int i = 2; i < format.length; i++) {
41: Object a = format[i];
42: if (a == null) {
43: container.add("", makePanel(container));
44: } else if (isArray(a)) {
45: Object nestedArray[] = (Object[]) format[i];
46: if (nestedArray[0] instanceof Class)
47: container.add("", Layout.layout(
48: makePanel(container), (Object[]) a));
49: else if (nestedArray[0] instanceof Container) {
50: if (!(nestedArray[1] instanceof Object[]))
51: throw new LayoutException(
52: "Second element must be an array: "
53: + nestedArray[1]);
54: Layout.layout((Container) nestedArray[0],
55: (Object[]) nestedArray[1]);
56: } else {
57: throw new LayoutException(
58: "GridLayout requires an array element to start with a Class or Container: "
59: + nestedArray[0]);
60: }
61: } else if (a instanceof Component) {
62: container.add("", (Component) a);
63: } else {
64: throw new LayoutException(
65: "GridLayout requires elements to be a Component, array, or null.");
66: }
67: }
68: return container;
69: }
70: }
|