KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > util > LayoutUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.junit.util;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.internal.junit.wizards.MethodStubsSelectionButtonGroup;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Label;
23
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.resource.JFaceResources;
26
27 import org.eclipse.jdt.internal.ui.util.PixelConverter;
28
29 public class LayoutUtil {
30     
31     /*
32      * Calculates the number of columns needed by field editors
33      */

34     public static int getNumberOfColumns(MethodStubsSelectionButtonGroup[] editors) {
35         int columnCount= 0;
36         for (int i= 0; i < editors.length; i++) {
37             columnCount= Math.max(editors[i].getNumberOfControls(), columnCount);
38         }
39         return columnCount;
40     }
41     
42     /*
43      * Creates a composite and fills in the given editors.
44      * @param labelOnTop Defines if the label of all fields should be on top of the fields
45      */

46     public static void doDefaultLayout(Composite parent, MethodStubsSelectionButtonGroup[] editors, boolean labelOnTop) {
47         doDefaultLayout(parent, editors, labelOnTop, 0, 0, 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      */

56     public static void doDefaultLayout(Composite parent, MethodStubsSelectionButtonGroup[] editors, boolean labelOnTop, int minWidth, int minHeight) {
57         doDefaultLayout(parent, editors, labelOnTop, minWidth, minHeight, 0, 0);
58     }
59     
60     public static void doDefaultLayout(Composite parent, MethodStubsSelectionButtonGroup[] editors, boolean labelOnTop, int minWidth, int minHeight, int marginWidth, int marginHeight) {
61         int nCulumns= getNumberOfColumns(editors);
62         Control[][] controls= new Control[editors.length][];
63         for (int i= 0; i < editors.length; i++) {
64             controls[i]= editors[i].doFillIntoGrid(parent, nCulumns);
65         }
66         if (labelOnTop) {
67             nCulumns--;
68             modifyLabelSpans(controls, nCulumns);
69         }
70         GridLayout layout= new GridLayout();
71         if (marginWidth != SWT.DEFAULT) {
72             layout.marginWidth= marginWidth;
73         }
74         if (marginHeight != SWT.DEFAULT) {
75             layout.marginHeight= marginHeight;
76         }
77 // layout.minimumWidth= minWidth;
78
// layout.minimumHeight= minHeight;
79
layout.numColumns= nCulumns;
80         parent.setLayout(layout);
81     }
82     
83     private static void modifyLabelSpans(Control[][] controls, int nCulumns) {
84         for (int i= 0; i < controls.length; i++) {
85             setHorizontalSpan(controls[i][0], nCulumns);
86         }
87     }
88     
89     /*
90      * Sets the span of a control. Assumes that MGridData is used.
91      */

92     public static void setHorizontalSpan(Control control, int span) {
93         Object JavaDoc ld= control.getLayoutData();
94         if (ld instanceof GridData) {
95             ((GridData)ld).horizontalSpan= span;
96         } else if (span != 1) {
97             GridData gd= new GridData();
98             gd.horizontalSpan= span;
99             control.setLayoutData(gd);
100         }
101     }
102
103     /*
104      * Sets the width hint of a control. Assumes that MGridData is used.
105      */

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

116     public static void setHorizontalIndent(Control control, int horizontalIndent) {
117         Object JavaDoc ld= control.getLayoutData();
118         if (ld instanceof GridData) {
119             ((GridData)ld).horizontalIndent= horizontalIndent;
120         }
121     }
122
123     /*
124      * Creates a spacer control with the given span.
125      * The composite is assumed to have <code>MGridLayout</code> as
126      * layout.
127      * @param parent The parent composite
128      */

129     public static Control createEmptySpace(Composite parent, int span) {
130         Label label= new Label(parent, SWT.LEFT);
131         GridData gd= new GridData();
132         gd.horizontalAlignment= GridData.BEGINNING;
133         gd.grabExcessHorizontalSpace= false;
134         gd.horizontalSpan= span;
135         gd.horizontalIndent= 0;
136         gd.widthHint= 0;
137         gd.heightHint= 0;
138         label.setLayoutData(gd);
139         return label;
140     }
141     
142
143     /**
144      * Returns a width hint for a button control.
145      * @param button the button for which to set the dimension hint
146      * @return the width hint
147      */

148     public static int getButtonWidthHint(Button button) {
149         button.setFont(JFaceResources.getDialogFont());
150         PixelConverter converter= new PixelConverter(button);
151         int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
152         return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
153     }
154
155     /**
156      * Sets width and height hint for the button control.
157      * <b>Note:</b> This is a NOP if the button's layout data is not
158      * an instance of <code>GridData</code>.
159      *
160      * @param button the button for which to set the dimension hint
161      */

162     public static void setButtonDimensionHint(Button button) {
163         Assert.isNotNull(button);
164         Object JavaDoc gd= button.getLayoutData();
165         if (gd instanceof GridData) {
166             ((GridData)gd).widthHint= getButtonWidthHint(button);
167             ((GridData)gd).horizontalAlignment = GridData.FILL;
168         }
169     }
170     
171 }
172
Popular Tags