use of games.strategy.triplea.delegate.dataObjects.CasualtyList in project triplea by triplea-game.
the class BattleDisplay method getCasualties.
CasualtyDetails getCasualties(final Collection<Unit> selectFrom, final Map<Unit, Collection<Unit>> dependents, final int count, final String message, final DiceRoll dice, final PlayerID hit, final CasualtyList defaultCasualties, final boolean allowMultipleHitsPerUnit) {
if (SwingUtilities.isEventDispatchThread()) {
throw new IllegalStateException("This method should not be run in the event dispatch thread");
}
final AtomicReference<CasualtyDetails> casualtyDetails = new AtomicReference<>(new CasualtyDetails());
final CountDownLatch continueLatch = new CountDownLatch(1);
SwingUtilities.invokeLater(() -> {
final boolean isEditMode = (dice == null);
if (!isEditMode) {
actionLayout.show(actionPanel, DICE_KEY);
dicePanel.setDiceRoll(dice);
}
final boolean plural = isEditMode || (count > 1);
final String countStr = isEditMode ? "" : "" + count;
final String btnText = hit.getName() + " select " + countStr + (plural ? " casualties" : " casualty");
actionButton.setAction(new AbstractAction(btnText) {
private static final long serialVersionUID = -2156028313292233568L;
private UnitChooser chooser;
private JScrollPane chooserScrollPane;
@Override
public void actionPerformed(final ActionEvent e) {
final String messageText = message + " " + btnText + ".";
if (chooser == null || chooserScrollPane == null) {
chooser = new UnitChooser(selectFrom, defaultCasualties, dependents, allowMultipleHitsPerUnit, mapPanel.getUiContext());
chooser.setTitle(messageText);
if (isEditMode) {
chooser.setMax(selectFrom.size());
} else {
chooser.setMax(count);
}
chooserScrollPane = new JScrollPane(chooser);
final Dimension screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
final int availHeight = screenResolution.height - 130;
final int availWidth = screenResolution.width - 30;
chooserScrollPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
final Dimension size = chooserScrollPane.getPreferredSize();
chooserScrollPane.setPreferredSize(new Dimension(Math.min(availWidth, size.width + (size.height > availHeight ? chooserScrollPane.getVerticalScrollBar().getPreferredSize().width : 0)), Math.min(availHeight, size.height)));
}
final String[] options = { "Ok", "Cancel" };
final String focus = ClientSetting.SPACE_BAR_CONFIRMS_CASUALTIES.booleanValue() ? options[0] : null;
final int option = JOptionPane.showOptionDialog(BattleDisplay.this, chooserScrollPane, hit.getName() + " select casualties", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, focus);
if (option != 0) {
return;
}
final List<Unit> killed = chooser.getSelected(false);
final List<Unit> damaged = chooser.getSelectedDamagedMultipleHitPointUnits();
if (!isEditMode && (killed.size() + damaged.size() != count)) {
JOptionPane.showMessageDialog(BattleDisplay.this, "Wrong number of casualties selected", hit.getName() + " select casualties", JOptionPane.ERROR_MESSAGE);
} else {
final CasualtyDetails response = new CasualtyDetails(killed, damaged, false);
casualtyDetails.set(response);
dicePanel.clear();
actionButton.setEnabled(false);
actionButton.setAction(nullAction);
continueLatch.countDown();
}
}
});
});
mapPanel.getUiContext().addShutdownLatch(continueLatch);
Interruptibles.await(continueLatch);
mapPanel.getUiContext().removeShutdownLatch(continueLatch);
return casualtyDetails.get();
}
Aggregations