1 11 package org.eclipse.jdt.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 43 public static Display getStandardDisplay() { 44 Display display; 45 display= Display.getCurrent(); 46 if (display == null) 47 display= Display.getDefault(); 48 return display; 49 } 50 51 57 public static Shell getShell(Widget widget) { 58 if (widget instanceof Control) 59 return ((Control)widget).getShell(); 60 if (widget instanceof Caret) 61 return ((Caret)widget).getParent().getShell(); 62 if (widget instanceof DragSource) 63 return ((DragSource)widget).getControl().getShell(); 64 if (widget instanceof DropTarget) 65 return ((DropTarget)widget).getControl().getShell(); 66 if (widget instanceof Menu) 67 return ((Menu)widget).getParent().getShell(); 68 if (widget instanceof ScrollBar) 69 return ((ScrollBar)widget).getParent().getShell(); 70 71 return null; 72 } 73 74 75 78 public static int getButtonWidthHint(Button button) { 79 button.setFont(JFaceResources.getDialogFont()); 80 PixelConverter converter= new PixelConverter(button); 81 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 82 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 83 } 84 85 92 public static void setButtonDimensionHint(Button button) { 93 Assert.isNotNull(button); 94 Object gd= button.getLayoutData(); 95 if (gd instanceof GridData) { 96 ((GridData)gd).widthHint= getButtonWidthHint(button); 97 ((GridData)gd).horizontalAlignment = GridData.FILL; 98 } 99 } 100 101 public static int getTableHeightHint(Table table, int rows) { 102 if (table.getFont().equals(JFaceResources.getDefaultFont())) 103 table.setFont(JFaceResources.getDialogFont()); 104 int result= table.getItemHeight() * rows + table.getHeaderHeight(); 105 if (table.getLinesVisible()) 106 result+= table.getGridLineWidth() * (rows - 1); 107 return result; 108 } 109 110 111 } 112 | Popular Tags |