From bf77c1307214807e9dbfbe79c2996577b0520586 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 27 Sep 2021 18:06:33 +0200 Subject: [PATCH] Fixes some behavior regarding backups Makes sure to only proceed with backups if a valid path is given Makes the backup run in its own thread to prevent the software from locking up Makes sure only one backup can be running at once --- .../userinterface/ServerLauncherGUI.java | 19 +++++++++-------- .../utility/CommonFunctions.java | 21 +++++++++++++++++-- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java index 4f43417..a47bbb0 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java @@ -33,7 +33,7 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getR */ public class ServerLauncherGUI extends MessageHandler implements ActionListener, GUI { - private final JLabel lblStatuslabel = new JLabel("Servers are stopped"); + private final JLabel lblStatusLabel = new JLabel("Servers are stopped"); private final ServerLauncherController controller; private Map textStrings; private Tray applicationTray; @@ -101,7 +101,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, @Override public void setStatus(String text) { - this.lblStatuslabel.setText(text); + this.lblStatusLabel.setText(text); this.logMessage(text); } @@ -243,11 +243,11 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, panelBasic.add(profiles); profiles.addActionListener(this); - sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 6, SpringLayout.SOUTH, addProfileButton); - sl_panel.putConstraint(SpringLayout.SOUTH, lblStatuslabel, -10, SpringLayout.SOUTH, panelBasic); - sl_panel.putConstraint(SpringLayout.WEST, lblStatuslabel, 10, SpringLayout.WEST, panelBasic); - sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, -10, SpringLayout.EAST, panelBasic); - panelBasic.add(lblStatuslabel); + sl_panel.putConstraint(SpringLayout.NORTH, lblStatusLabel, 6, SpringLayout.SOUTH, addProfileButton); + sl_panel.putConstraint(SpringLayout.SOUTH, lblStatusLabel, -10, SpringLayout.SOUTH, panelBasic); + sl_panel.putConstraint(SpringLayout.WEST, lblStatusLabel, 10, SpringLayout.WEST, panelBasic); + sl_panel.putConstraint(SpringLayout.EAST, lblStatusLabel, -10, SpringLayout.EAST, panelBasic); + panelBasic.add(lblStatusLabel); addServerButton = new JButton("Add server"); sl_panel.putConstraint(SpringLayout.NORTH, addServerButton, 0, SpringLayout.NORTH, startServerButton); @@ -314,7 +314,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, @Override public void actionPerformed(ActionEvent e) { Object actionSource = e.getSource(); - //Registers actions on the main tab + //Register actions on the main tab handleMainTabButtons(actionSource); } @@ -333,7 +333,8 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, } else if (actionSource == addServerButton) { addServer(); } else if (actionSource == backupButton) { - CommonFunctions.backup(this); + //Run backup in its own thread to prevent locking up + Executors.newSingleThreadExecutor().execute(() -> CommonFunctions.backup(this)); } else if (actionSource == addProfileButton) { controller.addProfile(JOptionPane.showInputDialog("Profile name: ")); updateProfiles(); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java index c20d2b1..08efa37 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java @@ -36,6 +36,7 @@ import java.util.Scanner; public final class CommonFunctions { private static final String filesDirectory = Main.getApplicationWorkDirectory() + File.separator + "files"; + private static boolean backupRunning = false; /** * Creates all folders necessary for tests and normal operation @@ -53,7 +54,7 @@ public final class CommonFunctions { * @param folder

The folder to create

* @throws FileNotFoundException

If unable to create the folder

*/ - protected static void createFolder(File folder) throws FileNotFoundException { + public static void createFolder(File folder) throws FileNotFoundException { if (!folder.exists()) { if (!folder.mkdirs()) { throw new FileNotFoundException("Cannot create necessary directory."); @@ -298,8 +299,21 @@ public final class CommonFunctions { /** * Copies all server directories to a folder specified by the user. */ - public static void backup(GUI gui) { + public synchronized static void backup(GUI gui) { + if (backupRunning) { + return; + } else { + backupRunning = true; + } + + //Get the folder to save the backed up files in File path = gui.askForDirectory("Backup folder"); + if (path == null || !path.isDirectory()) { + backupRunning = false; + return; + } + + gui.setStatus("Backup running..."); for (Collection collection : Main.getController().getCurrentProfile().getCollections()) { //Ignore disabled and invalid servers if (collection.getServer().getPath().equals("") || !collection.getServer().isEnabled()) { @@ -312,6 +326,7 @@ public final class CommonFunctions { File destinationFolder = new File(path, name); //Create child folders if (!destinationFolder.exists() && !destinationFolder.mkdirs()) { + backupRunning = false; throw new IllegalArgumentException("Unable to create necessary sub-folder in the backup folder"); } //Backup @@ -319,9 +334,11 @@ public final class CommonFunctions { CommonFunctions.copyFolder(gui, srcFolder, destinationFolder); } catch (IOException e) { e.printStackTrace(); + backupRunning = false; } } gui.setStatus("Backup finished"); + backupRunning = false; } /**