-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModifierController.java
103 lines (92 loc) · 3.44 KB
/
ModifierController.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
package application;
import java.io.IOException;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
public class ModifierController {
/**
* Enum for delay and speed up constants.
*/
public enum DelaySpeedUp {DELAY, SPEED_UP}
/**
* Choice box that allows selection between delaying and speeding up.
*/
@FXML private ChoiceBox<String> delaySpeedUpSelector;
/**
* The text fields holding the amount of seconds and miliseconds to delay/speed up with.
*/
@FXML private TextField secondsField, milisecondsField;
/**
* The button that performs the updating of the file when clicked.
*/
@FXML private Button updateButton;
@FXML public void initialize() {
delaySpeedUpSelector.setItems(FXCollections.observableArrayList("Delay","Speed up"));
delaySpeedUpSelector.setValue("Delay");
delaySpeedUpSelector.setTooltip(new Tooltip("Delaying or speeding up is needed"));
}
/**
* Fires when the {@link #updateButton} is clicked.
*/
@FXML public void updateButtonClicked() {
Alert msg = new Alert(AlertType.ERROR);
if(SelectorController.getSelectedFile() == null) { //no selected file
msg.setContentText("Select an SRT subtitle file first!");
msg.showAndWait();
return;
}
int seconds, miliseconds;
try {
seconds = Integer.parseInt(secondsField.getText());
} catch(NumberFormatException e) { //invalid seconds input
secondsField.setStyle("-fx-background-color: red");
msg.setContentText("Invalid amount of seconds!");
msg.showAndWait();
return;
}
try {
miliseconds = Integer.parseInt(milisecondsField.getText());
} catch(NumberFormatException e) { //invalid seconds input
milisecondsField.setStyle("-fx-background-color: red");
msg.setContentText("Invalid amount of miliseconds!");
msg.showAndWait();
return;
}
secondsField.setStyle("-fx-background-color: white"); //correct input, make backgrounds white
milisecondsField.setStyle("-fx-background-color: white");
try {
boolean completeSucess = new FileModifier(SelectorController.getSelectedFile(), seconds, miliseconds, delayOrSpeedUp()).replaceTimestamps();
displayFinalMessage(completeSucess); //if completeSucess is false, that means some timestamps were poorly formatted, and could not be changed.
} catch(IOException e) {
msg.setContentText("Could not read from or write to selected file!");
msg.showAndWait();
return;
}
}
/**
* Displays the result of the subtitle modification.
*/
private void displayFinalMessage(boolean sucess) {
if(sucess) {
Alert info = new Alert(AlertType.INFORMATION);
info.setContentText("Sucessfully changed SRT file!");
info.showAndWait();
} else {
Alert warning = new Alert(AlertType.WARNING);
warning.setHeaderText("Partial sucess!");
warning.setContentText("Some timestamps were poorly formatted, and could not be changed.");
warning.showAndWait();
}
}
/**
* @return The enum constant of the selected value of {@link #delaySpeedUpSelector}.
*/
private DelaySpeedUp delayOrSpeedUp() {
return delaySpeedUpSelector.getSelectionModel().getSelectedItem().equals("Delay") ? DelaySpeedUp.DELAY : DelaySpeedUp.SPEED_UP;
}
}