This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/ui/Startup.java

98 lines
2.9 KiB
Java

package envoy.client.ui;
import java.awt.EventQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import envoy.client.Client;
import envoy.client.Config;
import envoy.client.LocalDB;
import envoy.client.Settings;
import envoy.exception.EnvoyException;
/**
* Starts the Envoy client and prompts the user to enter their name.
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>Startup.java</strong><br>
* Created: <strong>12 Oct 2019</strong><br>
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S. K. Engelbart
* @since Envoy v0.1-alpha
*/
public class Startup {
private static final Logger logger = Logger.getLogger(Startup.class.getSimpleName());
public static void main(String[] args) {
logger.setLevel(Level.ALL);
Config config = Config.getInstance();
try {
// Load the configuration from client.properties first
config.load();
// Override configuration values with command line arguments
if (args.length > 0) config.load(args);
// Check if all configuration values have been initialized
if (!config.isInitialized()) throw new EnvoyException("Server or port are not defined");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error loading configuration values: \n" + e.toString(), "Configuration error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
e.printStackTrace();
}
// Ask the user for their user name
String userName = JOptionPane.showInputDialog("Please enter your username");
if (userName == null || userName.isEmpty()) {
logger.severe("User name is not set or empty. Exiting...");
System.exit(1);
}
// Acquire the client user (with ID) either from the server or from the local
// cache (Preferences), which triggers offline mode
Client client;
try {
client = new Client(config, userName);
} catch (Exception e1) {
logger.log(Level.SEVERE, "Failed to acquire client user ID", e1);
JOptionPane.showMessageDialog(null, e1.toString(), "Client error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
return;
}
// Load the local database
LocalDB localDB = new LocalDB(client.getSender());
try {
localDB.initializeDBFile(config.getLocalDB());
} catch (EnvoyException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,
"Error while loading local database: " + e.toString() + "\nChats will not be stored locally.",
"Local DB error",
JOptionPane.WARNING_MESSAGE);
}
Settings.getInstance().setUsername(userName);
EventQueue.invokeLater(() -> {
try {
ChatWindow chatWindow = new ChatWindow(client, localDB);
chatWindow.setVisible(true);
try {
new StatusTrayIcon(chatWindow).show();
} catch (EnvoyException e) {
logger.warning("The StatusTrayIcon is not supported on this platform!");
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
}