Search in sources :

Example 6 with CasualtyList

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();
}
Also used : JScrollPane(javax.swing.JScrollPane) ActionEvent(java.awt.event.ActionEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Dimension(java.awt.Dimension) CountDownLatch(java.util.concurrent.CountDownLatch) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) List(java.util.List) ArrayList(java.util.ArrayList) CasualtyList(games.strategy.triplea.delegate.dataObjects.CasualtyList) JList(javax.swing.JList) AbstractAction(javax.swing.AbstractAction)

Aggregations

CasualtyList (games.strategy.triplea.delegate.dataObjects.CasualtyList)6 CasualtyDetails (games.strategy.triplea.delegate.dataObjects.CasualtyDetails)5 Unit (games.strategy.engine.data.Unit)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 GameData (games.strategy.engine.data.GameData)2 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 PlayerID (games.strategy.engine.data.PlayerID)1 UnitType (games.strategy.engine.data.UnitType)1 WeakAi (games.strategy.triplea.ai.weak.WeakAi)1 BattleDelegate (games.strategy.triplea.delegate.BattleDelegate)1 IBattle (games.strategy.triplea.delegate.IBattle)1 ITripleAPlayer (games.strategy.triplea.player.ITripleAPlayer)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 Collection (java.util.Collection)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1