Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 27ae5d2

Browse files
1 parent ccef24a commit 27ae5d2

File tree

3 files changed

+141
-19
lines changed

3 files changed

+141
-19
lines changed

src/main/java/com/jagrosh/jmusicbot/gui/GUI.java

+83-19
Original file line numberDiff line numberDiff line change
@@ -15,59 +15,123 @@
1515
*/
1616
package com.jagrosh.jmusicbot.gui;
1717

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+
2322
import com.jagrosh.jmusicbot.Bot;
2423

2524

2625
/**
2726
*
2827
* @author John Grosh <[email protected]>
2928
*/
30-
public class GUI extends JFrame
29+
public class GUI extends JFrame
3130
{
3231
private final ConsolePanel console;
3332
private final Bot bot;
34-
35-
public GUI(Bot bot)
33+
private final OptionsPanel optionsPanel;
34+
35+
public GUI(Bot bot)
3636
{
3737
super();
3838
this.bot = bot;
3939
console = new ConsolePanel();
40+
optionsPanel = new OptionsPanel();
4041
}
41-
42+
4243
public void init()
4344
{
4445
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
4546
setTitle("JMusicBot");
4647
JTabbedPane tabs = new JTabbedPane();
4748
tabs.add("Console", console);
49+
tabs.add("Options", optionsPanel);
4850
getContentPane().add(tabs);
4951
pack();
5052
setLocationRelativeTo(null);
5153
setVisible(true);
52-
addWindowListener(new WindowListener()
54+
addWindowListener(new WindowListener()
5355
{
5456
@Override public void windowOpened(WindowEvent e) { /* unused */ }
55-
@Override public void windowClosing(WindowEvent e)
57+
@Override public void windowClosing(WindowEvent e)
5658
{
57-
try
58-
{
59-
bot.shutdown();
60-
}
61-
catch(Exception ex)
62-
{
63-
System.exit(0);
64-
}
59+
shutdown();
6560
}
6661
@Override public void windowClosed(WindowEvent e) { /* unused */ }
6762
@Override public void windowIconified(WindowEvent e) { /* unused */ }
6863
@Override public void windowDeiconified(WindowEvent e) { /* unused */ }
6964
@Override public void windowActivated(WindowEvent e) { /* unused */ }
7065
@Override public void windowDeactivated(WindowEvent e) { /* unused */ }
7166
});
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+
}
72136
}
73137
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2016 John Grosh <[email protected]>.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.jagrosh.jmusicbot.gui;
17+
18+
import javax.swing.*;
19+
import java.util.prefs.Preferences;
20+
21+
/**
22+
* @author Wolfgang Schwendtbauer
23+
*/
24+
public class OptionsPanel extends JPanel
25+
{
26+
private static final String KEY_MINIMIZE_TO_SYSTEM_TRAY = "minimizeToSystemTray";
27+
28+
private final Preferences prefs;
29+
private final JCheckBox minimizeToTrayCheckbox;
30+
31+
public OptionsPanel()
32+
{
33+
prefs = Preferences.userNodeForPackage(getClass());
34+
35+
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
36+
37+
minimizeToTrayCheckbox = new JCheckBox("Minimize to system tray");
38+
add(minimizeToTrayCheckbox);
39+
40+
loadPreferences();
41+
addListeners();
42+
}
43+
44+
private void loadPreferences()
45+
{
46+
minimizeToTrayCheckbox.setSelected(prefs.getBoolean(KEY_MINIMIZE_TO_SYSTEM_TRAY, false));
47+
}
48+
49+
private void addListeners()
50+
{
51+
minimizeToTrayCheckbox.addActionListener(e -> prefs.putBoolean(KEY_MINIMIZE_TO_SYSTEM_TRAY, minimizeToTrayCheckbox.isSelected()));
52+
}
53+
54+
public boolean isMinimizeToTraySelected()
55+
{
56+
return minimizeToTrayCheckbox.isSelected();
57+
}
58+
}

src/main/resources/icon16.png

877 Bytes
Loading

0 commit comments

Comments
 (0)