1 11 package org.eclipse.search.internal.ui.util; 12 13 14 import org.eclipse.core.runtime.Assert; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.dnd.DragSource; 18 import org.eclipse.swt.dnd.DropTarget; 19 import org.eclipse.swt.layout.GridData; 20 import org.eclipse.swt.widgets.Button; 21 import org.eclipse.swt.widgets.Caret; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Display; 24 import org.eclipse.swt.widgets.Menu; 25 import org.eclipse.swt.widgets.ScrollBar; 26 import org.eclipse.swt.widgets.Shell; 27 import org.eclipse.swt.widgets.Table; 28 import org.eclipse.swt.widgets.Widget; 29 30 import org.eclipse.jface.dialogs.IDialogConstants; 31 import org.eclipse.jface.resource.JFaceResources; 32 33 36 public class SWTUtil { 37 38 44 public static Display getStandardDisplay() { 45 Display display; 46 display= Display.getCurrent(); 47 if (display == null) 48 display= Display.getDefault(); 49 return display; 50 } 51 52 59 public static Shell getShell(Widget widget) { 60 if (widget instanceof Control) 61 return ((Control)widget).getShell(); 62 if (widget instanceof Caret) 63 return ((Caret)widget).getParent().getShell(); 64 if (widget instanceof DragSource) 65 return ((DragSource)widget).getControl().getShell(); 66 if (widget instanceof DropTarget) 67 return ((DropTarget)widget).getControl().getShell(); 68 if (widget instanceof Menu) 69 return ((Menu)widget).getParent().getShell(); 70 if (widget instanceof ScrollBar) 71 return ((ScrollBar)widget).getParent().getShell(); 72 73 return null; 74 } 75 76 77 82 public static int getButtonWidthHint(Button button) { 83 button.setFont(JFaceResources.getDialogFont()); 84 PixelConverter converter= new PixelConverter(button); 85 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 86 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 87 } 88 89 96 public static void setButtonDimensionHint(Button button) { 97 Assert.isNotNull(button); 98 Object gd= button.getLayoutData(); 99 if (gd instanceof GridData) { 100 ((GridData)gd).widthHint= getButtonWidthHint(button); 101 ((GridData)gd).horizontalAlignment = GridData.FILL; 102 } 103 } 104 105 public static int getTableHeightHint(Table table, int rows) { 106 if (table.getFont().equals(JFaceResources.getDefaultFont())) 107 table.setFont(JFaceResources.getDialogFont()); 108 int result= table.getItemHeight() * rows + table.getHeaderHeight(); 109 if (table.getLinesVisible()) 110 result+= table.getGridLineWidth() * (rows - 1); 111 return result; 112 } 113 114 115 } 116 | Popular Tags |