Changes:
[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          * @param labelOnTop Defines if the label of all fields should be on top of the fields
35          */     
36         public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop) {
37                 doDefaultLayout(parent, editors, labelOnTop, 0, 0, 0, 0);
38         }
39
40         /**
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 
45          */
46         public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop, int minWidth, int minHeight) {
47                 doDefaultLayout(parent, editors, labelOnTop, minWidth, minHeight, 0, 0);
48         }
49
50         /**
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
57          * @deprecated
58          */     
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);
64                 }
65                 if (labelOnTop) {
66                         nCulumns--;
67                         modifyLabelSpans(controls, nCulumns);
68                 }
69                 GridLayout layout= new GridLayout();
70                 if (marginWidth != SWT.DEFAULT) {
71                         layout.marginWidth= marginWidth;
72                 }
73                 if (marginHeight != SWT.DEFAULT) {
74                         layout.marginHeight= marginHeight;
75                 }
76                 layout.numColumns= nCulumns;            
77                 parent.setLayout(layout);
78         }
79         
80         private static void modifyLabelSpans(Control[][] controls, int nCulumns) {
81                 for (int i= 0; i < controls.length; i++) {
82                         setHorizontalSpan(controls[i][0], nCulumns);
83                 }
84         }
85         
86         /**
87          * Sets the span of a control. Assumes that GridData is used.
88          */
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);
97                 }
98         }       
99
100         /**
101          * Sets the width hint of a control. Assumes that GridData is used.
102          */
103         public static void setWidthHint(Control control, int widthHint) {
104                 Object ld= control.getLayoutData();
105                 if (ld instanceof GridData) {
106                         ((GridData)ld).widthHint= widthHint;
107                 }
108         }
109         
110         /**
111          * Sets the heigthHint hint of a control. Assumes that GridData is used.
112          */
113         public static void setHeigthHint(Control control, int heigthHint) {
114                 Object ld= control.getLayoutData();
115                 if (ld instanceof GridData) {
116                         ((GridData)ld).heightHint= heigthHint;
117                 }
118         }       
119         
120         /**
121          * Sets the horizontal indent of a control. Assumes that GridData is used.
122          */
123         public static void setHorizontalIndent(Control control, int horizontalIndent) {
124                 Object ld= control.getLayoutData();
125                 if (ld instanceof GridData) {
126                         ((GridData)ld).horizontalIndent= horizontalIndent;
127                 }
128         }
129         
130         /**
131          * Sets the horizontal indent of a control. Assumes that GridData is used.
132          */
133         public static void setHorizontalGrabbing(Control control) {
134                 Object ld= control.getLayoutData();
135                 if (ld instanceof GridData) {
136                         ((GridData)ld).grabExcessHorizontalSpace= true;
137                 }
138         }               
139
140 }