1 /**********************************************************************
2 * Copyright (c) 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
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.httpquery.preferences;
13 import org.eclipse.jface.dialogs.Dialog;
14 import org.eclipse.jface.dialogs.IDialogConstants;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.FontMetrics;
18 import org.eclipse.swt.graphics.GC;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
29 public class SWTUtil {
30 private static FontMetrics fontMetrics;
32 protected static void initializeDialogUnits(Control testControl) {
33 // Compute and store a font metric
34 GC gc = new GC(testControl);
35 gc.setFont(JFaceResources.getDialogFont());
36 fontMetrics = gc.getFontMetrics();
41 * Returns a width hint for a button control.
43 protected static int getButtonWidthHint(Button button) {
44 int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics,
45 IDialogConstants.BUTTON_WIDTH);
46 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT,
50 public static Button createButton(Composite comp, String label) {
51 Button b = new Button(comp, SWT.PUSH);
53 if (fontMetrics == null)
54 initializeDialogUnits(comp);
55 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
56 | GridData.VERTICAL_ALIGN_BEGINNING);
57 data.widthHint = getButtonWidthHint(b);
58 data.heightHint = Dialog.convertVerticalDLUsToPixels(fontMetrics,
59 IDialogConstants.BUTTON_HEIGHT);
60 b.setLayoutData(data);
64 public static Button createCheckbox(Composite comp, String txt,
66 Button button = new Button(comp, SWT.CHECK);
68 GridLayout layout = new GridLayout();
69 comp.setLayout(layout);
70 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
71 | GridData.VERTICAL_ALIGN_BEGINNING);
72 data.horizontalIndent = 10;
73 button.setLayoutData(data);
74 button.setSelection(isSelected);
78 public static Label createLabel(Composite comp, String txt) {
79 Label label = new Label(comp, SWT.NONE);
81 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
82 | GridData.VERTICAL_ALIGN_BEGINNING));