diff --git a/megameklab/src/megameklab/printing/PageBreak.java b/megameklab/src/megameklab/printing/PageBreak.java new file mode 100644 index 000000000..564239e57 --- /dev/null +++ b/megameklab/src/megameklab/printing/PageBreak.java @@ -0,0 +1,21 @@ +package megameklab.printing; + +import megamek.common.*; + +/** + * Dummy entity that indicates that the page should be broken when printing. + * + * @author pavelbraginskiy + */ +public class PageBreak implements BTObject { + + @Override + public String generalName() { + return "-PAGE BREAK-"; + } + + @Override + public String specificName() { + return ""; + } +} diff --git a/megameklab/src/megameklab/ui/dialog/PrintQueueDialog.java b/megameklab/src/megameklab/ui/dialog/PrintQueueDialog.java index c80558e71..055eef46a 100644 --- a/megameklab/src/megameklab/ui/dialog/PrintQueueDialog.java +++ b/megameklab/src/megameklab/ui/dialog/PrintQueueDialog.java @@ -20,8 +20,10 @@ import megamek.client.ui.baseComponents.MMButton; import megamek.client.ui.swing.UnitLoadingDialog; +import megamek.common.BTObject; import megamek.common.Entity; import megamek.common.MechFileParser; +import megameklab.printing.PageBreak; import megameklab.util.UnitPrintManager; import org.apache.logging.log4j.LogManager; @@ -50,10 +52,11 @@ public class PrintQueueDialog extends AbstractMMLButtonDialog { private final boolean printToPdf; private final JButton addFromFileButton = new JButton("Add From File"); 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 JCheckBox oneUnitPerSheetCheck = new JCheckBox("Print each unit to a separate page"); private final JFrame parent; - private final List units = new ArrayList<>(); + private final List units = new ArrayList<>(); private final JList queuedUnitList = new JList<>(); public PrintQueueDialog(JFrame parent, boolean printToPdf) { @@ -69,6 +72,8 @@ protected Container createCenterPane() { addFromCacheButton.setMnemonic(KeyEvent.VK_A); addFromFileButton.addActionListener(e -> selectFromFile()); addFromFileButton.setMnemonic(KeyEvent.VK_F); + addPageBreakButton.addActionListener(e -> pageBreak()); + addPageBreakButton.setMnemonic(KeyEvent.VK_P); removeButton.addActionListener(e -> removeSelectedUnits()); removeButton.setEnabled(false); removeButton.setMnemonic(KeyEvent.VK_R); @@ -80,9 +85,9 @@ protected Container createCenterPane() { queuedUnitList.setVisibleRowCount(15); JPanel buttonPanel = new FixedXYPanel(new GridLayout(4, 1)); - buttonPanel.add(new JLabel()); buttonPanel.add(addFromCacheButton); buttonPanel.add(addFromFileButton); + buttonPanel.add(addPageBreakButton); buttonPanel.add(removeButton); buttonPanel.setAlignmentY(JComponent.TOP_ALIGNMENT); JScrollPane queuedUnitListScrollPane = new JScrollPane(queuedUnitList); @@ -117,7 +122,7 @@ protected JPanel createButtonPanel() { private void refresh() { List nameList = units.stream() - .map(unit -> " " + unit.getChassis() + " " + unit.getModel()) + .map(unit -> ' ' + unit.generalName() + ' ' + unit.specificName()) .collect(toList()); var replacementModel = new DefaultListModel(); @@ -140,6 +145,11 @@ protected void okButtonActionPerformed(ActionEvent evt) { super.okButtonActionPerformed(evt); } + private void pageBreak() { + units.add(new PageBreak()); + refresh(); + } + private void selectFromCache() { UnitLoadingDialog unitLoadingDialog = new UnitLoadingDialog(parent); unitLoadingDialog.setVisible(true); @@ -193,7 +203,7 @@ private void selectFromFile() { } private void removeSelectedUnits() { - List newList = new ArrayList<>(); + List newList = new ArrayList<>(); for (int i = 0; i < units.size(); i++) { final int index = i; if (Arrays.stream(queuedUnitList.getSelectedIndices()).noneMatch(idx -> idx == index)) { diff --git a/megameklab/src/megameklab/util/UnitPrintManager.java b/megameklab/src/megameklab/util/UnitPrintManager.java index 58de92de6..b821d4f82 100644 --- a/megameklab/src/megameklab/util/UnitPrintManager.java +++ b/megameklab/src/megameklab/util/UnitPrintManager.java @@ -135,76 +135,106 @@ private static File getExportFile(Frame parent, String suggestedFileName) { return f.getSelectedFile(); } - private static List createSheets(List entities, boolean singlePrint, + private static List createSheets(List entities, boolean singlePrint, RecordSheetOptions options) { List sheets = new ArrayList<>(); List infList = new ArrayList<>(); List baList = new ArrayList<>(); List protoList = new ArrayList<>(); - List unprintable = new ArrayList<>(); + List unprintable = new ArrayList<>(); Tank tank1 = null; int pageCount = 0; - for (Entity unit : entities) { - if (unit instanceof Mech) { - UnitUtil.removeOneShotAmmo(unit); - MekUtil.expandUnitMounts((Mech) unit); - sheets.add(new PrintMech((Mech) unit, pageCount++, options)); - } else if ((unit instanceof Tank) && unit.getMovementMode().isMarine()) { - sheets.add(new PrintTank((Tank) unit, pageCount++, options)); - } else if (unit instanceof Tank) { - if (singlePrint || options.showReferenceCharts()) { - sheets.add(new PrintCompositeTankSheet((Tank) unit, null, pageCount++, options)); - } else if (null != tank1) { - sheets.add(new PrintCompositeTankSheet(tank1, (Tank) unit, pageCount++, options)); - tank1 = null; + for (BTObject object : entities) { + if (object instanceof Entity) { + Entity unit = (Entity) object; + if (unit instanceof Mech) { + UnitUtil.removeOneShotAmmo(unit); + MekUtil.expandUnitMounts((Mech) unit); + sheets.add(new PrintMech((Mech) unit, pageCount++, options)); + } else if ((unit instanceof Tank) && unit.getMovementMode().isMarine()) { + sheets.add(new PrintTank((Tank) unit, pageCount++, options)); + } else if (unit instanceof Tank) { + if (singlePrint || options.showReferenceCharts()) { + sheets.add(new PrintCompositeTankSheet((Tank) unit, null, pageCount++, options)); + } else if (null != tank1) { + sheets.add(new PrintCompositeTankSheet(tank1, (Tank) unit, pageCount++, options)); + tank1 = null; + } else { + tank1 = (Tank) unit; + } + } else if (unit.hasETypeFlag(Entity.ETYPE_AERO)) { + if (unit instanceof Jumpship) { + PrintCapitalShip pcs = new PrintCapitalShip((Jumpship) unit, pageCount, options); + pageCount += pcs.getPageCount(); + sheets.add(pcs); + } else if (unit instanceof Dropship) { + PrintDropship pds = new PrintDropship((Aero) unit, pageCount, options); + pageCount += pds.getPageCount(); + sheets.add(pds); + } else { + sheets.add(new PrintAero((Aero) unit, pageCount++, options)); + } + } else if (unit instanceof BattleArmor) { + baList.add((BattleArmor) unit); + if (singlePrint || baList.size() > 4) { + PrintRecordSheet prs = new PrintSmallUnitSheet(baList, pageCount, options); + pageCount += prs.getPageCount(); + sheets.add(prs); + baList = new ArrayList<>(); + } + } else if (unit instanceof Infantry) { + infList.add((Infantry) unit); + if (singlePrint || infList.size() > (options.showReferenceCharts() ? 2 : 3)) { + PrintRecordSheet prs = new PrintSmallUnitSheet(infList, pageCount, options); + pageCount += prs.getPageCount(); + sheets.add(prs); + infList = new ArrayList<>(); + } + } else if (unit instanceof Protomech) { + protoList.add((Protomech) unit); + if (singlePrint || protoList.size() > 4) { + PrintRecordSheet prs = new PrintSmallUnitSheet(protoList, pageCount, options); + pageCount += prs.getPageCount(); + sheets.add(prs); + protoList = new ArrayList<>(); + } } else { - tank1 = (Tank) unit; + unprintable.add(unit); } - } else if (unit.hasETypeFlag(Entity.ETYPE_AERO)) { - if (unit instanceof Jumpship) { - PrintCapitalShip pcs = new PrintCapitalShip((Jumpship) unit, pageCount, options); - pageCount += pcs.getPageCount(); - sheets.add(pcs); - } else if (unit instanceof Dropship) { - PrintDropship pds = new PrintDropship((Aero) unit, pageCount, options); - pageCount += pds.getPageCount(); - sheets.add(pds); - } else { - sheets.add(new PrintAero((Aero) unit, pageCount++, options)); - } - } else if (unit instanceof BattleArmor) { - baList.add((BattleArmor) unit); - if (singlePrint || baList.size() > 4) { - PrintRecordSheet prs = new PrintSmallUnitSheet(baList, pageCount, options); - pageCount += prs.getPageCount(); - sheets.add(prs); - baList = new ArrayList<>(); - } - } else if (unit instanceof Infantry) { - infList.add((Infantry) unit); - if (singlePrint || infList.size() > (options.showReferenceCharts() ? 2 : 3)) { - PrintRecordSheet prs = new PrintSmallUnitSheet(infList, pageCount, options); - pageCount += prs.getPageCount(); - sheets.add(prs); - infList = new ArrayList<>(); - } - } else if (unit instanceof Protomech) { - protoList.add((Protomech) unit); - if (singlePrint || protoList.size() > 4) { - PrintRecordSheet prs = new PrintSmallUnitSheet(protoList, pageCount, options); - pageCount += prs.getPageCount(); - sheets.add(prs); - protoList = new ArrayList<>(); + } else if (object instanceof PageBreak) { + if (!singlePrint) { + if (!baList.isEmpty()) { + PrintRecordSheet prs = new PrintSmallUnitSheet(baList, pageCount, options); + pageCount += prs.getPageCount(); + sheets.add(prs); + baList = new ArrayList<>(); + } + if (!infList.isEmpty()) { + PrintRecordSheet prs = new PrintSmallUnitSheet(infList, pageCount, options); + pageCount += prs.getPageCount(); + sheets.add(prs); + infList = new ArrayList<>(); + } + if (!protoList.isEmpty()) { + PrintRecordSheet prs = new PrintSmallUnitSheet(protoList, pageCount, options); + pageCount += prs.getPageCount(); + sheets.add(prs); + protoList = new ArrayList<>(); + } + if (null != tank1) { + sheets.add(new PrintCompositeTankSheet(tank1, null, pageCount++, options)); + tank1 = null; + } } } else { - unprintable.add(unit); + unprintable.add(object); } } if (!unprintable.isEmpty()) { JOptionPane.showMessageDialog(null, "Exporting is not currently supported for the following units:\n" - + unprintable.stream().map(en -> en.getChassis() + " " + en.getModel()) + + unprintable.stream().map(en -> en.generalName() + ' ' + en.specificName()) .collect(Collectors.joining("\n"))); } @@ -226,7 +256,7 @@ private static List createSheets(List entities, boolea return sheets; } - public static void exportUnits(List units, File exportFile, boolean singlePrint) { + public static void exportUnits(List units, File exportFile, boolean singlePrint) { RecordSheetOptions options = new RecordSheetOptions(); List sheets = createSheets(units, singlePrint, options); PageFormat pageFormat = new PageFormat(); @@ -241,7 +271,7 @@ public static void exportUnits(List units, File exportFile, boolean sing * @param loadedUnits The units to print * @param singlePrint Whether to limit each record sheet to a single unit */ - public static void printAllUnits(List loadedUnits, boolean singlePrint) { + public static void printAllUnits(List loadedUnits, boolean singlePrint) { printAllUnits(loadedUnits, singlePrint, new RecordSheetOptions()); } @@ -252,7 +282,7 @@ public static void printAllUnits(List loadedUnits, boolean singlePrint) * @param singlePrint Whether to limit each record sheet to a single unit * @param options The options to use for this print job */ - public static void printAllUnits(List loadedUnits, boolean singlePrint, + public static void printAllUnits(List loadedUnits, boolean singlePrint, RecordSheetOptions options) { HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(options.getPaperSize().sizeName); @@ -270,9 +300,21 @@ public static void printAllUnits(List loadedUnits, boolean singlePrint, List sheets = createSheets(loadedUnits, singlePrint, options); if (loadedUnits.size() > 1) { - masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw() + " etc"); + String name; + if (loadedUnits.get(0) instanceof Entity) { + name = ((Entity) loadedUnits.get(0)).getShortNameRaw(); + } else { + name = loadedUnits.get(0).generalName(); + } + masterPrintJob.setJobName(name + " etc"); } else if (!loadedUnits.isEmpty()) { - masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw()); + String name; + if (loadedUnits.get(0) instanceof Entity) { + name = ((Entity) loadedUnits.get(0)).getShortNameRaw(); + } else { + name = loadedUnits.get(0).generalName(); + } + masterPrintJob.setJobName(name); } RecordSheetTask task = RecordSheetTask.createPrintTask(sheets, masterPrintJob, aset, pageFormat);