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

Show a warning when attempting to print the invalid current unit #1478

Merged
merged 3 commits into from
Apr 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
42 changes: 24 additions & 18 deletions megameklab/src/megameklab/ui/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ private JMenu createPDFUnitExportMenu() {
final JMenuItem miExportCurrentUnitToPDF = new JMenuItem(resources.getString("CurrentUnit.text"));
miExportCurrentUnitToPDF.setName("miExportCurrentUnitToPDF");
miExportCurrentUnitToPDF.setMnemonic(KeyEvent.VK_U);
miExportCurrentUnitToPDF.addActionListener(evt -> UnitPrintManager.exportEntity(owner.getEntity(), owner.getFrame()));
miExportCurrentUnitToPDF.addActionListener(evt -> {
warnOnInvalid();
UnitPrintManager.exportEntity(owner.getEntity(), owner.getFrame());
});
miExportCurrentUnitToPDF.setEnabled(isUnitGui());
pdfUnitExportMenu.add(miExportCurrentUnitToPDF);

Expand Down Expand Up @@ -447,6 +450,7 @@ private JMenu createClipboardUnitExportMenu() {
miExportCurrentUnitToClipboard.setName("miExportCurrentUnitToClipboard");
miExportCurrentUnitToClipboard.setMnemonic(KeyEvent.VK_U);
miExportCurrentUnitToClipboard.addActionListener(evt -> {
warnOnInvalid();
StringSelection stringSelection = new StringSelection(entitySummaryText(false));
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, this);
});
Expand All @@ -468,7 +472,10 @@ private JMenu createPrintMenu() {
miPrintCurrentUnit.setName("miPrintCurrentUnit");
miPrintCurrentUnit.setMnemonic(KeyEvent.VK_U);
miPrintCurrentUnit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_DOWN_MASK));
miPrintCurrentUnit.addActionListener(evt -> UnitPrintManager.printEntity(owner.getEntity()));
miPrintCurrentUnit.addActionListener(evt -> {
warnOnInvalid();
UnitPrintManager.printEntity(owner.getEntity());
});
miPrintCurrentUnit.setEnabled(isUnitGui());
printMenu.add(miPrintCurrentUnit);

Expand Down Expand Up @@ -1030,10 +1037,7 @@ public boolean saveUnit() {
LogManager.getLogger().error("Tried to save null entity.");
return false;
} else {
String validationResult = UnitUtil.validateUnit(entity);
if (!validationResult.isBlank()) {
PopupMessages.showUnitInvalidWarning(owner.getFrame(), validationResult);
}
warnOnInvalid();
}

UnitUtil.compactCriticals(entity);
Expand All @@ -1055,10 +1059,7 @@ public boolean saveUnit() {
}

private void saveUnitAs() {
String validationResult = UnitUtil.validateUnit(owner.getEntity());
if (!validationResult.isBlank()) {
PopupMessages.showUnitInvalidWarning(owner.getFrame(), validationResult);
}
warnOnInvalid();

UnitUtil.compactCriticals(owner.getEntity());
owner.refreshAll(); // The crits may have moved
Expand Down Expand Up @@ -1119,10 +1120,7 @@ private String entitySummaryText(boolean html) {
}

private void exportSummary(boolean html) {
String validationResult = UnitUtil.validateUnit(owner.getEntity());
if (!validationResult.isBlank()) {
PopupMessages.showUnitInvalidWarning(owner.getFrame(), validationResult);
}
warnOnInvalid();

String unitName = owner.getEntity().getChassis() + ' ' + owner.getEntity().getModel();

Expand Down Expand Up @@ -1169,10 +1167,7 @@ private void loadUnitFromFile(int fileNumber) {
return;
}

String validationResult = UnitUtil.validateUnit(loadedUnit);
if (!validationResult.isBlank()) {
PopupMessages.showUnitInvalidWarning(owner.getFrame(), validationResult);
}
warnOnInvalid(loadedUnit);

newRecentUnit(unitFile.toString());
if (isStartupGui() || (loadedUnit.getEntityType() != owner.getEntity().getEntityType())) {
Expand Down Expand Up @@ -1297,6 +1292,17 @@ public static void showUnitSpecs(Entity unit, JFrame frame) {
}
}

private void warnOnInvalid() {
warnOnInvalid(owner.getEntity());
}

private void warnOnInvalid(Entity entity) {
var report = UnitUtil.validateUnit(entity);
if (!report.isBlank()) {
PopupMessages.showUnitInvalidWarning(owner.getFrame(), report);
}
}

public static void showUnitWeightBreakDown(Entity entity, JFrame frame) {
if (entity != null) {
new WeightDisplayDialog(frame, entity).setVisible(true);
Expand Down
Loading