fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / LayoutUtil.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
12
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;
18
19 public class LayoutUtil {
20
21         /**
22          * Calculates the number of columns needed by field editors
23          */
24         public static int getNumberOfColumns(DialogField[] editors) {
25                 int nCulumns = 0;
26                 for (int i = 0; i < editors.length; i++) {
27                         nCulumns = Math.max(editors[i].getNumberOfControls(), nCulumns);
28                 }
29                 return nCulumns;
30         }
31
32         /**
33          * Creates a composite and fills in the given editors.
34          * 
35          * @param labelOnTop
36          *            Defines if the label of all fields should be on top of the
37          *            fields
38          */
39         public static void doDefaultLayout(Composite parent, DialogField[] editors,
40                         boolean labelOnTop) {
41                 doDefaultLayout(parent, editors, labelOnTop, 0, 0, 0, 0);
42         }
43
44         /**
45          * Creates a composite and fills in the given editors.
46          * 
47          * @param labelOnTop
48          *            Defines if the label of all fields should be on top of the
49          *            fields
50          * @param minWidth
51          *            The minimal width of the composite
52          * @param minHeight
53          *            The minimal height of the composite
54          */
55         public static void doDefaultLayout(Composite parent, DialogField[] editors,
56                         boolean labelOnTop, int minWidth, int minHeight) {
57                 doDefaultLayout(parent, editors, labelOnTop, minWidth, minHeight, 0, 0);
58         }
59
60         /**
61          * Creates a composite and fills in the given editors.
62          * 
63          * @param labelOnTop
64          *            Defines if the label of all fields should be on top of the
65          *            fields
66          * @param minWidth
67          *            The minimal width of the composite
68          * @param minHeight
69          *            The minimal height of the composite
70          * @param marginWidth
71          *            The margin width to be used by the composite
72          * @param marginHeight
73          *            The margin height to be used by the composite
74          * @deprecated
75          */
76         public static void doDefaultLayout(Composite parent, DialogField[] editors,
77                         boolean labelOnTop, int minWidth, int minHeight, int marginWidth,
78                         int marginHeight) {
79                 int nCulumns = getNumberOfColumns(editors);
80                 Control[][] controls = new Control[editors.length][];
81                 for (int i = 0; i < editors.length; i++) {
82                         controls[i] = editors[i].doFillIntoGrid(parent, nCulumns);
83                 }
84                 if (labelOnTop) {
85                         nCulumns--;
86                         modifyLabelSpans(controls, nCulumns);
87                 }
88                 GridLayout layout = new GridLayout();
89                 if (marginWidth != SWT.DEFAULT) {
90                         layout.marginWidth = marginWidth;
91                 }
92                 if (marginHeight != SWT.DEFAULT) {
93                         layout.marginHeight = marginHeight;
94                 }
95                 layout.numColumns = nCulumns;
96                 parent.setLayout(layout);
97         }
98
99         private static void modifyLabelSpans(Control[][] controls, int nCulumns) {
100                 for (int i = 0; i < controls.length; i++) {
101                         setHorizontalSpan(controls[i][0], nCulumns);
102                 }
103         }
104
105         /**
106          * Sets the span of a control. Assumes that GridData is used.
107          */
108         public static void setHorizontalSpan(Control control, int span) {
109                 Object ld = control.getLayoutData();
110                 if (ld instanceof GridData) {
111                         ((GridData) ld).horizontalSpan = span;
112                 } else if (span != 1) {
113                         GridData gd = new GridData();
114                         gd.horizontalSpan = span;
115                         control.setLayoutData(gd);
116                 }
117         }
118
119         /**
120          * Sets the width hint of a control. Assumes that GridData is used.
121          */
122         public static void setWidthHint(Control control, int widthHint) {
123                 Object ld = control.getLayoutData();
124                 if (ld instanceof GridData) {
125                         ((GridData) ld).widthHint = widthHint;
126                 }
127         }
128
129         /**
130          * Sets the heigthHint hint of a control. Assumes that GridData is used.
131          */
132         public static void setHeigthHint(Control control, int heigthHint) {
133                 Object ld = control.getLayoutData();
134                 if (ld instanceof GridData) {
135                         ((GridData) ld).heightHint = heigthHint;
136                 }
137         }
138
139         /**
140          * Sets the horizontal indent of a control. Assumes that GridData is used.
141          */
142         public static void setHorizontalIndent(Control control, int horizontalIndent) {
143                 Object ld = control.getLayoutData();
144                 if (ld instanceof GridData) {
145                         ((GridData) ld).horizontalIndent = horizontalIndent;
146                 }
147         }
148
149         /**
150          * Sets the horizontal indent of a control. Assumes that GridData is used.
151          */
152         public static void setHorizontalGrabbing(Control control) {
153                 Object ld = control.getLayoutData();
154                 if (ld instanceof GridData) {
155                         ((GridData) ld).grabExcessHorizontalSpace = true;
156                 }
157         }
158
159 }