1 11 package org.eclipse.jdt.internal.junit.util; 12 13 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.Widget; 26 27 import org.eclipse.jface.dialogs.IDialogConstants; 28 import org.eclipse.jface.resource.JFaceResources; 29 import org.eclipse.jface.util.Assert; 30 31 35 public class SWTUtil { 36 private SWTUtil(){} 37 42 public static Display getStandardDisplay() { 43 Display display; 44 display= Display.getCurrent(); 45 if (display == null) 46 display= Display.getDefault(); 47 return display; 48 } 49 50 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(); 69 70 return null; 71 } 72 73 74 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); 82 } 83 84 87 public static int getButtonHeigthHint(Button button) { 88 button.setFont(JFaceResources.getDialogFont()); 89 PixelConverter converter= new PixelConverter(button); 90 return converter.convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT); 91 } 92 93 94 101 public static void setButtonDimensionHint(Button button) { 102 Assert.isNotNull(button); 103 Object gd= button.getLayoutData(); 104 if (gd instanceof GridData) { 105 ((GridData)gd).heightHint= getButtonHeigthHint(button); 106 ((GridData)gd).widthHint= getButtonWidthHint(button); 107 } 108 } 109 110 111 } 112 | Popular Tags |