Adds a proper GUI to display backup progress
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

Extracts backup code to its own class
Adds a new GUI to display progress and the file copied
This commit is contained in:
2021-09-27 21:39:15 +02:00
parent bf77c13072
commit 60fdcf5ddc
5 changed files with 322 additions and 101 deletions

View File

@ -0,0 +1,94 @@
package net.knarcraft.minecraftserverlauncher.userinterface;
import net.knarcraft.minecraftserverlauncher.utility.BackupUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* The Backup GUI is used to display backup progress
*/
public class BackupGUI implements ActionListener {
private static JFrame frame;
private static JTextArea progressTextArea;
private static JProgressBar progressBar;
private static JButton cancelButton;
/**
* Instantiates a new GUI
*/
public BackupGUI() {
instantiate();
}
/**
* Initializes the server consoles frame
*/
public void instantiate() {
if (frame != null) {
return;
}
frame = new JFrame("Running backup...");
frame.setBounds(100, 100, 500, 140);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
progressTextArea = new JTextArea();
progressTextArea.setEditable(false);
progressBar = new JProgressBar();
cancelButton = new JButton("Cancel");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(progressTextArea);
panel.add(Box.createRigidArea(new Dimension(0,5)));
panel.add(progressBar);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
buttonPane.add(Box.createHorizontalGlue());
buttonPane.add(cancelButton);
cancelButton.addActionListener(BackupGUI.this);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.PAGE_END);
frame.setVisible(true);
}
/**
* Updates information about the backup progress
*
* @param infoText <p>The text to display</p>
* @param progressPercent <p>The new percent of the progress bar</p>
*/
public static void updateProgress(String infoText, int progressPercent) {
if (progressTextArea == null || progressBar == null) {
return;
}
progressTextArea.setText(infoText);
progressBar.setValue(progressPercent);
}
/**
* Destroys the backup GUI
*/
public static void destroy() {
BackupUtil.abortBackup();
progressTextArea = null;
progressBar = null;
cancelButton = null;
if (frame != null) {
frame.dispose();
}
frame = null;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource() == cancelButton) {
destroy();
}
}
}

View File

@ -4,7 +4,7 @@ import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.profile.Collection;
import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController;
import net.knarcraft.minecraftserverlauncher.server.ServerHandler;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import net.knarcraft.minecraftserverlauncher.utility.BackupUtil;
import javax.imageio.ImageIO;
import javax.naming.ConfigurationException;
@ -334,7 +334,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
addServer();
} else if (actionSource == backupButton) {
//Run backup in its own thread to prevent locking up
Executors.newSingleThreadExecutor().execute(() -> CommonFunctions.backup(this));
Executors.newSingleThreadExecutor().execute(() -> BackupUtil.backup(this));
} else if (actionSource == addProfileButton) {
controller.addProfile(JOptionPane.showInputDialog("Profile name: "));
updateProfiles();