1 11 package org.eclipse.ui.internal.layout; 12 13 import java.util.List ; 14 15 import org.eclipse.jface.util.Geometry; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.graphics.Point; 18 import org.eclipse.swt.graphics.Rectangle; 19 import org.eclipse.swt.widgets.Button; 20 import org.eclipse.swt.widgets.Combo; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Label; 24 import org.eclipse.swt.widgets.ProgressBar; 25 import org.eclipse.swt.widgets.Sash; 26 import org.eclipse.swt.widgets.Scale; 27 import org.eclipse.swt.widgets.Slider; 28 import org.eclipse.swt.widgets.Text; 29 import org.eclipse.swt.widgets.ToolBar; 30 import org.eclipse.swt.widgets.Tree; 31 32 37 public class SizeCache { 38 private Control control; 39 40 private Point preferredSize; 41 42 private Point cachedWidth; 43 44 private Point cachedHeight; 45 46 49 private boolean flushChildren; 50 51 55 private boolean independentDimensions = false; 56 57 61 private boolean preferredWidthOrLargerIsMinimumHeight = false; 62 63 private int widthAdjustment = 0; 68 69 private int heightAdjustment = 0; 70 71 73 public SizeCache() { 74 this(null); 75 } 76 77 83 public SizeCache(Control control) { 84 setControl(control); 85 } 86 87 93 public void setControl(Control newControl) { 94 if (newControl != control) { 95 control = newControl; 96 if (control == null) { 97 independentDimensions = true; 98 preferredWidthOrLargerIsMinimumHeight = false; 99 widthAdjustment = 0; 100 heightAdjustment = 0; 101 } else { 102 independentDimensions = independentLengthAndWidth(control); 103 preferredWidthOrLargerIsMinimumHeight = isPreferredWidthMaximum(control); 104 computeHintOffset(control); 105 flush(); 106 } 107 } 108 } 109 110 115 public Control getControl() { 116 return control; 117 } 118 119 123 public void flush() { 124 flush(true); 125 } 126 127 public void flush(boolean recursive) { 128 preferredSize = null; 129 cachedWidth = null; 130 cachedHeight = null; 131 this.flushChildren = recursive; 132 } 133 134 private Point getPreferredSize() { 135 if (preferredSize == null) { 136 preferredSize = computeSize(control, SWT.DEFAULT, SWT.DEFAULT); 137 } 138 139 return preferredSize; 140 } 141 142 149 public Point computeSize(int widthHint, int heightHint) { 150 if (control == null) { 151 return new Point(0, 0); 152 } 153 154 if (widthHint != SWT.DEFAULT && heightHint != SWT.DEFAULT) { 156 return new Point(widthHint, heightHint); 157 } 158 159 if (widthHint == SWT.DEFAULT && heightHint == SWT.DEFAULT) { 161 return Geometry.copy(getPreferredSize()); 162 } 163 164 if (independentDimensions) { 167 Point result = Geometry.copy(getPreferredSize()); 168 169 if (widthHint != SWT.DEFAULT) { 170 result.x = widthHint; 171 } 172 173 if (heightHint != SWT.DEFAULT) { 174 result.y = heightHint; 175 } 176 177 return result; 178 } 179 180 if (heightHint == SWT.DEFAULT) { 182 if (preferredSize != null) { 184 if (widthHint == preferredSize.x) { 186 return Geometry.copy(preferredSize); 187 } 188 } 189 190 if (cachedHeight != null) { 192 if (cachedHeight.x == widthHint) { 194 return Geometry.copy(cachedHeight); 195 } 196 } 197 198 if (preferredWidthOrLargerIsMinimumHeight) { 202 getPreferredSize(); 204 205 if (widthHint >= preferredSize.x) { 208 Point result = Geometry.copy(preferredSize); 209 result.x = widthHint; 210 return result; 211 } 212 } 213 214 cachedHeight = computeSize(control, widthHint, heightHint); 217 218 return Geometry.copy(cachedHeight); 219 } 220 221 if (widthHint == SWT.DEFAULT) { 223 if (preferredSize != null) { 225 if (heightHint == preferredSize.y) { 227 return Geometry.copy(preferredSize); 228 } 229 } 230 231 if (cachedWidth != null) { 233 if (cachedWidth.y == heightHint) { 235 return Geometry.copy(cachedWidth); 236 } 237 } 238 239 cachedWidth = computeSize(control, widthHint, heightHint); 240 241 return Geometry.copy(cachedWidth); 242 } 243 244 return computeSize(control, widthHint, heightHint); 245 } 246 247 257 private Point computeSize(Control control, int widthHint, int heightHint) { 258 int adjustedWidthHint = widthHint == SWT.DEFAULT ? SWT.DEFAULT : Math 259 .max(0, widthHint - widthAdjustment); 260 int adjustedHeightHint = heightHint == SWT.DEFAULT ? SWT.DEFAULT : Math 261 .max(0, heightHint - heightAdjustment); 262 263 Point result = control.computeSize(adjustedWidthHint, 264 adjustedHeightHint, flushChildren); 265 flushChildren = false; 266 267 270 if (widthHint != SWT.DEFAULT) { 271 result.x = widthHint; 272 } 273 274 if (heightHint != SWT.DEFAULT) { 275 result.y = heightHint; 276 } 277 278 return result; 279 } 280 281 297 static boolean independentLengthAndWidth(Control control) { 298 if (control == null) { 299 return true; 300 } 301 302 if (control instanceof Button || control instanceof ProgressBar 303 || control instanceof Sash || control instanceof Scale 304 || control instanceof Slider || control instanceof List 305 || control instanceof Combo || control instanceof Tree) { 306 return true; 307 } 308 309 if (control instanceof Label || control instanceof Text) { 310 return (control.getStyle() & SWT.WRAP) == 0; 311 } 312 313 316 return false; 317 } 318 319 328 private void computeHintOffset(Control control) { 329 if (control instanceof Composite) { 330 Composite composite = (Composite) control; 332 Rectangle trim = composite.computeTrim(0, 0, 0, 0); 333 334 widthAdjustment = trim.width; 335 heightAdjustment = trim.height; 336 } else { 337 widthAdjustment = control.getBorderWidth() * 2; 339 heightAdjustment = widthAdjustment; 340 } 341 } 342 343 358 private static boolean isPreferredWidthMaximum(Control control) { 359 return (control instanceof ToolBar 360 || control instanceof Label); 362 } 363 364 } 365 | Popular Tags |