org.eclipse.cdt.internal.cppunit.util.SWTUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.cdt.internal.cppunit.util.SWTUtil.java

Source

/*
 * (c) Copyright IBM Corp. 2003.
 * Created on 27 fvr. 03
 * All Rights Reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 *
 * Contributors:
 * IBM Software Group - Rational Software
 */

package org.eclipse.cdt.internal.cppunit.util;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.util.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;

/**
 * Utility class to simplify access to some SWT resources. 
 * (copied from jdt ui)
 */
public class SWTUtil {
    private SWTUtil() {
    }

    /**
     * Returns a width hint for a button control.
     */
    public static int getButtonWidthHint(Button button) {
        PixelConverter converter = new PixelConverter(button);
        int widthHint = converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
        return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
    }

    /**
     * Returns a height hint for a button control.
     */
    public static int getButtonHeigthHint(Button button) {
        PixelConverter converter = new PixelConverter(button);
        return converter.convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
    }

    /**
     * Sets width and height hint for the button control.
     * <b>Note:</b> This is a NOP if the button's layout data is not
     * an instance of <code>GridData</code>.
     * 
     * @param   the button for which to set the dimension hint
     */
    public static void setButtonDimensionHint(Button button) {
        Assert.isNotNull(button);
        Object gd = button.getLayoutData();
        if (gd instanceof GridData) {
            ((GridData) gd).heightHint = getButtonHeigthHint(button);
            ((GridData) gd).widthHint = getButtonWidthHint(button);
        }
    }

}