From 5c6d1a03844be7f165a7d02b7430e84a45d0be12 Mon Sep 17 00:00:00 2001 From: frigoref Date: Sat, 27 Jul 2024 23:02:11 +0200 Subject: [PATCH] push-down units to SimpleUnitPanel (#12785) * opt_getUnitTypes #1 PlayerUnitsPanel.java - break up logic in method getAllUnitCategories into separate methods - add documentation * push-down units to SimpleUnitPanel #1 Replaces usages of method SimpleUnitPanel.setUnitsFromCategories with new method setUnits in: - BottomBar.java (passing through methods setTerritory -> updateTerritoryInfo -> createUnitBar) - HistoryDetailsPanel.java - MovePanel.java - PlacementUnitsCollapsiblePanel.java - PlacePanel.java - PurchasePanel.java SimpleUnitPanel.java - remove method setUnitsFromCategories - add new method setUnits --- .../games/strategy/triplea/ui/BottomBar.java | 15 ++++++--------- .../ui/PlacementUnitsCollapsiblePanel.java | 3 +-- .../strategy/triplea/ui/SimpleUnitPanel.java | 16 +++++++--------- .../ui/history/HistoryDetailsPanel.java | 5 +---- .../games/strategy/triplea/ui/PlacePanel.java | 18 +++++++----------- .../strategy/triplea/ui/PurchasePanel.java | 4 +--- .../triplea/ui/panel/move/MovePanel.java | 2 +- 7 files changed, 24 insertions(+), 39 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/BottomBar.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/BottomBar.java index 6c1d822f145..97b96feedc9 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/BottomBar.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/BottomBar.java @@ -5,12 +5,11 @@ import games.strategy.engine.data.Resource; import games.strategy.engine.data.Territory; import games.strategy.engine.data.TerritoryEffect; +import games.strategy.engine.data.Unit; import games.strategy.engine.data.events.TerritoryListener; import games.strategy.engine.data.events.ZoomMapListener; import games.strategy.triplea.Constants; import games.strategy.triplea.attachments.TerritoryAttachment; -import games.strategy.triplea.util.UnitCategory; -import games.strategy.triplea.util.UnitSeparator; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; @@ -141,10 +140,8 @@ public void setTerritory(final @Nullable Territory territory) { // Get all the needed data while holding a lock, then invoke UI updates on the EDT. try (GameData.Unlocker ignored = territory.getData().acquireReadLock()) { final String territoryName = territory.getName(); - final Collection units = - uiContext.isShowUnitsInStatusBar() - ? UnitSeparator.categorize(territory.getUnits()) - : List.of(); + final Collection units = + uiContext.isShowUnitsInStatusBar() ? territory.getUnits() : List.of(); final TerritoryAttachment ta = TerritoryAttachment.get(territory); final IntegerMap resources = new IntegerMap<>(); final List territoryEffectNames; @@ -170,7 +167,7 @@ public void setTerritory(final @Nullable Territory territory) { private void updateTerritoryInfo( String territoryName, List territoryEffectNames, - Collection units, + Collection units, IntegerMap resources) { // Box layout with horizontal glue on both sides achieves the following desirable properties: // 1. If the content is narrower than the available space, it will be centered. @@ -237,11 +234,11 @@ private Border createBorderToFillAvailableHeight(int componentHeight, int availa return BorderFactory.createEmptyBorder(topPad, 0, bottomPad, 0); } - private SimpleUnitPanel createUnitBar(Collection units) { + private SimpleUnitPanel createUnitBar(Collection units) { final var unitBar = new SimpleUnitPanel(uiContext, SimpleUnitPanel.Style.SMALL_ICONS_ROW); unitBar.setScaleFactor(0.5); unitBar.setShowCountsForSingleUnits(false); - unitBar.setUnitsFromCategories(units); + unitBar.setUnits(units); // Constrain the preferred size to the available size so that unit images that may not fully fit // don't cause layout issues. final int unitsWidth = unitBar.getPreferredSize().width; diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/PlacementUnitsCollapsiblePanel.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/PlacementUnitsCollapsiblePanel.java index ec9677d6545..3cf7dd43be6 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/PlacementUnitsCollapsiblePanel.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/PlacementUnitsCollapsiblePanel.java @@ -7,7 +7,6 @@ import games.strategy.engine.data.GameStep; import games.strategy.engine.data.Unit; import games.strategy.triplea.settings.ClientSetting; -import games.strategy.triplea.util.UnitSeparator; import java.util.Collection; import java.util.List; import javax.swing.SwingUtilities; @@ -48,7 +47,7 @@ private void updateStep() { SwingUtilities.invokeLater( () -> { if (shouldRenderPanelForThisGameStep && !playerUnits.isEmpty()) { - unitsToPlacePanel.setUnitsFromCategories(UnitSeparator.categorize(playerUnits)); + unitsToPlacePanel.setUnits(playerUnits); panel.setVisible(true); } else { panel.setVisible(false); diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/SimpleUnitPanel.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/SimpleUnitPanel.java index 6b04cd1f847..2ae6e10ecda 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/SimpleUnitPanel.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/SimpleUnitPanel.java @@ -1,6 +1,5 @@ package games.strategy.triplea.ui; -import com.google.common.collect.Lists; import games.strategy.engine.data.GameData; import games.strategy.engine.data.GamePlayer; import games.strategy.engine.data.GameState; @@ -17,7 +16,6 @@ import games.strategy.triplea.util.UnitCategory; import games.strategy.triplea.util.UnitSeparator; import java.awt.Image; -import java.util.ArrayList; import java.util.Collection; import java.util.Map; import java.util.Set; @@ -118,18 +116,18 @@ public void setUnitsFromRepairRuleMap( } /** - * Adds units to the panel based on the specified unit categories. + * Adds {@code units} to the panel in accordance to their {@code UnitCategory}. * - * @param categories a collection of UnitCategories. + * @param units units to be placed */ - public void setUnitsFromCategories(final Collection categories) { + public void setUnits(final Collection units) { removeAll(); - if (categories.isEmpty()) { + if (units.isEmpty()) { return; } - final GameData gameData = categories.iterator().next().getUnitAttachment().getData(); - final ArrayList unitCategories = Lists.newArrayList(categories); - UnitSeparator.sortUnitCategories(unitCategories, gameData); + final GameData gameData = units.iterator().next().getUnitAttachment().getData(); + final Collection unitCategories = + UnitSeparator.getSortedUnitCategories(units, gameData, uiContext.getMapData()); for (final UnitCategory category : unitCategories) { addUnits( category.getOwner(), diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryDetailsPanel.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryDetailsPanel.java index 5a57a461d31..87a84761ef0 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryDetailsPanel.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryDetailsPanel.java @@ -11,8 +11,6 @@ import games.strategy.triplea.ui.DicePanel; import games.strategy.triplea.ui.SimpleUnitPanel; import games.strategy.triplea.ui.panels.map.MapPanel; -import games.strategy.triplea.util.UnitCategory; -import games.strategy.triplea.util.UnitSeparator; import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; @@ -117,9 +115,8 @@ private void showTerritory(final Territory territory) { } private void renderUnits(final GridBagConstraints mainConstraints, final Collection units) { - final Collection unitsCategories = UnitSeparator.categorize(units); final SimpleUnitPanel unitsPanel = new SimpleUnitPanel(mapPanel.getUiContext()); - unitsPanel.setUnitsFromCategories(unitsCategories); + unitsPanel.setUnits(units); content.add(unitsPanel, mainConstraints); } } diff --git a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/PlacePanel.java b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/PlacePanel.java index 377ba6067e1..72491e05aa4 100644 --- a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/PlacePanel.java +++ b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/PlacePanel.java @@ -18,8 +18,6 @@ import games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate; import games.strategy.triplea.ui.panels.map.MapPanel; import games.strategy.triplea.ui.panels.map.MapSelectionListener; -import games.strategy.triplea.util.UnitCategory; -import games.strategy.triplea.util.UnitSeparator; import java.awt.Component; import java.awt.Dimension; import java.awt.Toolkit; @@ -113,7 +111,7 @@ public void territorySelected(final Territory territory, final MouseDetails e) { } private void updateStep() { - final Collection unitsToPlace; + final Collection unitsToPlace; final boolean showUnitsToPlace; final GameData data = getData(); try (GameData.Unlocker ignored = data.acquireReadLock()) { @@ -134,7 +132,7 @@ private void updateStep() { // If we're past the production step (even if player didn't produce anything) or // there are units that are available to place, show the panel (set unitsToPlace). showUnitsToPlace = (postProductionStep || !playerUnits.isEmpty()); - unitsToPlace = showUnitsToPlace ? UnitSeparator.categorize(playerUnits) : null; + unitsToPlace = showUnitsToPlace ? playerUnits : List.of(); if (GameStep.isPurchaseOrBidStep(step.getName())) { postProductionStep = true; } @@ -150,7 +148,7 @@ private void updateStep() { SwingUtilities.invokeLater( () -> { if (showUnitsToPlace) { - unitsToPlacePanel.setUnitsFromCategories(unitsToPlace); + unitsToPlacePanel.setUnits(unitsToPlace); SwingComponents.redraw(unitsToPlacePanel); } else { unitsToPlacePanel.removeAll(); @@ -160,19 +158,19 @@ private void updateStep() { @Override public void gameDataChanged(final Change change) { - final Collection unitsToPlace; + final Collection unitsToPlace; final GameData data = getData(); try (GameData.Unlocker ignored = data.acquireReadLock()) { final GamePlayer player = data.getSequence().getStep().getPlayerId(); if (player == null) { return; } - unitsToPlace = UnitSeparator.categorize(player.getUnits()); + unitsToPlace = player.getUnits(); } SwingUtilities.invokeLater( () -> { - unitsToPlacePanel.setUnitsFromCategories(unitsToPlace); + unitsToPlacePanel.setUnits(unitsToPlace); unitsToPlacePanel.revalidate(); unitsToPlacePanel.repaint(); }); @@ -308,8 +306,6 @@ protected final List getAdditionalButtons() { } private void updateUnits() { - final Collection unitCategories = - UnitSeparator.categorize(getCurrentPlayer().getUnits()); - unitsToPlacePanel.setUnitsFromCategories(unitCategories); + unitsToPlacePanel.setUnits(getCurrentPlayer().getUnits()); } } diff --git a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/PurchasePanel.java b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/PurchasePanel.java index 48640240439..f8a225159fb 100644 --- a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/PurchasePanel.java +++ b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/PurchasePanel.java @@ -16,7 +16,6 @@ import games.strategy.triplea.delegate.PurchaseDelegate; import games.strategy.triplea.formatter.MyFormatter; import games.strategy.triplea.ui.panels.map.MapPanel; -import games.strategy.triplea.util.UnitSeparator; import java.awt.event.ActionEvent; import java.util.Collection; import javax.swing.AbstractAction; @@ -134,8 +133,7 @@ public void display(final GamePlayer gamePlayer) { add(SwingComponents.leftBox(purchasedUnits)); try (GameData.Unlocker ignored = getData().acquireReadLock()) { - purchasedPreviousRoundsUnits.setUnitsFromCategories( - UnitSeparator.categorize(gamePlayer.getUnits())); + purchasedPreviousRoundsUnits.setUnits(gamePlayer.getUnits()); add(Box.createVerticalStrut(4)); if (!gamePlayer.getUnitCollection().isEmpty()) { add(SwingComponents.leftBox(purchasedPreviousRoundsLabel)); diff --git a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/panel/move/MovePanel.java b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/panel/move/MovePanel.java index d75053baf94..bd6db502f43 100644 --- a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/panel/move/MovePanel.java +++ b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/panel/move/MovePanel.java @@ -591,7 +591,7 @@ private boolean showScrambleWarningAndConfirmMove( new SimpleUnitPanel( getMap().getUiContext(), SimpleUnitPanel.Style.SMALL_ICONS_WRAPPED_WITH_LABEL_WHEN_EMPTY); - unitPanel.setUnitsFromCategories(UnitSeparator.categorize(possibleScramblers)); + unitPanel.setUnits(possibleScramblers); final String message = "Warning: Units may scramble from nearby territories to defend:"; final JPanel panel = new JPanelBuilder()