1 11 package org.eclipse.jdt.internal.ui.wizards.dialogfields; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.layout.GridData; 15 import org.eclipse.swt.layout.GridLayout; 16 import org.eclipse.swt.widgets.Composite; 17 import org.eclipse.swt.widgets.Control; 18 19 public class LayoutUtil { 20 21 24 public static int getNumberOfColumns(DialogField[] editors) { 25 int nCulumns= 0; 26 for (int i= 0; i < editors.length; i++) { 27 nCulumns= Math.max(editors[i].getNumberOfControls(), nCulumns); 28 } 29 return nCulumns; 30 } 31 32 36 public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop) { 37 doDefaultLayout(parent, editors, labelOnTop, 0, 0); 38 } 39 40 46 public static void doDefaultLayout(Composite parent, DialogField[] editors, boolean labelOnTop, int marginWidth, int marginHeight) { 47 int nCulumns= getNumberOfColumns(editors); 48 Control[][] controls= new Control[editors.length][]; 49 for (int i= 0; i < editors.length; i++) { 50 controls[i]= editors[i].doFillIntoGrid(parent, nCulumns); 51 } 52 if (labelOnTop) { 53 nCulumns--; 54 modifyLabelSpans(controls, nCulumns); 55 } 56 GridLayout layout= null; 57 if (parent.getLayout() instanceof GridLayout) { 58 layout= (GridLayout) parent.getLayout(); 59 } else { 60 layout= new GridLayout(); 61 } 62 if (marginWidth != SWT.DEFAULT) { 63 layout.marginWidth= marginWidth; 64 } 65 if (marginHeight != SWT.DEFAULT) { 66 layout.marginHeight= marginHeight; 67 } 68 layout.numColumns= nCulumns; 69 parent.setLayout(layout); 70 } 71 72 private static void modifyLabelSpans(Control[][] controls, int nCulumns) { 73 for (int i= 0; i < controls.length; i++) { 74 setHorizontalSpan(controls[i][0], nCulumns); 75 } 76 } 77 78 81 public static void setHorizontalSpan(Control control, int span) { 82 Object ld= control.getLayoutData(); 83 if (ld instanceof GridData) { 84 ((GridData)ld).horizontalSpan= span; 85 } else if (span != 1) { 86 GridData gd= new GridData(); 87 gd.horizontalSpan= span; 88 control.setLayoutData(gd); 89 } 90 } 91 92 95 public static void setWidthHint(Control control, int widthHint) { 96 Object ld= control.getLayoutData(); 97 if (ld instanceof GridData) { 98 ((GridData)ld).widthHint= widthHint; 99 } 100 } 101 102 105 public static void setHeightHint(Control control, int heightHint) { 106 Object ld= control.getLayoutData(); 107 if (ld instanceof GridData) { 108 ((GridData)ld).heightHint= heightHint; 109 } 110 } 111 112 115 public static void setHorizontalIndent(Control control, int horizontalIndent) { 116 Object ld= control.getLayoutData(); 117 if (ld instanceof GridData) { 118 ((GridData)ld).horizontalIndent= horizontalIndent; 119 } 120 } 121 122 125 public static void setHorizontalGrabbing(Control control) { 126 Object ld= control.getLayoutData(); 127 if (ld instanceof GridData) { 128 ((GridData)ld).grabExcessHorizontalSpace= true; 129 } 130 } 131 132 } 133 | Popular Tags |