From de93fc0a0a09e8a99a26c1a236189b233b3370e0 Mon Sep 17 00:00:00 2001 From: asvitkine <alexei.svitkine@gmail.com> Date: Sun, 29 May 2022 12:04:32 -0400 Subject: [PATCH] Fix national objectives with 2.5 save games. (#10524) * Fix national objectives with 2.5 save games. Fix national objectives with 2.5 save games, where the players list may be empty instead of null. Also clean up some code in ObjectivePanel.java. --- .../attachments/AbstractRulesAttachment.java | 6 +-- .../strategy/triplea/ui/ObjectivePanel.java | 44 +++++++++---------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/AbstractRulesAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/AbstractRulesAttachment.java index 48810f1f2a..b503071b15 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/AbstractRulesAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/AbstractRulesAttachment.java @@ -13,7 +13,6 @@ import games.strategy.triplea.delegate.Matches; import games.strategy.triplea.delegate.OriginalOwnerTracker; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -67,9 +66,8 @@ private void setPlayers(final List<GamePlayer> value) { } protected List<GamePlayer> getPlayers() { - return players == null - ? List.of((GamePlayer) getAttachedTo()) - : Collections.unmodifiableList(players); + List<GamePlayer> result = getListProperty(players); + return result.isEmpty() ? List.of((GamePlayer) getAttachedTo()) : result; } private void resetPlayers() { diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/ObjectivePanel.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/ObjectivePanel.java index bf618ffd53..2442c28897 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/ObjectivePanel.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/ObjectivePanel.java @@ -94,15 +94,15 @@ protected void initLayout() { column1.setCellEditor(new EditorPaneCellEditor()); column1.setCellRenderer(new EditorPaneTableCellRenderer()); final JScrollPane scroll = new JScrollPane(table); - final JButton refresh = new JButton("Refresh Objectives"); + final JButton refresh = + new JButton( + SwingAction.of( + "Refresh Objectives", + e -> { + objectiveModel.loadData(); + SwingUtilities.invokeLater(table::repaint); + })); refresh.setAlignmentY(Component.CENTER_ALIGNMENT); - refresh.addActionListener( - SwingAction.of( - "Refresh Objectives", - e -> { - objectiveModel.loadData(); - SwingUtilities.invokeLater(table::repaint); - })); add(Box.createVerticalStrut(6)); add(refresh); add(Box.createVerticalStrut(6)); @@ -249,10 +249,7 @@ public synchronized Object getValueAt(final int row, final int col) { } private synchronized void loadData() { - // copy so acquire/release read lock are on the same object! - final GameData gameData = ObjectivePanel.this.gameData; - gameData.acquireReadLock(); - try { + try (GameData.Unlocker ignored = gameData.acquireReadLock()) { final Map<ICondition, String> conditions = getConditionComment(getTestedConditions()); collectedData = new String[getRowTotal()][COLUMNS_TOTAL]; int row = 0; @@ -270,8 +267,6 @@ private synchronized void loadData() { collectedData[row][1] = "--------------------"; row++; } - } finally { - gameData.releaseReadLock(); } } @@ -285,13 +280,13 @@ public Map<ICondition, String> getConditionComment( final int each = AbstractTriggerAttachment.getEachMultiple(ta); final int uses = ta.getUses(); if (uses < 0) { - final String comment = satisfied ? (each > 1 ? "T" + each : "T") : "F"; + final String comment = formatStatus(satisfied, each); conditionsComments.put(entry.getKey(), comment); } else if (uses == 0) { final String comment = satisfied ? "Used" : "used"; conditionsComments.put(entry.getKey(), comment); } else { - final String comment = uses + "" + (satisfied ? (each > 1 ? "T" + each : "T") : "F"); + final String comment = uses + "" + formatStatus(satisfied, each); conditionsComments.put(entry.getKey(), comment); } } else if (entry.getKey() instanceof RulesAttachment) { @@ -305,7 +300,7 @@ public Map<ICondition, String> getConditionComment( final String comment = satisfied ? "Used" : "used"; conditionsComments.put(entry.getKey(), comment); } else { - final String comment = uses + "" + (satisfied ? (each > 1 ? "T" + each : "T") : "F"); + final String comment = uses + "" + formatStatus(satisfied, each); conditionsComments.put(entry.getKey(), comment); } } else { @@ -315,6 +310,10 @@ public Map<ICondition, String> getConditionComment( return conditionsComments; } + private String formatStatus(boolean satisfied, int each) { + return satisfied ? (each > 1 ? "T" + each : "T") : "F"; + } + public Map<ICondition, Boolean> getTestedConditions() { final Set<ICondition> myConditions = new HashSet<>(); for (final Map<ICondition, String> map : statsObjective.values()) { @@ -342,11 +341,8 @@ public synchronized int getRowCount() { return collectedData.length; } - gameData.acquireReadLock(); - try { + try (GameData.Unlocker ignored = gameData.acquireReadLock()) { return getRowTotal(); - } finally { - gameData.releaseReadLock(); } } @@ -507,12 +503,12 @@ private int findMaximumRowSize(final JTable table, final int row) { if (rows == null) { return 0; } - final Map<Integer, Integer> rowheights = rows.get(row); - if (rowheights == null) { + final Map<Integer, Integer> rowHeights = rows.get(row); + if (rowHeights == null) { return 0; } int maximumHeight = 0; - for (final Entry<Integer, Integer> entry : rowheights.entrySet()) { + for (final Entry<Integer, Integer> entry : rowHeights.entrySet()) { final int cellHeight = entry.getValue(); maximumHeight = Math.max(maximumHeight, cellHeight); }