001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.util;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.dialogs.Dialog;
014: import org.eclipse.jface.dialogs.IDialogConstants;
015: import org.eclipse.jface.resource.JFaceResources;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.dnd.DragSource;
018: import org.eclipse.swt.dnd.DropTarget;
019: import org.eclipse.swt.graphics.Point;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.widgets.Button;
022: import org.eclipse.swt.widgets.Caret;
023: import org.eclipse.swt.widgets.Control;
024: import org.eclipse.swt.widgets.Display;
025: import org.eclipse.swt.widgets.Menu;
026: import org.eclipse.swt.widgets.ScrollBar;
027: import org.eclipse.swt.widgets.Shell;
028: import org.eclipse.swt.widgets.Widget;
029:
030: /**
031: * Utility class to simplify access to some SWT resources.
032: */
033: public class SWTUtil {
034:
035: /**
036: * Returns the standard display to be used. The method first checks, if
037: * the thread calling this method has an associated disaply. If so, this
038: * display is returned. Otherwise the method returns the default display.
039: */
040: public static Display getStandardDisplay() {
041: Display display;
042: display = Display.getCurrent();
043: if (display == null)
044: display = Display.getDefault();
045: return display;
046: }
047:
048: /**
049: * Returns the shell for the given widget. If the widget doesn't represent
050: * a SWT object that manage a shell, <code>null</code> is returned.
051: *
052: * @return the shell for the given widget
053: */
054: public static Shell getShell(Widget widget) {
055: if (widget instanceof Control)
056: return ((Control) widget).getShell();
057: if (widget instanceof Caret)
058: return ((Caret) widget).getParent().getShell();
059: if (widget instanceof DragSource)
060: return ((DragSource) widget).getControl().getShell();
061: if (widget instanceof DropTarget)
062: return ((DropTarget) widget).getControl().getShell();
063: if (widget instanceof Menu)
064: return ((Menu) widget).getParent().getShell();
065: if (widget instanceof ScrollBar)
066: return ((ScrollBar) widget).getParent().getShell();
067:
068: return null;
069: }
070:
071: /**
072: * Returns a width hint for a button control.
073: */
074: public static int getButtonWidthHint(Button button) {
075: if (button.getFont().equals(JFaceResources.getDefaultFont()))
076: button.setFont(JFaceResources.getDialogFont());
077: PixelConverter converter = new PixelConverter(button);
078: int widthHint = converter
079: .convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
080: return Math.max(widthHint, button.computeSize(SWT.DEFAULT,
081: SWT.DEFAULT, true).x);
082: }
083:
084: /**
085: * Sets width and height hint for the button control.
086: * <b>Note:</b> This is a NOP if the button's layout data is not
087: * an instance of <code>GridData</code>.
088: *
089: * @param the button for which to set the dimension hint
090: */
091: public static void setButtonDimensionHint(Button button) {
092: Dialog.applyDialogFont(button);
093: Assert.isNotNull(button);
094: Object gd = button.getLayoutData();
095: if (gd instanceof GridData) {
096: ((GridData) gd).widthHint = getButtonWidthHint(button);
097: }
098: }
099:
100: public static void setDialogSize(Dialog dialog, int width,
101: int height) {
102: Point computedSize = dialog.getShell().computeSize(SWT.DEFAULT,
103: SWT.DEFAULT);
104: width = Math.max(computedSize.x, width);
105: height = Math.max(computedSize.y, height);
106: dialog.getShell().setSize(width, height);
107: }
108: }
|