1 11 package org.eclipse.jface.layout; 12 import org.eclipse.jface.util.Geometry; 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.ModifyListener; 15 import org.eclipse.swt.graphics.Point; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.layout.GridLayout; 18 import org.eclipse.swt.widgets.Button; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Layout; 22 import org.eclipse.swt.widgets.Scrollable; 23 24 class LayoutGenerator { 25 26 29 private static final Point defaultSize = new Point(150, 150); 30 31 34 private static final int wrapSize = 350; 35 36 private static final GridDataFactory nonWrappingLabelData = GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.CENTER).grab(false, false); 37 38 private static boolean hasStyle(Control c, int style) { 39 return (c.getStyle() & style) != 0; 40 } 41 42 50 public static void generateLayout(Composite toGenerate) { 51 Control[] children = toGenerate.getChildren(); 52 53 for (int i = 0; i < children.length; i++) { 54 Control control = children[i]; 55 56 if (control.getLayoutData() != null) { 58 continue; 59 } 60 61 applyLayoutDataTo(control); 62 } 63 } 64 65 private static void applyLayoutDataTo(Control control) { 66 defaultsFor(control).applyTo(control); 67 } 68 69 79 public static GridDataFactory defaultsFor(Control control) { 80 if (control instanceof Button) { 81 Button button = (Button) control; 82 83 if (hasStyle(button, SWT.CHECK)) { 84 return nonWrappingLabelData.copy(); 85 } else { 86 return GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).hint(Geometry.max(button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true), LayoutConstants.getMinButtonSize())); 87 } 88 } 89 90 if (control instanceof Scrollable) { 91 Scrollable scrollable = (Scrollable) control; 92 93 if (scrollable instanceof Composite) { 94 Composite composite = (Composite) control; 95 96 Layout theLayout = composite.getLayout(); 97 if (theLayout instanceof GridLayout) { 98 boolean growsHorizontally = false; 99 boolean growsVertically = false; 100 101 Control[] children = composite.getChildren(); 102 for (int i = 0; i < children.length; i++) { 103 Control child = children[i]; 104 105 GridData data = (GridData) child.getLayoutData(); 106 107 if (data != null) { 108 if (data.grabExcessHorizontalSpace) { 109 growsHorizontally = true; 110 } 111 if (data.grabExcessVerticalSpace) { 112 growsVertically = true; 113 } 114 } 115 } 116 117 return GridDataFactory.fillDefaults().grab(growsHorizontally, growsVertically); 118 } 119 } 120 } 121 122 boolean wrapping = hasStyle(control, SWT.WRAP); 123 124 boolean hScroll = hasStyle(control, SWT.H_SCROLL); 128 boolean vScroll = hasStyle(control, SWT.V_SCROLL); 129 130 boolean containsText = hasMethod(control, "setText", new Class [] { String .class }); 132 boolean userEditable = !hasStyle(control, SWT.READ_ONLY) && containsText && hasMethod(control, "addModifyListener", new Class [] { ModifyListener.class }); 137 if (userEditable) { 139 if (hasStyle(control, SWT.MULTI)) { 140 vScroll = true; 141 } 142 143 if (!wrapping) { 144 hScroll = true; 145 } 146 } 147 148 int hHint = SWT.DEFAULT; 150 boolean grabHorizontal = hScroll; 151 152 if (hScroll) { 156 hHint = defaultSize.x; 157 } else { 158 168 if (wrapping) { 169 if (containsText) { 170 hHint = wrapSize; 171 grabHorizontal = true; 172 } 173 } 174 } 175 176 int vAlign = SWT.FILL; 177 178 if (!vScroll && !wrapping && !userEditable && containsText) { 182 vAlign = SWT.CENTER; 183 } 184 185 return GridDataFactory.fillDefaults().grab(grabHorizontal, vScroll).align(SWT.FILL, vAlign).hint(hHint, vScroll ? defaultSize.y : SWT.DEFAULT); 186 } 187 188 private static boolean hasMethod(Control control, String name, Class [] parameterTypes) { 189 Class c = control.getClass(); 190 try { 191 return c.getMethod(name, parameterTypes) != null; 192 } catch (SecurityException e) { 193 return false; 194 } catch (NoSuchMethodException e) { 195 return false; 196 } 197 } 198 } 199 | Popular Tags |