From b943e5669e827be8fc69728310bb10b9ada9dd22 Mon Sep 17 00:00:00 2001 From: kuronekochomusuke Date: Thu, 3 Nov 2022 16:40:59 -0400 Subject: [PATCH] RFE #2403 Reinforce From MUL can select player to reinforce --- .../megamek/client/ui/swing/ClientGUI.java | 133 +++++++++--------- .../client/ui/swing/PlayerListDialog.java | 8 ++ 2 files changed, 78 insertions(+), 63 deletions(-) diff --git a/megamek/src/megamek/client/ui/swing/ClientGUI.java b/megamek/src/megamek/client/ui/swing/ClientGUI.java index 2dcf7ef0add..c52b00f1091 100644 --- a/megamek/src/megamek/client/ui/swing/ClientGUI.java +++ b/megamek/src/megamek/client/ui/swing/ClientGUI.java @@ -700,7 +700,10 @@ public void actionPerformed(ActionEvent event) { break; case FILE_UNITS_REINFORCE: ignoreHotKeys = true; - loadListFile(client.getLocalPlayer(), true); + PlayerListDialog playerListDialog = new PlayerListDialog(frame, client); + playerListDialog.setModal(true); + playerListDialog.setVisible(true); + loadListFile(playerListDialog.getSelected(), true); ignoreHotKeys = false; break; case FILE_UNITS_REINFORCE_RAT: @@ -1487,78 +1490,82 @@ public void loadListFile(Player player) { * @param player */ protected void loadListFile(Player player, boolean reinforce) { - boolean addedUnits = false; + if (player != null) { + boolean addedUnits = false; - if (reinforce && (player.getTeam() == Player.TEAM_UNASSIGNED)) { - String title = Messages.getString("ClientGUI.openUnitListFileDialog.noReinforceTitle"); - String msg = Messages.getString("ClientGUI.openUnitListFileDialog.noReinforceMessage"); - JOptionPane.showMessageDialog(frame, msg, title, JOptionPane.ERROR_MESSAGE, null); - return; - } - // Build the "load unit" dialog, if necessary. - if (dlgLoadList == null) { - dlgLoadList = new JFileChooser("."); - dlgLoadList.setLocation(frame.getLocation().x + 150, frame.getLocation().y + 100); - dlgLoadList.setDialogTitle(Messages.getString("ClientGUI.openUnitListFileDialog.title")); - dlgLoadList.setFileFilter(new FileFilter() { - @Override - public boolean accept(File dir) { - return (dir.getName().endsWith(".mul") || dir.isDirectory()); - } + if (reinforce && (player.getTeam() == Player.TEAM_UNASSIGNED)) { + String title = Messages.getString("ClientGUI.openUnitListFileDialog.noReinforceTitle"); + String msg = Messages.getString("ClientGUI.openUnitListFileDialog.noReinforceMessage"); + JOptionPane.showMessageDialog(frame, msg, title, JOptionPane.ERROR_MESSAGE, null); + return; + } + // Build the "load unit" dialog, if necessary. + if (dlgLoadList == null) { + dlgLoadList = new JFileChooser("."); + dlgLoadList.setLocation(frame.getLocation().x + 150, frame.getLocation().y + 100); + dlgLoadList.setDialogTitle(Messages.getString("ClientGUI.openUnitListFileDialog.title")); + dlgLoadList.setFileFilter(new FileFilter() { + @Override + public boolean accept(File dir) { + return (dir.getName().endsWith(".mul") || dir.isDirectory()); + } - @Override - public String getDescription() { - return "*.mul"; - } - }); - // Default to the player's name. - dlgLoadList.setSelectedFile(new File(player.getName() + ".mul")); - } + @Override + public String getDescription() { + return "*.mul"; + } + }); + // Default to the player's name. + dlgLoadList.setSelectedFile(new File(player.getName() + ".mul")); + } - int returnVal = dlgLoadList.showOpenDialog(frame); - if ((returnVal != JFileChooser.APPROVE_OPTION) || (dlgLoadList.getSelectedFile() == null)) { - // I want a file, y'know! - return; - } + int returnVal = dlgLoadList.showOpenDialog(frame); + if ((returnVal != JFileChooser.APPROVE_OPTION) || (dlgLoadList.getSelectedFile() == null)) { + // I want a file, y'know! + return; + } - // Did the player select a file? - File unitFile = dlgLoadList.getSelectedFile(); - if (unitFile != null) { - try { - // Read the units from the file. - final Vector loadedUnits = new MULParser(unitFile, - getClient().getGame().getOptions()).getEntities(); - - // Add the units from the file. - for (Entity entity : loadedUnits) { - entity.setOwner(player); - if (reinforce) { - entity.setDeployRound(client.getGame().getRoundCount() + 1); - entity.setGame(client.getGame()); - // Set these to true, otherwise units reinforced in - // the movement turn are considered selectable - entity.setDone(true); - entity.setUnloaded(true); - if (entity instanceof IBomber) { - ((IBomber) entity).applyBombs(); + // Did the player select a file? + File unitFile = dlgLoadList.getSelectedFile(); + if (unitFile != null) { + try { + // Read the units from the file. + final Vector loadedUnits = new MULParser(unitFile, getClient().getGame().getOptions()).getEntities(); + + // Add the units from the file. + for (Entity entity : loadedUnits) { + entity.setOwner(player); + if (reinforce) { + entity.setDeployRound(client.getGame().getRoundCount() + 1); + entity.setGame(client.getGame()); + // Set these to true, otherwise units reinforced in + // the movement turn are considered selectable + entity.setDone(true); + entity.setUnloaded(true); + if (entity instanceof IBomber) { + ((IBomber) entity).applyBombs(); + } } } - } - if (!loadedUnits.isEmpty()) { - client.sendAddEntity(loadedUnits); - addedUnits = true; + if (!loadedUnits.isEmpty()) { + client.sendAddEntity(loadedUnits); + addedUnits = true; + } + } catch (Exception ex) { + LogManager.getLogger().error("", ex); + doAlertDialog(Messages.getString("ClientGUI.errorLoadingFile"), ex.getMessage()); } - } catch (Exception ex) { - LogManager.getLogger().error("", ex); - doAlertDialog(Messages.getString("ClientGUI.errorLoadingFile"), ex.getMessage()); } - } - // If we've added reinforcements, then we need to set the round deployment up again. - if (addedUnits && reinforce) { - client.getGame().setupRoundDeployment(); - client.sendResetRoundDeployment(); + // If we've added reinforcements, then we need to set the round deployment up again. + if (addedUnits && reinforce) { + client.getGame().setupRoundDeployment(); + client.sendResetRoundDeployment(); + } + } + else { + doAlertDialog(Messages.getString("ClientGUI.errorLoadingFile"), "Error selecting player"); } } diff --git a/megamek/src/megamek/client/ui/swing/PlayerListDialog.java b/megamek/src/megamek/client/ui/swing/PlayerListDialog.java index 3027e63c3e3..3d1c42567ff 100644 --- a/megamek/src/megamek/client/ui/swing/PlayerListDialog.java +++ b/megamek/src/megamek/client/ui/swing/PlayerListDialog.java @@ -121,4 +121,12 @@ public void refreshPlayerList() { refreshPlayerList(playerList, client, true); pack(); } + + public Player getSelected() { + if (!playerList.isSelectionEmpty()) { + return client.getGame().getPlayersVectorSorted().elementAt(playerList.getSelectedIndex()); + } + + return null; + } }