1 7 package javax.swing; 8 9 10 import java.awt.*; 11 import java.io.Serializable ; 12 13 79 public class SizeRequirements implements Serializable { 80 81 87 public int minimum; 88 89 95 public int preferred; 96 97 103 public int maximum; 104 105 110 public float alignment; 111 112 117 public SizeRequirements() { 118 minimum = 0; 119 preferred = 0; 120 maximum = 0; 121 alignment = 0.5f; 122 } 123 124 133 public SizeRequirements(int min, int pref, int max, float a) { 134 minimum = min; 135 preferred = pref; 136 maximum = max; 137 alignment = a > 1.0f ? 1.0f : a < 0.0f ? 0.0f : a; 138 } 139 140 146 public String toString() { 147 return "[" + minimum + "," + preferred + "," + maximum + "]@" + alignment; 148 } 149 150 164 public static SizeRequirements getTiledSizeRequirements(SizeRequirements [] 165 children) { 166 SizeRequirements total = new SizeRequirements (); 167 for (int i = 0; i < children.length; i++) { 168 SizeRequirements req = children[i]; 169 total.minimum = (int) Math.min((long) total.minimum + (long) req.minimum, Integer.MAX_VALUE); 170 total.preferred = (int) Math.min((long) total.preferred + (long) req.preferred, Integer.MAX_VALUE); 171 total.maximum = (int) Math.min((long) total.maximum + (long) req.maximum, Integer.MAX_VALUE); 172 } 173 return total; 174 } 175 176 187 public static SizeRequirements getAlignedSizeRequirements(SizeRequirements [] 188 children) { 189 SizeRequirements totalAscent = new SizeRequirements (); 190 SizeRequirements totalDescent = new SizeRequirements (); 191 for (int i = 0; i < children.length; i++) { 192 SizeRequirements req = children[i]; 193 194 int ascent = (int) (req.alignment * req.minimum); 195 int descent = req.minimum - ascent; 196 totalAscent.minimum = Math.max(ascent, totalAscent.minimum); 197 totalDescent.minimum = Math.max(descent, totalDescent.minimum); 198 199 ascent = (int) (req.alignment * req.preferred); 200 descent = req.preferred - ascent; 201 totalAscent.preferred = Math.max(ascent, totalAscent.preferred); 202 totalDescent.preferred = Math.max(descent, totalDescent.preferred); 203 204 ascent = (int) (req.alignment * req.maximum); 205 descent = req.maximum - ascent; 206 totalAscent.maximum = Math.max(ascent, totalAscent.maximum); 207 totalDescent.maximum = Math.max(descent, totalDescent.maximum); 208 } 209 int min = (int) Math.min((long) totalAscent.minimum + (long) totalDescent.minimum, Integer.MAX_VALUE); 210 int pref = (int) Math.min((long) totalAscent.preferred + (long) totalDescent.preferred, Integer.MAX_VALUE); 211 int max = (int) Math.min((long) totalAscent.maximum + (long) totalDescent.maximum, Integer.MAX_VALUE); 212 float alignment = 0.0f; 213 if (min > 0) { 214 alignment = (float) totalAscent.minimum / min; 215 alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? 0.0f : alignment; 216 } 217 return new SizeRequirements (min, pref, max, alignment); 218 } 219 220 241 public static void calculateTiledPositions(int allocated, 242 SizeRequirements total, 243 SizeRequirements [] children, 244 int[] offsets, 245 int[] spans) { 246 calculateTiledPositions(allocated, total, children, offsets, spans, true); 247 } 248 249 280 public static void calculateTiledPositions(int allocated, 281 SizeRequirements total, 282 SizeRequirements [] children, 283 int[] offsets, 284 int[] spans, 285 boolean forward) { 286 long min = 0; 291 long pref = 0; 292 long max = 0; 293 for (int i = 0; i < children.length; i++) { 294 min += children[i].minimum; 295 pref += children[i].preferred; 296 max += children[i].maximum; 297 } 298 if (allocated >= pref) { 299 expandedTile(allocated, min, pref, max, children, offsets, spans, forward); 300 } else { 301 compressedTile(allocated, min, pref, max, children, offsets, spans, forward); 302 } 303 } 304 305 private static void compressedTile(int allocated, long min, long pref, long max, 306 SizeRequirements [] request, 307 int[] offsets, int[] spans, 308 boolean forward) { 309 310 float totalPlay = Math.min(pref - allocated, pref - min); 312 float factor = (pref - min == 0) ? 0.0f : totalPlay / (pref - min); 313 314 int totalOffset; 316 if( forward ) { 317 totalOffset = 0; 319 for (int i = 0; i < spans.length; i++) { 320 offsets[i] = totalOffset; 321 SizeRequirements req = request[i]; 322 float play = factor * (req.preferred - req.minimum); 323 spans[i] = (int)(req.preferred - play); 324 totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE); 325 } 326 } else { 327 totalOffset = allocated; 329 for (int i = 0; i < spans.length; i++) { 330 SizeRequirements req = request[i]; 331 float play = factor * (req.preferred - req.minimum); 332 spans[i] = (int)(req.preferred - play); 333 offsets[i] = totalOffset - spans[i]; 334 totalOffset = (int) Math.max((long) totalOffset - (long) spans[i], 0); 335 } 336 } 337 } 338 339 private static void expandedTile(int allocated, long min, long pref, long max, 340 SizeRequirements [] request, 341 int[] offsets, int[] spans, 342 boolean forward) { 343 344 float totalPlay = Math.min(allocated - pref, max - pref); 346 float factor = (max - pref == 0) ? 0.0f : totalPlay / (max - pref); 347 348 int totalOffset; 350 if( forward ) { 351 totalOffset = 0; 353 for (int i = 0; i < spans.length; i++) { 354 offsets[i] = totalOffset; 355 SizeRequirements req = request[i]; 356 int play = (int)(factor * (req.maximum - req.preferred)); 357 spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE); 358 totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE); 359 } 360 } else { 361 totalOffset = allocated; 363 for (int i = 0; i < spans.length; i++) { 364 SizeRequirements req = request[i]; 365 int play = (int)(factor * (req.maximum - req.preferred)); 366 spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE); 367 offsets[i] = totalOffset - spans[i]; 368 totalOffset = (int) Math.max((long) totalOffset - (long) spans[i], 0); 369 } 370 } 371 } 372 373 398 public static void calculateAlignedPositions(int allocated, 399 SizeRequirements total, 400 SizeRequirements [] children, 401 int[] offsets, 402 int[] spans) { 403 calculateAlignedPositions( allocated, total, children, offsets, spans, true ); 404 } 405 406 436 public static void calculateAlignedPositions(int allocated, 437 SizeRequirements total, 438 SizeRequirements [] children, 439 int[] offsets, 440 int[] spans, 441 boolean normal) { 442 float totalAlignment = normal ? total.alignment : 1.0f - total.alignment; 443 int totalAscent = (int)(allocated * totalAlignment); 444 int totalDescent = allocated - totalAscent; 445 for (int i = 0; i < children.length; i++) { 446 SizeRequirements req = children[i]; 447 float alignment = normal ? req.alignment : 1.0f - req.alignment; 448 int maxAscent = (int)(req.maximum * alignment); 449 int maxDescent = req.maximum - maxAscent; 450 int ascent = Math.min(totalAscent, maxAscent); 451 int descent = Math.min(totalDescent, maxDescent); 452 453 offsets[i] = totalAscent - ascent; 454 spans[i] = (int) Math.min((long) ascent + (long) descent, Integer.MAX_VALUE); 455 } 456 } 457 458 466 public static int[] adjustSizes(int delta, SizeRequirements [] children) { 467 return new int[0]; 468 } 469 } 470 | Popular Tags |