001: /*
002: * GridBagLayoutMapping.java
003: *
004: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.awt;
010:
011: import java.awt.*;
012: import java.util.*;
013:
014: /**
015: * GridBagLayout mapping of <a href="/pnuts/doc/hierarchicalLayout.html">Hierarchical Layout</a>.
016: *
017: * @author Toyokazu Tomatsu
018: * @author Nathan Sweet (misc@n4te.com)
019: */
020: public class GridBagLayoutMapping extends Layout {
021:
022: public Container createContainer(Container container,
023: Object[] format) {
024: GridBagLayout lm = new GridBagLayout();
025: container.setLayout(lm);
026:
027: for (int i = 1; i < format.length; i++) {
028: GridBagConstraints c = new GridBagConstraints();
029: if (!(format[i] instanceof Object[])) {
030: throw new LayoutException(
031: "GridBagLayout requires elements to be an array.");
032: }
033: Object a[] = (Object[]) format[i];
034: if (!(a[0] instanceof String)) {
035: throw new LayoutException(
036: "GridBagLayout requires the first element to be a String.");
037: }
038: Map table = PnutsLayout.str2table((String) a[0]);
039: String g;
040: if ((g = (String) table.get("gridx")) != null) {
041: c.gridx = Integer.parseInt(g);
042: }
043: if ((g = (String) table.get("gridy")) != null) {
044: c.gridy = Integer.parseInt(g);
045: }
046: if ((g = (String) table.get("gridwidth")) != null) {
047: c.gridwidth = Integer.parseInt(g);
048: }
049: if ((g = (String) table.get("gridheight")) != null) {
050: c.gridheight = Integer.parseInt(g);
051: }
052: if ((g = (String) table.get("weightx")) != null) {
053: c.weightx = Double.parseDouble(g);
054: }
055: if ((g = (String) table.get("weighty")) != null) {
056: c.weighty = Double.parseDouble(g);
057: }
058: if ((g = (String) table.get("anchor")) != null) {
059: try {
060: c.anchor = ((Integer) GridBagConstraints.class
061: .getField(g).get(null)).intValue();
062: } catch (Exception e) {
063: }
064: }
065: if ((g = (String) table.get("fill")) != null) {
066: try {
067: c.fill = ((Integer) GridBagConstraints.class
068: .getField(g).get(null)).intValue();
069: } catch (Exception e) {
070: }
071: }
072: if ((g = (String) table.get("insets")) != null) {
073: StringTokenizer st = new StringTokenizer(g, ":");
074: c.insets = new Insets(Integer.parseInt(st.nextToken()),
075: Integer.parseInt(st.nextToken()), Integer
076: .parseInt(st.nextToken()), Integer
077: .parseInt(st.nextToken()));
078: }
079: if ((g = (String) table.get("ipadx")) != null) {
080: c.ipadx = Integer.parseInt(g);
081: }
082: if ((g = (String) table.get("ipady")) != null) {
083: c.ipady = Integer.parseInt(g);
084: }
085: Component comp = null;
086: if (a[1] instanceof Object[]) {
087: comp = Layout.layout(makePanel(container),
088: (Object[]) a[1]);
089: } else if (a[1] instanceof Component) {
090: comp = (Component) a[1];
091: } else {
092: throw new LayoutException(
093: "GridBagLayout requires the second element to be a Component or array.");
094: }
095: lm.setConstraints(comp, c);
096: container.add(comp);
097: if (a.length > 2) {
098: if (!(comp instanceof Container))
099: throw new LayoutException(
100: "Component must be a Container when a third element is used: "
101: + a[0]);
102: if (!(a[2] instanceof Object[]))
103: throw new LayoutException(
104: "Third element must be an array: " + a[2]);
105: Layout.layout((Container) comp, (Object[]) a[2]);
106: }
107: }
108: return container;
109: }
110: }
|