4
4
import app .model .Operations ;
5
5
import app .model .ProtectionCustomSetting ;
6
6
import app .service .TaskManager ;
7
+ import javafx .application .Platform ;
7
8
import javafx .collections .FXCollections ;
8
9
import javafx .fxml .FXML ;
9
10
import javafx .fxml .Initializable ;
11
+ import javafx .scene .Cursor ;
10
12
import javafx .scene .control .Alert ;
13
+ import javafx .scene .control .Button ;
11
14
import javafx .scene .control .CheckBox ;
12
15
import javafx .scene .control .ChoiceBox ;
13
16
import javafx .scene .control .TextField ;
21
24
import java .net .URL ;
22
25
import java .util .LinkedHashMap ;
23
26
import java .util .ResourceBundle ;
27
+ import java .util .concurrent .CompletableFuture ;
24
28
25
29
public class MainController implements Initializable {
26
30
private LinkedHashMap <String , Integer > protectionLevelsOptions ;
@@ -33,7 +37,6 @@ public class MainController implements Initializable {
33
37
@ FXML
34
38
private TextField outputPathTextField ;
35
39
36
-
37
40
@ FXML
38
41
private ChoiceBox <String > protectionLevelsChoiceBox ;
39
42
@@ -46,6 +49,9 @@ public class MainController implements Initializable {
46
49
@ FXML
47
50
private VBox protectionAdvancedSettingsContainer ;
48
51
52
+ @ FXML
53
+ private Button runButton ;
54
+
49
55
@ Override
50
56
public void initialize (URL location , ResourceBundle resources ) {
51
57
setOperations ();
@@ -55,19 +61,8 @@ public void initialize(URL location, ResourceBundle resources) {
55
61
56
62
@ FXML
57
63
private void runApplication () {
58
- ApplicationExecutionSettings settings = new ApplicationExecutionSettings (
59
- sourcePathTextField .getText (),
60
- outputPathTextField .getText (),
61
- getOperations (),
62
- getProtectionLevel (),
63
- getProtectionCustomSetting ()
64
- );
65
- try {
66
- long elapsedTime = TaskManager .runApplicationWithSettings (settings );
67
- displaySucces (elapsedTime );
68
- } catch (Error error ) {
69
- displayError (error );
70
- }
64
+ ApplicationExecutionSettings settings = getApplicationExecutionSettings ();
65
+ runApplicationAsync (settings );
71
66
}
72
67
73
68
@ FXML
@@ -113,6 +108,38 @@ private void displayAbout() {
113
108
alert .showAndWait ();
114
109
}
115
110
111
+ private void runApplicationAsync (ApplicationExecutionSettings settings ) {
112
+ // Change the UI to reflect that the app is running
113
+ this .runButton .setDisable (true );
114
+ this .runButton .getScene ().setCursor (Cursor .WAIT );
115
+ // Run tasks asynchronously
116
+ CompletableFuture
117
+ .supplyAsync (() -> TaskManager .runApplicationWithSettings (settings ))
118
+ .handle ((elapsedTime , error ) -> {
119
+ if (error == null ) {
120
+ Platform .runLater (() -> displaySucces (elapsedTime ));
121
+ } else {
122
+ Platform .runLater (() -> displayError (error .getCause ().getMessage ()));
123
+ }
124
+ // Restore the UI
125
+ Platform .runLater (() -> {
126
+ this .runButton .setDisable (false );
127
+ this .runButton .getScene ().setCursor (Cursor .DEFAULT );
128
+ });
129
+ return null ;
130
+ });
131
+ }
132
+
133
+ private ApplicationExecutionSettings getApplicationExecutionSettings () {
134
+ return new ApplicationExecutionSettings (
135
+ sourcePathTextField .getText (),
136
+ outputPathTextField .getText (),
137
+ getOperations (),
138
+ getProtectionLevel (),
139
+ getProtectionCustomSetting ()
140
+ );
141
+ }
142
+
116
143
private void setOperations () {
117
144
operationsChoiceBox .setItems (
118
145
FXCollections .observableArrayList (
@@ -198,11 +225,11 @@ private void displaySucces(long elapsedTime) {
198
225
alert .showAndWait ();
199
226
}
200
227
201
- private void displayError (Error error ) {
228
+ private void displayError (String message ) {
202
229
Alert alert = new Alert (Alert .AlertType .ERROR );
203
230
alert .setTitle ("Error" );
204
231
alert .setHeaderText ("Ooops, something went wrong!" );
205
- alert .setContentText (error . getMessage () );
232
+ alert .setContentText (message );
206
233
alert .showAndWait ();
207
234
}
208
235
}
0 commit comments