1 19 20 package org.netbeans.modules.form.layoutdesign; 21 22 import java.awt.Rectangle ; 23 24 29 30 class LayoutRegion implements LayoutConstants { 31 32 static final int[] POINT_COUNT = new int[] { 3, 4 }; 34 35 static final int ALL_POINTS = Integer.MAX_VALUE; 37 38 static final int NO_POINT = Integer.MIN_VALUE; 40 41 static final int UNKNOWN = Integer.MIN_VALUE; 43 44 int positions[][]; 49 50 LayoutRegion() { 51 positions = new int[DIM_COUNT][]; 52 positions[HORIZONTAL] = new int[] { UNKNOWN, UNKNOWN, UNKNOWN }; 53 positions[VERTICAL] = new int[] { UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN }; } 55 56 LayoutRegion(LayoutRegion reg) { 57 this(); 58 set(reg); 59 } 60 61 boolean isSet() { 62 return isSet(HORIZONTAL) && isSet(VERTICAL); 63 } 64 65 boolean isSet(int dimension) { 66 return positions[dimension][LEADING] != UNKNOWN 67 && positions[dimension][TRAILING] != UNKNOWN; 68 } 69 70 int size(int dimension) { 71 int trail = positions[dimension][TRAILING]; 72 int lead = positions[dimension][LEADING]; 73 return trail != UNKNOWN && lead != UNKNOWN ? trail - lead : UNKNOWN; 74 } 75 76 79 void set(Rectangle bounds, int baselinePos) { 80 int[] horiz = positions[HORIZONTAL]; 81 horiz[LEADING] = bounds.x; 82 horiz[TRAILING] = bounds.x + bounds.width; 83 horiz[CENTER] = bounds.x + bounds.width / 2; 84 85 int[] vert = positions[VERTICAL]; 86 vert[LEADING] = bounds.y; 87 vert[TRAILING] = bounds.y + bounds.height; 88 vert[CENTER] = bounds.y + bounds.height / 2; 89 vert[BASELINE] = baselinePos; 90 } 91 92 98 Rectangle toRectangle(Rectangle bounds) { 99 int[] horiz = positions[HORIZONTAL]; 100 bounds.x = horiz[LEADING]; 101 bounds.width = horiz[TRAILING] - bounds.x; 102 int[] vert = positions[VERTICAL]; 103 bounds.y = vert[LEADING]; 104 bounds.height = vert[TRAILING] - bounds.y; 105 return bounds; 106 } 107 108 111 void set(LayoutRegion reg) { 112 for (int i=0; i < DIM_COUNT; i++) { 113 set(i, reg); 114 } 115 } 116 117 120 void set(int dimension, LayoutRegion reg) { 121 int[] pos = positions[dimension]; 122 int[] setPos = reg.positions[dimension]; 123 for (int j=0; j < pos.length; j++) { 124 pos[j] = setPos[j]; 125 } 126 } 127 128 void set(int dimension, int leading, int trailing) { 129 int[] pos = positions[dimension]; 130 if (pos[LEADING] != leading || pos[TRAILING] != trailing) { 131 pos[LEADING] = leading; 132 pos[TRAILING] = trailing; 133 pos[CENTER] = leading != UNKNOWN && trailing != UNKNOWN ? 134 (leading + trailing) / 2 : UNKNOWN; 135 if (dimension == VERTICAL) { 136 pos[BASELINE] = UNKNOWN; } 138 } 139 } 140 141 144 void reset() { 145 for (int i=0; i < DIM_COUNT; i++) { 146 int[] pos = positions[i]; 147 for (int j=0; j < pos.length; j++) 148 pos[j] = UNKNOWN; 149 } 150 } 151 152 157 void reshape(int[] points, int[] moves) { 158 for (int i=0; i < DIM_COUNT; i++) { 159 reshape(i, (points != null ? points[i] : ALL_POINTS), moves[i]); 160 } 161 } 162 163 void reshape(int dimension, int align, int move) { 164 int[] pos = positions[dimension]; 165 if (align == ALL_POINTS) { for (int j=0; j < pos.length; j++) { 167 if (pos[j] != UNKNOWN) 168 pos[j] += move; 169 } 170 } 171 else if (align != NO_POINT) { assert align == LEADING || align == TRAILING; 173 if (pos[align] != UNKNOWN) { 174 pos[align] += move; 175 if (pos[LEADING] != UNKNOWN && pos[TRAILING] != UNKNOWN) { 176 if (pos[LEADING] > pos[TRAILING]) { pos[align] = pos[align^1]; 178 } 179 pos[CENTER] = (pos[LEADING] + pos[TRAILING]) / 2; 180 } 181 if (dimension == VERTICAL && move != 0) { 182 pos[BASELINE] = UNKNOWN; } 184 } 185 } 186 } 187 188 191 void expand(LayoutRegion reg) { 192 for (int i=0; i < DIM_COUNT; i++) { 193 expand(reg, i); 194 } 195 } 196 197 void expand(LayoutRegion reg, int dimension) { 198 int[] pos = positions[dimension]; 199 int[] exPos = reg.positions[dimension]; 200 if (exPos[LEADING] != UNKNOWN 201 && (pos[LEADING] == UNKNOWN || exPos[LEADING] < pos[LEADING])) 202 { 203 pos[LEADING] = exPos[LEADING]; 204 } 205 if (exPos[TRAILING] != UNKNOWN 206 && (pos[TRAILING] == UNKNOWN || exPos[TRAILING] > pos[TRAILING])) 207 { 208 pos[TRAILING] = exPos[TRAILING]; 209 } 210 if (pos[LEADING] != UNKNOWN && pos[TRAILING] != UNKNOWN) { 211 pos[CENTER] = (pos[LEADING] + pos[TRAILING]) / 2; 212 } 213 } 214 215 222 238 239 static boolean isValidCoordinate(int pos) { 240 return pos > Short.MIN_VALUE && pos < Short.MAX_VALUE; 241 } 242 243 251 static int distance(LayoutRegion r1, LayoutRegion r2, 252 int dimension, 253 int align1, int align2) 254 { 255 int pos1 = r1.positions[dimension][align1]; 256 int pos2 = r2.positions[dimension][align2]; 257 return pos1 != UNKNOWN && pos2 != UNKNOWN ? pos2 - pos1 : UNKNOWN; 258 } 259 260 267 static int minDistance(LayoutRegion r1, LayoutRegion r2, int dimension) { 268 int[] pos1 = r1.positions[dimension]; 269 int[] pos2 = r2.positions[dimension]; 270 int min = UNKNOWN; 271 int sign = 1; 272 for (int i=0; i < pos1.length; i++) { 273 if (pos1[i] != UNKNOWN && pos2[i] != UNKNOWN) { 274 int dst = pos2[i] - pos1[i]; 275 int s; 276 if (dst < 0) { 277 dst = -dst; 278 s = -1; 279 } 280 else s = 1; 281 if (min == UNKNOWN || dst < min) { 282 min = dst; 283 sign = s; 284 } 285 } 286 } 287 return min * sign; 288 } 289 290 298 static int nonOverlapDistance(LayoutRegion r1, LayoutRegion r2, int dimension) { 299 int[] pos1 = r1.positions[dimension]; 300 int[] pos2 = r2.positions[dimension]; 301 int dst = pos2[LEADING] - pos1[TRAILING]; 302 if (dst >= 0) { 303 return dst; 304 } 305 dst = pos2[TRAILING] - pos1[LEADING]; 306 if (dst <= 0) { 307 return dst; 308 } 309 return 0; 310 } 311 312 317 static boolean pointInside(LayoutRegion contained, int alignment, LayoutRegion container, int dimension) { 318 int[] pos = container.positions[dimension]; 319 int point = contained.positions[dimension][alignment]; 320 assert point != UNKNOWN && pos[LEADING] != UNKNOWN && pos[TRAILING] != UNKNOWN; 321 if (alignment == LEADING) { 322 return point >= pos[LEADING] && point < pos[TRAILING]; 323 } 324 return point > pos[LEADING] && point <= pos[TRAILING]; 326 } 328 329 332 static boolean overlap(LayoutRegion r1, LayoutRegion r2, int dimension, 333 int margin) 334 { 335 int[] pos1 = r1.positions[dimension]; 336 int[] pos2 = r2.positions[dimension]; 337 assert pos1[LEADING] != UNKNOWN && pos1[TRAILING] != UNKNOWN 338 && pos2[LEADING] != UNKNOWN && pos2[TRAILING] != UNKNOWN; 339 return pos1[TRAILING] + margin > pos2[LEADING] 340 && pos1[LEADING] - margin < pos2[TRAILING]; 341 } 342 343 346 static boolean sameSpace(LayoutRegion r1, LayoutRegion r2) { 347 return sameSpace(r1, r2, HORIZONTAL) && sameSpace(r1, r2, VERTICAL); 348 } 349 350 353 static boolean sameSpace(LayoutRegion r1, LayoutRegion r2, int dimension) { 354 return r1.positions[dimension][LEADING] == r2.positions[dimension][LEADING] 355 && r1.positions[dimension][TRAILING] == r2.positions[dimension][TRAILING]; 356 } 357 } 358 | Popular Tags |