01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.texteditor;
11:
12: import org.eclipse.swt.SWT;
13: import org.eclipse.swt.layout.GridData;
14: import org.eclipse.swt.widgets.Button;
15:
16: import org.eclipse.core.runtime.Assert;
17:
18: import org.eclipse.jface.dialogs.IDialogConstants;
19: import org.eclipse.jface.resource.JFaceResources;
20:
21: /**
22: * Utility class to simplify access to some SWT resources.
23: *
24: * @since 3.3
25: */
26: public class SWTUtil {
27:
28: /**
29: * Returns a width hint for the given button.
30: *
31: * @param button the button
32: * @return the width hint for the button
33: */
34: public static int getButtonWidthHint(Button button) {
35: button.setFont(JFaceResources.getDialogFont());
36: PixelConverter converter = new PixelConverter(button);
37: int widthHint = converter
38: .convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
39: return Math.max(widthHint, button.computeSize(SWT.DEFAULT,
40: 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 gd = button.getLayoutData();
53: if (gd instanceof GridData) {
54: ((GridData) gd).widthHint = getButtonWidthHint(button);
55: ((GridData) gd).horizontalAlignment = GridData.FILL;
56: }
57: }
58: }
|