1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
19 public class LayoutUtil {
22 * Calculates the number of columns needed by field editors
24 public static int getNumberOfColumns(DialogField[] editors) {
26 for (int i= 0; i < editors.length; i++) {
27 nCulumns= Math.max(editors[i].getNumberOfControls(), nCulumns);
33 * Creates a composite and fills in the given editors.
34 * @param labelOnTop Defines if the label of all fields should be on top of the fields
36 public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop) {
37 doDefaultLayout(parent, editors, labelOnTop, 0, 0, 0, 0);
41 * Creates a composite and fills in the given editors.
42 * @param labelOnTop Defines if the label of all fields should be on top of the fields
43 * @param minWidth The minimal width of the composite
44 * @param minHeight The minimal height of the composite
46 public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop, int minWidth, int minHeight) {
47 doDefaultLayout(parent, editors, labelOnTop, minWidth, minHeight, 0, 0);
51 * Creates a composite and fills in the given editors.
52 * @param labelOnTop Defines if the label of all fields should be on top of the fields
53 * @param minWidth The minimal width of the composite
54 * @param minHeight The minimal height of the composite
55 * @param marginWidth The margin width to be used by the composite
56 * @param marginHeight The margin height to be used by the composite
59 public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop, int minWidth, int minHeight, int marginWidth, int marginHeight) {
60 int nCulumns= getNumberOfColumns(editors);
61 Control[][] controls= new Control[editors.length][];
62 for (int i= 0; i < editors.length; i++) {
63 controls[i]= editors[i].doFillIntoGrid(parent, nCulumns);
67 modifyLabelSpans(controls, nCulumns);
69 GridLayout layout= new GridLayout();
70 if (marginWidth != SWT.DEFAULT) {
71 layout.marginWidth= marginWidth;
73 if (marginHeight != SWT.DEFAULT) {
74 layout.marginHeight= marginHeight;
76 layout.numColumns= nCulumns;
77 parent.setLayout(layout);
80 private static void modifyLabelSpans(Control[][] controls, int nCulumns) {
81 for (int i= 0; i < controls.length; i++) {
82 setHorizontalSpan(controls[i][0], nCulumns);
87 * Sets the span of a control. Assumes that GridData is used.
89 public static void setHorizontalSpan(Control control, int span) {
90 Object ld= control.getLayoutData();
91 if (ld instanceof GridData) {
92 ((GridData)ld).horizontalSpan= span;
93 } else if (span != 1) {
94 GridData gd= new GridData();
95 gd.horizontalSpan= span;
96 control.setLayoutData(gd);
101 * Sets the width hint of a control. Assumes that GridData is used.
103 public static void setWidthHint(Control control, int widthHint) {
104 Object ld= control.getLayoutData();
105 if (ld instanceof GridData) {
106 ((GridData)ld).widthHint= widthHint;
111 * Sets the heigthHint hint of a control. Assumes that GridData is used.
113 public static void setHeigthHint(Control control, int heigthHint) {
114 Object ld= control.getLayoutData();
115 if (ld instanceof GridData) {
116 ((GridData)ld).heightHint= heigthHint;
121 * Sets the horizontal indent of a control. Assumes that GridData is used.
123 public static void setHorizontalIndent(Control control, int horizontalIndent) {
124 Object ld= control.getLayoutData();
125 if (ld instanceof GridData) {
126 ((GridData)ld).horizontalIndent= horizontalIndent;
131 * Sets the horizontal indent of a control. Assumes that GridData is used.
133 public static void setHorizontalGrabbing(Control control) {
134 Object ld= control.getLayoutData();
135 if (ld instanceof GridData) {
136 ((GridData)ld).grabExcessHorizontalSpace= true;