1 11 package org.eclipse.update.internal.ui.parts; 12 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.jface.dialogs.*; 18 import org.eclipse.jface.dialogs.Dialog; 19 import org.eclipse.jface.resource.*; 20 import org.eclipse.swt.*; 21 import org.eclipse.swt.dnd.*; 22 import org.eclipse.swt.graphics.*; 23 import org.eclipse.swt.layout.*; 24 import org.eclipse.swt.widgets.*; 25 26 29 public class SWTUtil { 30 31 36 public static Display getStandardDisplay() { 37 Display display; 38 display = Display.getCurrent(); 39 if (display == null) 40 display = Display.getDefault(); 41 return display; 42 } 43 44 50 public static Shell getShell(Widget widget) { 51 if (widget instanceof Control) 52 return ((Control) widget).getShell(); 53 if (widget instanceof Caret) 54 return ((Caret) widget).getParent().getShell(); 55 if (widget instanceof DragSource) 56 return ((DragSource) widget).getControl().getShell(); 57 if (widget instanceof DropTarget) 58 return ((DropTarget) widget).getControl().getShell(); 59 if (widget instanceof Menu) 60 return ((Menu) widget).getParent().getShell(); 61 if (widget instanceof ScrollBar) 62 return ((ScrollBar) widget).getParent().getShell(); 63 64 return null; 65 } 66 67 70 public static int getButtonWidthHint(Button button) { 71 if (button.getFont().equals(JFaceResources.getDefaultFont())) 72 button.setFont(JFaceResources.getDialogFont()); 73 PixelConverter converter= new PixelConverter(button); 74 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 75 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 76 } 77 78 85 public static void setButtonDimensionHint(Button button) { 86 Dialog.applyDialogFont(button); 87 Assert.isNotNull(button); 88 Object gd = button.getLayoutData(); 89 if (gd instanceof GridData) { 90 ((GridData) gd).widthHint = getButtonWidthHint(button); 91 } 92 } 93 94 public static void setDialogSize(Dialog dialog, int width, int height) { 95 Point computedSize = 96 dialog.getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 97 width = Math.max(computedSize.x, width); 98 height = Math.max(computedSize.y, height); 99 dialog.getShell().setSize(width, height); 100 } 101 } 102 | Popular Tags |