Documentation update from php.net
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / httpquery / preferences / SWTUtil.java
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
7  �*
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.httpquery.preferences;
12
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;
25
26 /**
27  * SWT Utility class.
28  */
29 public class SWTUtil {
30         private static FontMetrics fontMetrics;
31
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();
37                 gc.dispose();
38         }
39
40         /**
41          * Returns a width hint for a button control.
42          */
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,
47                                 true).x);
48         }
49
50         public static Button createButton(Composite comp, String label) {
51                 Button b = new Button(comp, SWT.PUSH);
52                 b.setText(label);
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);
61                 return b;
62         }
63
64         public static Button createCheckbox(Composite comp, String txt,
65                         boolean isSelected) {
66                 Button button = new Button(comp, SWT.CHECK);
67                 button.setText(txt);
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);
75                 return button;
76         }
77
78         public static Label createLabel(Composite comp, String txt) {
79                 Label label = new Label(comp, SWT.NONE);
80                 label.setText(txt);
81                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
82                                 | GridData.VERTICAL_ALIGN_BEGINNING));
83                 return label;
84         }
85 }