Adds comments and refactors several classes
All checks were successful
KnarCraft/Minecraft-Server-Launcher/master This commit looks good

Adds comments to Collection
Makes some variable and function names more descriptive
Adds some new methods for showing messsages and errors
Adds a lot of missing comments
Enhances some existing comments
This commit is contained in:
2020-02-13 21:10:18 +01:00
parent c59cbcefbb
commit 040740db84
11 changed files with 489 additions and 383 deletions

View File

@ -63,7 +63,8 @@ public class Console implements ActionListener, KeyListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name.
//Sends the command from the input to the server with the same name.
if (e.getSource() == textInput) {
java.lang.String text = textInput.getText();
Profile.getCurrent().sendCommand(this.name, text);
commands.add(text);
@ -77,19 +78,11 @@ public class Console implements ActionListener, KeyListener {
@Override
public void keyPressed(KeyEvent e) {
//Cycles through previously used commands
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (commands.size() > 0 && commandIndex > 0) {
textInput.setText(commands.get(--commandIndex));
}
showPreviousCommand();
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (commands.size() > 0) {
if (commandIndex == commands.size() - 1) {
commandIndex++;
textInput.setText("");
} else if (commandIndex >= 0 && commandIndex <= commands.size() - 1) {
textInput.setText(commands.get(++commandIndex));
}
}
showNextCommand();
}
}
@ -100,4 +93,33 @@ public class Console implements ActionListener, KeyListener {
@Override
public void keyTyped(KeyEvent e) {
}
/**
* Shows the previously executed command in the input field
*
* <p>Shows the previously executed command if a command was just executed.
* Shows the command executed earlier if already showing a previously executed command.</p>
*/
private void showPreviousCommand() {
if (commands.size() > 0 && commandIndex > 0) {
textInput.setText(commands.get(--commandIndex));
}
}
/**
* Shows the next previously executed command or clears the input field
*
* <p>Shows the next previously executed command if such a command exists.
* Clears the input field if no next used command exists.</p>
*/
private void showNextCommand() {
if (commands.size() > 0) {
if (commandIndex == commands.size() - 1) {
commandIndex++;
textInput.setText("");
} else if (commandIndex >= 0 && commandIndex <= commands.size() - 1) {
textInput.setText(commands.get(++commandIndex));
}
}
}
}

View File

@ -1,5 +1,6 @@
package net.knarcraft.minecraftserverlauncher.userinterface;
import net.knarcraft.minecraftserverlauncher.Shared;
import net.knarcraft.minecraftserverlauncher.profile.Collection;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.Server;
@ -10,8 +11,6 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;
@ -60,7 +59,7 @@ public class GUI implements ActionListener {
private TrayIcon trayIcon;
/**
* Create the application window.
* Creates the application window
*/
public GUI() throws IOException {
initialize(440, 170);
@ -69,10 +68,9 @@ public class GUI implements ActionListener {
}
/**
* Creates the application window with a preferred width and height.
*
* @param width The preferred width
* @param height The preferred height
* Creates the application window with a preferred width and height
* @param width <p>The preferred width</p>
* @param height <p>The preferred height</p>
*/
public GUI(int width, int height) throws IOException {
initialize(width, height);
@ -96,7 +94,7 @@ public class GUI implements ActionListener {
*/
public void setStatus(String text) {
this.lblStatuslabel.setText(text);
try (PrintWriter file = new PrintWriter(new FileWriter(Main.getAppDir() + File.separator + "latestrun.log", true))) {
try (PrintWriter file = new PrintWriter(new FileWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log", true))) {
file.println(text);
} catch (IOException e ) {
e.printStackTrace();
@ -152,7 +150,7 @@ public class GUI implements ActionListener {
}
chckbxmntmRunInBackground.setState(Profile.getCurrent().getRunInBackground());
chckbxmntmDelayStartup.setState(Profile.getCurrent().getDelayStartup() > 0);
chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadJars());
chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadAllAvailableJARFiles());
this.targetServer.removeAllItems();
this.targetServer.addItem("All");
for (Collection collection : Profile.getCurrent().getCollections()) {
@ -547,46 +545,21 @@ public class GUI implements ActionListener {
} else if (e.getSource() == chckbxmntmDownloadJars) {
downloadJars();
} else if (e.getSource() == mntmErrors) {
goToURL("https://archive.knarcraft.net/BungeeMinecraftServerLauncherInfo/");
Shared.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherInfo/");
} else if (e.getSource() == mntmSetup) {
JOptionPane.showMessageDialog(
null,
setupText,
"Setup",
JOptionPane.INFORMATION_MESSAGE
);
Profile.showMessage("Setup", setupText);
} else if (e.getSource() == mntmManualUpdate) {
goToURL("https://git.knarcraft.net/EpicKnarvik97/Minecraft-Server-Launcher");
Shared.goToURL("https://git.knarcraft.net/KnarCraft/Minecraft-Server-Launcher/releases");
} else if (e.getSource() == mntmRunInBackground) {
JOptionPane.showMessageDialog(
null,
runInBackgroundText,
"Run in background",
JOptionPane.INFORMATION_MESSAGE
);
Profile.showMessage("Run in background", runInBackgroundText);
} else if (e.getSource() == mntmDelayStartup) {
JOptionPane.showMessageDialog(
null,
delayStartupText,
"Delay startup",
JOptionPane.INFORMATION_MESSAGE
);
Profile.showMessage("Delay startup", delayStartupText);
} else if (e.getSource() == mntmDownloadJars) {
JOptionPane.showMessageDialog(
null,
downloadJarsText,
"Download jars",
JOptionPane.INFORMATION_MESSAGE
);
Profile.showMessage("Download jars", downloadJarsText);
} else if (e.getSource() == mntmAbout) {
JOptionPane.showMessageDialog(
null,
aboutText,
"About",
JOptionPane.INFORMATION_MESSAGE
);
Profile.showMessage("About", aboutText);
} else if (e.getSource() == mntmStory) {
goToURL("https://archive.knarcraft.net/BungeeminecraftserverlauncherStory/");
Shared.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherStory/");
} else if (e.getSource() == btnStartServer) {
try {
Profile.getCurrent().save();
@ -648,7 +621,7 @@ public class GUI implements ActionListener {
Profile.getCurrent().sendCommand(selectedServerValue, "reload");
}
} else if (e.getSource() == btnServerConsoles) {
ServerConsoles.show();
ServerConsoles.setAsVisible();
} else if (e.getSource() == targetServer) {
updatePlayers();
}
@ -691,12 +664,7 @@ public class GUI implements ActionListener {
setStatus("Servers are stopping...");
Server.stop();
} catch (IOException e1) {
JOptionPane.showMessageDialog(
null,
"Could not stop server.",
"Error",
JOptionPane.ERROR_MESSAGE
);
Profile.showError("Could not stop server.");
e1.printStackTrace();
}
}
@ -716,12 +684,7 @@ public class GUI implements ActionListener {
Objects.requireNonNull(profile).setDelayStartup(0);
}
} else {
JOptionPane.showMessageDialog(
null,
"No profile selected",
"Error",
JOptionPane.ERROR_MESSAGE
);
Profile.showError("No profile selected");
}
}
@ -734,12 +697,7 @@ public class GUI implements ActionListener {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected());
} else {
JOptionPane.showMessageDialog(
null,
"No profile selected",
"Error",
JOptionPane.ERROR_MESSAGE
);
Profile.showError("No profile selected");
}
}
@ -750,14 +708,9 @@ public class GUI implements ActionListener {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setDownloadJars(chckbxmntmDownloadJars.isSelected());
Objects.requireNonNull(profile).setDownloadAllAvailableJARFiles(chckbxmntmDownloadJars.isSelected());
} else {
JOptionPane.showMessageDialog(
null,
"No profile selected",
"Error",
JOptionPane.ERROR_MESSAGE
);
Profile.showError("No profile selected");
}
}
@ -770,7 +723,6 @@ public class GUI implements ActionListener {
chooser.setDialogTitle("Backup folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File path = chooser.getSelectedFile();
for (Collection collection : Profile.getCurrent().getCollections()) {
@ -781,7 +733,7 @@ public class GUI implements ActionListener {
if (!destFolder.exists()) {
if (destFolder.mkdirs()) {
try {
copyFolder(srcFolder, destFolder);
Shared.copyFolder(this, srcFolder, destFolder);
} catch (IOException e) {
e.printStackTrace();
return;
@ -817,20 +769,6 @@ public class GUI implements ActionListener {
}
}
/**
* Opens an url in the user's default application.
*
* @param url URL to open
*/
private void goToURL(String url) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (URISyntaxException | IOException e1) {
e1.printStackTrace();
}
}
/**
* Loads popup messages from a text file.
*/
@ -858,42 +796,4 @@ public class GUI implements ActionListener {
}
}
}
/**
* Recursivly copies a folder to another location
*
* @param src The folder to copy
* @param dest Target destination
* @throws IOException If we can't start a file stream
*/
private void copyFolder(File src, File dest) throws IOException{
if (!src.isDirectory()) {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
this.setStatus("Copied file " + src);
} else {
if(!dest.exists()){
if (dest.mkdir()) {
this.setStatus("Copied directory " + src);
} else {
return;
}
}
String[] files = src.list();
if (files != null) {
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
copyFolder(srcFile, destFile);
}
}
}
}
}

View File

@ -15,25 +15,40 @@ import java.awt.BorderLayout;
*/
public class ServerConsoles {
private static JFrame frame;
private static JTabbedPane consolesTab;
private static JTabbedPane consolesTabbedPane;
/**
* Initializes the server consoles frame
*/
public ServerConsoles() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
consolesTab = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(consolesTab, BorderLayout.CENTER);
consolesTabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(consolesTabbedPane, BorderLayout.CENTER);
}
public static Console addTab(String name) {
return new Console(consolesTab, name);
/**
* Adds a new console tab
* @param name <p>The name of the consoles tab</p>
* @return <p>A new console element with the new tabbed pane</p>
*/
public static Console addConsoleTab(String name) {
return new Console(consolesTabbedPane, name);
}
public static void show() {
/**
* Sets the server consoles frame as visible
*/
public static void setAsVisible() {
frame.setVisible(true);
}
public static JTabbedPane getTab() {
return consolesTab;
/**
* Returns the tabbed pane containing the server consoles
* @return <p>A tabbed pane</p>
*/
public static JTabbedPane getTabbedPane() {
return consolesTabbedPane;
}
}