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

Pagebreaks #1447

Merged
merged 5 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions megameklab/src/megameklab/printing/PageBreak.java
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
18 changes: 14 additions & 4 deletions megameklab/src/megameklab/ui/dialog/PrintQueueDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Entity> units = new ArrayList<>();
private final List<BTObject> units = new ArrayList<>();
private final JList<String> queuedUnitList = new JList<>();

public PrintQueueDialog(JFrame parent, boolean printToPdf) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -117,7 +122,7 @@ protected JPanel createButtonPanel() {

private void refresh() {
List<String> nameList = units.stream()
.map(unit -> " " + unit.getChassis() + " " + unit.getModel())
.map(unit -> ' ' + unit.generalName() + ' ' + unit.specificName())
.collect(toList());

var replacementModel = new DefaultListModel<String>();
Expand All @@ -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);
Expand Down Expand Up @@ -193,7 +203,7 @@ private void selectFromFile() {
}

private void removeSelectedUnits() {
List<Entity> newList = new ArrayList<>();
List<BTObject> newList = new ArrayList<>();
for (int i = 0; i < units.size(); i++) {
final int index = i;
if (Arrays.stream(queuedUnitList.getSelectedIndices()).noneMatch(idx -> idx == index)) {
Expand Down
158 changes: 100 additions & 58 deletions megameklab/src/megameklab/util/UnitPrintManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,76 +135,106 @@ private static File getExportFile(Frame parent, String suggestedFileName) {
return f.getSelectedFile();
}

private static List<PrintRecordSheet> createSheets(List<Entity> entities, boolean singlePrint,
private static List<PrintRecordSheet> createSheets(List<? extends BTObject> entities, boolean singlePrint,
RecordSheetOptions options) {
List<PrintRecordSheet> sheets = new ArrayList<>();
List<Infantry> infList = new ArrayList<>();
List<BattleArmor> baList = new ArrayList<>();
List<Protomech> protoList = new ArrayList<>();
List<Entity> unprintable = new ArrayList<>();
List<BTObject> 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")));
}

Expand All @@ -226,7 +256,7 @@ private static List<PrintRecordSheet> createSheets(List<Entity> entities, boolea
return sheets;
}

public static void exportUnits(List<Entity> units, File exportFile, boolean singlePrint) {
public static void exportUnits(List<? extends BTObject> units, File exportFile, boolean singlePrint) {
RecordSheetOptions options = new RecordSheetOptions();
List<PrintRecordSheet> sheets = createSheets(units, singlePrint, options);
PageFormat pageFormat = new PageFormat();
Expand All @@ -241,7 +271,7 @@ public static void exportUnits(List<Entity> 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<Entity> loadedUnits, boolean singlePrint) {
public static void printAllUnits(List<? extends BTObject> loadedUnits, boolean singlePrint) {
printAllUnits(loadedUnits, singlePrint, new RecordSheetOptions());
}

Expand All @@ -252,7 +282,7 @@ public static void printAllUnits(List<Entity> 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<Entity> loadedUnits, boolean singlePrint,
public static void printAllUnits(List<? extends BTObject> loadedUnits, boolean singlePrint,
RecordSheetOptions options) {
HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(options.getPaperSize().sizeName);
Expand All @@ -270,9 +300,21 @@ public static void printAllUnits(List<Entity> loadedUnits, boolean singlePrint,
List<PrintRecordSheet> 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);
Expand Down
Loading