|
15 | 15 | */
|
16 | 16 | package com.jagrosh.jmusicbot.gui;
|
17 | 17 |
|
18 |
| -import java.awt.event.WindowEvent; |
19 |
| -import java.awt.event.WindowListener; |
20 |
| -import javax.swing.JFrame; |
21 |
| -import javax.swing.JTabbedPane; |
22 |
| -import javax.swing.WindowConstants; |
| 18 | +import java.awt.*; |
| 19 | +import java.awt.event.*; |
| 20 | +import javax.swing.*; |
| 21 | + |
23 | 22 | import com.jagrosh.jmusicbot.Bot;
|
24 | 23 |
|
25 | 24 |
|
26 | 25 | /**
|
27 | 26 | *
|
28 | 27 | * @author John Grosh <[email protected]>
|
29 | 28 | */
|
30 |
| -public class GUI extends JFrame |
| 29 | +public class GUI extends JFrame |
31 | 30 | {
|
32 | 31 | private final ConsolePanel console;
|
33 | 32 | private final Bot bot;
|
34 |
| - |
35 |
| - public GUI(Bot bot) |
| 33 | + private final OptionsPanel optionsPanel; |
| 34 | + |
| 35 | + public GUI(Bot bot) |
36 | 36 | {
|
37 | 37 | super();
|
38 | 38 | this.bot = bot;
|
39 | 39 | console = new ConsolePanel();
|
| 40 | + optionsPanel = new OptionsPanel(); |
40 | 41 | }
|
41 |
| - |
| 42 | + |
42 | 43 | public void init()
|
43 | 44 | {
|
44 | 45 | setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
45 | 46 | setTitle("JMusicBot");
|
46 | 47 | JTabbedPane tabs = new JTabbedPane();
|
47 | 48 | tabs.add("Console", console);
|
| 49 | + tabs.add("Options", optionsPanel); |
48 | 50 | getContentPane().add(tabs);
|
49 | 51 | pack();
|
50 | 52 | setLocationRelativeTo(null);
|
51 | 53 | setVisible(true);
|
52 |
| - addWindowListener(new WindowListener() |
| 54 | + addWindowListener(new WindowListener() |
53 | 55 | {
|
54 | 56 | @Override public void windowOpened(WindowEvent e) { /* unused */ }
|
55 |
| - @Override public void windowClosing(WindowEvent e) |
| 57 | + @Override public void windowClosing(WindowEvent e) |
56 | 58 | {
|
57 |
| - try |
58 |
| - { |
59 |
| - bot.shutdown(); |
60 |
| - } |
61 |
| - catch(Exception ex) |
62 |
| - { |
63 |
| - System.exit(0); |
64 |
| - } |
| 59 | + shutdown(); |
65 | 60 | }
|
66 | 61 | @Override public void windowClosed(WindowEvent e) { /* unused */ }
|
67 | 62 | @Override public void windowIconified(WindowEvent e) { /* unused */ }
|
68 | 63 | @Override public void windowDeiconified(WindowEvent e) { /* unused */ }
|
69 | 64 | @Override public void windowActivated(WindowEvent e) { /* unused */ }
|
70 | 65 | @Override public void windowDeactivated(WindowEvent e) { /* unused */ }
|
71 | 66 | });
|
| 67 | + |
| 68 | + if (SystemTray.isSupported()) |
| 69 | + { |
| 70 | + setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
| 71 | + setupMinimizeToTray(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void setupMinimizeToTray() |
| 76 | + { |
| 77 | + Image icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icon16.png")); |
| 78 | + MenuItem exit = new MenuItem("Exit"); |
| 79 | + exit.addActionListener(e -> shutdown()); |
| 80 | + |
| 81 | + MenuItem nameLabel = new MenuItem("JMusicBot"); |
| 82 | + nameLabel.setEnabled(false); |
| 83 | + |
| 84 | + PopupMenu menu = new PopupMenu(); |
| 85 | + menu.add(nameLabel); |
| 86 | + menu.addSeparator(); |
| 87 | + menu.add(exit); |
| 88 | + |
| 89 | + SystemTray tray = SystemTray.getSystemTray(); |
| 90 | + TrayIcon trayIcon = new TrayIcon(icon, "JMusicBot", menu); |
| 91 | + |
| 92 | + // Restore the window when the user clicks the tray icon |
| 93 | + trayIcon.addMouseListener(new MouseAdapter() |
| 94 | + { |
| 95 | + @Override public void mouseClicked(MouseEvent e) |
| 96 | + { |
| 97 | + if (e.getButton() != MouseEvent.BUTTON1) |
| 98 | + { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + setVisible(true); |
| 103 | + setExtendedState(JFrame.NORMAL); |
| 104 | + tray.remove(trayIcon); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + // Minimize the window to the system tray when the user clicks the minimize button |
| 109 | + // and the option "minimize to tray" is active |
| 110 | + addWindowStateListener(e -> { |
| 111 | + if (e.getNewState() == JFrame.ICONIFIED && optionsPanel.isMinimizeToTraySelected()) |
| 112 | + { |
| 113 | + try |
| 114 | + { |
| 115 | + setVisible(false); |
| 116 | + tray.add(trayIcon); |
| 117 | + } |
| 118 | + catch (AWTException ex) |
| 119 | + { |
| 120 | + System.err.println("TrayIcon could not be added."); |
| 121 | + } |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + private void shutdown() |
| 127 | + { |
| 128 | + try |
| 129 | + { |
| 130 | + bot.shutdown(); |
| 131 | + } |
| 132 | + catch(Exception ex) |
| 133 | + { |
| 134 | + System.exit(0); |
| 135 | + } |
72 | 136 | }
|
73 | 137 | }
|
0 commit comments