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;
28 public class SWTUtil {
29 private static FontMetrics fontMetrics;
31 protected static void initializeDialogUnits(Control testControl) {
32 // Compute and store a font metric
33 GC gc = new GC(testControl);
34 gc.setFont(JFaceResources.getDialogFont());
35 fontMetrics = gc.getFontMetrics();
40 * Returns a width hint for a button control.
42 protected static int getButtonWidthHint(Button button) {
43 int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_WIDTH);
44 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
47 public static Button createButton(Composite comp, String label) {
48 Button b = new Button(comp, SWT.PUSH);
50 if (fontMetrics == null)
51 initializeDialogUnits(comp);
52 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
53 data.widthHint = getButtonWidthHint(b);
54 data.heightHint = Dialog.convertVerticalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_HEIGHT);
55 b.setLayoutData(data);
59 public static Button createCheckbox(Composite comp, String txt, boolean isSelected){
60 Button button = new Button(comp, SWT.CHECK);
62 GridLayout layout = new GridLayout();
63 comp.setLayout(layout);
64 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
65 data.horizontalIndent = 10;
66 button.setLayoutData(data);
67 button.setSelection(isSelected);
71 public static Label createLabel(Composite comp, String txt) {
72 Label label = new Label(comp, SWT.NONE);
74 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));