Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor saveUnitList() to not make a new ClientGUI #1597

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions megameklab/src/megameklab/ui/dialog/PrintQueueDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package megameklab.ui.dialog;

import static java.util.stream.Collectors.toList;
import static megamek.client.ui.swing.ClientGUI.CG_FILEPATHMUL;

import java.awt.Container;
import java.awt.Dimension;
Expand All @@ -28,6 +29,7 @@
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -41,23 +43,20 @@
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import megamek.client.ui.Messages;
import megamek.common.*;
import org.apache.commons.io.FilenameUtils;

import megamek.client.Client;
import megamek.client.generator.RandomNameGenerator;
import megamek.client.ui.baseComponents.MMButton;
import megamek.client.ui.swing.ClientGUI;
import megamek.client.ui.swing.UnitLoadingDialog;
import megamek.common.BTObject;
import megamek.common.Configuration;
import megamek.common.Entity;
import megamek.common.Game;
import megamek.common.MekFileParser;
import megamek.common.Player;
import megamek.common.util.C3Util;
import megamek.logging.MMLogger;
import megameklab.printing.PageBreak;
import megameklab.util.UnitPrintManager;
import org.apache.logging.log4j.util.Strings;

/**
* Allows selecting multiple units and printing their record sheets.
Expand Down Expand Up @@ -282,10 +281,34 @@ protected void okButtonActionPerformed(ActionEvent evt) {
}

private void saveUnitList() {
// todo: Refactor saveListFile to be a static method of some util class
// --Pavel
var entities = getEntities();
if (entities.isEmpty()) {
return;
}

linkForce();
new ClientGUI(new Client("", "", 0), null).saveListFile(getEntities(), "Units");

var fileChooser = new JFileChooser(".");
fileChooser.setDialogTitle(Messages.getString("ClientGUI.saveUnitListFileDialog.title"));
var filter = new FileNameExtensionFilter(
Messages.getString("ClientGUI.descriptionMULFiles"), CG_FILEPATHMUL);
fileChooser.setFileFilter(filter);
fileChooser.setSelectedFile(new File(Strings.isNotBlank(mulFileName) ? mulFileName : entities.get(0).getShortName() + " etc." + CG_FILEPATHMUL));

if (!(fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) || fileChooser.getSelectedFile() == null) {
return;
}

File file = fileChooser.getSelectedFile();
if (!FilenameUtils.getExtension(file.getName()).equalsIgnoreCase(CG_FILEPATHMUL)) {
file = new File(file + "." + CG_FILEPATHMUL);
}

try {
EntityListFile.saveTo(file, entities);
} catch (IOException e) {
logger.error(e, String.format("Failed to save units to file: %s", e.getMessage()), "Error");
}
}

private void pageBreak() {
Expand Down