ui: fix component layouting on toolbar with non-zero insets

This commit is contained in:
Aleksey Pivovarov
2016-02-01 11:38:17 +03:00
parent 52f14f5377
commit 22320b183e
@@ -456,44 +456,41 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
final int maxHeight = getMaxButtonHeight();
if (myOrientation == SwingConstants.HORIZONTAL) {
int xOffset = insets.left;
int offset = 0;
for (int i = 0; i < componentCount; i++) {
final Rectangle r = bounds.get(i);
r.setBounds(xOffset, (height - maxHeight) / 2, maxWidth, maxHeight);
xOffset += maxWidth;
r.setBounds(insets.left + offset, insets.top + (height - maxHeight) / 2, maxWidth, maxHeight);
offset += maxWidth;
}
}
else {
int yOffset = insets.top;
int offset = 0;
for (int i = 0; i < componentCount; i++) {
final Rectangle r = bounds.get(i);
r.setBounds((width - maxWidth) / 2, yOffset, maxWidth, maxHeight);
yOffset += maxHeight;
r.setBounds(insets.left + (width - maxWidth) / 2, insets.top + offset, maxWidth, maxHeight);
offset += maxHeight;
}
}
}
else {
if (myOrientation == SwingConstants.HORIZONTAL) {
final int maxHeight = getMaxButtonHeight();
int xOffset = insets.left;
final int yOffset = insets.top;
int offset = 0;
for (int i = 0; i < componentCount; i++) {
final Dimension d = getChildPreferredSize(i);
final Rectangle r = bounds.get(i);
r.setBounds(xOffset, yOffset + (maxHeight - d.height) / 2, d.width, d.height);
xOffset += d.width;
r.setBounds(insets.left + offset, insets.top + (maxHeight - d.height) / 2, d.width, d.height);
offset += d.width;
}
}
else {
final int maxWidth = getMaxButtonWidth();
final int xOffset = insets.left;
int yOffset = insets.top;
int offset = 0;
for (int i = 0; i < componentCount; i++) {
final Dimension d = getChildPreferredSize(i);
final Rectangle r = bounds.get(i);
r.setBounds(xOffset + (maxWidth - d.width) / 2, yOffset, d.width, d.height);
yOffset += d.height;
r.setBounds(insets.left + (maxWidth - d.width) / 2, insets.top + offset, d.width, d.height);
offset += d.height;
}
}
}
@@ -513,11 +510,12 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
boolean full = false;
final Insets insets = getInsets();
int widthToFit = sizeToFit.width - insets.left - insets.right;
int heightToFit = sizeToFit.height - insets.top - insets.bottom;
if (myOrientation == SwingConstants.HORIZONTAL) {
int eachX = insets.left;
int eachY = insets.top;
int maxHeight = sizeToFit.height;
int eachX = 0;
int maxHeight = heightToFit;
for (int i = 0; i < componentCount; i++) {
final Component eachComp = getComponent(i);
final boolean isLast = i == componentCount - 1;
@@ -528,26 +526,26 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
if (!full) {
boolean inside;
if (isLast) {
inside = eachX + eachBound.width <= sizeToFit.width;
inside = eachX + eachBound.width <= widthToFit;
} else {
inside = eachX + eachBound.width + autoButtonSize <= sizeToFit.width;
inside = eachX + eachBound.width + autoButtonSize <= widthToFit;
}
if (inside) {
if (eachComp == mySecondaryActionsButton) {
assert isLast;
if (sizeToFit.width != Integer.MAX_VALUE) {
eachBound.x = sizeToFit.width - eachBound.width;
eachX = (int)eachBound.getMaxX();
eachBound.x = sizeToFit.width - insets.right - eachBound.width;
eachX = (int)eachBound.getMaxX() - insets.left;
}
else {
eachBound.x = eachX;
eachBound.x = insets.left + eachX;
}
} else {
eachBound.x = eachX;
eachBound.x = insets.left + eachX;
eachX += eachBound.width;
}
eachBound.y = eachY;
eachBound.y = insets.top;
}
else {
full = true;
@@ -556,7 +554,7 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
if (full) {
if (myAutoPopupRec == null) {
myAutoPopupRec = new Rectangle(eachX, eachY, sizeToFit.width - eachX - 1, sizeToFit.height - 1);
myAutoPopupRec = new Rectangle(insets.left + eachX, insets.top, widthToFit - eachX, heightToFit);
myFirstOutsideIndex = i;
}
eachBound.x = Integer.MAX_VALUE;
@@ -574,21 +572,20 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
}
else {
int eachX = insets.left;
int eachY = insets.top;
int eachY = 0;
for (int i = 0; i < componentCount; i++) {
final Rectangle eachBound = new Rectangle(getChildPreferredSize(i));
if (!full) {
boolean outside;
if (i < componentCount - 1) {
outside = eachY + eachBound.height + autoButtonSize < sizeToFit.height;
outside = eachY + eachBound.height + autoButtonSize < heightToFit;
}
else {
outside = eachY + eachBound.height < sizeToFit.height;
outside = eachY + eachBound.height < heightToFit;
}
if (outside) {
eachBound.x = eachX;
eachBound.y = eachY;
eachBound.x = insets.left;
eachBound.y = insets.top + eachY;
eachY += eachBound.height;
}
else {
@@ -598,7 +595,7 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
if (full) {
if (myAutoPopupRec == null) {
myAutoPopupRec = new Rectangle(eachX, eachY, sizeToFit.width - 1, sizeToFit.height - eachY - 1);
myAutoPopupRec = new Rectangle(insets.left, insets.top + eachY, widthToFit, heightToFit - eachY);
myFirstOutsideIndex = i;
}
eachBound.x = Integer.MAX_VALUE;
@@ -630,6 +627,8 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
LOG.assertTrue(componentCount <= bounds.size());
final Insets insets = getInsets();
int widthToFit = sizeToFit.width - insets.left - insets.right;
int heightToFit = sizeToFit.height - insets.top - insets.bottom;
if (myAdjustTheSameSize) {
if (myOrientation == SwingConstants.HORIZONTAL) {
@@ -637,18 +636,18 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
final int maxHeight = getMaxButtonHeight();
// Lay components out
int xOffset = insets.left;
int yOffset = insets.top;
int xOffset = 0;
int yOffset = 0;
// Calculate max size of a row. It's not possible to make more than 3 row toolbar
final int maxRowWidth = Math.max(sizeToFit.width, componentCount * maxWidth / 3);
final int maxRowWidth = Math.max(widthToFit, componentCount * maxWidth / 3);
for (int i = 0; i < componentCount; i++) {
if (xOffset + maxWidth > maxRowWidth) { // place component at new row
xOffset = insets.left;
xOffset = 0;
yOffset += maxHeight;
}
final Rectangle each = bounds.get(i);
each.setBounds(xOffset, yOffset, maxWidth, maxHeight);
each.setBounds(insets.left + xOffset, insets.top + yOffset, maxWidth, maxHeight);
xOffset += maxWidth;
}
@@ -658,18 +657,18 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
final int maxHeight = getMaxButtonHeight();
// Lay components out
int xOffset = insets.left;
int yOffset = insets.top;
int xOffset = 0;
int yOffset = 0;
// Calculate max size of a row. It's not possible to make more then 3 column toolbar
final int maxRowHeight = Math.max(sizeToFit.height, componentCount * myMinimumButtonSize.height / 3);
final int maxRowHeight = Math.max(heightToFit, componentCount * myMinimumButtonSize.height / 3);
for (int i = 0; i < componentCount; i++) {
if (yOffset + maxHeight > maxRowHeight) { // place component at new row
yOffset = insets.top;
yOffset = 0;
xOffset += maxWidth;
}
final Rectangle each = bounds.get(i);
each.setBounds(xOffset, yOffset, maxWidth, maxHeight);
each.setBounds(insets.left + xOffset, insets.top + yOffset, maxWidth, maxHeight);
yOffset += maxHeight;
}
@@ -687,19 +686,19 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
}
// Lay components out
int xOffset = insets.left;
int yOffset = insets.top;
int xOffset = 0;
int yOffset = 0;
// Calculate max size of a row. It's not possible to make more then 3 row toolbar
final int maxRowWidth = Math.max(getWidth(), componentCount * myMinimumButtonSize.width / 3);
final int maxRowWidth = Math.max(widthToFit, componentCount * myMinimumButtonSize.width / 3);
for (int i = 0; i < componentCount; i++) {
final Dimension d = dims[i];
if (xOffset + d.width > maxRowWidth) { // place component at new row
xOffset = insets.left;
xOffset = 0;
yOffset += rowHeight;
}
final Rectangle each = bounds.get(i);
each.setBounds(xOffset, yOffset + (rowHeight - d.height) / 2, d.width, d.height);
each.setBounds(insets.left + xOffset, insets.top + yOffset + (rowHeight - d.height) / 2, d.width, d.height);
xOffset += d.width;
}
@@ -715,19 +714,19 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
}
// Lay components out
int xOffset = insets.left;
int yOffset = insets.top;
int xOffset = 0;
int yOffset = 0;
// Calculate max size of a row. It's not possible to make more then 3 column toolbar
final int maxRowHeight = Math.max(getHeight(), componentCount * myMinimumButtonSize.height / 3);
final int maxRowHeight = Math.max(heightToFit, componentCount * myMinimumButtonSize.height / 3);
for (int i = 0; i < componentCount; i++) {
final Dimension d = dims[i];
if (yOffset + d.height > maxRowHeight) { // place component at new row
yOffset = insets.top;
yOffset = 0;
xOffset += rowWidth;
}
final Rectangle each = bounds.get(i);
each.setBounds(xOffset + (rowWidth - d.width) / 2, yOffset, d.width, d.height);
each.setBounds(insets.left + xOffset + (rowWidth - d.width) / 2, insets.top + yOffset, d.width, d.height);
yOffset += d.height;
}