1 11 package org.eclipse.swt.layout; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.widgets.*; 17 18 45 public final class FormData { 46 55 public int width = SWT.DEFAULT; 56 65 public int height = SWT.DEFAULT; 66 70 public FormAttachment left; 71 75 public FormAttachment right; 76 79 public FormAttachment top; 80 84 public FormAttachment bottom; 85 86 int cacheWidth = -1, cacheHeight = -1; 87 int defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1; 88 int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1; 89 FormAttachment cacheLeft, cacheRight, cacheTop, cacheBottom; 90 boolean isVisited, needed; 91 92 96 public FormData () { 97 } 98 99 107 public FormData (int width, int height) { 108 this.width = width; 109 this.height = height; 110 } 111 112 void computeSize (Control control, int wHint, int hHint, boolean flushCache) { 113 if (cacheWidth != -1 && cacheHeight != -1) return; 114 if (wHint == this.width && hHint == this.height) { 115 if (defaultWidth == -1 || defaultHeight == -1 || wHint != defaultWhint || hHint != defaultHhint) { 116 Point size = control.computeSize (wHint, hHint, flushCache); 117 defaultWhint = wHint; 118 defaultHhint = hHint; 119 defaultWidth = size.x; 120 defaultHeight = size.y; 121 } 122 cacheWidth = defaultWidth; 123 cacheHeight = defaultHeight; 124 return; 125 } 126 if (currentWidth == -1 || currentHeight == -1 || wHint != currentWhint || hHint != currentHhint) { 127 Point size = control.computeSize (wHint, hHint, flushCache); 128 currentWhint = wHint; 129 currentHhint = hHint; 130 currentWidth = size.x; 131 currentHeight = size.y; 132 } 133 cacheWidth = currentWidth; 134 cacheHeight = currentHeight; 135 } 136 137 void flushCache () { 138 cacheWidth = cacheHeight = -1; 139 defaultHeight = defaultWidth = -1; 140 currentHeight = currentWidth = -1; 141 } 142 143 int getWidth (Control control, boolean flushCache) { 144 needed = true; 145 computeSize (control, width, height, flushCache); 146 return cacheWidth; 147 } 148 149 int getHeight (Control control, boolean flushCache) { 150 computeSize (control, width, height, flushCache); 151 return cacheHeight; 152 } 153 154 FormAttachment getBottomAttachment (Control control, int spacing, boolean flushCache) { 155 if (cacheBottom != null) return cacheBottom; 156 if (isVisited) return cacheBottom = new FormAttachment (0, getHeight (control, flushCache)); 157 if (bottom == null) { 158 if (top == null) return cacheBottom = new FormAttachment (0, getHeight (control, flushCache)); 159 return cacheBottom = getTopAttachment (control, spacing, flushCache).plus (getHeight (control, flushCache)); 160 } 161 Control bottomControl = bottom.control; 162 if (bottomControl != null) { 163 if (bottomControl.isDisposed ()) { 164 bottom.control = bottomControl = null; 165 } else { 166 if (bottomControl.getParent () != control.getParent ()) { 167 bottomControl = null; 168 } 169 } 170 } 171 if (bottomControl == null) return cacheBottom = bottom; 172 isVisited = true; 173 FormData bottomData = (FormData) bottomControl.getLayoutData (); 174 FormAttachment bottomAttachment = bottomData.getBottomAttachment (bottomControl, spacing, flushCache); 175 switch (bottom.alignment) { 176 case SWT.BOTTOM: 177 cacheBottom = bottomAttachment.plus (bottom.offset); 178 break; 179 case SWT.CENTER: { 180 FormAttachment topAttachment = bottomData.getTopAttachment (bottomControl, spacing, flushCache); 181 FormAttachment bottomHeight = bottomAttachment.minus (topAttachment); 182 cacheBottom = bottomAttachment.minus (bottomHeight.minus (getHeight (control, flushCache)).divide (2)); 183 break; 184 } 185 default: { 186 FormAttachment topAttachment = bottomData.getTopAttachment (bottomControl, spacing, flushCache); 187 cacheBottom = topAttachment.plus (bottom.offset - spacing); 188 break; 189 } 190 } 191 isVisited = false; 192 return cacheBottom; 193 } 194 195 FormAttachment getLeftAttachment (Control control, int spacing, boolean flushCache) { 196 if (cacheLeft != null) return cacheLeft; 197 if (isVisited) return cacheLeft = new FormAttachment (0, 0); 198 if (left == null) { 199 if (right == null) return cacheLeft = new FormAttachment (0, 0); 200 return cacheLeft = getRightAttachment (control, spacing, flushCache).minus (getWidth (control, flushCache)); 201 } 202 Control leftControl = left.control; 203 if (leftControl != null) { 204 if (leftControl.isDisposed ()) { 205 left.control = leftControl = null; 206 } else { 207 if (leftControl.getParent () != control.getParent ()) { 208 leftControl = null; 209 } 210 } 211 } 212 if (leftControl == null) return cacheLeft = left; 213 isVisited = true; 214 FormData leftData = (FormData) leftControl.getLayoutData (); 215 FormAttachment leftAttachment = leftData.getLeftAttachment (leftControl, spacing, flushCache); 216 switch (left.alignment) { 217 case SWT.LEFT: 218 cacheLeft = leftAttachment.plus (left.offset); 219 break; 220 case SWT.CENTER: { 221 FormAttachment rightAttachment = leftData.getRightAttachment (leftControl, spacing, flushCache); 222 FormAttachment leftWidth = rightAttachment.minus (leftAttachment); 223 cacheLeft = leftAttachment.plus (leftWidth.minus (getWidth (control, flushCache)).divide (2)); 224 break; 225 } 226 default: { 227 FormAttachment rightAttachment = leftData.getRightAttachment (leftControl, spacing, flushCache); 228 cacheLeft = rightAttachment.plus (left.offset + spacing); 229 } 230 } 231 isVisited = false; 232 return cacheLeft; 233 } 234 235 String getName () { 236 String string = getClass ().getName (); 237 int index = string.lastIndexOf ('.'); 238 if (index == -1) return string; 239 return string.substring (index + 1, string.length ()); 240 } 241 242 FormAttachment getRightAttachment (Control control, int spacing, boolean flushCache) { 243 if (cacheRight != null) return cacheRight; 244 if (isVisited) return cacheRight = new FormAttachment (0, getWidth (control, flushCache)); 245 if (right == null) { 246 if (left == null) return cacheRight = new FormAttachment (0, getWidth (control, flushCache)); 247 return cacheRight = getLeftAttachment (control, spacing, flushCache).plus (getWidth (control, flushCache)); 248 } 249 Control rightControl = right.control; 250 if (rightControl != null) { 251 if (rightControl.isDisposed ()) { 252 right.control = rightControl = null; 253 } else { 254 if (rightControl.getParent () != control.getParent ()) { 255 rightControl = null; 256 } 257 } 258 } 259 if (rightControl == null) return cacheRight = right; 260 isVisited = true; 261 FormData rightData = (FormData) rightControl.getLayoutData (); 262 FormAttachment rightAttachment = rightData.getRightAttachment (rightControl, spacing, flushCache); 263 switch (right.alignment) { 264 case SWT.RIGHT: 265 cacheRight = rightAttachment.plus (right.offset); 266 break; 267 case SWT.CENTER: { 268 FormAttachment leftAttachment = rightData.getLeftAttachment (rightControl, spacing, flushCache); 269 FormAttachment rightWidth = rightAttachment.minus (leftAttachment); 270 cacheRight = rightAttachment.minus (rightWidth.minus (getWidth (control, flushCache)).divide (2)); 271 break; 272 } 273 default: { 274 FormAttachment leftAttachment = rightData.getLeftAttachment (rightControl, spacing, flushCache); 275 cacheRight = leftAttachment.plus (right.offset - spacing); 276 break; 277 } 278 } 279 isVisited = false; 280 return cacheRight; 281 } 282 283 FormAttachment getTopAttachment (Control control, int spacing, boolean flushCache) { 284 if (cacheTop != null) return cacheTop; 285 if (isVisited) return cacheTop = new FormAttachment (0, 0); 286 if (top == null) { 287 if (bottom == null) return cacheTop = new FormAttachment (0, 0); 288 return cacheTop = getBottomAttachment (control, spacing, flushCache).minus (getHeight (control, flushCache)); 289 } 290 Control topControl = top.control; 291 if (topControl != null) { 292 if (topControl.isDisposed ()) { 293 top.control = topControl = null; 294 } else { 295 if (topControl.getParent () != control.getParent ()) { 296 topControl = null; 297 } 298 } 299 } 300 if (topControl == null) return cacheTop = top; 301 isVisited = true; 302 FormData topData = (FormData) topControl.getLayoutData (); 303 FormAttachment topAttachment = topData.getTopAttachment (topControl, spacing, flushCache); 304 switch (top.alignment) { 305 case SWT.TOP: 306 cacheTop = topAttachment.plus (top.offset); 307 break; 308 case SWT.CENTER: { 309 FormAttachment bottomAttachment = topData.getBottomAttachment (topControl, spacing, flushCache); 310 FormAttachment topHeight = bottomAttachment.minus (topAttachment); 311 cacheTop = topAttachment.plus (topHeight.minus (getHeight (control, flushCache)).divide (2)); 312 break; 313 } 314 default: { 315 FormAttachment bottomAttachment = topData.getBottomAttachment (topControl, spacing, flushCache); 316 cacheTop = bottomAttachment.plus (top.offset + spacing); 317 break; 318 } 319 } 320 isVisited = false; 321 return cacheTop; 322 } 323 324 330 public String toString () { 331 String string = getName()+" {"; 332 if (width != SWT.DEFAULT) string += "width="+width+" "; 333 if (height != SWT.DEFAULT) string += "height="+height+" "; 334 if (left != null) string += "left="+left+" "; 335 if (right != null) string += "right="+right+" "; 336 if (top != null) string += "top="+top+" "; 337 if (bottom != null) string += "bottom="+bottom+" "; 338 string = string.trim(); 339 string += "}"; 340 return string; 341 } 342 343 } 344 | Popular Tags |