KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > search > SWTUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.ui.internal.search;
12 import org.eclipse.jface.dialogs.*;
13 import org.eclipse.jface.util.*;
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.layout.*;
17 import org.eclipse.swt.widgets.*;
18 /**
19  * Utility class to simplify access to some SWT resources.
20  */

21 public class SWTUtil {
22     private static double fgHorizontalDialogUnitSize = 0.0;
23     private static double fgVerticalDialogUnitSize = 0.0;
24     private static void initializeDialogUnits(Control control) {
25         GC gc = new GC(control);
26         gc.setFont(control.getFont());
27         int averageWidth = gc.getFontMetrics().getAverageCharWidth();
28         int height = gc.getFontMetrics().getHeight();
29         gc.dispose();
30         fgHorizontalDialogUnitSize = averageWidth * 0.25;
31         fgVerticalDialogUnitSize = height * 0.125;
32     }
33     /**
34      * @see DialogPage#convertHorizontalDLUsToPixels
35      */

36     private static int convertHorizontalDLUsToPixels(int dlus) {
37         return (int) Math.round(dlus * fgHorizontalDialogUnitSize);
38     }
39     /**
40      * @see DialogPage#convertVerticalDLUsToPixels
41      */

42     private static int convertVerticalDLUsToPixels(int dlus) {
43         return (int) Math.round(dlus * fgVerticalDialogUnitSize);
44     }
45     /**
46      * @see DialogPage#convertWidthInCharsToPixels
47      */

48     public static int convertWidthInCharsToPixels(int chars, Text text) {
49         if (fgHorizontalDialogUnitSize == 0.0)
50             initializeDialogUnits(text);
51         return convertHorizontalDLUsToPixels(chars * 4);
52     }
53     /**
54      * Returns a width hint for a button control.
55      */

56     public static int getButtonWidthHint(Button button) {
57         if (fgHorizontalDialogUnitSize == 0.0) {
58             initializeDialogUnits(button);
59         }
60         int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
61         return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT,
62                 true).x);
63     }
64     /**
65      * Returns a height hint for a button control.
66      */

67     public static int getButtonHeigthHint(Button button) {
68         if (fgHorizontalDialogUnitSize == 0.0) {
69             initializeDialogUnits(button);
70         }
71         return convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
72     }
73     /**
74      * Sets width and height hint for the button control. <b>Note: </b> This is
75      * a NOP if the button's layout data is not an instance of
76      * <code>GridData</code>.
77      *
78      * @param button
79      * the button for which to set the dimension hint
80      */

81     public static void setButtonDimensionHint(Button button) {
82         Assert.isNotNull(button);
83         Object JavaDoc gd = button.getLayoutData();
84         if (gd instanceof GridData) {
85             ((GridData) gd).heightHint = SWTUtil.getButtonHeigthHint(button);
86             ((GridData) gd).widthHint = SWTUtil.getButtonWidthHint(button);
87         }
88     }
89 }
90
Popular Tags