KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > dialogfields > LayoutUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.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);
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 marginWidth The margin width to be used by the composite
44      * @param marginHeight The margin height to be used by the composite
45      */

46     public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop, int marginWidth, int marginHeight) {
47         int nCulumns= getNumberOfColumns(editors);
48         Control[][] controls= new Control[editors.length][];
49         for (int i= 0; i < editors.length; i++) {
50             controls[i]= editors[i].doFillIntoGrid(parent, nCulumns);
51         }
52         if (labelOnTop) {
53             nCulumns--;
54             modifyLabelSpans(controls, nCulumns);
55         }
56         GridLayout layout= null;
57         if (parent.getLayout() instanceof GridLayout) {
58             layout= (GridLayout) parent.getLayout();
59         } else {
60             layout= new GridLayout();
61         }
62         if (marginWidth != SWT.DEFAULT) {
63             layout.marginWidth= marginWidth;
64         }
65         if (marginHeight != SWT.DEFAULT) {
66             layout.marginHeight= marginHeight;
67         }
68         layout.numColumns= nCulumns;
69         parent.setLayout(layout);
70     }
71     
72     private static void modifyLabelSpans(Control[][] controls, int nCulumns) {
73         for (int i= 0; i < controls.length; i++) {
74             setHorizontalSpan(controls[i][0], nCulumns);
75         }
76     }
77     
78     /**
79      * Sets the span of a control. Assumes that GridData is used.
80      */

81     public static void setHorizontalSpan(Control control, int span) {
82         Object JavaDoc ld= control.getLayoutData();
83         if (ld instanceof GridData) {
84             ((GridData)ld).horizontalSpan= span;
85         } else if (span != 1) {
86             GridData gd= new GridData();
87             gd.horizontalSpan= span;
88             control.setLayoutData(gd);
89         }
90     }
91
92     /**
93      * Sets the width hint of a control. Assumes that GridData is used.
94      */

95     public static void setWidthHint(Control control, int widthHint) {
96         Object JavaDoc ld= control.getLayoutData();
97         if (ld instanceof GridData) {
98             ((GridData)ld).widthHint= widthHint;
99         }
100     }
101     
102     /**
103      * Sets the heightHint hint of a control. Assumes that GridData is used.
104      */

105     public static void setHeightHint(Control control, int heightHint) {
106         Object JavaDoc ld= control.getLayoutData();
107         if (ld instanceof GridData) {
108             ((GridData)ld).heightHint= heightHint;
109         }
110     }
111     
112     /**
113      * Sets the horizontal indent of a control. Assumes that GridData is used.
114      */

115     public static void setHorizontalIndent(Control control, int horizontalIndent) {
116         Object JavaDoc ld= control.getLayoutData();
117         if (ld instanceof GridData) {
118             ((GridData)ld).horizontalIndent= horizontalIndent;
119         }
120     }
121     
122     /**
123      * Sets the horizontal grabbing of a control to true. Assumes that GridData is used.
124      */

125     public static void setHorizontalGrabbing(Control control) {
126         Object JavaDoc ld= control.getLayoutData();
127         if (ld instanceof GridData) {
128             ((GridData)ld).grabExcessHorizontalSpace= true;
129         }
130     }
131
132 }
133
Popular Tags