01: /*
02: * BorderLayoutMapping.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: * BorderLayout mapping of <a href="/pnuts/doc/hierarchicalLayout.html">Hierarchical Layout</a>.
15: *
16: * @version 1.1
17: * @author Toyokazu Tomatsu
18: * @author Nathan Sweet (misc@n4te.com)
19: */
20: public class BorderLayoutMapping extends Layout {
21:
22: public Container createContainer(Container container,
23: Object[] format) {
24: if (!(format[1] instanceof Object[])) {
25: throw new LayoutException(
26: "Element after the BorderLayout class must be an array of constructor arguments.");
27: }
28: Object args[] = (Object[]) format[1];
29: BorderLayout lm = null;
30: if (args.length == 0) {
31: lm = new BorderLayout();
32: } else if (args.length == 2) {
33: lm = new BorderLayout(((Integer) args[0]).intValue(),
34: ((Integer) args[1]).intValue());
35: }
36: container.setLayout(lm);
37: for (int i = 2; i < format.length; i++) {
38: if (!(format[i] instanceof Object[]))
39: throw new LayoutException(
40: "BorderLayout requires all elements to be an array.");
41: Object[] a = (Object[]) format[i];
42: if (!(a[0] instanceof String))
43: throw new LayoutException(
44: "BorderLayout requires the first element to be a String.");
45: Component compToAdd;
46: if (a[1] == null) {
47: compToAdd = makePanel(container);
48: } else if (isArray(a[1])) {
49: compToAdd = Layout.layout(makePanel(container),
50: (Object[]) a[1]);
51: } else if (a[1] instanceof Component) {
52: compToAdd = (Component) a[1];
53: } else {
54: throw new LayoutException(
55: "BorderLayout requires the second element to be a Component, array, or null.");
56: }
57: container.add((String) a[0], compToAdd);
58: if (a.length > 2) {
59: if (!(compToAdd instanceof Container))
60: throw new LayoutException(
61: "Component must be a Container when a third element is used: "
62: + a[0]);
63: if (!(a[2] instanceof Object[]))
64: throw new LayoutException(
65: "Third element must be an array: " + a[2]);
66: Layout.layout((Container) compToAdd, (Object[]) a[2]);
67: }
68: }
69: return container;
70: }
71: }
|