KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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.texteditor;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.widgets.Button;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.resource.JFaceResources;
21
22
23 /**
24  * Utility class to simplify access to some SWT resources.
25  *
26  * @since 3.3
27  */

28 public class SWTUtil {
29
30     /**
31      * Returns a width hint for the given button.
32      *
33      * @param button the button
34      * @return the width hint for the button
35      */

36     public static int getButtonWidthHint(Button button) {
37         button.setFont(JFaceResources.getDialogFont());
38         PixelConverter converter= new PixelConverter(button);
39         int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
40         return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
41     }
42
43     /**
44      * Sets width and height hint for the button control.
45      * <b>Note:</b> This is a NOP if the button's layout data is not
46      * an instance of <code>GridData</code>.
47      *
48      * @param button the button for which to set the dimension hint
49      */

50     public static void setButtonDimensionHint(Button button) {
51         Assert.isNotNull(button);
52         Object JavaDoc gd= button.getLayoutData();
53         if (gd instanceof GridData) {
54             ((GridData)gd).widthHint= getButtonWidthHint(button);
55             ((GridData)gd).horizontalAlignment = GridData.FILL;
56         }
57     }
58 }
59
Popular Tags