1
/*******************************************************************************
2
* Copyright (c) 2001, 2011 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
9
* Rational Software - initial implementation
10
* Markus Schorn (Wind River Systems)
11
* Sergey Prigogin (Google)
12
*******************************************************************************/
13
package org.eclipse.cdt.internal.ui.wizards.dialogfields;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.layout.GridData;
17
import org.eclipse.swt.layout.GridLayout;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.swt.widgets.Layout;
22
public class LayoutUtil {
24
* Calculates the number of columns needed by field editors
26
public static int getNumberOfColumns(DialogField[] editors) {
28
for (int i= 0; i < editors.length; i++) {
29
nColumns= Math.max(editors[i].getNumberOfControls(), nColumns);
35
* Returns the number of columns in the layout of a composite,
36
* or 1 if the composite doesn't have a grid layout.
38
public static int getNumberOfColumns(Composite composite) {
39
Layout layout = composite.getLayout();
40
return layout instanceof GridLayout ? ((GridLayout) layout).numColumns : 1;
44
* Creates a composite and fills in the given editors.
45
* @param labelOnTop Defines if the label of all fields should be on top of the fields
47
public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop) {
48
doDefaultLayout(parent, editors, labelOnTop, 0, 0, 0, 0);
52
* Creates a composite and fills in the given editors.
53
* @param labelOnTop Defines if the label of all fields should be on top of the fields
54
* @param minWidth The minimal width of the composite
55
* @param minHeight The minimal height of the composite
57
public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop,
58
int minWidth, int minHeight) {
59
doDefaultLayout(parent, editors, labelOnTop, minWidth, minHeight, 0, 0);
63
* Creates a composite and fills in the given editors.
64
* @param labelOnTop Defines if the label of all fields should be on top of the fields
65
* @param minWidth The minimal width of the composite
66
* @param minHeight The minimal height of the composite
67
* @param marginWidth The margin width to be used by the composite
68
* @param marginHeight The margin height to be used by the composite
70
private static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop,
71
int minWidth, int minHeight, int marginWidth, int marginHeight) {
72
int nCulumns= getNumberOfColumns(editors);
73
Control[][] controls= new Control[editors.length][];
74
for (int i= 0; i < editors.length; i++) {
75
controls[i]= editors[i].doFillIntoGrid(parent, nCulumns);
79
modifyLabelSpans(controls, nCulumns);
81
GridLayout layout= new GridLayout();
82
if (marginWidth != SWT.DEFAULT) {
83
layout.marginWidth= marginWidth;
85
if (marginHeight != SWT.DEFAULT) {
86
layout.marginHeight= marginHeight;
88
layout.numColumns= nCulumns;
89
parent.setLayout(layout);
92
private static void modifyLabelSpans(Control[][] controls, int nCulumns) {
93
for (int i= 0; i < controls.length; i++) {
94
setHorizontalSpan(controls[i][0], nCulumns);
99
* Sets the span of a control. Assumes that GridData is used.
101
public static void setHorizontalSpan(Control control, int span) {
102
Object ld= control.getLayoutData();
103
if (ld instanceof GridData) {
104
((GridData) ld).horizontalSpan= span;
105
} else if (span != 1) {
106
GridData gd= new GridData();
107
gd.horizontalSpan= span;
108
control.setLayoutData(gd);
113
* Sets the width hint of a control. Assumes that GridData is used.
115
public static void setWidthHint(Control control, int widthHint) {
116
Object ld= control.getLayoutData();
117
if (ld instanceof GridData) {
118
((GridData) ld).widthHint= widthHint;
123
* Sets the heigthHint hint of a control. Assumes that GridData is used.
125
public static void setHeightHint(Control control, int heigthHint) {
126
Object ld= control.getLayoutData();
127
if (ld instanceof GridData) {
128
((GridData) ld).heightHint= heigthHint;
133
* Sets the horizontal indent of a control. Assumes that GridData is used.
135
public static void setHorizontalIndent(Control control, int horizontalIndent) {
136
Object ld= control.getLayoutData();
137
if (ld instanceof GridData) {
138
((GridData) ld).horizontalIndent= horizontalIndent;
143
* Sets the horizontal alignment of a control. Assumes that GridData is used.
145
public static void setHorizontalAlignment(Control control, int horizontalAlignment) {
146
Object ld= control.getLayoutData();
147
if (ld instanceof GridData) {
148
((GridData) ld).horizontalAlignment= horizontalAlignment;
153
* Makes a control grab all available horizontal space. Assumes that GridData is used.
155
public static void setHorizontalGrabbing(Control control) {
156
setHorizontalGrabbing(control, true);
160
* Makes a control grab all available horizontal space. Assumes that GridData is used.
161
* @param value <code>true</code> to grab, <code>false</code> not to grab
163
public static void setHorizontalGrabbing(Control control, boolean value) {
164
Object ld= control.getLayoutData();
165
if (ld instanceof GridData) {
166
((GridData) ld).grabExcessHorizontalSpace= value;
171
* Makes a control grab all available horizontal space. Assumes that GridData is used.
172
* @param value <code>true</code> to grab, <code>false</code> not to grab
174
public static void setVerticalGrabbing(Control control, boolean value) {
175
Object ld= control.getLayoutData();
176
if (ld instanceof GridData) {
177
((GridData) ld).grabExcessVerticalSpace= value;