Skip to content

Commit

Permalink
Merge pull request #3958 from kuronekochomusuke/RFE2403
Browse files Browse the repository at this point in the history
Fix #2403
  • Loading branch information
NickAragua authored Nov 11, 2022
2 parents cd98841 + b943e56 commit 48c2b6e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 63 deletions.
133 changes: 70 additions & 63 deletions megamek/src/megamek/client/ui/swing/ClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<Entity> 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<Entity> 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");
}
}

Expand Down
8 changes: 8 additions & 0 deletions megamek/src/megamek/client/ui/swing/PlayerListDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 48c2b6e

Please sign in to comment.