Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbraginskiy committed Jan 7, 2025
1 parent ac5de3b commit fef0bbc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion megameklab/src/megameklab/ui/MegaMekLabTabbedUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public MegaMekLabTabbedUI(MegaMekLabMainUI... entities) {
// Enable opening unit and mul files by drag-and-drop
setTransferHandler(new MMLFileDropTransferHandler(this));

// If you can think of a way to detect when the user makes a change to the entity, let me know.
// I can't, so we just poll the entity for changed to set the "unsaved work" indicator periodically
// --Pavel Braginskiy (cat /dev/random)
new Timer(500, e -> checkChanged(tabs.getSelectedIndex())).start();

// Remember the size and position of the window from last time MML was launched
Expand Down Expand Up @@ -240,6 +243,7 @@ private boolean exitPrompt() {
if (CConfig.getBooleanParam(CConfig.MISC_SKIP_SAFETY_PROMPTS)) {
return true;
}
// No editors have changes, no need to prompt for saving
if (editors.stream().limit(editors.size() - 1).noneMatch(EntityChangedUtil::hasEntityChanged)) {
return true;
}
Expand All @@ -251,13 +255,16 @@ private boolean exitPrompt() {
if (savePrompt == JOptionPane.NO_OPTION) {
return true;
}
// For each editor with unsaved changes, switch to that editor and save it.
if (savePrompt == JOptionPane.YES_OPTION) {
return editors.stream().limit(editors.size() - 1)
.filter(EntityChangedUtil::hasEntityChanged)
.noneMatch(editor -> {
tabs.setSelectedComponent(editor.getContentPane());
// paintImmediately means that the user can see which unit they're about to pick a file for
// because it updates the UI without waiting for this method to return
tabs.paintImmediately(tabs.getBounds());
return !editor.getMMLMenuBar().saveUnit();
return !menuBar.saveUnit();
});
}
return false;
Expand Down
15 changes: 15 additions & 0 deletions megameklab/src/megameklab/util/EntityChangedUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,28 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Helps to determine if a tab should have an "Unsaved Work" indicator and if the "would you like to save" prompt should appear.
*/
public class EntityChangedUtil {
private static final MMLogger logger = MMLogger.create(EntityChangedUtil.class);

private static final Map<String, Entity> cache = new ConcurrentHashMap<>();

/**
* Determines if the editor has unsaved changes.
* @param editor The MainUI to check for changes
* @return true if saving the unit would produce a different unit than the one saved already, or if there is no existing file for the unit.
*/
public static boolean hasEntityChanged(MegaMekLabMainUI editor) {
var filename = editor.getFileName();
if (filename == null || filename.isBlank()) {
return true;
}

// The idea behind how this works is to store a copy of the unit based on that unit's current file.
// By encoding both the original and current version of the entity to mtf/blk at the same time,
// We ensure that identical units will encode to identical mtf/blk strings, which indicates no unsaved work.

if (!cache.containsKey(filename)) {
var f = new File(filename);
Expand All @@ -70,6 +81,10 @@ public static boolean hasEntityChanged(MegaMekLabMainUI editor) {
}
}

/**
*
* @param editor The editor containing the unit that was just saved.
*/
public static void editorSaved(MegaMekLabMainUI editor) {
var filename = editor.getFileName();
if (filename == null || filename.isBlank()) {
Expand Down

0 comments on commit fef0bbc

Please sign in to comment.