295 |
{ |
{ |
296 |
// Adjust size if the size is changed. |
// Adjust size if the size is changed. |
297 |
Rectangle bounds = a.getBounds(); |
Rectangle bounds = a.getBounds(); |
|
|
|
298 |
if (bounds.width != getWidth() || bounds.height != getHeight()) |
if (bounds.width != getWidth() || bounds.height != getHeight()) |
299 |
setSize(bounds.width, bounds.height); |
setSize(bounds.width, bounds.height); |
300 |
|
|
356 |
} |
} |
357 |
|
|
358 |
/** |
/** |
359 |
|
* Calculates the layout of the children of this <code>BoxView</code> along |
360 |
|
* the specified axis. |
361 |
|
* |
362 |
|
* @param span the target span |
363 |
|
* @param axis the axis that is examined |
364 |
|
* @param offsets an empty array, filled with the offsets of the children |
365 |
|
* @param spans an empty array, filled with the spans of the children |
366 |
|
*/ |
367 |
|
protected void baselineLayout(int span, int axis, int[] offsets, |
368 |
|
int[] spans) |
369 |
|
{ |
370 |
|
if (axis == myAxis) |
371 |
|
layoutMajorAxis(span, axis, offsets, spans); |
372 |
|
else |
373 |
|
layoutMinorAxis(span, axis, offsets, spans); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
* Calculates the size requirements of this <code>BoxView</code> along |
* Calculates the size requirements of this <code>BoxView</code> along |
378 |
* its major axis, that is the axis specified in the constructor. |
* its major axis, that is the axis specified in the constructor. |
379 |
* |
* |
387 |
protected SizeRequirements calculateMajorAxisRequirements(int axis, |
protected SizeRequirements calculateMajorAxisRequirements(int axis, |
388 |
SizeRequirements sr) |
SizeRequirements sr) |
389 |
{ |
{ |
390 |
if (sr == null) |
SizeRequirements[] childReqs = getChildRequirements(axis); |
391 |
sr = new SizeRequirements(); |
return SizeRequirements.getTiledSizeRequirements(childReqs); |
|
else |
|
|
{ |
|
|
sr.maximum = 0; |
|
|
sr.minimum = 0; |
|
|
sr.preferred = 0; |
|
|
sr.alignment = 0.5F; |
|
|
} |
|
|
|
|
|
int count = getViewCount(); |
|
|
|
|
|
// Sum up the sizes of the children along the specified axis. |
|
|
for (int i = 0; i < count; ++i) |
|
|
{ |
|
|
View child = getView(i); |
|
|
sr.minimum += child.getMinimumSpan(axis); |
|
|
sr.preferred += child.getPreferredSpan(axis); |
|
|
sr.maximum += child.getMaximumSpan(axis); |
|
|
} |
|
|
return sr; |
|
392 |
} |
} |
393 |
|
|
394 |
/** |
/** |
406 |
protected SizeRequirements calculateMinorAxisRequirements(int axis, |
protected SizeRequirements calculateMinorAxisRequirements(int axis, |
407 |
SizeRequirements sr) |
SizeRequirements sr) |
408 |
{ |
{ |
409 |
if (sr == null) |
SizeRequirements[] childReqs = getChildRequirements(axis); |
410 |
sr = new SizeRequirements(); |
return SizeRequirements.getAlignedSizeRequirements(childReqs); |
|
else |
|
|
{ |
|
|
sr.maximum = 0; |
|
|
sr.minimum = 0; |
|
|
sr.preferred = 0; |
|
|
sr.alignment = 0.5F; |
|
|
} |
|
|
|
|
|
int count = getViewCount(); |
|
|
|
|
|
int aboveBaseline = 0; |
|
|
int belowBaseline = 0; |
|
|
int aboveBaselineMin = 0; |
|
|
int belowBaselineMin = 0; |
|
|
int aboveBaselineMax = 0; |
|
|
int belowBaselineMax = 0; |
|
|
|
|
|
for (int i = 0; i < count; ++i) |
|
|
{ |
|
|
View child = getView(i); |
|
|
float align = child.getAlignment(axis); |
|
|
int pref = (int) child.getPreferredSpan(axis); |
|
|
int min = (int) child.getMinimumSpan(axis); |
|
|
int max = (int) child.getMaximumSpan(axis); |
|
|
aboveBaseline += (int) (align * pref); |
|
|
belowBaseline += (int) ((1.F - align) * pref); |
|
|
aboveBaselineMin += (int) (align * min); |
|
|
belowBaselineMin += (int) ((1.F - align) * min); |
|
|
aboveBaselineMax += (int) (align * max); |
|
|
belowBaselineMax += (int) ((1.F - align) * max); |
|
|
} |
|
|
sr.minimum = aboveBaselineMin + belowBaselineMin; |
|
|
sr.maximum = aboveBaselineMax + belowBaselineMax; |
|
|
sr.preferred = aboveBaseline + belowBaseline; |
|
|
if (aboveBaseline == 0) |
|
|
sr.alignment = 1.0F; |
|
|
else |
|
|
sr.alignment = (float) (sr.preferred / aboveBaseline); |
|
|
|
|
|
return sr; |
|
411 |
} |
} |
412 |
|
|
413 |
/** |
/** |
525 |
this.width = width; |
this.width = width; |
526 |
this.height = height; |
this.height = height; |
527 |
|
|
528 |
if (myAxis == X_AXIS) |
baselineLayout(width, X_AXIS, offsetsX, spansX); |
529 |
{ |
baselineLayout(height, Y_AXIS, offsetsY, spansY); |
|
layoutMajorAxis(width, X_AXIS, offsetsX, spansX); |
|
|
layoutMinorAxis(height, Y_AXIS, offsetsY, spansY); |
|
|
} |
|
|
else |
|
|
{ |
|
|
layoutMajorAxis(height, Y_AXIS, offsetsY, spansY); |
|
|
layoutMinorAxis(width, X_AXIS, offsetsX, spansX); |
|
|
} |
|
530 |
} |
} |
531 |
|
|
532 |
/** |
/** |
541 |
protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, |
protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, |
542 |
int[] spans) |
int[] spans) |
543 |
{ |
{ |
544 |
// Allocate SizeRequirements for each child view. |
SizeRequirements[] childReqs = getChildRequirements(axis); |
|
int count = getViewCount(); |
|
|
SizeRequirements[] childReqs = new SizeRequirements[count]; |
|
|
for (int i = 0; i < count; ++i) |
|
|
{ |
|
|
View view = getView(i); |
|
|
childReqs[i] = new SizeRequirements((int) view.getMinimumSpan(axis), |
|
|
(int) view.getPreferredSpan(axis), |
|
|
(int) view.getMaximumSpan(axis), |
|
|
view.getAlignment(axis)); |
|
|
} |
|
|
|
|
545 |
// Calculate the spans and offsets using the SizeRequirements uility |
// Calculate the spans and offsets using the SizeRequirements uility |
546 |
// methods. |
// methods. |
547 |
SizeRequirements.calculateTiledPositions(targetSpan, null, childReqs, |
SizeRequirements.calculateTiledPositions(targetSpan, null, childReqs, |
548 |
offsets, spans); |
offsets, spans); |
|
|
|
549 |
validateLayout(axis); |
validateLayout(axis); |
550 |
} |
} |
551 |
|
|
561 |
protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, |
protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, |
562 |
int[] spans) |
int[] spans) |
563 |
{ |
{ |
564 |
// Allocate SizeRequirements for each child view. |
SizeRequirements[] childReqs = getChildRequirements(axis); |
|
int count = getViewCount(); |
|
|
SizeRequirements[] childReqs = new SizeRequirements[count]; |
|
|
for (int i = 0; i < count; ++i) |
|
|
{ |
|
|
View view = getView(i); |
|
|
childReqs[i] = new SizeRequirements((int) view.getMinimumSpan(axis), |
|
|
(int) view.getPreferredSpan(axis), |
|
|
(int) view.getMaximumSpan(axis), |
|
|
view.getAlignment(axis)); |
|
|
} |
|
|
|
|
565 |
// Calculate the spans and offsets using the SizeRequirements uility |
// Calculate the spans and offsets using the SizeRequirements uility |
566 |
// methods. |
// methods. |
567 |
SizeRequirements.calculateAlignedPositions(targetSpan, null, childReqs, |
SizeRequirements.calculateAlignedPositions(targetSpan, null, childReqs, |
633 |
if (axis == Y_AXIS) |
if (axis == Y_AXIS) |
634 |
yLayoutValid = true; |
yLayoutValid = true; |
635 |
} |
} |
636 |
|
|
637 |
|
/** |
638 |
|
* Returns the size requirements of this view's children for the major |
639 |
|
* axis. |
640 |
|
* |
641 |
|
* @return the size requirements of this view's children for the major |
642 |
|
* axis |
643 |
|
*/ |
644 |
|
SizeRequirements[] getChildRequirements(int axis) |
645 |
|
{ |
646 |
|
// Allocate SizeRequirements for each child view. |
647 |
|
int count = getViewCount(); |
648 |
|
SizeRequirements[] childReqs = new SizeRequirements[count]; |
649 |
|
for (int i = 0; i < count; ++i) |
650 |
|
{ |
651 |
|
View view = getView(i); |
652 |
|
childReqs[i] = new SizeRequirements((int) view.getMinimumSpan(axis), |
653 |
|
(int) view.getPreferredSpan(axis), |
654 |
|
(int) view.getMaximumSpan(axis), |
655 |
|
view.getAlignment(axis)); |
656 |
|
} |
657 |
|
return childReqs; |
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
|
* Returns the span for the child view with the given index for the specified |
662 |
|
* axis. |
663 |
|
* |
664 |
|
* @param axis the axis to examine, either <code>X_AXIS</code> or |
665 |
|
* <code>Y_AXIS</code> |
666 |
|
* @param index the index of the child for for which to return the span |
667 |
|
* |
668 |
|
* @return the span for the child view with the given index for the specified |
669 |
|
* axis |
670 |
|
*/ |
671 |
|
protected int getSpan(int axis, int childIndex) |
672 |
|
{ |
673 |
|
if (axis == X_AXIS) |
674 |
|
return spansX[childIndex]; |
675 |
|
else |
676 |
|
return spansY[childIndex]; |
677 |
|
} |
678 |
|
|
679 |
|
/** |
680 |
|
* Returns the offset for the child view with the given index for the |
681 |
|
* specified axis. |
682 |
|
* |
683 |
|
* @param axis the axis to examine, either <code>X_AXIS</code> or |
684 |
|
* <code>Y_AXIS</code> |
685 |
|
* @param index the index of the child for for which to return the span |
686 |
|
* |
687 |
|
* @return the offset for the child view with the given index for the |
688 |
|
* specified axis |
689 |
|
*/ |
690 |
|
protected int getOffset(int axis, int childIndex) |
691 |
|
{ |
692 |
|
if (axis == X_AXIS) |
693 |
|
return offsetsX[childIndex]; |
694 |
|
else |
695 |
|
return offsetsY[childIndex]; |
696 |
|
} |
697 |
|
|
698 |
|
/** |
699 |
|
* Returns the alignment for this box view for the specified axis. The |
700 |
|
* axis that is tiled (the major axis) will be requested to be aligned |
701 |
|
* centered (0.5F). The minor axis alignment depends on the child view's |
702 |
|
* total alignment. |
703 |
|
* |
704 |
|
* @param axis the axis which is examined |
705 |
|
* |
706 |
|
* @return the alignment for this box view for the specified axis |
707 |
|
*/ |
708 |
|
public float getAlignment(int axis) |
709 |
|
{ |
710 |
|
if (axis == myAxis) |
711 |
|
return 0.5F; |
712 |
|
else |
713 |
|
return baselineRequirements(axis, null).alignment; |
714 |
|
} |
715 |
} |
} |