From fc32e66d5fa665ef25d6bf4fa78fb27de295399d Mon Sep 17 00:00:00 2001 From: asvitkine Date: Tue, 22 Aug 2023 14:35:42 -0400 Subject: [PATCH 1/2] Various code clean ups. --- .../delegate/AbstractPlaceDelegate.java | 14 +- .../delegate/battle/MustFightBattle.java | 8 +- .../calculator/BattleCalculatorPanel.java | 752 ++---------------- .../games/strategy/triplea/ui/UiContext.java | 9 +- 4 files changed, 94 insertions(+), 689 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractPlaceDelegate.java b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractPlaceDelegate.java index 220607ccf00..ba58836b0af 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractPlaceDelegate.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractPlaceDelegate.java @@ -1068,15 +1068,10 @@ private IntegerMap getMaxUnitsToBePlacedMap( final Collection notUsableAsOtherProducers = new ArrayList<>(producers); final Map currentAvailablePlacementForOtherProducers = new HashMap<>(); for (final Territory producerTerritory : producers) { - final Collection unitsCanBePlacedByThisProducer = - (Properties.getUnitPlacementRestrictions(getData().getProperties()) - ? CollectionUtils.getMatches( - units, unitWhichRequiresUnitsHasRequiredUnits(producerTerritory, true)) - : new ArrayList<>(units)); final int prodT = getMaxUnitsToBePlacedFrom( producerTerritory, - unitsCanBePlacedByThisProducer, + units, to, player, true, @@ -1152,8 +1147,7 @@ units, unitWhichRequiresUnitsHasRequiredUnits(producer, true)) return Math.max(0, ra.getMaxPlacePerTerritory() - unitCountAlreadyProduced); } // a factory can produce the same number of units as the number of PUs the territory generates - // each turn (or not, if - // it has canProduceXUnits) + // each turn (or not, if it has canProduceXUnits) final int maxConstructions = howManyOfEachConstructionCanPlace(to, producer, unitsCanBePlacedByThisProducer, player) .totalValues(); @@ -1590,7 +1584,7 @@ public Collection unitsAtStartOfStepInTerritory(final Territory to) { return new ArrayList<>(); } final Collection unitsPlacedAlready = getAlreadyProduced(to); - if (Matches.territoryIsWater().test(to)) { + if (to.isWater()) { for (final Territory current : getAllProducers(to, player, null, true)) { unitsPlacedAlready.addAll(getAlreadyProduced(current)); } @@ -1626,7 +1620,7 @@ private boolean wasOwnedUnitThatCanProduceUnitsOrIsFactoryInTerritoryAtStartOfSt // land factories in water can't produce, and sea factories in land can't produce. // air can produce like land if in land, and like sea if in water. .and(to.isWater() ? Matches.unitIsLand().negate() : Matches.unitIsSea().negate()); - return CollectionUtils.countMatches(unitsAtStartOfTurnInTo, factoryMatch) > 0; + return unitsAtStartOfTurnInTo.stream().anyMatch(factoryMatch); } /** diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/battle/MustFightBattle.java b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/battle/MustFightBattle.java index 08224322655..8ec6a9e37c4 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/battle/MustFightBattle.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/battle/MustFightBattle.java @@ -1363,7 +1363,7 @@ private void defenderWins(final IDelegateBridge bridge) { whoWon = WhoWon.DEFENDER; bridge.getDisplayChannelBroadcaster().battleEnd(battleId, defender.getName() + " win"); if (Properties.getAbandonedTerritoriesMayBeTakenOverImmediately(gameData.getProperties())) { - if (CollectionUtils.countMatches(defendingUnits, Matches.unitIsNotInfrastructure()) == 0) { + if (defendingUnits.stream().noneMatch(Matches.unitIsNotInfrastructure())) { final List allyOfAttackerUnits = battleSite.getUnitCollection().getMatches(Matches.unitIsNotInfrastructure()); if (!allyOfAttackerUnits.isEmpty()) { @@ -1378,14 +1378,12 @@ private void defenderWins(final IDelegateBridge bridge) { + " as there are no defenders left", allyOfAttackerUnits); // should we create a new battle records to show the ally capturing the territory (in the - // case where they - // didn't already own/allied it)? + // case where they didn't already own/allied it)? battleTracker.takeOver(battleSite, abandonedToPlayer, bridge, null, allyOfAttackerUnits); } } else { // should we create a new battle records to show the defender capturing the territory (in - // the case where they - // didn't already own/allied it)? + // the case where they didn't already own/allied it)? battleTracker.takeOver(battleSite, defender, bridge, null, defendingUnits); } } diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java b/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java index 57f697437e9..6ae19675db5 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java @@ -25,9 +25,7 @@ import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; -import java.awt.GridBagConstraints; import java.awt.GridBagLayout; -import java.awt.Insets; import java.awt.Window; import java.awt.event.WindowEvent; import java.text.DecimalFormat; @@ -59,6 +57,9 @@ import org.triplea.java.collections.CollectionUtils; import org.triplea.swing.IntTextField; import org.triplea.swing.SwingComponents; +import org.triplea.swing.jpanel.GridBagConstraintsAnchor; +import org.triplea.swing.jpanel.GridBagConstraintsBuilder; +import org.triplea.swing.jpanel.GridBagConstraintsFill; @Slf4j class BattleCalculatorPanel extends JPanel { @@ -208,176 +209,28 @@ class BattleCalculatorPanel extends JPanel { attackAndDefend.setLayout(new GridBagLayout()); final int gap = 20; int row0 = 0; + var builder0 = + new GridBagConstraintsBuilder().gridY(row0++).anchor(GridBagConstraintsAnchor.EAST); + attackAndDefend.add(new JLabel("Attacker: "), builder0.gridX(0).insets(0, gap, gap, 0).build()); + attackAndDefend.add(attackerCombo, builder0.gridX(1).insets(0, 0, gap / 2, gap).build()); + attackAndDefend.add(new JLabel("Defender: "), builder0.gridX(2).insets(0, gap, gap, 0).build()); + attackAndDefend.add(defenderCombo, builder0.gridX(3).insets(0, 0, gap / 2, gap).build()); + builder0 = builder0.gridY(row0++); + attackAndDefend.add(attackerUnitsTotalNumber, builder0.gridX(0).insets(0, gap, 0, 0).build()); attackAndDefend.add( - new JLabel("Attacker: "), - new GridBagConstraints( - 0, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap, gap, 0), - 0, - 0)); + attackerUnitsTotalTuv, builder0.gridX(1).insets(0, gap / 2, 0, gap * 2).build()); + attackAndDefend.add(defenderUnitsTotalNumber, builder0.gridX(2).insets(0, gap, 0, 0).build()); attackAndDefend.add( - attackerCombo, - new GridBagConstraints( - 1, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, gap / 2, gap), - 0, - 0)); + defenderUnitsTotalTuv, builder0.gridX(3).insets(0, gap / 2, 0, gap * 2).build()); + builder0 = builder0.gridY(row0); attackAndDefend.add( - new JLabel("Defender: "), - new GridBagConstraints( - 2, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap, gap, 0), - 0, - 0)); + attackerUnitsTotalHitpoints, builder0.gridX(0).insets(0, gap, gap / 2, 0).build()); attackAndDefend.add( - defenderCombo, - new GridBagConstraints( - 3, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, gap / 2, gap), - 0, - 0)); - row0++; + attackerUnitsTotalPower, builder0.gridX(1).insets(0, gap / 2, gap / 2, gap * 2).build()); attackAndDefend.add( - attackerUnitsTotalNumber, - new GridBagConstraints( - 0, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap, 0, 0), - 0, - 0)); + defenderUnitsTotalHitpoints, builder0.gridX(2).insets(0, gap, gap / 2, 0).build()); attackAndDefend.add( - attackerUnitsTotalTuv, - new GridBagConstraints( - 1, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap / 2, 0, gap * 2), - 0, - 0)); - attackAndDefend.add( - defenderUnitsTotalNumber, - new GridBagConstraints( - 2, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap, 0, 0), - 0, - 0)); - attackAndDefend.add( - defenderUnitsTotalTuv, - new GridBagConstraints( - 3, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap / 2, 0, gap * 2), - 0, - 0)); - row0++; - attackAndDefend.add( - attackerUnitsTotalHitpoints, - new GridBagConstraints( - 0, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap, gap / 2, 0), - 0, - 0)); - attackAndDefend.add( - attackerUnitsTotalPower, - new GridBagConstraints( - 1, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap / 2, gap / 2, gap * 2), - 0, - 0)); - attackAndDefend.add( - defenderUnitsTotalHitpoints, - new GridBagConstraints( - 2, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap, gap / 2, 0), - 0, - 0)); - attackAndDefend.add( - defenderUnitsTotalPower, - new GridBagConstraints( - 3, - row0, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, gap / 2, gap / 2, gap * 2), - 0, - 0)); + defenderUnitsTotalPower, builder0.gridX(3).insets(0, gap / 2, gap / 2, gap * 2).build()); final JPanel attackAndDefendAlignLeft = new JPanel(); attackAndDefendAlignLeft.setLayout(new BorderLayout()); attackAndDefendAlignLeft.add(attackAndDefend, BorderLayout.WEST); @@ -404,533 +257,96 @@ class BattleCalculatorPanel extends JPanel { final JPanel resultsText = new JPanel(); resultsText.setLayout(new GridBagLayout()); int row1 = 0; + GridBagConstraintsBuilder builder = + new GridBagConstraintsBuilder().anchor(GridBagConstraintsAnchor.EAST); + resultsText.add(new JLabel("Attacker Wins:"), builder.gridY(row1++).build()); + resultsText.add(new JLabel("Draw:"), builder.gridY(row1++).build()); + resultsText.add(new JLabel("Defender Wins:"), builder.gridY(row1++).build()); resultsText.add( - new JLabel("Attacker Wins:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, 0, 0), - 0, - 0)); - resultsText.add( - new JLabel("Draw:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, 0, 0), - 0, - 0)); - resultsText.add( - new JLabel("Defender Wins:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, 0, 0), - 0, - 0)); - resultsText.add( - new JLabel("Ave. Defender Units Left:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(6, 0, 0, 0), - 0, - 0)); + new JLabel("Ave. Defender Units Left:"), builder.gridY(row1++).insets(6, 0, 0, 0).build()); resultsText.add( - new JLabel("Units Left If Def Won:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, 0, 0), - 0, - 0)); + new JLabel("Units Left If Def Won:"), builder.gridY(row1++).insets(0, 0, 0, 0).build()); resultsText.add( - new JLabel("Ave. Attacker Units Left:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(6, 0, 0, 0), - 0, - 0)); + new JLabel("Ave. Attacker Units Left:"), builder.gridY(row1++).insets(6, 0, 0, 0).build()); resultsText.add( - new JLabel("Units Left If Att Won:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, 0, 0), - 0, - 0)); + new JLabel("Units Left If Att Won:"), builder.gridY(row1++).insets(0, 0, 0, 0).build()); resultsText.add( - new JLabel("Average TUV Swing:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(6, 0, 0, 0), - 0, - 0)); + new JLabel("Average TUV Swing:"), builder.gridY(row1++).insets(6, 0, 0, 0).build()); resultsText.add( - new JLabel("Average Rounds:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, 0, 0), - 0, - 0)); + new JLabel("Average Rounds:"), builder.gridY(row1++).insets(0, 0, 0, 0).build()); resultsText.add( - new JLabel("Simulation Count:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(15, 0, 0, 0), - 0, - 0)); + new JLabel("Simulation Count:"), builder.gridY(row1++).insets(15, 0, 0, 0).build()); + resultsText.add(new JLabel("Time:"), builder.gridY(row1++).insets(0, 0, 0, 0).build()); + GridBagConstraintsBuilder builder2 = + new GridBagConstraintsBuilder() + .gridWidth(2) + .anchor(GridBagConstraintsAnchor.WEST) + .fill(GridBagConstraintsFill.BOTH); resultsText.add( - new JLabel("Time:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(0, 0, 0, 0), - 0, - 0)); - resultsText.add( - calculateButton, - new GridBagConstraints( - 0, - row1++, - 2, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.BOTH, - new Insets(20, 60, 0, 100), - 0, - 0)); + calculateButton, builder2.gridY(row1++).gridWidth(2).insets(20, 60, 0, 100).build()); final JButton clearButton = new JButton("Clear"); - resultsText.add( - clearButton, - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.BOTH, - new Insets(6, 60, 0, 0), - 0, - 0)); - resultsText.add( - new JLabel("Run Count:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(20, 0, 0, 0), - 0, - 0)); - resultsText.add( - new JLabel("Retreat After Round:"), - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(10, 0, 0, 0), - 0, - 0)); - resultsText.add( - new JLabel("Retreat When X Units Left:"), - new GridBagConstraints( - 0, - row1, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.NONE, - new Insets(10, 0, 0, 0), - 0, - 0)); + resultsText.add(clearButton, builder2.gridY(row1++).gridWidth(1).insets(6, 60, 0, 0).build()); + resultsText.add(new JLabel("Run Count:"), builder.gridY(row1++).insets(20, 0, 0, 0).build()); + resultsText.add( + new JLabel("Retreat After Round:"), builder.gridY(row1++).insets(10, 0, 0, 0).build()); + resultsText.add(new JLabel("Retreat When X Units Left:"), builder.gridY(row1).build()); + GridBagConstraintsBuilder builder3 = + new GridBagConstraintsBuilder() + .gridX(1) + .anchor(GridBagConstraintsAnchor.WEST) + .insets(0, 10, 0, 0); int row2 = 0; - resultsText.add( - attackerWin, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(0, 10, 0, 0), - 0, - 0)); - resultsText.add( - draw, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(0, 10, 0, 0), - 0, - 0)); - resultsText.add( - defenderWin, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(0, 10, 0, 0), - 0, - 0)); - resultsText.add( - defenderLeft, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(6, 10, 0, 0), - 0, - 0)); - resultsText.add( - defenderLeftWhenDefenderWon, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(0, 10, 0, 0), - 0, - 0)); - resultsText.add( - attackerLeft, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(6, 10, 0, 0), - 0, - 0)); - resultsText.add( - attackerLeftWhenAttackerWon, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(0, 10, 0, 0), - 0, - 0)); - resultsText.add( - averageChangeInTuv, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(6, 10, 0, 0), - 0, - 0)); - resultsText.add( - roundsAverage, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(0, 10, 0, 0), - 0, - 0)); - resultsText.add( - count, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(15, 10, 0, 0), - 0, - 0)); - resultsText.add( - time, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(0, 10, 0, 0), - 0, - 0)); + resultsText.add(attackerWin, builder3.gridY(row2++).build()); + resultsText.add(draw, builder3.gridY(row2++).build()); + resultsText.add(defenderWin, builder3.gridY(row2++).build()); + resultsText.add(defenderLeft, builder3.gridY(row2++).build()); + resultsText.add(defenderLeftWhenDefenderWon, builder3.gridY(row2++).build()); + resultsText.add(attackerLeft, builder3.gridY(row2++).insets(6, 10, 0, 0).build()); + resultsText.add( + attackerLeftWhenAttackerWon, builder3.gridY(row2++).insets(0, 10, 0, 0).build()); + resultsText.add(averageChangeInTuv, builder3.gridY(row2++).insets(6, 10, 0, 0).build()); + resultsText.add(roundsAverage, builder3.gridY(row2++).insets(0, 10, 0, 0).build()); + resultsText.add(count, builder3.gridY(row2++).insets(15, 10, 0, 0).build()); + resultsText.add(time, builder3.gridY(row2++).insets(0, 10, 0, 0).build()); + row2++; final JButton swapSidesButton = new JButton("Swap Sides"); resultsText.add( swapSidesButton, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.BOTH, - new Insets(6, 10, 0, 100), - 0, - 0)); + builder3.gridY(row2++).fill(GridBagConstraintsFill.BOTH).insets(6, 10, 0, 100).build()); resultsText.add( numRuns, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(20, 10, 0, 0), - 0, - 0)); - resultsText.add( - retreatAfterXRounds, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(10, 10, 0, 0), - 0, - 0)); - resultsText.add( - retreatAfterXUnitsLeft, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(10, 10, 0, 0), - 0, - 0)); - row1 = row2; + builder3.gridY(row2++).fill(GridBagConstraintsFill.NONE).insets(20, 10, 0, 0).build()); + resultsText.add(retreatAfterXRounds, builder3.gridY(row2++).insets(10, 10, 0, 0).build()); + resultsText.add(retreatAfterXUnitsLeft, builder3.gridY(row2++).build()); + row1 = row2; final JButton orderOfLossesButton = new JButton("Order Of Losses"); resultsText.add( - orderOfLossesButton, - new GridBagConstraints( - 0, - row1++, - 1, - 1, - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.BOTH, - new Insets(10, 15, 0, 0), - 0, - 0)); + orderOfLossesButton, builder.gridX(0).gridY(row1++).insets(10, 15, 0, 0).build()); if (territoryEffectsJList != null) { resultsText.add( new JScrollPane(territoryEffectsJList), - new GridBagConstraints( - 0, - row1, - 1, - territoryEffectsJList.getVisibleRowCount(), - 0, - 0, - GridBagConstraints.EAST, - GridBagConstraints.BOTH, - new Insets(10, 15, 0, 0), - 0, - 0)); + builder + .gridY(row1) + .gridHeight(territoryEffectsJList.getVisibleRowCount()) + .fill(GridBagConstraintsFill.BOTH) + .build()); } resultsText.add( retreatWhenOnlyAirLeftCheckBox, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(10, 10, 0, 5), - 0, - 0)); - resultsText.add( - keepOneAttackingLandUnitCheckBox, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(2, 10, 0, 5), - 0, - 0)); - resultsText.add( - amphibiousCheckBox, - new GridBagConstraints( - 1, - row2++, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(2, 10, 0, 5), - 0, - 0)); - resultsText.add( - landBattleCheckBox, - new GridBagConstraints( - 1, - row2, - 1, - 1, - 0, - 0, - GridBagConstraints.WEST, - GridBagConstraints.NONE, - new Insets(2, 10, 0, 5), - 0, - 0)); + builder + .gridX(1) + .gridY(row2++) + .gridHeight(1) + .anchor(GridBagConstraintsAnchor.WEST) + .fill(GridBagConstraintsFill.NONE) + .insets(10, 10, 0, 5) + .build()); + resultsText.add( + keepOneAttackingLandUnitCheckBox, builder.gridY(row2++).insets(2, 10, 0, 5).build()); + resultsText.add(amphibiousCheckBox, builder.gridY(row2++).build()); + resultsText.add(landBattleCheckBox, builder.gridY(row2).build()); final JPanel resultsPanel = new JPanel(); resultsPanel.add(resultsText); diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/UiContext.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/UiContext.java index e6804c19691..0e1a10cdd80 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/UiContext.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/UiContext.java @@ -334,12 +334,9 @@ private static void closeWindow(final Window window) { // waiting for that lock, then you have a deadlock. // A real life example: player disconnects while you have the battle calc open. // Non-EDT thread does shutdown on IGame and UiContext, causing btl calc to shutdown, - // which calls the - // window closed event on the EDT, and waits for the lock on UiContext to - // removeShutdownWindow, meanwhile - // our non-EDT tries to dispose the battle panel, which requires the EDT with a invokeAndWait, - // resulting in a - // deadlock. + // which calls the window closed event on the EDT, and waits for the lock on UiContext to + // removeShutdownWindow, meanwhile our non-EDT tries to dispose the battle panel, which requires + // the EDT with a invokeAndWait, resulting in a deadlock. SwingUtilities.invokeLater(window::dispose); } From 2c484d635f04554f004e8ddf7df524549473c61f Mon Sep 17 00:00:00 2001 From: asvitkine Date: Tue, 22 Aug 2023 14:42:41 -0400 Subject: [PATCH 2/2] Tweaks. --- .../calculator/BattleCalculatorPanel.java | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java b/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java index 6ae19675db5..7d6a92d167d 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java @@ -209,22 +209,22 @@ class BattleCalculatorPanel extends JPanel { attackAndDefend.setLayout(new GridBagLayout()); final int gap = 20; int row0 = 0; - var builder0 = + GridBagConstraintsBuilder builder0 = new GridBagConstraintsBuilder().gridY(row0++).anchor(GridBagConstraintsAnchor.EAST); attackAndDefend.add(new JLabel("Attacker: "), builder0.gridX(0).insets(0, gap, gap, 0).build()); attackAndDefend.add(attackerCombo, builder0.gridX(1).insets(0, 0, gap / 2, gap).build()); attackAndDefend.add(new JLabel("Defender: "), builder0.gridX(2).insets(0, gap, gap, 0).build()); attackAndDefend.add(defenderCombo, builder0.gridX(3).insets(0, 0, gap / 2, gap).build()); - builder0 = builder0.gridY(row0++); - attackAndDefend.add(attackerUnitsTotalNumber, builder0.gridX(0).insets(0, gap, 0, 0).build()); + attackAndDefend.add( + attackerUnitsTotalNumber, builder0.gridY(row0++).gridX(0).insets(0, gap, 0, 0).build()); attackAndDefend.add( attackerUnitsTotalTuv, builder0.gridX(1).insets(0, gap / 2, 0, gap * 2).build()); attackAndDefend.add(defenderUnitsTotalNumber, builder0.gridX(2).insets(0, gap, 0, 0).build()); attackAndDefend.add( defenderUnitsTotalTuv, builder0.gridX(3).insets(0, gap / 2, 0, gap * 2).build()); - builder0 = builder0.gridY(row0); attackAndDefend.add( - attackerUnitsTotalHitpoints, builder0.gridX(0).insets(0, gap, gap / 2, 0).build()); + attackerUnitsTotalHitpoints, + builder0.gridY(row0).gridX(0).insets(0, gap, gap / 2, 0).build()); attackAndDefend.add( attackerUnitsTotalPower, builder0.gridX(1).insets(0, gap / 2, gap / 2, gap * 2).build()); attackAndDefend.add( @@ -257,26 +257,26 @@ class BattleCalculatorPanel extends JPanel { final JPanel resultsText = new JPanel(); resultsText.setLayout(new GridBagLayout()); int row1 = 0; - GridBagConstraintsBuilder builder = + GridBagConstraintsBuilder builder1 = new GridBagConstraintsBuilder().anchor(GridBagConstraintsAnchor.EAST); - resultsText.add(new JLabel("Attacker Wins:"), builder.gridY(row1++).build()); - resultsText.add(new JLabel("Draw:"), builder.gridY(row1++).build()); - resultsText.add(new JLabel("Defender Wins:"), builder.gridY(row1++).build()); + resultsText.add(new JLabel("Attacker Wins:"), builder1.gridY(row1++).build()); + resultsText.add(new JLabel("Draw:"), builder1.gridY(row1++).build()); + resultsText.add(new JLabel("Defender Wins:"), builder1.gridY(row1++).build()); resultsText.add( - new JLabel("Ave. Defender Units Left:"), builder.gridY(row1++).insets(6, 0, 0, 0).build()); + new JLabel("Ave. Defender Units Left:"), builder1.gridY(row1++).insets(6, 0, 0, 0).build()); resultsText.add( - new JLabel("Units Left If Def Won:"), builder.gridY(row1++).insets(0, 0, 0, 0).build()); + new JLabel("Units Left If Def Won:"), builder1.gridY(row1++).insets(0, 0, 0, 0).build()); resultsText.add( - new JLabel("Ave. Attacker Units Left:"), builder.gridY(row1++).insets(6, 0, 0, 0).build()); + new JLabel("Ave. Attacker Units Left:"), builder1.gridY(row1++).insets(6, 0, 0, 0).build()); resultsText.add( - new JLabel("Units Left If Att Won:"), builder.gridY(row1++).insets(0, 0, 0, 0).build()); + new JLabel("Units Left If Att Won:"), builder1.gridY(row1++).insets(0, 0, 0, 0).build()); resultsText.add( - new JLabel("Average TUV Swing:"), builder.gridY(row1++).insets(6, 0, 0, 0).build()); + new JLabel("Average TUV Swing:"), builder1.gridY(row1++).insets(6, 0, 0, 0).build()); resultsText.add( - new JLabel("Average Rounds:"), builder.gridY(row1++).insets(0, 0, 0, 0).build()); + new JLabel("Average Rounds:"), builder1.gridY(row1++).insets(0, 0, 0, 0).build()); resultsText.add( - new JLabel("Simulation Count:"), builder.gridY(row1++).insets(15, 0, 0, 0).build()); - resultsText.add(new JLabel("Time:"), builder.gridY(row1++).insets(0, 0, 0, 0).build()); + new JLabel("Simulation Count:"), builder1.gridY(row1++).insets(15, 0, 0, 0).build()); + resultsText.add(new JLabel("Time:"), builder1.gridY(row1++).insets(0, 0, 0, 0).build()); GridBagConstraintsBuilder builder2 = new GridBagConstraintsBuilder() .gridWidth(2) @@ -286,10 +286,10 @@ class BattleCalculatorPanel extends JPanel { calculateButton, builder2.gridY(row1++).gridWidth(2).insets(20, 60, 0, 100).build()); final JButton clearButton = new JButton("Clear"); resultsText.add(clearButton, builder2.gridY(row1++).gridWidth(1).insets(6, 60, 0, 0).build()); - resultsText.add(new JLabel("Run Count:"), builder.gridY(row1++).insets(20, 0, 0, 0).build()); + resultsText.add(new JLabel("Run Count:"), builder1.gridY(row1++).insets(20, 0, 0, 0).build()); resultsText.add( - new JLabel("Retreat After Round:"), builder.gridY(row1++).insets(10, 0, 0, 0).build()); - resultsText.add(new JLabel("Retreat When X Units Left:"), builder.gridY(row1).build()); + new JLabel("Retreat After Round:"), builder1.gridY(row1++).insets(10, 0, 0, 0).build()); + resultsText.add(new JLabel("Retreat When X Units Left:"), builder1.gridY(row1).build()); GridBagConstraintsBuilder builder3 = new GridBagConstraintsBuilder() .gridX(1) @@ -323,11 +323,11 @@ class BattleCalculatorPanel extends JPanel { row1 = row2; final JButton orderOfLossesButton = new JButton("Order Of Losses"); resultsText.add( - orderOfLossesButton, builder.gridX(0).gridY(row1++).insets(10, 15, 0, 0).build()); + orderOfLossesButton, builder1.gridX(0).gridY(row1++).insets(10, 15, 0, 0).build()); if (territoryEffectsJList != null) { resultsText.add( new JScrollPane(territoryEffectsJList), - builder + builder1 .gridY(row1) .gridHeight(territoryEffectsJList.getVisibleRowCount()) .fill(GridBagConstraintsFill.BOTH) @@ -335,7 +335,7 @@ class BattleCalculatorPanel extends JPanel { } resultsText.add( retreatWhenOnlyAirLeftCheckBox, - builder + builder1 .gridX(1) .gridY(row2++) .gridHeight(1) @@ -344,9 +344,9 @@ class BattleCalculatorPanel extends JPanel { .insets(10, 10, 0, 5) .build()); resultsText.add( - keepOneAttackingLandUnitCheckBox, builder.gridY(row2++).insets(2, 10, 0, 5).build()); - resultsText.add(amphibiousCheckBox, builder.gridY(row2++).build()); - resultsText.add(landBattleCheckBox, builder.gridY(row2).build()); + keepOneAttackingLandUnitCheckBox, builder1.gridY(row2++).insets(2, 10, 0, 5).build()); + resultsText.add(amphibiousCheckBox, builder1.gridY(row2++).build()); + resultsText.add(landBattleCheckBox, builder1.gridY(row2).build()); final JPanel resultsPanel = new JPanel(); resultsPanel.add(resultsText);