Implements run in background on exit

The GUI will be minimized to tray on exit, if runInBackground is enabled.
Starting with runInBackground enabled, hides the GUI and runs the servers.
This commit is contained in:
2018-02-04 10:20:58 +01:00
parent e9e500fb2f
commit 9b14603d85
4 changed files with 135 additions and 24 deletions

View File

@ -7,10 +7,7 @@ import net.knarcraft.serverlauncher.profile.Profile;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
@ -19,6 +16,9 @@ import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.Executors;
import static java.awt.Frame.NORMAL;
import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;
/**
* Generates a GUI.
*
@ -54,6 +54,10 @@ public class GUI implements ActionListener {
private JTabbedPane serversPane;
private SystemTray tray;
private TrayIcon trayIcon;
/**
* Create the application window.
*/
@ -100,6 +104,9 @@ public class GUI implements ActionListener {
for (Collection collection : Profile.getCurrent().getCollections()) {
serversPane.addTab(collection.getName(), collection.getServerTab().getPanel());
}
chckbxmntmRunInBackground.setState(Profile.getCurrent().getRunInBackground());
chckbxmntmDelayStartup.setState(Profile.getCurrent().getDelayStartup() > 0);
chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadJars());
}
public void setStatus(String text) {
@ -126,7 +133,7 @@ public class GUI implements ActionListener {
}
frame = new JFrame("Minecraft server launcher");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
frame.setResizable(false);
ImageIcon img;
try {
@ -136,13 +143,6 @@ public class GUI implements ActionListener {
}
frame.setIconImage(img.getImage());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
save();
stop();
}
});
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
@ -389,6 +389,85 @@ public class GUI implements ActionListener {
frame.validate();
frame.pack();
frame.setVisible(true);
tray();
}
public void hide() {
frame.setVisible(false);
try {
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
private void tray() {
if (SystemTray.isSupported()) {
tray = SystemTray.getSystemTray();
Image trayImage = Toolkit.getDefaultToolkit().getImage("files/GUIIcon.png");
PopupMenu popup = new PopupMenu();
trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup);
trayIcon.setImageAutoSize(true);
ActionListener exitListener= e -> {
save();
System.exit(0);
};
MenuItem restoreItem = new MenuItem("Restore");
popup.add(restoreItem);
restoreItem.addActionListener(e -> {
frame.setExtendedState(NORMAL);
tray.remove(trayIcon);
frame.setVisible(true);
});
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(exitListener);
popup.add(exitItem);
frame.addWindowStateListener(e -> {
if (e.getNewState() == NORMAL) {
tray.remove(trayIcon);
frame.setVisible(true);
}
});
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (Profile.getCurrent().getRunInBackground() && SystemTray.isSupported()) {
try {
tray.add(trayIcon);
frame.setVisible(false);
} catch (AWTException e1) {
e1.printStackTrace();
}
} else {
save();
stop();
System.exit(0);
}
}
});
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(e.getClickCount() >= 1 && e.getButton() == MouseEvent.BUTTON1){
frame.setExtendedState(NORMAL);
tray.remove(trayIcon);
frame.setVisible(true);
}
}
});
} else {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
save();
stop();
System.exit(0);
}
});
}
}
public void updateSelectServers() {
@ -545,11 +624,6 @@ public class GUI implements ActionListener {
} else {
JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE);
}
if (chckbxmntmRunInBackground.isSelected()) {
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
} else {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
/**