Skip to content

Commit

Permalink
Refactor saveUnitList() to not make a new ClientGUI
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbraginskiy committed Aug 22, 2024
1 parent 91bb560 commit 93ccf31
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions megameklab/src/megameklab/ui/dialog/PrintQueueDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import megamek.client.Client;
import megamek.client.generator.RandomNameGenerator;
import megamek.client.ui.Messages;
import megamek.client.ui.baseComponents.MMButton;
import megamek.client.ui.swing.ClientGUI;
import megamek.client.ui.swing.UnitLoadingDialog;
Expand All @@ -28,7 +29,9 @@
import megameklab.printing.PageBreak;
import megameklab.util.UnitPrintManager;
import org.apache.commons.io.FilenameUtils;
import org.apache.fop.afp.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.util.Strings;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
Expand All @@ -40,13 +43,15 @@
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;
import java.util.List;
import java.util.stream.IntStream;

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

/**
* Allows selecting multiple units and printing their record sheets.
Expand Down Expand Up @@ -265,10 +270,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) {
JOptionPane.showMessageDialog(getFrame(), "Error: " + e.getMessage());
}
}

private void pageBreak() {
Expand Down

0 comments on commit 93ccf31

Please sign in to comment.