fix: add a delete button for saved states

Files are deleted from disk when the game is saved.
This commit is contained in:
lantzelot-swe 2021-11-20 22:41:42 +01:00
parent 7d40e17db5
commit 178f8a0b7e
3 changed files with 201 additions and 14 deletions

View File

@ -67,6 +67,7 @@ public class SaveStatePanel extends JPanel
private FileNameExtensionFilter imagefilter =
new FileNameExtensionFilter("png, gif, jpeg, bmp", "png", "gif", "jpg", "jpeg", "bmp");
private JButton deleteButton;
public SaveStatePanel(MainViewModel model, SAVESTATE saveState)
{
@ -83,7 +84,7 @@ public class SaveStatePanel extends JPanel
gbc_screenshotLabel.weighty = 1.0;
gbc_screenshotLabel.gridheight = 3;
gbc_screenshotLabel.anchor = GridBagConstraints.NORTHWEST;
gbc_screenshotLabel.insets = new Insets(1, 1, 0, 1);
gbc_screenshotLabel.insets = new Insets(1, 1, 0, 5);
gbc_screenshotLabel.gridx = 0;
gbc_screenshotLabel.gridy = 0;
add(getScreenshotLabel(), gbc_screenshotLabel);
@ -91,11 +92,12 @@ public class SaveStatePanel extends JPanel
gbc_timeTextField.fill = GridBagConstraints.HORIZONTAL;
gbc_timeTextField.anchor = GridBagConstraints.NORTHWEST;
gbc_timeTextField.weighty = 1.0;
gbc_timeTextField.insets = new Insets(0, 5, 5, 5);
gbc_timeTextField.gridx = 3;
gbc_timeTextField.insets = new Insets(0, 0, 5, 5);
gbc_timeTextField.gridx = 4;
gbc_timeTextField.gridy = 1;
add(getTimeField(), gbc_timeTextField);
GridBagConstraints gbc_snapshotTextField = new GridBagConstraints();
gbc_snapshotTextField.gridwidth = 2;
gbc_snapshotTextField.weightx = 1.0;
gbc_snapshotTextField.anchor = GridBagConstraints.NORTHWEST;
gbc_snapshotTextField.insets = new Insets(0, 0, 5, 5);
@ -105,43 +107,50 @@ public class SaveStatePanel extends JPanel
add(getSnapshotTextField(), gbc_snapshotTextField);
GridBagConstraints gbc_snapshotButton = new GridBagConstraints();
gbc_snapshotButton.insets = new Insets(-1, 0, 5, 5);
gbc_snapshotButton.anchor = GridBagConstraints.NORTHWEST;
gbc_snapshotButton.gridx = 2;
gbc_snapshotButton.anchor = GridBagConstraints.NORTHEAST;
gbc_snapshotButton.gridx = 3;
gbc_snapshotButton.gridy = 1;
add(getGameButton(), gbc_snapshotButton);
GridBagConstraints gbc_timeLabel = new GridBagConstraints();
gbc_timeLabel.anchor = GridBagConstraints.NORTHWEST;
gbc_timeLabel.insets = new Insets(5, 5, 0, 0);
gbc_timeLabel.gridx = 3;
gbc_timeLabel.insets = new Insets(5, 5, 5, 0);
gbc_timeLabel.gridx = 4;
gbc_timeLabel.gridy = 0;
add(getTimeLabel(), gbc_timeLabel);
GridBagConstraints gbc_snapshotLabel = new GridBagConstraints();
gbc_snapshotLabel.gridwidth = 2;
gbc_snapshotLabel.weightx = 1.0;
gbc_snapshotLabel.insets = new Insets(5, 0, 0, 5);
gbc_snapshotLabel.insets = new Insets(5, 0, 5, 5);
gbc_snapshotLabel.anchor = GridBagConstraints.NORTHWEST;
gbc_snapshotLabel.gridx = 1;
gbc_snapshotLabel.gridy = 0;
add(getSnapshotLabel(), gbc_snapshotLabel);
GridBagConstraints gbc_screenshotButton = new GridBagConstraints();
gbc_screenshotButton.weightx = 1.0;
gbc_screenshotButton.anchor = GridBagConstraints.SOUTHWEST;
gbc_screenshotButton.insets = new Insets(0, 0, 0, 5);
gbc_screenshotButton.gridx = 1;
gbc_screenshotButton.gridy = 2;
add(getScreenshotButton(), gbc_screenshotButton);
GridBagConstraints gbc_deleteButton = new GridBagConstraints();
gbc_deleteButton.gridwidth = 2;
gbc_deleteButton.anchor = GridBagConstraints.SOUTHEAST;
gbc_deleteButton.insets = new Insets(0, 0, 0, 5);
gbc_deleteButton.gridx = 2;
gbc_deleteButton.gridy = 2;
add(getDeleteButton(), gbc_deleteButton);
GridBagConstraints gbc_runButton = new GridBagConstraints();
gbc_runButton.insets = new Insets(0, 0, 0, 5);
gbc_runButton.weighty = 1.0;
gbc_runButton.anchor = GridBagConstraints.SOUTHEAST;
gbc_runButton.gridx = 3;
gbc_runButton.gridx = 4;
gbc_runButton.gridy = 2;
add(getRunButton(), gbc_runButton);
if (saveState != SAVESTATE.Save3)
{
GridBagConstraints gbc_separator = new GridBagConstraints();
gbc_separator.fill = GridBagConstraints.HORIZONTAL;
gbc_separator.insets = new Insets(5, 20, 0, 20);
gbc_separator.gridwidth = 4;
gbc_separator.insets = new Insets(5, 5, 0, 5);
gbc_separator.gridwidth = 5;
gbc_separator.gridx = 0;
gbc_separator.gridy = 3;
add(getSeparator(), gbc_separator);
@ -193,6 +202,7 @@ public class SaveStatePanel extends JPanel
getSnapshotTextField().setText(getSnapshotFileName());
reloadScreen();
getRunButton().setEnabled(!getSnapshotTextField().getText().isEmpty());
getDeleteButton().setEnabled(!getSnapshotTextField().getText().isEmpty());
getTimeField().setEnabled(!getSnapshotTextField().getText().isEmpty());
}
@ -778,4 +788,44 @@ public class SaveStatePanel extends JPanel
}
setTimeToModel();
}
private JButton getDeleteButton()
{
if (deleteButton == null)
{
deleteButton = new JButton("Delete");
deleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int value = JOptionPane.showConfirmDialog(SaveStatePanel.this,
"Are you sure you want to delete the saved state?",
"Delete saved state",
JOptionPane.YES_NO_OPTION);
if (value == JOptionPane.YES_OPTION)
{
gamesFileUpdated = false;
switch (saveState)
{
case Save0:
stateModel.setState1Deleted(true);
break;
case Save1:
stateModel.setState2Deleted(true);
break;
case Save2:
stateModel.setState3Deleted(true);
break;
case Save3:
stateModel.setState4Deleted(true);
break;
default:
break;
}
}
}
});
}
return deleteButton;
}
}

View File

@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory;
import se.lantz.model.MainViewModel;
import se.lantz.model.SavedStatesModel;
import se.lantz.model.SavedStatesModel.SAVESTATE;
import se.lantz.util.ExceptionHandler;
import se.lantz.util.FileManager;
@ -75,6 +76,24 @@ public class SavedStatesManager
public void saveSavedStates()
{
//First, check if any saved states has been deleted. Delete the corresponding files.
if (savedStatesModel.isState1Deleted())
{
deleteSavedState(SAVESTATE.Save0);
}
if (savedStatesModel.isState2Deleted())
{
deleteSavedState(SAVESTATE.Save1);
}
if (savedStatesModel.isState3Deleted())
{
deleteSavedState(SAVESTATE.Save2);
}
if (savedStatesModel.isState4Deleted())
{
deleteSavedState(SAVESTATE.Save3);
}
//If the game has been renamed, make sure to rename the saves folder also
if (model.getInfoModel().isTitleChanged())
{
@ -306,6 +325,56 @@ public class SavedStatesManager
return this.importOverwrite;
}
private void deleteSavedState(SAVESTATE state)
{
String fileName = model.getInfoModel().getGamesFile();
Path saveFolder = new File(SAVES + fileName).toPath();
try
{
switch (state)
{
case Save0:
Path mta0Path = saveFolder.resolve(MTA0);
Path vsz0Path = saveFolder.resolve(VSZ0);
Path png0Path = saveFolder.resolve(PNG0);
Files.deleteIfExists(mta0Path);
Files.deleteIfExists(vsz0Path);
Files.deleteIfExists(png0Path);
break;
case Save1:
Path mta1Path = saveFolder.resolve(MTA1);
Path vsz1Path = saveFolder.resolve(VSZ1);
Path png1Path = saveFolder.resolve(PNG1);
Files.deleteIfExists(mta1Path);
Files.deleteIfExists(vsz1Path);
Files.deleteIfExists(png1Path);
break;
case Save2:
Path mta2Path = saveFolder.resolve(MTA2);
Path vsz2Path = saveFolder.resolve(VSZ2);
Path png2Path = saveFolder.resolve(PNG2);
Files.deleteIfExists(mta2Path);
Files.deleteIfExists(vsz2Path);
Files.deleteIfExists(png2Path);
break;
case Save3:
Path mta3Path = saveFolder.resolve(MTA3);
Path vsz3Path = saveFolder.resolve(VSZ3);
Path png3Path = saveFolder.resolve(PNG3);
Files.deleteIfExists(mta3Path);
Files.deleteIfExists(vsz3Path);
Files.deleteIfExists(png3Path);
break;
default:
break;
}
}
catch (IOException e)
{
ExceptionHandler.handleException(e, "Could not delete saved state files.");
}
}
public void exportSavedStates(StringBuilder infoBuilder)
{
noFilesCopied = 0;

View File

@ -4,6 +4,8 @@ import java.awt.image.BufferedImage;
import java.nio.file.Path;
import java.util.Objects;
import se.lantz.model.SavedStatesModel.SAVESTATE;
public class SavedStatesModel extends AbstractModel
{
public static enum SAVESTATE {Save0, Save1, Save2, Save3};
@ -32,6 +34,11 @@ public class SavedStatesModel extends AbstractModel
private String state3time = "00:00:00";
private String state4time = "00:00:00";
private boolean state1Deleted = false;
private boolean state2Deleted = false;
private boolean state3Deleted = false;
private boolean state4Deleted = false;
public String getState1PngFile()
{
return state1PngFile;
@ -295,6 +302,63 @@ public class SavedStatesModel extends AbstractModel
notifyChange();
}
}
public boolean isState1Deleted()
{
return state1Deleted;
}
public void setState1Deleted(boolean state1Deleted)
{
this.state1Deleted = state1Deleted;
state1PngImage = null;
state1PngFile = "";
state1Path = null;
state1time = "00:00:00";
state1File = "";
notifyChange();
}
public boolean isState2Deleted()
{
return state2Deleted;
}
public void setState2Deleted(boolean state2Deleted)
{
this.state2Deleted = state2Deleted;
state2PngImage = null;
state2PngFile = "";
state2Path = null;
state2time = "00:00:00";
state2File = "";
notifyChange();
}
public boolean isState3Deleted()
{
return state3Deleted;
}
public void setState3Deleted(boolean state3Deleted)
{
this.state3Deleted = state3Deleted;
state3PngImage = null;
state3PngFile = "";
state3Path = null;
state3time = "00:00:00";
state3File = "";
notifyChange();
}
public boolean isState4Deleted()
{
return state4Deleted;
}
public void setState4Deleted(boolean state4Deleted)
{
this.state4Deleted = state4Deleted;
state4PngImage = null;
state4PngFile = "";
state4Path = null;
state4time = "00:00:00";
state4File = "";
notifyChange();
}
public void resetProperties()
{
state1PngFile = "";
@ -317,6 +381,10 @@ public class SavedStatesModel extends AbstractModel
state2File = "";
state3File = "";
state4File = "";
state1Deleted = false;
state2Deleted = false;
state3Deleted = false;
state4Deleted = false;
}
public void notifyChange()