1 11 package org.eclipse.pde.internal.ui.util; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.jface.dialogs.Dialog; 15 import org.eclipse.jface.dialogs.IDialogConstants; 16 import org.eclipse.jface.resource.JFaceResources; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.dnd.DragSource; 19 import org.eclipse.swt.dnd.DropTarget; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.widgets.Button; 23 import org.eclipse.swt.widgets.Caret; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Display; 26 import org.eclipse.swt.widgets.Menu; 27 import org.eclipse.swt.widgets.ScrollBar; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.swt.widgets.Widget; 30 31 34 public class SWTUtil { 35 36 41 public static Display getStandardDisplay() { 42 Display display; 43 display = Display.getCurrent(); 44 if (display == null) 45 display = Display.getDefault(); 46 return display; 47 } 48 49 55 public static Shell getShell(Widget widget) { 56 if (widget instanceof Control) 57 return ((Control) widget).getShell(); 58 if (widget instanceof Caret) 59 return ((Caret) widget).getParent().getShell(); 60 if (widget instanceof DragSource) 61 return ((DragSource) widget).getControl().getShell(); 62 if (widget instanceof DropTarget) 63 return ((DropTarget) widget).getControl().getShell(); 64 if (widget instanceof Menu) 65 return ((Menu) widget).getParent().getShell(); 66 if (widget instanceof ScrollBar) 67 return ((ScrollBar) widget).getParent().getShell(); 68 69 return null; 70 } 71 72 75 public static int getButtonWidthHint(Button button) { 76 if (button.getFont().equals(JFaceResources.getDefaultFont())) 77 button.setFont(JFaceResources.getDialogFont()); 78 PixelConverter converter= new PixelConverter(button); 79 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 80 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 81 } 82 83 90 public static void setButtonDimensionHint(Button button) { 91 Dialog.applyDialogFont(button); 92 Assert.isNotNull(button); 93 Object gd = button.getLayoutData(); 94 if (gd instanceof GridData) { 95 ((GridData) gd).widthHint = getButtonWidthHint(button); 96 } 97 } 98 99 public static void setDialogSize(Dialog dialog, int width, int height) { 100 Point computedSize = 101 dialog.getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 102 width = Math.max(computedSize.x, width); 103 height = Math.max(computedSize.y, height); 104 dialog.getShell().setSize(width, height); 105 } 106 } 107 | Popular Tags |