001: package fr.aliacom.form.swt.maker;
002:
003: import java.util.Iterator;
004:
005: import org.eclipse.swt.SWT;
006: import org.eclipse.swt.layout.GridData;
007: import org.eclipse.swt.layout.GridLayout;
008: import org.eclipse.swt.widgets.Composite;
009: import org.eclipse.swt.widgets.Control;
010: import org.w3c.dom.Element;
011:
012: import fr.aliacom.form.common.IFormComponent;
013: import fr.aliacom.form.common.FormLoader;
014: import fr.aliacom.form.swt.BuilderFactory;
015: import fr.aliacom.form.swt.SWTBuilder;
016: import fr.aliacom.form.swt.ui.SWTComposite;
017:
018: /**
019: * @author tom
020: *
021: * (C) 2001, 2002 Thomas Cataldo
022: */
023: public final class PanelBuilder extends SWTBuilder {
024:
025: /**
026: * @see fr.aliacom.form.swt.SWTBuilder#build(Element, IFormComponent, FormLoader, BuilderFactory)
027: */
028: public IFormComponent build(Element elem, IFormComponent parent,
029: FormLoader loader, BuilderFactory factory) {
030:
031: Composite c = new Composite((Composite) parent
032: .getNativeWidget(), SWT.NONE);
033: IFormComponent ret = new SWTComposite(c);
034: GridLayout layout = new GridLayout();
035: layout.numColumns = 3;
036: layout.makeColumnsEqualWidth = false;
037: int padding = (elem.hasAttribute("padding") ? Integer
038: .parseInt(elem.getAttribute("padding")) : 0);
039: layout.horizontalSpacing = padding;
040: layout.verticalSpacing = padding;
041: int border = (elem.hasAttribute("border") ? Integer
042: .parseInt(elem.getAttribute("border")) : 0);
043: layout.marginHeight = border;
044: layout.marginWidth = border;
045: c.setLayout(layout);
046:
047: Element north = null;
048: Element south = null;
049: Element east = null;
050: Element west = null;
051: Element center = null;
052:
053: Iterator it = new ElementIterator(elem.getChildNodes());
054: while (it.hasNext()) {
055: Element cur = (Element) it.next();
056: String constraint = cur.getAttribute("constraint");
057: if (constraint.length() == 0 || constraint.equals("Center")) {
058: center = cur;
059: } else if (constraint.equals("West")) {
060: west = cur;
061: } else if (constraint.equals("North")) {
062: north = cur;
063: } else if (constraint.equals("South")) {
064: south = cur;
065: } else {
066: east = cur;
067: }
068: }
069:
070: GridData centerData = new GridData(
071: GridData.HORIZONTAL_ALIGN_FILL
072: | GridData.VERTICAL_ALIGN_FILL);
073: centerData.horizontalSpan = 3;
074: centerData.grabExcessHorizontalSpace = true;
075: centerData.grabExcessVerticalSpace = true;
076:
077: Control ctrl;
078: Control centerCtrl;
079: if (north != null) {
080: GridData northData = new GridData(
081: GridData.HORIZONTAL_ALIGN_FILL);
082: northData.horizontalSpan = 3;
083: ctrl = buildChild(north, ret, loader, factory);
084: ctrl.setLayoutData(northData);
085: }
086: if (west != null) {
087: GridData westData = new GridData(
088: GridData.VERTICAL_ALIGN_FILL);
089: ctrl = buildChild(west, ret, loader, factory);
090: ctrl.setLayoutData(westData);
091: centerData.horizontalSpan--;
092: }
093: if (center != null) {
094: centerCtrl = buildChild(center, ret, loader, factory);
095: } else {
096: centerCtrl = new Composite(c, SWT.NONE);
097: centerData.heightHint = 0;
098: }
099: if (east != null) {
100: GridData eastData = new GridData(
101: GridData.VERTICAL_ALIGN_FILL);
102: ctrl = buildChild(east, ret, loader, factory);
103: ctrl.setLayoutData(eastData);
104: centerData.horizontalSpan--;
105: }
106: if (south != null) {
107: GridData southData = new GridData(
108: GridData.HORIZONTAL_ALIGN_FILL);
109: southData.horizontalSpan = 3;
110: ctrl = buildChild(south, ret, loader, factory);
111: ctrl.setLayoutData(southData);
112: }
113: centerCtrl.setLayoutData(centerData);
114:
115: c.pack();
116:
117: return ret;
118: }
119:
120: private Control buildChild(Element elem, IFormComponent panel,
121: FormLoader loader, BuilderFactory factory) {
122: SWTBuilder builder = factory.getBuilder(elem.getNodeName());
123: IFormComponent ret = builder
124: .build(elem, panel, loader, factory);
125: if (elem.hasAttribute("id")) {
126: loader.getCtx().addVariable(elem.getAttribute("id"), ret);
127: }
128:
129: return (Control) ret.getNativeWidget();
130: }
131:
132: }
|