1 11 package org.eclipse.jdt.internal.junit.util; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jdt.internal.junit.wizards.MethodStubsSelectionButtonGroup; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.layout.GridLayout; 19 import org.eclipse.swt.widgets.Button; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Label; 23 24 import org.eclipse.jface.dialogs.IDialogConstants; 25 import org.eclipse.jface.resource.JFaceResources; 26 27 import org.eclipse.jdt.internal.ui.util.PixelConverter; 28 29 public class LayoutUtil { 30 31 34 public static int getNumberOfColumns(MethodStubsSelectionButtonGroup[] editors) { 35 int columnCount= 0; 36 for (int i= 0; i < editors.length; i++) { 37 columnCount= Math.max(editors[i].getNumberOfControls(), columnCount); 38 } 39 return columnCount; 40 } 41 42 46 public static void doDefaultLayout(Composite parent, MethodStubsSelectionButtonGroup[] editors, boolean labelOnTop) { 47 doDefaultLayout(parent, editors, labelOnTop, 0, 0, 0, 0); 48 } 49 50 56 public static void doDefaultLayout(Composite parent, MethodStubsSelectionButtonGroup[] editors, boolean labelOnTop, int minWidth, int minHeight) { 57 doDefaultLayout(parent, editors, labelOnTop, minWidth, minHeight, 0, 0); 58 } 59 60 public static void doDefaultLayout(Composite parent, MethodStubsSelectionButtonGroup[] editors, boolean labelOnTop, int minWidth, int minHeight, int marginWidth, int marginHeight) { 61 int nCulumns= getNumberOfColumns(editors); 62 Control[][] controls= new Control[editors.length][]; 63 for (int i= 0; i < editors.length; i++) { 64 controls[i]= editors[i].doFillIntoGrid(parent, nCulumns); 65 } 66 if (labelOnTop) { 67 nCulumns--; 68 modifyLabelSpans(controls, nCulumns); 69 } 70 GridLayout layout= new GridLayout(); 71 if (marginWidth != SWT.DEFAULT) { 72 layout.marginWidth= marginWidth; 73 } 74 if (marginHeight != SWT.DEFAULT) { 75 layout.marginHeight= marginHeight; 76 } 77 layout.numColumns= nCulumns; 80 parent.setLayout(layout); 81 } 82 83 private static void modifyLabelSpans(Control[][] controls, int nCulumns) { 84 for (int i= 0; i < controls.length; i++) { 85 setHorizontalSpan(controls[i][0], nCulumns); 86 } 87 } 88 89 92 public static void setHorizontalSpan(Control control, int span) { 93 Object ld= control.getLayoutData(); 94 if (ld instanceof GridData) { 95 ((GridData)ld).horizontalSpan= span; 96 } else if (span != 1) { 97 GridData gd= new GridData(); 98 gd.horizontalSpan= span; 99 control.setLayoutData(gd); 100 } 101 } 102 103 106 public static void setWidthHint(Control control, int widthHint) { 107 Object ld= control.getLayoutData(); 108 if (ld instanceof GridData) { 109 ((GridData)ld).widthHint= widthHint; 110 } 111 } 112 113 116 public static void setHorizontalIndent(Control control, int horizontalIndent) { 117 Object ld= control.getLayoutData(); 118 if (ld instanceof GridData) { 119 ((GridData)ld).horizontalIndent= horizontalIndent; 120 } 121 } 122 123 129 public static Control createEmptySpace(Composite parent, int span) { 130 Label label= new Label(parent, SWT.LEFT); 131 GridData gd= new GridData(); 132 gd.horizontalAlignment= GridData.BEGINNING; 133 gd.grabExcessHorizontalSpace= false; 134 gd.horizontalSpan= span; 135 gd.horizontalIndent= 0; 136 gd.widthHint= 0; 137 gd.heightHint= 0; 138 label.setLayoutData(gd); 139 return label; 140 } 141 142 143 148 public static int getButtonWidthHint(Button button) { 149 button.setFont(JFaceResources.getDialogFont()); 150 PixelConverter converter= new PixelConverter(button); 151 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 152 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 153 } 154 155 162 public static void setButtonDimensionHint(Button button) { 163 Assert.isNotNull(button); 164 Object gd= button.getLayoutData(); 165 if (gd instanceof GridData) { 166 ((GridData)gd).widthHint= getButtonWidthHint(button); 167 ((GridData)gd).horizontalAlignment = GridData.FILL; 168 } 169 } 170 171 } 172 | Popular Tags |