/**
* Calculates the desired bounds for the component and returns them within the
* given rectangle.
*/
private void calculateComponentPosition(int index, int defaultWidth, Rectangle rect, Dimension rendererSize, Dimension selectedSize, boolean beforeSelected) {
Style style = getStyle();
int initialY = style.getPadding(TOP);
int initialX = style.getPadding(LEFT);
int selection = getSelectedIndex();
Dimension d = rect.getSize();
int selectedDiff;
// the algorithm illustrated here is very simple despite the "mess" of code...
// The idea is that if we have a "fixed" element we just add up the amount of pixels
// to get it into its place in the screen (nothing for top obviously).
// In order to cause the list to be cyclic we just subtract the list size
// which will cause the bottom elements to "return" from the top.
if (orientation == VERTICAL) {
int height = rendererSize.getHeight();
selectedDiff = selectedSize.getHeight() - height;
rect.setX(initialX);
d.setHeight(height);
d.setWidth(defaultWidth);
int y = 0;
int listHeight = getHeight() - style.getPadding(TOP) - style.getPadding(BOTTOM);
int totalHeight = (height + itemGap) * getModel().getSize() + selectedDiff;
switch (fixedSelection) {
case FIXED_CENTER:
y = listHeight / 2 - (height + itemGap+ selectedDiff) / 2 +
(index - selection) * (height + itemGap);
if(!beforeSelected){
y += selectedDiff;
}
y = recalcOffset(y, totalHeight, listHeight, height + itemGap);
break;
case FIXED_TRAIL:
y = listHeight - (height + itemGap + selectedDiff);
case FIXED_LEAD:
y += (index - selection) * (height + itemGap);
if(index - selection > 0){
y += selectedDiff;
}
y = recalcOffset(y, totalHeight, listHeight, height + itemGap);
break;
default:
y = index * (height + itemGap);
if(!beforeSelected){
y += selectedDiff;
}
break;
}
rect.setY(y + initialY);
if(index == selection){
d.setHeight(d.getHeight() + selectedDiff);
}
} else {
int width = rendererSize.getWidth();
selectedDiff = selectedSize.getWidth() - width;
rect.setY(initialY);
d.setHeight(getHeight() - style.getPadding(TOP) - style.getPadding(BOTTOM));
d.setWidth(width);
int x = 0;
int listWidth = getWidth() - style.getPadding(RIGHT) - style.getPadding(LEFT);
int totalWidth = (width + itemGap) * getModel().getSize() + selectedDiff;
switch (fixedSelection) {
case FIXED_CENTER:
x = listWidth / 2 - (width + itemGap +selectedDiff) / 2 +
(index - selection) * (width + itemGap);