/
MainExit.java
215 lines (186 loc) · 8.63 KB
/
MainExit.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.goxr3plus.xr3player.application;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.goxr3plus.xr3player.database.DatabaseTool;
import com.goxr3plus.xr3player.enums.NotificationType;
import com.goxr3plus.xr3player.services.database.VacuumProgressService;
import com.goxr3plus.xr3player.utils.general.OSTool;
import com.goxr3plus.xr3player.utils.io.IOInfo;
import com.goxr3plus.xr3player.utils.javafx.AlertTool;
import com.goxr3plus.xr3player.utils.javafx.JavaFXTool;
import javafx.animation.PauseTransition;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.StageStyle;
import javafx.util.Duration;
/**
* This class contains methods for exiting and restarting the application
*/
public class MainExit {
/**
* Terminate the application.
*
* @param vacuum the vacuum
*/
private static void terminate(final boolean vacuum) {
// I need to check it in case no user is logged in
if (Main.dbManager == null)
terminateXR3Player(0);
else if (Main.libraryMode.openedLibrariesViewer.isFree(true)) {
if (!vacuum)
terminateXR3Player(0);
else {
final VacuumProgressService vService = new VacuumProgressService();
Main.updateScreen.getLabel().textProperty().bind(vService.messageProperty());
Main.updateScreen.getProgressBar().setProgress(-1);
Main.updateScreen.getProgressBar().progressProperty().bind(vService.progressProperty());
Main.updateScreen.setVisible(true);
vService.start(new File(DatabaseTool.getUserFolderAbsolutePathWithSeparator() + "dbFile.db"),
new File(DatabaseTool.getUserFolderAbsolutePathWithSeparator() + "dbFile.db-journal"));
Main.dbManager.commitAndVacuum();
}
}
}
/**
* This method is used to exit the application
*/
public static void confirmApplicationExit() {
final Alert alert = JavaFXTool.createAlert("Exit XR3Player?",
"Vacuum is clearing junks from database\n(In future updates it will be automatical)",
"Pros:\nThe database file may be shrinked \n\nCons:\nIt may take some seconds to be done\n",
Alert.AlertType.CONFIRMATION, StageStyle.UTILITY, Main.window, null);
// Create Custom Buttons
final ButtonType exit = new ButtonType("Exit", ButtonBar.ButtonData.OK_DONE);
final ButtonType vacuum = new ButtonType("Vacuum + Exit", ButtonBar.ButtonData.OK_DONE);
final ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
((Button) alert.getDialogPane().lookupButton(ButtonType.CANCEL)).setDefaultButton(true);
alert.getButtonTypes().setAll(exit, vacuum, cancel);
// Pick the answer
alert.showAndWait().ifPresent(answer -> {
if (answer == exit)
terminate(false);
else if (answer == vacuum)
terminate(true);
});
}
/**
* Use this code to terminate XR3Player
*
* @param exitCode The exit code of System.exit();
*/
public static void terminateXR3Player(final int exitCode) {
System.out.println("Terminating XR3Player OS->" + OSTool.getOS());
switch (OSTool.getOS()) {
case WINDOWS:
new Thread(() -> {
// Disposing all Browsers...
Main.webBrowser.disposeAllBrowsers();
System.exit(exitCode);
}).start();
break;
case LINUX:
case MAC:
Platform.runLater(() -> {
// Disposing all Browsers...
Main.webBrowser.disposeAllBrowsers();
System.exit(exitCode);
});
break;
default:
System.out.println("Can't dispose browser instance!!!");
break;
}
}
/**
* Calling this method restarts the application
*
* @param askUser Ask the User if he/she wants to restart the application
*/
public static void restartTheApplication(final boolean askUser) {
// Restart XR3Player
new Thread(() -> {
final String path = IOInfo.getBasePathForClass(Main.class);
final String[] applicationPath = {new File(path + "XR3Player.exe").getAbsolutePath()};
// Check if the file exists
if (!new File(applicationPath[0]).exists()) {
// Show message that application is restarting
Platform.runLater(() -> AlertTool.showNotification("Application File can't be found",
"XR3Player can't be restarted due to unexpected problem ", Duration.seconds(2),
NotificationType.ERROR));
if (!askUser)
terminate(false);
else
return;
}
try {
System.out.println("XR3PlayerPath is : " + applicationPath[0]);
// ProcessBuilder builder = new ProcessBuilder("java", "-jar",
// applicationPath[0])
// builder.redirectErrorStream(true)
// Process process = builder.start()
final Process process = Runtime.getRuntime().exec("cmd.exe /c \"" + applicationPath[0] + "\"");
final BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
// Show message that application is restarting
Platform.runLater(
() -> AlertTool.showNotification("Restarting Application",
"If restart takes a lot of time exit application and restart it manually.\n[ "
+ applicationPath[0] + " ]",
Duration.seconds(20), NotificationType.INFORMATION));
// startExitPauseTransition
startExitPauseTransition(20, askUser);
// Continuously Read Output
String line;
while (process.isAlive())
while ((line = bufferedReader.readLine()) != null) {
if (line.isEmpty())
break;
if (line.contains("Outside of Application Start Method"))
terminate(false);
}
} catch (final Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.INFO, null, ex);
Platform.runLater(() -> {
Main.updateScreen.setVisible(false);
// Show failed message
Platform.runLater(() -> AlertTool.showNotification("Restart seems to failed",
"Wait some more seconds before trying to restart/exit XR3Player manually",
Duration.seconds(20), NotificationType.ERROR));
// startExitPauseTransition
startExitPauseTransition(0, askUser);
});
}
}, "Restart Application Thread").start();
}
private static void startExitPauseTransition(final int seconds, final boolean askUser) {
// Wait 20 seconds
final PauseTransition pause = new PauseTransition(Duration.seconds(seconds));
pause.setOnFinished(f -> {
Main.updateScreen.setVisible(false);
// Show failed message
if (seconds != 0 && askUser)
Platform.runLater(() -> AlertTool.showNotification("Restart seems to failed",
"Wait some more seconds before trying to restart/exit XR3Player manually", Duration.seconds(20),
NotificationType.ERROR));
// Ask the user
if (askUser)
Platform.runLater(() -> {
if (AlertTool.doQuestion(null, "Restart failed.... force shutdown?", null, Main.window))
terminate(false);
});
else {
// Terminate after showing the message for a while
final PauseTransition forceTerminate = new PauseTransition(Duration.seconds(2));
forceTerminate.setOnFinished(fn -> terminate(false));
forceTerminate.play();
}
});
pause.play();
}
}