Fixes an error caused by the BuildTools directory not being created Creates functions for writing to files in CommonFunctions Adds some more error info to the log when BuildTools fails to download Fixes some typos Makes sure all visible text is logged
103 lines
2.8 KiB
Java
103 lines
2.8 KiB
Java
package net.knarcraft.minecraftserverlauncher.userinterface;
|
|
|
|
import net.knarcraft.minecraftserverlauncher.Main;
|
|
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
|
|
|
import javax.swing.*;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.OutputStreamWriter;
|
|
|
|
/**
|
|
* This class handles displaying messages to the user
|
|
*/
|
|
public abstract class MessageHandler implements GUI {
|
|
|
|
private final boolean silent;
|
|
private final BufferedWriter writer;
|
|
private final String logFile = Main.getApplicationWorkDirectory() + File.separator + "latestrun.log";
|
|
|
|
/***
|
|
* Initializes a new message handler
|
|
*
|
|
* @param silent <p>Whether to print to cli instead of showing a GUI element</p>
|
|
*/
|
|
public MessageHandler(boolean silent) {
|
|
this.silent = silent;
|
|
this.writer = new BufferedWriter(new OutputStreamWriter(System.out));
|
|
}
|
|
|
|
@Override
|
|
public void logMessage(String message) {
|
|
try {
|
|
CommonFunctions.appendFile(logFile, "[Info]: " + message);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void logError(String message) {
|
|
try {
|
|
CommonFunctions.appendFile(logFile, "[Error]: " + message);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void showError(String title, String message) {
|
|
if (silent) {
|
|
try {
|
|
writer.write("[Error]: ");
|
|
writer.write(message);
|
|
writer.newLine();
|
|
writer.flush();
|
|
} catch (IOException e) {
|
|
System.out.println(message);
|
|
}
|
|
} else {
|
|
showJOptionPane(title, message, JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
logError(message);
|
|
}
|
|
|
|
@Override
|
|
public void showError(String message) {
|
|
showError("Error", message);
|
|
}
|
|
|
|
@Override
|
|
public void showMessage(String title, String message) {
|
|
if (silent) {
|
|
try {
|
|
writer.write("[Info]: " + message);
|
|
writer.newLine();
|
|
writer.flush();
|
|
} catch (IOException e) {
|
|
System.out.println(message);
|
|
}
|
|
} else {
|
|
showJOptionPane(title, message, JOptionPane.INFORMATION_MESSAGE);
|
|
}
|
|
logMessage(message);
|
|
}
|
|
|
|
@Override
|
|
public void showMessage(String message) {
|
|
showMessage("Info", message);
|
|
}
|
|
|
|
/**
|
|
* Shows a JOptionPane
|
|
*
|
|
* @param title <p>The title of the pane</p>
|
|
* @param message <p>The message of the pane</p>
|
|
* @param paneType <p>The type of the pane</p>
|
|
*/
|
|
private void showJOptionPane(String title, String message, int paneType) {
|
|
JOptionPane.showMessageDialog(null, message, title, paneType);
|
|
}
|
|
}
|