Skip to content

Commit

Permalink
Add option to drop unsaved changes to unit
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbraginskiy committed Jan 11, 2025
1 parent 7de7b89 commit 6561f8e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
6 changes: 6 additions & 0 deletions megameklab/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ plugins {
id 'jacoco'
id 'java'
id 'org.ec4j.editorconfig' version '0.1.0'
id 'org.openjfx.javafxplugin' version '0.1.0'
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

javafx {
version = "22.0.2"
modules = [ 'javafx.base' ]
}

sourceSets {
main {
java {
Expand Down
1 change: 1 addition & 0 deletions megameklab/resources/megameklab/resources/Menu.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ MenuBar.accessibleName=Main Menu Bar
### File Menu
fileMenu.text=File
miResetCurrentUnit.text=Reset Current Unit
miDropChanges.text=Revert Unsaved Changes
miNewTab.text=New Tab
miCloseTab.text=Close Tab
miReopenTab.text=Reopen Closed Tab
Expand Down
14 changes: 14 additions & 0 deletions megameklab/src/megameklab/ui/MegaMekLabMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ public boolean safetyPrompt() {
}
}

public boolean safetyPrompt(boolean askSave) {
if (askSave) {
return safetyPrompt();
} else {
int savePrompt = JOptionPane.showConfirmDialog(this,
"All unsaved changes in the current unit will be discarded. Are you sure you wish to continue?",
"Discard Changes?",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);

return savePrompt == JOptionPane.YES_OPTION;
}
}

@Override
public boolean exit() {
if (!safetyPrompt()) {
Expand Down
24 changes: 20 additions & 4 deletions megameklab/src/megameklab/ui/MegaMekLabTabbedUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -538,20 +538,35 @@ public void markChanged(boolean changed) {
public EditorTab(String name, MegaMekLabMainUI mainUI) {
JLabel unitName = new JLabel(name);
changesIndicator = new JLabel();
add(unitName);
add(changesIndicator);

editor = mainUI;

setOpaque(false);


// ⮐ symbol
JButton resetButton = new JButton("\u2B90");
resetButton.setFont(symbolFont);
resetButton.setForeground(Color.ORANGE);
resetButton.setFocusable(false);
resetButton.setBorder(BorderFactory.createEmptyBorder());
resetButton.setToolTipText("Undo any unsaved changes");
resetButton.addActionListener(l -> {
tabs.setSelectedComponent(editor.getContentPane());
getMMLMenuBar().dropChanges();
}
);
add(resetButton);

// ✖ symbol
JButton closeButton = new JButton("\u2716");
closeButton.setFont(symbolFont);
closeButton.setForeground(Color.RED);
closeButton.setFocusable(false);
closeButton.setBorder(BorderFactory.createEmptyBorder());
closeButton.setToolTipText("Shift-click to skip the save confirmation dialog");
add(unitName);
add(changesIndicator);
add(closeButton);
closeButton.setToolTipText("<html>Close tab<br>Shift-click to skip the save confirmation dialog</html>");
closeButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Expand All @@ -560,6 +575,7 @@ public void mouseClicked(MouseEvent e) {
}
}
});
add(closeButton);
}
}

Expand Down
42 changes: 40 additions & 2 deletions megameklab/src/megameklab/ui/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.loaders.EntityLoadingException;
import megamek.common.templates.TROView;
import megamek.logging.MMLogger;
import megameklab.MMLConstants;
Expand Down Expand Up @@ -156,10 +157,17 @@ private JMenu createFileMenu() {
final JMenuItem miResetCurrentUnit = new JMenuItem(resources.getString("miResetCurrentUnit.text"));
miResetCurrentUnit.setName("miResetCurrentUnit");
miResetCurrentUnit.setMnemonic(KeyEvent.VK_R);
miResetCurrentUnit.addActionListener(this::jMenuResetEntity_actionPerformed);
miResetCurrentUnit.addActionListener(this::resetEntity);
miResetCurrentUnit.setEnabled(isUnitGui());
fileMenu.add(miResetCurrentUnit);

final JMenuItem miDropChanges = new JMenuItem(resources.getString("miDropChanges.text"));
miDropChanges.setName("miDropChanges");
miDropChanges.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
miDropChanges.addActionListener(i -> dropChanges());
miDropChanges.setEnabled(isUnitGui());
fileMenu.add(miDropChanges);

fileMenu.add(createSwitchUnitTypeMenu());
fileMenu.add(createLoadMenu());
fileMenu.add(createSaveMenu());
Expand Down Expand Up @@ -1076,7 +1084,7 @@ private void recordSheetImagesAction() {
dlg.setVisible(true);
}

private void jMenuResetEntity_actionPerformed(ActionEvent event) {
private void resetEntity(ActionEvent event) {
if (!owner.safetyPrompt() || !isUnitGui()) {
return;
}
Expand Down Expand Up @@ -1107,11 +1115,41 @@ private void jMenuResetEntity_actionPerformed(ActionEvent event) {
} else {
logger.warn("Received unknown entityType!");
}
getUnitMainUi().setFileName("");
reload();
refresh();
getUnitMainUi().repaint();
}

public void dropChanges() {
var editor = getUnitMainUi();

if (!editor.safetyPrompt(false)) {
return;
}

var filename = editor.getFileName();
if (filename == null || filename.isBlank()) {
resetEntity(null);
return;
}

var file = new File(filename);
if (!file.isFile()) {
resetEntity(null);
} else {
try {
var entity = new MekFileParser(file).getEntity();
UnitUtil.updateLoadedUnit(entity);
editor.setEntity(entity, filename);
reload();
refresh();
} catch (EntityLoadingException e) {
logger.error(e, "Could not reset unit", "Reset error");
}
}
}

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
18 changes: 14 additions & 4 deletions megameklab/src/megameklab/ui/mek/BMBuildTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import javafx.collections.ListChangeListener;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.Mounted;
import megameklab.ui.EntitySource;
Expand All @@ -38,7 +39,10 @@
import megameklab.util.MekUtil;
import megameklab.util.UnitUtil;

public class BMBuildTab extends ITab {
public class BMBuildTab extends ITab implements ListChangeListener<Mounted<?>> {

// Upon loading a unit, we don't want to run auto-sort or auto-compact until the user has made any sort of change related to crit slots.
private boolean touched = false;

private RefreshListener refresh = null;
private final BMCriticalView critView;
Expand Down Expand Up @@ -70,6 +74,7 @@ public BMBuildTab(EntitySource eSource) {
add(leftSide);
add(buildView);
refresh();
getMek().replaceEquipmentChangedListener(this);
}

private JComponent createButtonPanel() {
Expand Down Expand Up @@ -137,7 +142,7 @@ public void refresh() {
}

private void autoFillUnHitTables() {
if (autoFillUnHitTables.isSelected()) {
if (autoFillUnHitTables.isSelected() && touched) {
MekUtil.fillInFMU(getMek());
}
}
Expand All @@ -163,7 +168,7 @@ private void resetCrits() {
* It is important that this method does not call a refresh to avoid a loop!
*/
private void autoCompactCrits() {
if (autoCompact.isSelected() && !autoSort.isSelected()) {
if (autoCompact.isSelected() && !autoSort.isSelected() && touched) {
MekUtil.compactCriticals(getMek());
}
}
Expand Down Expand Up @@ -191,7 +196,7 @@ private void sortCrits() {
* It is important that this method does not call a refresh to avoid a loop!
*/
private void autoSortCrits() {
if (autoSort.isSelected()) {
if (autoSort.isSelected() && touched) {
MekUtil.sortCrits(getMek());
}
}
Expand All @@ -207,4 +212,9 @@ public void refreshAll() {
refresh.refreshAll();
}
}

@Override
public void onChanged(Change<? extends Mounted<?>> c) {
touched = true;
}
}

0 comments on commit 6561f8e

Please sign in to comment.