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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.util;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.dnd.DragSource;
16 import org.eclipse.swt.dnd.DropTarget;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Caret;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Menu;
23 import org.eclipse.swt.widgets.ScrollBar;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.Table;
26 import org.eclipse.swt.widgets.Widget;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.jface.resource.JFaceResources;
30 import org.eclipse.jface.util.Assert;
33 * Utility class to simplify access to some SWT resources.
35 public class SWTUtil {
38 * Returns the standard display to be used. The method first checks, if
39 * the thread calling this method has an associated disaply. If so, this
40 * display is returned. Otherwise the method returns the default display.
42 public static Display getStandardDisplay() {
44 display= Display.getCurrent();
46 display= Display.getDefault();
51 * Returns the shell for the given widget. If the widget doesn't represent
52 * a SWT object that manage a shell, <code>null</code> is returned.
54 * @return the shell for the given widget
56 public static Shell getShell(Widget widget) {
57 if (widget instanceof Control)
58 return ((Control)widget).getShell();
59 if (widget instanceof Caret)
60 return ((Caret)widget).getParent().getShell();
61 if (widget instanceof DragSource)
62 return ((DragSource)widget).getControl().getShell();
63 if (widget instanceof DropTarget)
64 return ((DropTarget)widget).getControl().getShell();
65 if (widget instanceof Menu)
66 return ((Menu)widget).getParent().getShell();
67 if (widget instanceof ScrollBar)
68 return ((ScrollBar)widget).getParent().getShell();
75 * Returns a width hint for a button control.
77 public static int getButtonWidthHint(Button button) {
78 button.setFont(JFaceResources.getDialogFont());
79 PixelConverter converter= new PixelConverter(button);
80 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
81 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
85 * Returns a height hint for a button control.
87 public static int getButtonHeightHint(Button button) {
88 button.setFont(JFaceResources.getDialogFont());
89 PixelConverter converter= new PixelConverter(button);
90 return converter.convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
94 * Sets width and height hint for the button control.
95 * <b>Note:</b> This is a NOP if the button's layout data is not
96 * an instance of <code>GridData</code>.
98 * @param button the button for which to set the dimension hint
100 public static void setButtonDimensionHint(Button button) {
101 Assert.isNotNull(button);
102 Object gd= button.getLayoutData();
103 if (gd instanceof GridData) {
104 ((GridData)gd).heightHint= getButtonHeightHint(button);
105 ((GridData)gd).widthHint= getButtonWidthHint(button);
106 ((GridData)gd).horizontalAlignment = GridData.FILL;
110 public static int getTableHeightHint(Table table, int rows) {
111 if (table.getFont().equals(JFaceResources.getDefaultFont()))
112 table.setFont(JFaceResources.getDialogFont());
113 int result= table.getItemHeight() * rows + table.getHeaderHeight();
114 if (table.getLinesVisible())
115 result+= table.getGridLineWidth() * (rows - 1);