Catch exception if reading webbrowser favorites
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / 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.phpeclipse.webbrowser.internal;
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  * SWT Utility class.
27  */
28 public class SWTUtil {
29         private static FontMetrics fontMetrics;
30
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();
36                 gc.dispose();
37         }
38
39         /**
40          * Returns a width hint for a button control.
41          */
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);
45         }
46         
47         public static Button createButton(Composite comp, String label) {
48                 Button b = new Button(comp, SWT.PUSH);
49                 b.setText(label);
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);
56                 return b;
57         }
58         
59         public static Button createCheckbox(Composite comp, String txt, boolean isSelected){
60                 Button button = new Button(comp, SWT.CHECK);
61                 button.setText(txt);
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);
68                 return button;
69         }
70         
71         public static Label createLabel(Composite comp, String txt) {
72                 Label label = new Label(comp, SWT.NONE);
73                 label.setText(txt);
74                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
75                 return label;
76         }
77 }