KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > SWTUtil


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.ui.internal.browser;
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.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24 /**
25  * SWT Utility class.
26  */

27 public class SWTUtil {
28     private static FontMetrics fontMetrics;
29
30     protected static void initializeDialogUnits(Control testControl) {
31         // Compute and store a font metric
32
GC gc = new GC(testControl);
33         gc.setFont(JFaceResources.getDialogFont());
34         fontMetrics = gc.getFontMetrics();
35         gc.dispose();
36     }
37
38     /**
39      * Returns a width hint for a button control.
40      */

41     protected static int getButtonWidthHint(Button button) {
42         int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_WIDTH);
43         return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
44     }
45
46     /**
47      * Create a new button with the standard size.
48      *
49      * @param comp a component to add the button to
50      * @param label the button label
51      * @return a button
52      */

53     public static Button createButton(Composite comp, String JavaDoc label) {
54         Button b = new Button(comp, SWT.PUSH);
55         b.setText(label);
56         if (fontMetrics == null)
57             initializeDialogUnits(comp);
58         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
59         data.widthHint = getButtonWidthHint(b);
60         b.setLayoutData(data);
61         return b;
62     }
63     
64     /**
65      * Create a new standard label.
66      *
67      * @param comp a component to add the label to
68      * @param text the label text
69      * @return a label
70      */

71     public static Label createLabel(Composite comp, String JavaDoc text) {
72         Label label = new Label(comp, SWT.NONE);
73         label.setText(text);
74         label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
75         return label;
76     }
77 }
Popular Tags