From 7dfa486570129f546d0853dc13dcb60b05368ec9 Mon Sep 17 00:00:00 2001 From: Stian Grenborgen Date: Sun, 14 Jan 2024 13:47:41 +0100 Subject: [PATCH] The WrapLayout can now make every child component have the same size as the largest child. --- .../freecol/client/gui/panel/WrapLayout.java | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/net/sf/freecol/client/gui/panel/WrapLayout.java b/src/net/sf/freecol/client/gui/panel/WrapLayout.java index c0f269f84..d042351d0 100644 --- a/src/net/sf/freecol/client/gui/panel/WrapLayout.java +++ b/src/net/sf/freecol/client/gui/panel/WrapLayout.java @@ -97,6 +97,7 @@ public class WrapLayout implements LayoutManager { private LayoutStyle layoutStyle = LayoutStyle.BALANCED; private HorizontalGap horizontalGap = HorizontalGap.AUTO; private Dimension forceComponentSize = null; + private boolean allComponentsWithTheSameSize = false; private int minHorizontalGap = 0; private int minVerticalGap = 0; @@ -176,6 +177,20 @@ public class WrapLayout implements LayoutManager { */ public WrapLayout withForceComponentSize(Dimension forceComponentSize) { this.forceComponentSize = forceComponentSize; + this.allComponentsWithTheSameSize = false; + return this; + } + + /** + * Uses the biggest preferred/minimum size for all child components. + * + * @param allComponentsWithTheSameSize {@code true} if all components should + * get the same same. + * @return This object, in order to support method chaining. + */ + public WrapLayout withAllComponentsWithTheSameSize(boolean allComponentsWithTheSameSize) { + this.allComponentsWithTheSameSize = allComponentsWithTheSameSize; + this.forceComponentSize = null; return this; } @@ -267,12 +282,28 @@ public class WrapLayout implements LayoutManager { final List rows = new ArrayList<>(); final Dimension currentLayoutSize = new Dimension(0, 0); + Dimension sharedComponentSize = forceComponentSize; + if (allComponentsWithTheSameSize) { + sharedComponentSize = new Dimension(0, 0); + for (Component component : getVisibleComponents(target, layoutBottomToTop)) { + final Dimension d = preferred ? component.getPreferredSize() : component.getMinimumSize(); + if (d.width > sharedComponentSize.width) { + sharedComponentSize.width = d.width; + } + if (d.height > sharedComponentSize.height) { + sharedComponentSize.height = d.height; + } + } + } + List currentChildren = new ArrayList<>(); Dimension currentRowSize = new Dimension(0, 0); for (Component component : getVisibleComponents(target, layoutBottomToTop)) { - Dimension d = preferred ? component.getPreferredSize() : component.getMinimumSize(); - if (forceComponentSize != null) { - d = forceComponentSize; + final Dimension d; + if (sharedComponentSize != null) { + d = sharedComponentSize; + } else { + d = preferred ? component.getPreferredSize() : component.getMinimumSize(); } // Can't add the component to current row. Start a new row.