Skip to content

Commit

Permalink
Use print queue dialog for printing mul files
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbraginskiy committed Mar 17, 2024
1 parent 20ab7ba commit 108eda2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 54 deletions.
15 changes: 1 addition & 14 deletions megameklab/src/megameklab/ui/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import megamek.common.annotations.Nullable;
import megamek.common.loaders.BLKFile;
import megamek.common.templates.TROView;
import megamek.common.util.ImageUtil;
import megameklab.MMLConstants;
import megameklab.ui.dialog.MMLFileChooser;
import megameklab.ui.dialog.MegaMekLabUnitSelectorDialog;
Expand Down Expand Up @@ -394,15 +393,9 @@ private JMenu createPDFUnitExportMenu() {
final JMenuItem miExportUnitsFromMULFileToPDF = new JMenuItem(resources.getString("FromMUL.text"));
miExportUnitsFromMULFileToPDF.setName("miExportUnitsFromMULFileToPDF");
miExportUnitsFromMULFileToPDF.setMnemonic(KeyEvent.VK_M);
miExportUnitsFromMULFileToPDF.addActionListener(evt -> UnitPrintManager.exportMUL(owner.getFrame(), false));
miExportUnitsFromMULFileToPDF.addActionListener(evt -> UnitPrintManager.printMUL(owner.getFrame(), true));
pdfUnitExportMenu.add(miExportUnitsFromMULFileToPDF);

final JMenuItem miExportUnitsFromMULFileToSinglePDFPages = new JMenuItem(resources.getString("FromMULSingle.text"));
miExportUnitsFromMULFileToSinglePDFPages.setName("miExportUnitsFromMULFileToSinglePDFPages");
miExportUnitsFromMULFileToSinglePDFPages.setMnemonic(KeyEvent.VK_L);
miExportUnitsFromMULFileToSinglePDFPages.addActionListener(evt -> UnitPrintManager.exportMUL(owner.getFrame(), true));
pdfUnitExportMenu.add(miExportUnitsFromMULFileToSinglePDFPages);

return pdfUnitExportMenu;
}

Expand Down Expand Up @@ -509,12 +502,6 @@ private JMenu createPrintMenu() {
miPrintUnitsFromMULFile.addActionListener(evt -> UnitPrintManager.printMUL(owner.getFrame(), false));
printMenu.add(miPrintUnitsFromMULFile);

final JMenuItem miPrintUnitsFromMULFileToSinglePages = new JMenuItem(resources.getString("FromMULSingle.text"));
miPrintUnitsFromMULFileToSinglePages.setName("miPrintUnitsFromMULFileToSinglePages");
miPrintUnitsFromMULFileToSinglePages.setMnemonic(KeyEvent.VK_L);
miPrintUnitsFromMULFileToSinglePages.addActionListener(evt -> UnitPrintManager.printMUL(owner.getFrame(), true));
printMenu.add(miPrintUnitsFromMULFileToSinglePages);

return printMenu;
}

Expand Down
30 changes: 25 additions & 5 deletions megameklab/src/megameklab/ui/dialog/PrintQueueDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class PrintQueueDialog extends AbstractMMLButtonDialog {
private final JButton addFromCacheButton = new JButton("Add From Cache");
private final JButton addPageBreakButton = new JButton("Add Page Break");
private final JButton removeButton = new JButton("Remove Selected");

private final JButton moveTopButton = new JButton(icon("moveTop.png"));
private final JButton moveUpButton = new JButton(icon("moveUp.png"));
private final JButton moveDownButton = new JButton(icon("moveDown.png"));
Expand All @@ -69,11 +69,22 @@ public class PrintQueueDialog extends AbstractMMLButtonDialog {
private final List<BTObject> units = new ArrayList<>();
private final JList<String> queuedUnitList = new JList<>();

public PrintQueueDialog(JFrame parent, boolean printToPdf) {
private final boolean fromMul;

public PrintQueueDialog(JFrame parent, boolean printToPdf, List<? extends BTObject> units, boolean fromMul) {
super(parent, true, "PrintQueueDialog", "PrintQueueDialog.windowName.text");
this.parent = parent;
this.printToPdf = printToPdf;
this.fromMul = fromMul;
initialize();
if (units != null) {
this.units.addAll(units);
refresh();
}
}

public PrintQueueDialog(JFrame parent, boolean printToPdf) {
this(parent, printToPdf, null, false);
}

private static ImageIcon icon(String name) {
Expand Down Expand Up @@ -118,8 +129,10 @@ protected Container createCenterPane() {
queuedUnitList.setVisibleRowCount(15);

JPanel buttonPanel = new FixedXYPanel(new GridLayout(4, 1));
buttonPanel.add(addFromCacheButton);
buttonPanel.add(addFromFileButton);
if (!fromMul) {
buttonPanel.add(addFromCacheButton);
buttonPanel.add(addFromFileButton);
}
buttonPanel.add(addPageBreakButton);
buttonPanel.add(removeButton);
buttonPanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
Expand Down Expand Up @@ -164,7 +177,14 @@ protected JPanel createButtonPanel() {

private void refresh() {
List<String> nameList = units.stream()
.map(unit -> ' ' + unit.generalName() + ' ' + unit.specificName())
.map(unit -> {
String title = String.format(" %s %s", unit.generalName(), unit.specificName());
if (fromMul && unit instanceof Entity) {
var crew = ((Entity) unit).getCrew();
title += String.format(" {%s %d/%d}", crew.getName(), crew.getGunnery(), crew.getPiloting());
}
return title;
})
.collect(toList());

var replacementModel = new DefaultListModel<String>();
Expand Down
38 changes: 3 additions & 35 deletions megameklab/src/megameklab/util/UnitPrintManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import megamek.common.*;
import megameklab.printing.*;
import megameklab.ui.dialog.MegaMekLabUnitSelectorDialog;
import org.apache.commons.io.FilenameUtils;
import megameklab.ui.dialog.PrintQueueDialog;
import org.apache.logging.log4j.LogManager;

import javax.print.attribute.HashPrintRequestAttributeSet;
Expand Down Expand Up @@ -50,7 +50,7 @@ public static void exportEntity(Entity entity, JFrame parent) {
}
}

public static void printMUL(Frame parent, boolean singlePrint) {
public static void printMUL(JFrame parent, boolean printToPdf) {
JFileChooser f = new JFileChooser(System.getProperty("user.dir"));
f.setLocation(parent.getLocation().x + 150, parent.getLocation().y + 100);
f.setDialogTitle("Print From MUL");
Expand All @@ -75,39 +75,7 @@ public static void printMUL(Frame parent, boolean singlePrint) {
return;
}

printAllUnits(loadedUnits, singlePrint);
}

public static void exportMUL(Frame parent, boolean singlePrint) {
JFileChooser f = new JFileChooser(System.getProperty("user.dir"));
f.setLocation(parent.getLocation().x + 150, parent.getLocation().y + 100);
f.setDialogTitle("Export from MUL");
f.setMultiSelectionEnabled(false);

FileNameExtensionFilter filter = new FileNameExtensionFilter("Mul Files", "mul");

// Add a filter for mul files
f.setFileFilter(filter);

int returnVal = f.showOpenDialog(parent);
if ((returnVal != JFileChooser.APPROVE_OPTION) || (f.getSelectedFile() == null)) {
// I want a file, y'know!
return;
}
File mulFile = f.getSelectedFile();
final Vector<Entity> loadedUnits;
try {
loadedUnits = new MULParser(mulFile, null).getEntities();
loadedUnits.trimToSize();
} catch (Exception ex) {
LogManager.getLogger().error("", ex);
return;
}

File exportFile = getExportFile(parent, FilenameUtils.removeExtension(mulFile.getPath()) + ".pdf");
if (exportFile != null) {
exportUnits(loadedUnits, exportFile, singlePrint);
}
new PrintQueueDialog(parent, printToPdf, loadedUnits, true).setVisible(true);
}

public static File getExportFile(Frame parent) {
Expand Down

0 comments on commit 108eda2

Please sign in to comment.