KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > SWTUtils


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
12 package org.eclipse.team.internal.ui;
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.ui.dialogs.PreferenceLinkArea;
22 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
23
24
25
26 /**
27  *
28  */

29 public class SWTUtils {
30     
31     public static final int MARGINS_DEFAULT= -1;
32     public static final int MARGINS_NONE= 0;
33     public static final int MARGINS_DIALOG= 1;
34
35     public static PreferenceLinkArea createPreferenceLink(IWorkbenchPreferenceContainer container, Composite parent, String JavaDoc pageId, String JavaDoc text) {
36         final PreferenceLinkArea area = new PreferenceLinkArea(parent, SWT.NONE, pageId, text, container, null);
37         return area;
38     }
39     
40     public static GridData createGridData(int width, int height, boolean hFill, boolean vFill) {
41         return createGridData(width, height, hFill ? SWT.FILL : SWT.BEGINNING, vFill ? SWT.FILL : SWT.CENTER, hFill, vFill);
42     }
43     
44     public static GridData createGridData(int width, int height, int hAlign, int vAlign, boolean hGrab, boolean vGrab) {
45         final GridData gd= new GridData(hAlign, vAlign, hGrab, vGrab);
46         gd.widthHint= width;
47         gd.heightHint= height;
48         return gd;
49     }
50
51     public static GridData createHFillGridData() {
52         return createHFillGridData(1);
53     }
54     
55     public static GridData createHFillGridData(int span) {
56         final GridData gd= createGridData(0, SWT.DEFAULT, SWT.FILL, SWT.CENTER, true, false);
57         gd.horizontalSpan= span;
58         return gd;
59     }
60
61     public static Composite createHFillComposite(Composite parent, int margins) {
62         return createHFillComposite(parent, margins, 1);
63     }
64     
65     public static Composite createHFillComposite(Composite parent, int margins, int columns) {
66         final Composite composite= new Composite(parent, SWT.NONE);
67         composite.setFont(parent.getFont());
68         composite.setLayoutData(createHFillGridData());
69         composite.setLayout(createGridLayout(columns, new PixelConverter(parent), margins));
70         return composite;
71     }
72     
73     public static Composite createHVFillComposite(Composite parent, int margins) {
74         return createHVFillComposite(parent, margins, 1);
75     }
76     
77     public static Composite createHVFillComposite(Composite parent, int margins, int columns) {
78         final Composite composite= new Composite(parent, SWT.NONE);
79         composite.setFont(parent.getFont());
80         composite.setLayoutData(createHVFillGridData());
81         composite.setLayout(createGridLayout(columns, new PixelConverter(parent), margins));
82         return composite;
83     }
84
85     
86     /**
87      * Groups
88      */

89     
90     public static Group createHFillGroup(Composite parent, String JavaDoc text, int margins) {
91         return createHFillGroup(parent, text, margins, 1);
92     }
93     
94     public static Group createHFillGroup(Composite parent, String JavaDoc text, int margins, int rows) {
95         final Group group= new Group(parent, SWT.NONE);
96         group.setFont(parent.getFont());
97         group.setLayoutData(createHFillGridData());
98         if (text != null)
99             group.setText(text);
100         group.setLayout(createGridLayout(rows, new PixelConverter(parent), margins));
101         return group;
102     }
103     
104     public static Group createHVFillGroup(Composite parent, String JavaDoc text, int margins) {
105         return createHVFillGroup(parent, text, margins, 1);
106     }
107     
108     public static Group createHVFillGroup(Composite parent, String JavaDoc text, int margins, int rows) {
109         final Group group= new Group(parent, SWT.NONE);
110         group.setFont(parent.getFont());
111         group.setLayoutData(createHVFillGridData());
112         if (text != null)
113             group.setText(text);
114         group.setLayout(createGridLayout(rows, new PixelConverter(parent), margins));
115         return group;
116     }
117     
118
119     /**
120      * Grid data
121      */

122     
123     public static GridData createHVFillGridData() {
124         return createHVFillGridData(1);
125     }
126     
127     public static GridData createHVFillGridData(int span) {
128         final GridData gd= createGridData(0, 0, true, true);
129         gd.horizontalSpan= span;
130         return gd;
131     }
132
133     
134     /**
135      * Create a grid layout with the specified number of columns and the
136      * standard spacings.
137      *
138      * @param numColumns
139      * the number of columns
140      * @param converter
141      * the pixel converter
142      * @param margins
143      * One of <code>MARGINS_DEFAULT</code>,
144      * <code>MARGINS_NONE</code> or <code>MARGINS_DIALOG</code>.
145      * @return the grid layout
146      */

147     public static GridLayout createGridLayout(int numColumns, PixelConverter converter, int margins) {
148         Assert.isTrue(margins == MARGINS_DEFAULT || margins == MARGINS_NONE || margins == MARGINS_DIALOG);
149         
150         final GridLayout layout= new GridLayout(numColumns, false);
151         layout.horizontalSpacing= converter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
152         layout.verticalSpacing= converter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
153         
154         switch (margins) {
155         case MARGINS_NONE:
156             layout.marginLeft= layout.marginRight= 0;
157             layout.marginTop= layout.marginBottom= 0;
158             break;
159         case MARGINS_DIALOG:
160             layout.marginLeft= layout.marginRight= converter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
161             layout.marginTop= layout.marginBottom= converter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
162             break;
163         case MARGINS_DEFAULT:
164             layout.marginLeft= layout.marginRight= layout.marginWidth;
165             layout.marginTop= layout.marginBottom= layout.marginHeight;
166         }
167         layout.marginWidth= layout.marginHeight= 0;
168         return layout;
169     }
170     
171     
172     public static Label createLabel(Composite parent, String JavaDoc message) {
173         return createLabel(parent, message, 1);
174     }
175
176     public static Label createLabel(Composite parent, String JavaDoc message, int span) {
177         final Label label= new Label(parent, SWT.WRAP);
178         if (message != null)
179             label.setText(message);
180         label.setLayoutData(createHFillGridData(span));
181         return label;
182     }
183     
184     public static Button createCheckBox(Composite parent, String JavaDoc message) {
185         return createCheckBox(parent, message, 1);
186     }
187
188     public static Button createCheckBox(Composite parent, String JavaDoc message, int span) {
189         final Button button= new Button(parent, SWT.CHECK);
190         button.setText(message);
191         button.setLayoutData(createHFillGridData(span));
192         return button;
193     }
194     
195     public static Button createRadioButton(Composite parent, String JavaDoc message) {
196         return createRadioButton(parent, message, 1);
197     }
198
199     public static Button createRadioButton(Composite parent, String JavaDoc message, int span) {
200         final Button button= new Button(parent, SWT.RADIO);
201         button.setText(message);
202         button.setLayoutData(createHFillGridData(span));
203         return button;
204     }
205
206     
207     public static Text createText(Composite parent) {
208         return createText(parent, 1);
209     }
210
211     public static Text createText(Composite parent, int span) {
212         final Text text= new Text(parent, SWT.SINGLE | SWT.BORDER);
213         text.setLayoutData(createHFillGridData(span));
214         return text;
215     }
216     
217
218     public static Control createPlaceholder(Composite parent, int heightInChars, int span) {
219         Assert.isTrue(heightInChars > 0);
220         final Control placeHolder= new Composite(parent, SWT.NONE);
221         final GridData gd= new GridData(SWT.BEGINNING, SWT.TOP, false, false);
222         gd.heightHint= new PixelConverter(parent).convertHeightInCharsToPixels(heightInChars);
223         gd.horizontalSpan= span;
224         placeHolder.setLayoutData(gd);
225         return placeHolder;
226     }
227
228     
229     public static Control createPlaceholder(Composite parent, int heightInChars) {
230         return createPlaceholder(parent, heightInChars, 1);
231     }
232     
233     public static PixelConverter createDialogPixelConverter(Control control) {
234         Dialog.applyDialogFont(control);
235         return new PixelConverter(control);
236     }
237     
238     public static int calculateControlSize(PixelConverter converter, Control [] controls) {
239         return calculateControlSize(converter, controls, 0, controls.length - 1);
240     }
241
242     public static int calculateControlSize(PixelConverter converter, Control [] controls, int start, int end) {
243         int minimum= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
244         for (int i = start; i <= end; i++) {
245             final int length= controls[i].computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
246             if (minimum < length)
247                 minimum= length;
248         }
249         return minimum;
250     }
251     
252     public static void equalizeControls(PixelConverter converter, Control [] controls) {
253         equalizeControls(converter, controls, 0, controls.length - 1);
254     }
255
256     public static void equalizeControls(PixelConverter converter, Control [] controls, int start, int end) {
257         final int size= calculateControlSize(converter, controls, start, end);
258         for (int i = start; i <= end; i++) {
259             final Control button= controls[i];
260             if (button.getLayoutData() instanceof GridData) {
261                 ((GridData)button.getLayoutData()).widthHint= size;
262             }
263         }
264     }
265
266     public static int getWidthInCharsForLongest(PixelConverter converter, String JavaDoc [] strings) {
267         int minimum= 0;
268         for (int i = 0; i < strings.length; i++) {
269             final int length= converter.convertWidthInCharsToPixels(strings[i].length());
270             if (minimum < length)
271                 minimum= length;
272         }
273         return minimum;
274     }
275 }
276
Popular Tags