package ie.dcu.swt.layout; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; /** * Port of the AWT BorderLayout. * * @author Kevin McGuinness */ public class BorderLayout extends Layout { // Typedef indices private static final int N = BorderData.North.index; private static final int S = BorderData.South.index; private static final int C = BorderData.Center.index; private static final int E = BorderData.East.index; private static final int W = BorderData.West.index; // Controls in all the regions. private Control[] controls = new Control[5]; // Cached sizes. private Point[] sizes; // Preferred width and height int width, height; // Spacing between components int hgap, vgap; public BorderLayout() { this(0); } public BorderLayout(int space) { hgap = vgap = space; } public BorderLayout(int hgap, int vgap) { this.hgap = hgap; this.vgap = vgap; } /* * (non-Javadoc) * * @see * org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite * , int, int, boolean) */ protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { if (sizes == null || flushCache == true) { refreshSizes(composite.getChildren()); } int w = wHint; int h = hHint; if (w == SWT.DEFAULT) w = width; if (h == SWT.DEFAULT) h = height; return new Point(w, h); } /* * (non-Javadoc) * * @see * org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite, * boolean) */ protected void layout(Composite composite, boolean flushCache) { if (flushCache || sizes == null) { refreshSizes(composite.getChildren()); } Rectangle r = composite.getClientArea(); int tg = 0; int bg = 0; int lg = 0; int rg = 0; if (controls[N] != null) { int x = r.x; int y = r.y; int w = r.width; int h = sizes[N].y; controls[N].setBounds(x, y, w, h); tg += hgap; } if (controls[S] != null) { int x = r.x; int y = r.y + r.height - sizes[S].y; int w = r.width; int h = sizes[S].y; controls[S].setBounds(x, y, w, h); bg += hgap; } if (controls[W] != null) { int x = r.x; int y = r.y + sizes[N].y + tg; int w = sizes[W].x; int h = r.height - sizes[N].y - sizes[S].y - (tg + bg); controls[W].setBounds(x, y, w, h); lg += vgap; } if (controls[E] != null) { int x = r.x + r.width - sizes[E].x; int y = r.y + sizes[N].y + tg; int w = sizes[E].x; int h = r.height - sizes[N].y - sizes[S].y - (tg + bg); controls[E].setBounds(x, y, w, h); rg += vgap; } if (controls[C] != null) { int x = r.x + sizes[W].x + lg; int y = r.y + sizes[N].y + tg; int w = r.width - sizes[W].x - sizes[E].x - (lg + rg); int h = r.height - sizes[N].y - sizes[S].y - (tg + bg); controls[C].setBounds(x, y, w, h); } return; } private void refreshSizes(Control[] children) { for (Control c : children) { Object o = c.getLayoutData(); if (o instanceof BorderData) { BorderData data = (BorderData) o; if (data != BorderData.Ignore) { controls[data.index] = c; } } else { // Default controls[C] = c; } } width = height = 0; if (sizes == null) { sizes = new Point[5]; } for (int i = 0; i < controls.length; i++) { Control control = controls[i]; if (control == null) { sizes[i] = new Point(0, 0); } else { sizes[i] = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); } } width = Math.max(width, sizes[N].x); width = Math.max(width, sizes[W].x + sizes[C].x + sizes[E].x); width = Math.max(width, sizes[S].x); height = Math.max(Math.max(sizes[W].y, sizes[E].y), sizes[C].y) + sizes[N].y + sizes[S].y; return; } }