This repository was archived by the owner on Dec 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathMainGui.java
More file actions
143 lines (121 loc) · 4.56 KB
/
MainGui.java
File metadata and controls
143 lines (121 loc) · 4.56 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
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.stfl;
import com.stfl.misc.UTF8Control;
import com.stfl.ui.MainLayoutController;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Logger;
public class MainGui extends Application {
private static Logger logger = Logger.getLogger(MainGui.class.getName());
private Stage primaryStage;
private Scene rootScene;
private MainLayoutController controller;
private TrayIcon trayIcon;
@Override
public void start(Stage primaryStage) throws Exception {
Platform.setImplicitExit(false);
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Server Configuration");
try {
// Load the root layout from the fxml file
FXMLLoader mainLayoutLoader = new FXMLLoader(MainGui.class.getResource("/resources/ui/MainLayout.fxml"));
mainLayoutLoader.setResources(ResourceBundle.getBundle("resources.bundle.ui", Constant.LOCALE, new UTF8Control()));
Pane rootLayout = mainLayoutLoader.load();
rootScene = new Scene(rootLayout);
primaryStage.setScene(rootScene);
primaryStage.setResizable(false);
controller = mainLayoutLoader.getController();
controller.setMainGui(this);
addToTray();
primaryStage.getIcons().add(new Image(MainGui.class.getResource("/resources/image/icon.png").toString()));
primaryStage.show();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
private void addToTray() {
// ensure awt is initialized
java.awt.Toolkit.getDefaultToolkit();
// make sure system tray is supported
if (!java.awt.SystemTray.isSupported()) {
logger.warning("No system tray support!");
}
final java.awt.SystemTray tray = java.awt.SystemTray.getSystemTray();
try {
java.awt.Image image = ImageIO.read(MainGui.class.getResource("/resources/image/icon.png"));
trayIcon = new TrayIcon(image);
trayIcon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
primaryStage.show();
}
});
}
});
java.awt.MenuItem openItem = new java.awt.MenuItem("Configuration");
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
show();
}
});
}
});
java.awt.MenuItem exitItem = new java.awt.MenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
controller.closeServer();
Platform.exit();
tray.remove(trayIcon);
}
});
PopupMenu popup = new PopupMenu();
popup.add(openItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
trayIcon.setToolTip("Not Connected");
tray.add(trayIcon);
} catch (IOException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
}
}
public void show() {
primaryStage.show();
}
public void hide() {
primaryStage.hide();
}
public void setTooltip(String message) {
if (trayIcon != null) {
trayIcon.setToolTip(message);
}
}
public void showNotification(String message) {
//trayIcon.displayMessage(
// "shadowsocks-java",
// message,
// java.awt.TrayIcon.MessageType.INFO
//);
}
}