248 lines
9.8 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.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;
//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;
//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 == 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 == 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);
}
/**
* 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);
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);
}
}
}
/**
* Asks the user for a delay if checked, and sets the value to the current profile
*/
private void delay() {
String selectedProfile = serverLauncherGUI.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.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.getSelectedProfile();
if (selectedProfile != null) {
controller.setDownloadAllJars(downloadJarsCheckBoxMenuItem.isSelected());
} else {
serverLauncherGUI.showError("No profile selected");
}
}
}