forked from Qortal/qortal
Splash startup & sys tray
This commit is contained in:
parent
e5b3166df4
commit
57be191814
@ -24,6 +24,7 @@ import org.qora.data.block.BlockData;
|
||||
import org.qora.data.network.BlockSummaryData;
|
||||
import org.qora.data.network.PeerData;
|
||||
import org.qora.data.transaction.TransactionData;
|
||||
import org.qora.gui.GUI;
|
||||
import org.qora.network.Network;
|
||||
import org.qora.network.Peer;
|
||||
import org.qora.network.message.BlockMessage;
|
||||
@ -142,6 +143,9 @@ public class Controller extends Thread {
|
||||
public static void main(String args[]) {
|
||||
LOGGER.info("Starting up...");
|
||||
|
||||
// Potential GUI startup with splash screen, etc.
|
||||
GUI.getInstance();
|
||||
|
||||
Security.insertProviderAt(new BouncyCastleProvider(), 0);
|
||||
Security.insertProviderAt(new BouncyCastleJsseProvider(), 1);
|
||||
|
||||
@ -202,6 +206,9 @@ public class Controller extends Thread {
|
||||
// Auto-update service
|
||||
LOGGER.info("Starting auto-update");
|
||||
AutoUpdate.getInstance().start();
|
||||
|
||||
// If GUI is enabled, we're no longer starting up but actually running now
|
||||
GUI.getInstance().notifyRunning();
|
||||
}
|
||||
|
||||
// Main thread
|
||||
|
71
src/main/java/org/qora/gui/GUI.java
Normal file
71
src/main/java/org/qora/gui/GUI.java
Normal file
@ -0,0 +1,71 @@
|
||||
package org.qora.gui;
|
||||
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class GUI {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(GUI.class);
|
||||
private static GUI instance;
|
||||
|
||||
private boolean isHeadless;
|
||||
private SplashFrame splash = null;
|
||||
private SysTray sysTray = null;
|
||||
|
||||
private GUI() {
|
||||
this.isHeadless = GraphicsEnvironment.isHeadless();
|
||||
|
||||
if (!this.isHeadless)
|
||||
showSplash();
|
||||
}
|
||||
|
||||
private void showSplash() {
|
||||
LOGGER.trace("Splash");
|
||||
this.splash = SplashFrame.getInstance();
|
||||
}
|
||||
|
||||
protected static BufferedImage loadImage(String resourceName) {
|
||||
try (InputStream in = ClassLoader.getSystemResourceAsStream("images/" + resourceName)) {
|
||||
return ImageIO.read(in);
|
||||
} catch (IOException e) {
|
||||
LOGGER.warn(String.format("Couldn't locate image resource \"images/%s\"", resourceName));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static GUI getInstance() {
|
||||
if (instance == null)
|
||||
instance = new GUI();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void notifyRunning() {
|
||||
if (this.isHeadless)
|
||||
return;
|
||||
|
||||
this.splash.dispose();
|
||||
this.splash = null;
|
||||
|
||||
this.sysTray = SysTray.getInstance();
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
if (this.isHeadless)
|
||||
return;
|
||||
|
||||
if (this.splash != null)
|
||||
this.splash.dispose();
|
||||
|
||||
if (this.sysTray != null)
|
||||
this.sysTray.dispose();
|
||||
}
|
||||
|
||||
}
|
79
src/main/java/org/qora/gui/SplashFrame.java
Normal file
79
src/main/java/org/qora/gui/SplashFrame.java
Normal file
@ -0,0 +1,79 @@
|
||||
package org.qora.gui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Image;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class SplashFrame {
|
||||
|
||||
protected static final Logger LOGGER = LogManager.getLogger(SplashFrame.class);
|
||||
|
||||
private static SplashFrame instance;
|
||||
private JDialog splashDialog;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class SplashPanel extends JPanel {
|
||||
private BufferedImage image;
|
||||
|
||||
public SplashPanel() {
|
||||
image = GUI.loadImage("splash.png");
|
||||
this.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
|
||||
this.setLayout(new BorderLayout());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.drawImage(image, 0, 0, null);
|
||||
}
|
||||
}
|
||||
|
||||
private SplashFrame() {
|
||||
this.splashDialog = new JDialog();
|
||||
|
||||
List<Image> icons = new ArrayList<Image>();
|
||||
icons.add(GUI.loadImage("icons/icon16.png"));
|
||||
icons.add(GUI.loadImage("icons/icon32.png"));
|
||||
icons.add(GUI.loadImage("icons/icon64.png"));
|
||||
icons.add(GUI.loadImage("icons/icon128.png"));
|
||||
this.splashDialog.setIconImages(icons);
|
||||
|
||||
this.splashDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
|
||||
this.splashDialog.setTitle("qora-core");
|
||||
this.splashDialog.setContentPane(new SplashPanel());
|
||||
|
||||
this.splashDialog.setUndecorated(true);
|
||||
this.splashDialog.setModal(false);
|
||||
this.splashDialog.pack();
|
||||
this.splashDialog.setLocationRelativeTo(null);
|
||||
this.splashDialog.toFront();
|
||||
this.splashDialog.setVisible(true);
|
||||
this.splashDialog.repaint();
|
||||
}
|
||||
|
||||
public static SplashFrame getInstance() {
|
||||
if (instance == null)
|
||||
instance = new SplashFrame();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void setVisible(boolean b) {
|
||||
this.splashDialog.setVisible(b);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
this.splashDialog.dispose();
|
||||
}
|
||||
|
||||
}
|
120
src/main/java/org/qora/gui/SysTray.java
Normal file
120
src/main/java/org/qora/gui/SysTray.java
Normal file
@ -0,0 +1,120 @@
|
||||
package org.qora.gui;
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.MenuItem;
|
||||
import java.awt.PopupMenu;
|
||||
import java.awt.SystemTray;
|
||||
import java.awt.TrayIcon;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingWorker;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.qora.controller.Controller;
|
||||
|
||||
public class SysTray {
|
||||
|
||||
protected static final Logger LOGGER = LogManager.getLogger(SplashFrame.class);
|
||||
|
||||
private static SysTray instance;
|
||||
private TrayIcon trayIcon = null;
|
||||
private PopupMenu popupMenu = null;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class SplashPanel extends JPanel {
|
||||
private BufferedImage image;
|
||||
|
||||
public SplashPanel() {
|
||||
try (InputStream in = ClassLoader.getSystemResourceAsStream("images/splash.png")) {
|
||||
image = ImageIO.read(in);
|
||||
this.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
|
||||
this.setLayout(new BorderLayout());
|
||||
} catch (IOException ex) {
|
||||
LOGGER.error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.drawImage(image, 0, 0, null);
|
||||
}
|
||||
}
|
||||
|
||||
private SysTray() {
|
||||
if (!SystemTray.isSupported())
|
||||
return;
|
||||
|
||||
this.popupMenu = createPopupMenu();
|
||||
|
||||
this.trayIcon = new TrayIcon(GUI.loadImage("icons/icon32.png"), "qora-core", popupMenu);
|
||||
|
||||
this.trayIcon.setImageAutoSize(true);
|
||||
|
||||
try {
|
||||
SystemTray.getSystemTray().add(this.trayIcon);
|
||||
} catch (AWTException e) {
|
||||
this.trayIcon = null;
|
||||
}
|
||||
}
|
||||
|
||||
class ClosingWorker extends SwingWorker<Void, Void> {
|
||||
@Override
|
||||
protected Void doInBackground() {
|
||||
Controller.getInstance().shutdown();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void done() {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
private PopupMenu createPopupMenu() {
|
||||
PopupMenu menu = new PopupMenu();
|
||||
|
||||
MenuItem exit = new MenuItem("Exit");
|
||||
exit.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
new ClosingWorker().execute();
|
||||
}
|
||||
});
|
||||
menu.add(exit);
|
||||
|
||||
return menu;
|
||||
}
|
||||
|
||||
public static SysTray getInstance() {
|
||||
if (instance == null)
|
||||
instance = new SysTray();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void showMessage(String caption, String text, TrayIcon.MessageType messagetype) {
|
||||
if (trayIcon != null)
|
||||
trayIcon.displayMessage(caption, text, messagetype);
|
||||
}
|
||||
|
||||
public void setToolTipText(String text) {
|
||||
if (trayIcon != null)
|
||||
this.trayIcon.setToolTip(text);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
if (trayIcon != null)
|
||||
SystemTray.getSystemTray().remove(this.trayIcon);
|
||||
}
|
||||
|
||||
}
|
BIN
src/main/resources/images/icons/icon128.png
Normal file
BIN
src/main/resources/images/icons/icon128.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
BIN
src/main/resources/images/icons/icon16.png
Normal file
BIN
src/main/resources/images/icons/icon16.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 673 B |
BIN
src/main/resources/images/icons/icon32.png
Normal file
BIN
src/main/resources/images/icons/icon32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
src/main/resources/images/icons/icon64.png
Normal file
BIN
src/main/resources/images/icons/icon64.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
BIN
src/main/resources/images/splash.png
Executable file
BIN
src/main/resources/images/splash.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
Loading…
Reference in New Issue
Block a user