forked from siblis/pocket-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTray.java
More file actions
130 lines (110 loc) · 4.79 KB
/
Tray.java
File metadata and controls
130 lines (110 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package client.view;
import client.controller.ClientController;
import javafx.application.Platform;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import static client.Main.primaryStage;
public class Tray {
private static final Logger trayLogger = LogManager.getLogger(Tray.class.getName());
private static SystemTray tray;
public static Stage currentStage;
public static void trayON(Stage stage){
System.out.println("сворачиваем в трей");
stage.hide();
}
public static void trayOFF(Stage stage){
if (stage != null) {
stage.show();
stage.toFront();
}
System.out.println("выходим из трея");
}
public void setTrayIcon() {
final TrayIcon trayIcon;
Platform.setImplicitExit(false);
if(!SystemTray.isSupported()){
System.out.println("No system tray support");
}
else{
tray = SystemTray.getSystemTray();
trayIcon = new TrayIcon(new ImageIcon(Tray.class.getResource("/client/images/icon.png")).getImage().getScaledInstance(16, -1, 4));
trayIcon.setToolTip("Меню Pocket");
ActionListener exitListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ClientController clientController = ClientController.getInstance();
clientController.dbServiceClose();
clientController.disconnect();
System.out.println("Выход из приложения");
System.exit(0);
}
};
final JPopupMenu popup = new JPopupMenu();
JMenuItem exit = new JMenuItem("Выход");
JMenuItem open = new JMenuItem("Развернуть");
JMenuItem close = new JMenuItem("Свернуть в трей");
JMenuItem quietMode = new JMenuItem("Тихий режим");
trayIcon.addActionListener(event-> Platform.runLater(new Runnable(){@Override public void run() {trayOFF(currentStage == null?primaryStage:currentStage);}}));
open.addActionListener(event->Platform.runLater(new Runnable(){@Override public void run() {trayOFF(currentStage == null?primaryStage:currentStage);}}));
close.addActionListener(event->Platform.runLater(new Runnable(){@Override public void run() {trayON(currentStage == null?primaryStage:currentStage);}}));
quietMode.addActionListener(event->Platform.runLater(new Runnable(){@Override public void run() {System.out.println("Пока не сделанно");}}));
exit.addActionListener(exitListener);
popup.add(open);
popup.addSeparator();
popup.add(quietMode);
popup.addSeparator();
popup.add(close);
popup.addSeparator();
popup.add(exit);
popup.setFocusable(true);
popup.requestFocus();
popup.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(6000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
popup.setVisible(false);
}
}).start();
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
});
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
if (e.isPopupTrigger()){
popup.setLocation(e.getX(), e.getY());
popup.setInvoker(popup);
popup.setVisible(true);
}
}
});
try {
tray.add(trayIcon);
} catch (AWTException e) {
trayLogger.error("trayIcon_error", e);
}
}
}
}