Splits the GUI into more components to increase maintainability
This commit is contained in:
@ -0,0 +1,195 @@
|
||||
package net.knarcraft.minecraftserverlauncher.userinterface;
|
||||
|
||||
import net.knarcraft.minecraftserverlauncher.profile.Controller;
|
||||
import net.knarcraft.minecraftserverlauncher.profile.Profile;
|
||||
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 JMenuBar menuBar;
|
||||
//Options
|
||||
private JCheckBoxMenuItem runInBackgroundCheckBoxMenuItem;
|
||||
private JCheckBoxMenuItem delayStartupCheckBoxMenuItem;
|
||||
private JCheckBoxMenuItem downloadJarsCheckBoxMenuItem;
|
||||
//Help
|
||||
private JMenuItem errorsMenuItem;
|
||||
private JMenuItem setupMenuItem;
|
||||
private JMenuItem manualUpdateMenuItem;
|
||||
//Info/options
|
||||
private JMenuItem runInBackgroundMenuItem;
|
||||
private JMenuItem delayStartupMenuItem;
|
||||
private JMenuItem downloadJarsMenuItem;
|
||||
//Info/about
|
||||
private JMenuItem aboutMenuItem;
|
||||
private JMenuItem storyMenuItem;
|
||||
|
||||
private Controller controller;
|
||||
private 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 = Controller.getInstance();
|
||||
this.menuBar = menuBar;
|
||||
this.serverLauncherGUI = serverLauncherGUI;
|
||||
initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes all GUI elements
|
||||
*/
|
||||
public void initialize() {
|
||||
JMenu mnOptions = new JMenu("Options");
|
||||
menuBar.add(mnOptions);
|
||||
|
||||
runInBackgroundCheckBoxMenuItem = new JCheckBoxMenuItem("Run in background on exit");
|
||||
mnOptions.add(runInBackgroundCheckBoxMenuItem);
|
||||
runInBackgroundCheckBoxMenuItem.addActionListener(this);
|
||||
|
||||
delayStartupCheckBoxMenuItem = new JCheckBoxMenuItem("Delay Startup");
|
||||
mnOptions.add(delayStartupCheckBoxMenuItem);
|
||||
delayStartupCheckBoxMenuItem.addActionListener(this);
|
||||
|
||||
downloadJarsCheckBoxMenuItem = new JCheckBoxMenuItem("Download jars");
|
||||
mnOptions.add(downloadJarsCheckBoxMenuItem);
|
||||
downloadJarsCheckBoxMenuItem.addActionListener(this);
|
||||
|
||||
JMenu mnHelp = new JMenu("Help");
|
||||
menuBar.add(mnHelp);
|
||||
|
||||
errorsMenuItem = new JMenuItem("Errors");
|
||||
mnHelp.add(errorsMenuItem);
|
||||
errorsMenuItem.addActionListener(this);
|
||||
|
||||
setupMenuItem = new JMenuItem("Setup");
|
||||
mnHelp.add(setupMenuItem);
|
||||
setupMenuItem.addActionListener(this);
|
||||
|
||||
manualUpdateMenuItem = new JMenuItem("Manual update");
|
||||
mnHelp.add(manualUpdateMenuItem);
|
||||
manualUpdateMenuItem.addActionListener(this);
|
||||
|
||||
JMenu mnInfo = new JMenu("Info");
|
||||
menuBar.add(mnInfo);
|
||||
|
||||
JMenu mnOptionsInfo = new JMenu("Options");
|
||||
mnInfo.add(mnOptionsInfo);
|
||||
|
||||
runInBackgroundMenuItem = new JMenuItem("Run in background on exit");
|
||||
mnOptionsInfo.add(runInBackgroundMenuItem);
|
||||
runInBackgroundMenuItem.addActionListener(this);
|
||||
|
||||
delayStartupMenuItem = new JMenuItem("Delay Startup");
|
||||
mnOptionsInfo.add(delayStartupMenuItem);
|
||||
delayStartupMenuItem.addActionListener(this);
|
||||
|
||||
downloadJarsMenuItem = new JMenuItem("Download jars");
|
||||
mnOptionsInfo.add(downloadJarsMenuItem);
|
||||
downloadJarsMenuItem.addActionListener(this);
|
||||
|
||||
JMenu mnAbout = new JMenu("About");
|
||||
mnInfo.add(mnAbout);
|
||||
|
||||
aboutMenuItem = new JMenuItem("About");
|
||||
mnAbout.add(aboutMenuItem);
|
||||
aboutMenuItem.addActionListener(this);
|
||||
|
||||
storyMenuItem = new JMenuItem("Story");
|
||||
mnAbout.add(storyMenuItem);
|
||||
storyMenuItem.addActionListener(this);
|
||||
}
|
||||
|
||||
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 == 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 == aboutMenuItem) {
|
||||
serverLauncherGUI.showMessage("About", serverLauncherGUI.getMessage("aboutText"));
|
||||
} else if (actionSource == storyMenuItem) {
|
||||
CommonFunctions.goToURL(serverLauncherGUI.getMessage("storyURL"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user