Skip to content

Commit

Permalink
Fix national objectives with 2.5 save games. (#10524)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
asvitkine authored May 29, 2022
1 parent 05850c5 commit de93fc0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -270,8 +267,6 @@ private synchronized void loadData() {
collectedData[row][1] = "--------------------";
row++;
}
} finally {
gameData.releaseReadLock();
}
}

Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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()) {
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit de93fc0

Please sign in to comment.