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

MHQ RFE 5604: save refit files from MHQ MekLab tab (MML side) #1678

Merged
merged 3 commits into from
Jan 5, 2025
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
20 changes: 20 additions & 0 deletions megameklab/src/megameklab/ui/FileNameManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* MegaMek - Copyright (C) 2024 - The MegaMek Team
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
package megameklab.ui;

public interface FileNameManager {
String getFileName();
boolean hasEntityNameChanged();
void setFileName(String fileName);
}
10 changes: 6 additions & 4 deletions megameklab/src/megameklab/ui/MegaMekLabMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import megameklab.MMLConstants;
import megameklab.MegaMekLab;
import megameklab.ui.util.ExitOnWindowClosingListener;
import megameklab.ui.util.MegaMekLabFileSaver;
import megameklab.ui.util.RefreshListener;
import megameklab.util.CConfig;
import megameklab.util.MMLFileDropTransferHandler;

import javax.swing.*;
import java.awt.*;

public abstract class MegaMekLabMainUI extends JFrame implements RefreshListener, EntitySource, MenuBarOwner {
public abstract class MegaMekLabMainUI extends JFrame implements RefreshListener, EntitySource, MenuBarOwner, FileNameManager {
private Entity entity = null;
private String fileName = "";
protected MenuBar mmlMenuBar;
Expand Down Expand Up @@ -158,7 +159,7 @@ public Entity getEntity() {

public void setEntity(Entity entity, String currentEntityFilename) {
this.entity = entity;
originalName = MenuBar.createUnitFilename(entity);
originalName = MegaMekLabFileSaver.createUnitFilename(entity);
setFileName(currentEntityFilename);
}

Expand Down Expand Up @@ -187,10 +188,11 @@ public String getFileName() {
return fileName;
}

@Override
public void setFileName(String fileName) {
this.fileName = fileName;
// If the filename is reloaded, restart tracking of the unit name changing.
this.originalName = MenuBar.createUnitFilename(entity);
this.originalName = MegaMekLabFileSaver.createUnitFilename(entity);
refreshHeader();
}

Expand All @@ -201,7 +203,7 @@ public void refreshMenuBar() {

@Override
public boolean hasEntityNameChanged() {
return !MenuBar.createUnitFilename(entity).equals(originalName);
return !MegaMekLabFileSaver.createUnitFilename(entity).equals(originalName);
}

@Override
Expand Down
142 changes: 35 additions & 107 deletions megameklab/src/megameklab/ui/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.loaders.BLKFile;
import megamek.common.templates.TROView;
import megamek.logging.MMLogger;
import megameklab.MMLConstants;
Expand All @@ -51,6 +50,7 @@
import megameklab.ui.dialog.PrintQueueDialog;
import megameklab.ui.dialog.UiLoader;
import megameklab.ui.dialog.settings.SettingsDialog;
import megameklab.ui.util.MegaMekLabFileSaver;
import megameklab.util.CConfig;
import megameklab.util.UnitPrintManager;
import megameklab.util.UnitUtil;
Expand All @@ -66,10 +66,11 @@ public class MenuBar extends JMenuBar implements ClipboardOwner {
private final MenuBarOwner owner;
private final ResourceBundle resources = ResourceBundle.getBundle("megameklab.resources.Menu");
private final MMLFileChooser loadUnitFileChooser = new MMLFileChooser();
private final MMLFileChooser saveUnitFileChooser = new MMLFileChooser();
public final MMLFileChooser loadImageFileChooser = new MMLFileChooser();
private final JMenu fileMenu = new JMenu(resources.getString("fileMenu.text"));

private MegaMekLabFileSaver fileSaver;

public MenuBar(MenuBarOwner owner) {
this.owner = owner;
initialize();
Expand Down Expand Up @@ -118,7 +119,6 @@ private void initialize() {
loadImageFileChooser.setDialogTitle(resources.getString("dialog.chooseUnit.title"));
loadImageFileChooser.setFileFilter(new FileNameExtensionFilter("Image files (.png, .jpg, .gif)",
"png", "jpg", "jpeg", "gif"));
saveUnitFileChooser.setDialogTitle(resources.getString("dialog.saveAs.title"));
}

private JMenu createFileMenu() {
Expand Down Expand Up @@ -351,6 +351,7 @@ private JMenu createLoadMenu() {
* @return the created Save menu
*/
private JMenu createSaveMenu() {
fileSaver = new MegaMekLabFileSaver(logger, resources.getString("dialog.saveAs.title"));
final JMenu saveMenu = new JMenu(resources.getString("Save.text"));
saveMenu.setName("saveMenu");
saveMenu.setMnemonic(KeyEvent.VK_S);
Expand All @@ -374,6 +375,37 @@ private JMenu createSaveMenu() {
return saveMenu;
}

public boolean saveUnitAs() {
warnOnInvalid();
Entity entity = getUnitMainUi().getEntity();
UnitUtil.compactCriticals(entity);
getUnitMainUi().refreshAll(); // The crits may have moved
String file = fileSaver.saveUnitAs(getUnitMainUi(), entity);
if (file == null) {
return false;
}
getUnitMainUi().setFileName(file);
return true;
}

public boolean saveUnit() {
Entity entity = getUnitMainUi().getEntity();
if (entity == null) {
logger.error("Tried to save null entity.");
return false;
} else {
warnOnInvalid();
}
UnitUtil.compactCriticals(entity);
getUnitMainUi().refreshAll(); // The crits may have moved
String file = fileSaver.saveUnit(getUnitMainUi().getFrame(), getUnitMainUi(), entity);
if (file == null) {
return false;
}
getUnitMainUi().setFileName(file);
return true;
}

/**
* @return the created Export menu
*/
Expand Down Expand Up @@ -1080,110 +1112,6 @@ private void jMenuResetEntity_actionPerformed(ActionEvent event) {
getUnitMainUi().repaint();
}

/**
* Constructs a file name for the current Entity using the chassis and model
* name and the
* correct extension for the unit type. Any character that is not legal for a
* Windows filename
* is replaced by an underscore.
*
* @param entity The Entity
* @return A default filename for the Entity
*/
public static String createUnitFilename(Entity entity) {
String fileName = (entity.getChassis() + ' ' + entity.getModel()).trim();
fileName = fileName.replaceAll("[/\\\\<>:\"|?*]", "_");
return fileName + ((entity instanceof Mek) ? ".mtf" : ".blk");
}

/**
* Tries to save the unit directly to its file, if it has a filename already. If
* it hasn't, it performs a Save As... Returns true when it successfully saves
* the
* unit, false if not.
*
* @return True when the unit was actually saved, false otherwise
*/
public boolean saveUnit() {
Entity entity = owner.getEntity();
if (entity == null) {
logger.error("Tried to save null entity.");
return false;
} else {
warnOnInvalid();
}

UnitUtil.compactCriticals(entity);
owner.refreshAll(); // The crits may have moved

String filePathName = owner.getFileName();
// For safety, save automatically only to .mtf or .blk files, otherwise ask
if (!(filePathName.endsWith(".mtf") || filePathName.endsWith(".blk"))
|| !new File(filePathName).exists()
|| owner.hasEntityNameChanged()) {
File selectedFile = chooseSaveFile();
if (selectedFile == null) {
return false;
}

filePathName = selectedFile.getPath();
}

CConfig.setMostRecentFile(filePathName);
return saveUnitTo(new File(filePathName));
}

private void saveUnitAs() {
warnOnInvalid();

UnitUtil.compactCriticals(owner.getEntity());
owner.refreshAll(); // The crits may have moved

File saveFile = chooseSaveFile();
if (saveFile != null) {
CConfig.setMostRecentFile(saveFile.toString());
saveUnitTo(saveFile);
}
}

private @Nullable File chooseSaveFile() {
if (getUnitMainUi().getEntity() instanceof Mek) {
saveUnitFileChooser.setFileFilter(new FileNameExtensionFilter("Mek files", "mtf"));
} else {
saveUnitFileChooser.setFileFilter(new FileNameExtensionFilter("Unit files", "blk"));
}
saveUnitFileChooser.setSelectedFile(new File(createUnitFilename(getUnitMainUi().getEntity())));
int result = saveUnitFileChooser.showSaveDialog(owner.getFrame());
if ((result != JFileChooser.APPROVE_OPTION) || (saveUnitFileChooser.getSelectedFile() == null)) {
return null;
} else {
return saveUnitFileChooser.getSelectedFile();
}
}

private boolean saveUnitTo(File file) {
if (getUnitMainUi().getEntity() == null) {
return false;
}
try {
if (getUnitMainUi().getEntity() instanceof Mek) {
try (FileOutputStream fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos)) {
ps.println(((Mek) owner.getEntity()).getMtf());
}
} else {
BLKFile.encode(file.getPath(), getUnitMainUi().getEntity());
}
PopupMessages.showUnitSavedMessage(owner.getFrame(), getUnitMainUi().getEntity(), file);
getUnitMainUi().setFileName(file.toString());
return true;
} catch (Exception ex) {
PopupMessages.showFileWriteError(owner.getFrame(), ex.getMessage());
logger.error("", ex);
return false;
}
}

private String entitySummaryText(ViewFormatting formatting) {
if (CConfig.getBooleanParam(CConfig.MISC_SUMMARY_FORMAT_TRO) && formatting != ViewFormatting.DISCORD) {
TROView view = TROView.createView(owner.getEntity(), formatting);
Expand Down
3 changes: 2 additions & 1 deletion megameklab/src/megameklab/ui/StartupGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import megameklab.ui.dialog.MegaMekLabUnitSelectorDialog;
import megameklab.ui.dialog.UiLoader;
import megameklab.ui.util.ExitOnWindowClosingListener;
import megameklab.ui.util.MegaMekLabFileSaver;
import megameklab.util.CConfig;
import megameklab.util.MMLFileDropTransferHandler;
import megameklab.util.UnitUtil;
Expand Down Expand Up @@ -264,7 +265,7 @@ public static void selectAndLoadUnitFromCache(MenuBarOwner previousFrame) {
if (fileName.toLowerCase().endsWith(".zip")) {
fileName = viewer.getSelectedMekSummary().getSourceFile().getAbsolutePath();
fileName = fileName.substring(0, fileName.lastIndexOf(File.separatorChar) + 1);
fileName = fileName + MenuBar.createUnitFilename(newUnit);
fileName = fileName + MegaMekLabFileSaver.createUnitFilename(newUnit);
}

if (!previousFrame.safetyPrompt()) {
Expand Down
Loading
Loading