All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
Additionally fixes some formatting mistakes Updates version to 1.4.1
285 lines
12 KiB
Java
285 lines
12 KiB
Java
package net.knarcraft.minecraftserverlauncher.userinterface;
|
|
|
|
import net.knarcraft.minecraftserverlauncher.profile.Profile;
|
|
import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController;
|
|
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.io.File;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* This class takes care of the GUI's top menu
|
|
*/
|
|
public class ServerLauncherMenu implements ActionListener {
|
|
|
|
private final JMenuBar menuBar;
|
|
//Options
|
|
private JCheckBoxMenuItem runInBackgroundCheckBoxMenuItem;
|
|
private JCheckBoxMenuItem delayStartupCheckBoxMenuItem;
|
|
private JCheckBoxMenuItem downloadJarsCheckBoxMenuItem;
|
|
private JMenuItem javaCommandMenuItem;
|
|
private JMenuItem oldJavaCommandMenuItem;
|
|
private JMenuItem deleteBuiltJarsMenuItem;
|
|
//Help
|
|
private JMenuItem errorsMenuItem;
|
|
private JMenuItem setupMenuItem;
|
|
private JMenuItem manualUpdateMenuItem;
|
|
//Info/options
|
|
private JMenuItem runInBackgroundMenuItem;
|
|
private JMenuItem delayStartupMenuItem;
|
|
private JMenuItem downloadJarsMenuItem;
|
|
private JMenuItem javaCommandInfoMenuItem;
|
|
private JMenuItem oldJavaCommandInfoMenuItem;
|
|
private JMenuItem deleteBuiltJarsInfoMenuItem;
|
|
//Info/about
|
|
private JMenuItem aboutMenuItem;
|
|
private JMenuItem storyMenuItem;
|
|
|
|
private final ServerLauncherController controller;
|
|
private final ServerLauncherGUI serverLauncherGUI;
|
|
|
|
/**
|
|
* Initializes a new server launcher menu
|
|
*
|
|
* @param menuBar <p>The menu bar to attach items to</p>
|
|
* @param serverLauncherGUI <p>The server launcher GUI to use</p>
|
|
*/
|
|
public ServerLauncherMenu(JMenuBar menuBar, ServerLauncherGUI serverLauncherGUI) {
|
|
this.controller = ServerLauncherController.getInstance();
|
|
this.menuBar = menuBar;
|
|
this.serverLauncherGUI = serverLauncherGUI;
|
|
initialize();
|
|
}
|
|
|
|
/**
|
|
* Initializes all GUI elements
|
|
*/
|
|
public void initialize() {
|
|
JMenu mnOptions = new JMenu("Options");
|
|
menuBar.add(mnOptions);
|
|
generateOptionsMenuItems(mnOptions);
|
|
|
|
JMenu mnHelp = new JMenu("Help");
|
|
menuBar.add(mnHelp);
|
|
|
|
errorsMenuItem = createMenuItem("Errors", mnHelp);
|
|
setupMenuItem = createMenuItem("Setup", mnHelp);
|
|
manualUpdateMenuItem = createMenuItem("Manual update", mnHelp);
|
|
|
|
JMenu mnInfo = new JMenu("Info");
|
|
menuBar.add(mnInfo);
|
|
generateInfoMenuItems(mnInfo);
|
|
}
|
|
|
|
/**
|
|
* Updates the state of checkboxes based on whether options are enabled
|
|
*/
|
|
public void update() {
|
|
runInBackgroundCheckBoxMenuItem.setState(controller.getRunInBackground());
|
|
delayStartupCheckBoxMenuItem.setState(controller.getDelayStartup() > 0);
|
|
downloadJarsCheckBoxMenuItem.setState(controller.getDownloadAllJars());
|
|
}
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent actionEvent) {
|
|
Object actionSource = actionEvent.getSource();
|
|
if (actionSource == runInBackgroundCheckBoxMenuItem) {
|
|
background();
|
|
} else if (actionSource == delayStartupCheckBoxMenuItem) {
|
|
delay();
|
|
} else if (actionSource == downloadJarsCheckBoxMenuItem) {
|
|
downloadJars();
|
|
} else if (actionSource == javaCommandMenuItem) {
|
|
configureJava(false);
|
|
} else if (actionSource == oldJavaCommandMenuItem) {
|
|
configureJava(true);
|
|
} else if (actionSource == deleteBuiltJarsMenuItem) {
|
|
deleteBuiltJars();
|
|
} else if (actionSource == errorsMenuItem) {
|
|
CommonFunctions.goToURL(serverLauncherGUI.getMessage("infoURL"));
|
|
} else if (actionSource == setupMenuItem) {
|
|
serverLauncherGUI.showMessage("Setup", serverLauncherGUI.getMessage("setupText"));
|
|
} else if (actionSource == manualUpdateMenuItem) {
|
|
CommonFunctions.goToURL(serverLauncherGUI.getMessage("manualUpdateURL"));
|
|
} else if (actionSource == runInBackgroundMenuItem) {
|
|
serverLauncherGUI.showMessage("Run in background", serverLauncherGUI.getMessage("runInBackgroundText"));
|
|
} else if (actionSource == delayStartupMenuItem) {
|
|
serverLauncherGUI.showMessage("Delay startup", serverLauncherGUI.getMessage("delayStartupText"));
|
|
} else if (actionSource == downloadJarsMenuItem) {
|
|
serverLauncherGUI.showMessage("Download jars", serverLauncherGUI.getMessage("downloadJarsText"));
|
|
} else if (actionSource == javaCommandInfoMenuItem) {
|
|
serverLauncherGUI.showMessage("Java command", serverLauncherGUI.getMessage("javaCommandText"));
|
|
} else if (actionSource == oldJavaCommandInfoMenuItem) {
|
|
serverLauncherGUI.showMessage("Old Java command", serverLauncherGUI.getMessage("oldJavaCommandText"));
|
|
} else if (actionSource == deleteBuiltJarsInfoMenuItem) {
|
|
serverLauncherGUI.showMessage("Delete built jar files", serverLauncherGUI.getMessage("deleteBuiltJarFilesText"));
|
|
} else if (actionSource == aboutMenuItem) {
|
|
serverLauncherGUI.showMessage("About", serverLauncherGUI.getMessage("aboutText"));
|
|
} else if (actionSource == storyMenuItem) {
|
|
CommonFunctions.goToURL(serverLauncherGUI.getMessage("storyURL"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generates the children of the options menu
|
|
*
|
|
* @param mnOptions <p>A reference to the options menu</p>
|
|
*/
|
|
private void generateOptionsMenuItems(JMenu mnOptions) {
|
|
runInBackgroundCheckBoxMenuItem = createCheckBoxMenuItem("Run in background on exit", mnOptions);
|
|
delayStartupCheckBoxMenuItem = createCheckBoxMenuItem("Delay Startup", mnOptions);
|
|
downloadJarsCheckBoxMenuItem = createCheckBoxMenuItem("Download jars", mnOptions);
|
|
javaCommandMenuItem = createMenuItem("Java command", mnOptions);
|
|
oldJavaCommandMenuItem = createMenuItem("Old Java command", mnOptions);
|
|
deleteBuiltJarsMenuItem = createMenuItem("Delete built jar files", mnOptions);
|
|
}
|
|
|
|
/**
|
|
* Generates the children of the info menu
|
|
*
|
|
* @param mnInfo <p>A reference to the info menu</p>
|
|
*/
|
|
private void generateInfoMenuItems(JMenu mnInfo) {
|
|
JMenu mnOptionsInfo = new JMenu("Options");
|
|
mnInfo.add(mnOptionsInfo);
|
|
|
|
runInBackgroundMenuItem = createMenuItem("Run in background on exit", mnOptionsInfo);
|
|
delayStartupMenuItem = createMenuItem("Delay Startup", mnOptionsInfo);
|
|
downloadJarsMenuItem = createMenuItem("Download jars", mnOptionsInfo);
|
|
javaCommandInfoMenuItem = createMenuItem("Java command", mnOptionsInfo);
|
|
oldJavaCommandInfoMenuItem = createMenuItem("Old Java command", mnOptionsInfo);
|
|
deleteBuiltJarsInfoMenuItem = createMenuItem("Delete built jar files", mnOptionsInfo);
|
|
|
|
JMenu mnAbout = new JMenu("About");
|
|
mnInfo.add(mnAbout);
|
|
|
|
aboutMenuItem = createMenuItem("About", mnAbout);
|
|
storyMenuItem = createMenuItem("Story", mnAbout);
|
|
}
|
|
|
|
/**
|
|
* Creates a checkbox menu item
|
|
*
|
|
* @param itemName <p>The name of the new checkbox item</p>
|
|
* @param parent <p>The parent menu the item belongs to</p>
|
|
* @return <p>The created checkbox menu item</p>
|
|
*/
|
|
private JCheckBoxMenuItem createCheckBoxMenuItem(String itemName, JMenu parent) {
|
|
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(itemName);
|
|
parent.add(menuItem);
|
|
menuItem.addActionListener(this);
|
|
return menuItem;
|
|
}
|
|
|
|
/**
|
|
* Creates a menu item
|
|
*
|
|
* @param itemName <p>The name of the new item</p>
|
|
* @param parent <p>The parent menu the item belongs to</p>
|
|
* @return <p>The created menu item</p>
|
|
*/
|
|
private JMenuItem createMenuItem(String itemName, JMenu parent) {
|
|
JMenuItem menuItem = new JMenuItem(itemName);
|
|
parent.add(menuItem);
|
|
menuItem.addActionListener(this);
|
|
return menuItem;
|
|
}
|
|
|
|
/**
|
|
* Asks the user for the new Java path
|
|
*
|
|
* @param old <p>Whether asking for the path to the old java version</p>
|
|
*/
|
|
private void configureJava(boolean old) {
|
|
if (old) {
|
|
String response = JOptionPane.showInputDialog("Command or path to Java: ", controller.getOldJavaCommand());
|
|
if (response != null) {
|
|
controller.setOldJavaCommand(response);
|
|
}
|
|
} else {
|
|
String response = JOptionPane.showInputDialog("Command or path to Java: ", controller.getJavaCommand());
|
|
if (response != null) {
|
|
controller.setJavaCommand(response);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletes build Spigot and Bukkit .jar files if the user accepts
|
|
*/
|
|
private void deleteBuiltJars() {
|
|
int answer = JOptionPane.showConfirmDialog(null, "This will delete built .jar files, causing them " +
|
|
"to be rebuilt on the next run. Do you want to continue?", "Delete built .jar files",
|
|
JOptionPane.YES_NO_OPTION
|
|
);
|
|
if (answer == JOptionPane.YES_NO_OPTION) {
|
|
String jarDirectory = controller.getJarDirectory();
|
|
File spigotFile = new File(jarDirectory + "SpigotLatest.jar");
|
|
File bukkitFile = new File(jarDirectory + "BukkitLatest.jar");
|
|
boolean success = true;
|
|
if (spigotFile.exists() && !spigotFile.delete()) {
|
|
serverLauncherGUI.showError("Unable to delete latest spigot .jar");
|
|
success = false;
|
|
}
|
|
if (bukkitFile.exists() && !bukkitFile.delete()) {
|
|
serverLauncherGUI.showError("Unable to delete latest bukkit .jar");
|
|
success = false;
|
|
}
|
|
if (success) {
|
|
serverLauncherGUI.showMessage("Deletion successful", "Deleted built .jar files");
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Asks the user for a delay if checked, and sets the value to the current profile
|
|
*/
|
|
private void delay() {
|
|
String selectedProfile = serverLauncherGUI.getControlPanelTab().getSelectedProfile();
|
|
if (selectedProfile != null) {
|
|
Profile profile = controller.getProfileByName(selectedProfile);
|
|
if (delayStartupCheckBoxMenuItem.isSelected()) {
|
|
String response = JOptionPane.showInputDialog("Seconds to delay: ");
|
|
if (response == null) {
|
|
delayStartupCheckBoxMenuItem.setState(false);
|
|
return;
|
|
}
|
|
int parsed = Integer.parseInt(response);
|
|
Objects.requireNonNull(profile).setDelayStartup(parsed);
|
|
} else {
|
|
Objects.requireNonNull(profile).setDelayStartup(0);
|
|
}
|
|
} else {
|
|
serverLauncherGUI.showError("No profile selected");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Saves the runInBackground setting to the current profile
|
|
*/
|
|
private void background() {
|
|
String selectedProfile = serverLauncherGUI.getControlPanelTab().getSelectedProfile();
|
|
if (selectedProfile != null) {
|
|
Profile profile = controller.getProfileByName(selectedProfile);
|
|
Objects.requireNonNull(profile).setRunInBackground(runInBackgroundCheckBoxMenuItem.isSelected());
|
|
} else {
|
|
serverLauncherGUI.showError("No profile selected");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Saves the downloadJars setting to the current profile
|
|
*/
|
|
private void downloadJars() {
|
|
String selectedProfile = serverLauncherGUI.getControlPanelTab().getSelectedProfile();
|
|
if (selectedProfile != null) {
|
|
controller.setDownloadAllJars(downloadJarsCheckBoxMenuItem.isSelected());
|
|
} else {
|
|
serverLauncherGUI.showError("No profile selected");
|
|
}
|
|
}
|
|
}
|