1 11 package org.eclipse.swt.layout; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.widgets.*; 15 16 78 public final class FormAttachment { 79 83 public int numerator; 84 85 91 public int denominator = 100; 92 93 104 public int offset; 105 106 110 public Control control; 111 112 130 public int alignment; 131 132 141 public FormAttachment () { 142 } 143 144 154 public FormAttachment (int numerator) { 155 this (numerator, 100, 0); 156 } 157 158 167 public FormAttachment (int numerator, int offset) { 168 this (numerator, 100, offset); 169 } 170 171 181 public FormAttachment (int numerator, int denominator, int offset) { 182 if (denominator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO); 183 this.numerator = numerator; 184 this.denominator = denominator; 185 this.offset = offset; 186 } 187 188 197 public FormAttachment (Control control) { 198 this (control, 0, SWT.DEFAULT); 199 } 200 201 210 public FormAttachment (Control control, int offset) { 211 this (control, offset, SWT.DEFAULT); 212 } 213 214 222 public FormAttachment (Control control, int offset, int alignment) { 223 this.control = control; 224 this.offset = offset; 225 this.alignment = alignment; 226 } 227 228 FormAttachment divide (int value) { 229 return new FormAttachment (numerator, denominator * value, offset / value); 230 } 231 232 int gcd (int m, int n) { 233 int temp; 234 m = Math.abs (m); 235 n = Math.abs (n); 236 if (m < n) { 237 temp = m; 238 m = n; 239 n = temp; 240 } 241 while (n != 0){ 242 temp = m; 243 m = n; 244 n = temp % n; 245 } 246 return m; 247 } 248 249 FormAttachment minus (FormAttachment attachment) { 250 FormAttachment solution = new FormAttachment (); 251 solution.numerator = numerator * attachment.denominator - denominator * attachment.numerator; 252 solution.denominator = denominator * attachment.denominator; 253 int gcd = gcd (solution.denominator, solution.numerator); 254 solution.numerator = solution.numerator / gcd; 255 solution.denominator = solution.denominator / gcd; 256 solution.offset = offset - attachment.offset; 257 return solution; 258 } 259 260 FormAttachment minus (int value) { 261 return new FormAttachment (numerator, denominator, offset - value); 262 } 263 264 FormAttachment plus (FormAttachment attachment) { 265 FormAttachment solution = new FormAttachment (); 266 solution.numerator = numerator * attachment.denominator + denominator * attachment.numerator; 267 solution.denominator = denominator * attachment.denominator; 268 int gcd = gcd (solution.denominator, solution.numerator); 269 solution.numerator = solution.numerator / gcd; 270 solution.denominator = solution.denominator / gcd; 271 solution.offset = offset + attachment.offset; 272 return solution; 273 } 274 275 FormAttachment plus (int value) { 276 return new FormAttachment (numerator, denominator, offset + value); 277 } 278 279 int solveX (int value) { 280 if (denominator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO); 281 return ((numerator * value) / denominator) + offset; 282 } 283 284 int solveY (int value) { 285 if (numerator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO); 286 return (value - offset) * denominator / numerator; 287 } 288 289 295 public String toString () { 296 String string = control != null ? control.toString () : numerator + "/" + denominator; 297 return "{y = (" + string + (offset >= 0 ? ")x + " + offset: ")x - " + (-offset))+"}"; 298 } 299 300 } 301 | Popular Tags |