Load next message file to message buffer
This commit is contained in:
parent
e4ad5d16d6
commit
12df6d97fa
@ -5,6 +5,7 @@ import com.leakyabstractions.result.core.Results;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@ -121,18 +122,14 @@ public class MainController implements Initializable {
|
|||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void handleSetAsNextMessageButtonPress() {
|
private void handleSetAsNextMessageButtonPress() {
|
||||||
Path stationDirPath = selectedStation.stationPath();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!Files.exists(stationDirPath)) {
|
if (!Files.exists(selectedStation.stationPath())) {
|
||||||
Files.createDirectories(stationDirPath);
|
Files.createDirectories(selectedStation.stationPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
Path nextMessagePath = stationDirPath.resolve("next-message.txt");
|
Files.write(selectedStation.nextMessagePath(), messageTextArea.getText().getBytes());
|
||||||
|
|
||||||
Files.write(nextMessagePath, messageTextArea.getText().getBytes());
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
String message = "Failed to write next message to " + stationDirPath.toString();
|
String message = "Failed to write next message to " + selectedStation.nextMessagePath().toString();
|
||||||
logger.log(Level.SEVERE, message, e);
|
logger.log(Level.SEVERE, message, e);
|
||||||
Alert alert = new Alert(Alert.AlertType.ERROR);
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||||
alert.setTitle("Exception");
|
alert.setTitle("Exception");
|
||||||
@ -180,6 +177,8 @@ public class MainController implements Initializable {
|
|||||||
selectedStationName.set(selectedStation.getName());
|
selectedStationName.set(selectedStation.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateStationSettings(selectedStation);
|
||||||
|
|
||||||
messageTextArea.addEventFilter(KeyEvent.ANY, event -> {
|
messageTextArea.addEventFilter(KeyEvent.ANY, event -> {
|
||||||
int cursorPosition = messageTextArea.getCaretPosition();
|
int cursorPosition = messageTextArea.getCaretPosition();
|
||||||
|
|
||||||
@ -250,17 +249,53 @@ public class MainController implements Initializable {
|
|||||||
|
|
||||||
// TODO: Load message from file
|
// TODO: Load message from file
|
||||||
// If the message we're overwriting is different from its file, then prompt to confirm
|
// If the message we're overwriting is different from its file, then prompt to confirm
|
||||||
|
Path nextMessagePath = selectedStation.stationPath().resolve("next-message.txt");
|
||||||
|
|
||||||
Random random = new Random();
|
String messageText;
|
||||||
StringBuilder messageBuilder = new StringBuilder();
|
if (!Files.exists(nextMessagePath)) {
|
||||||
|
try {
|
||||||
for (int i = 0; i < selectedStation.getMessageLength(); i++) {
|
messageText = selectedStation.generateMessage(settings.getMessageGenerationAttempts());
|
||||||
if (i > 0 && i % selectedStation.getDigitsPerGroup() == 0) {
|
} catch (StationSettings.MessageGenerationException e) {
|
||||||
messageBuilder.append(" ");
|
String errMessage = "Failed to generate message";
|
||||||
|
logger.log(Level.SEVERE, errMessage, e);
|
||||||
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||||
|
alert.setTitle("Exception");
|
||||||
|
alert.setHeaderText(null);
|
||||||
|
alert.setContentText(errMessage + ": " + e.getMessage());
|
||||||
|
alert.showAndWait();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!Files.exists(selectedStation.stationPath())) {
|
||||||
|
Files.createDirectories(selectedStation.stationPath());
|
||||||
|
}
|
||||||
|
Files.write(nextMessagePath, messageText.getBytes(StandardCharsets.UTF_8));
|
||||||
|
} catch (IOException e) {
|
||||||
|
String errMessage = "Failed to save message";
|
||||||
|
logger.log(Level.SEVERE, errMessage, e);
|
||||||
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||||
|
alert.setTitle("Exception");
|
||||||
|
alert.setHeaderText(null);
|
||||||
|
alert.setContentText(errMessage + ": " + e.getMessage());
|
||||||
|
alert.showAndWait();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
messageText = new String(Files.readAllBytes(nextMessagePath));
|
||||||
|
} catch (IOException e) {
|
||||||
|
String errMessage = "Failed to read message";
|
||||||
|
logger.log(Level.SEVERE, errMessage, e);
|
||||||
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||||
|
alert.setTitle("Exception");
|
||||||
|
alert.setHeaderText(null);
|
||||||
|
alert.setContentText(errMessage + ": " + e.getMessage());
|
||||||
|
alert.showAndWait();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
messageBuilder.append(random.nextInt(10));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
messageTextArea.setText(messageBuilder.toString());
|
messageTextArea.setText(messageText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,6 +81,10 @@ public class StationSettings {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Path nextMessagePath() {
|
||||||
|
return stationPath().resolve("next-message.txt");
|
||||||
|
}
|
||||||
|
|
||||||
public Path stationPath() {
|
public Path stationPath() {
|
||||||
String configHome = System.getenv("XDG_CONFIG_HOME");
|
String configHome = System.getenv("XDG_CONFIG_HOME");
|
||||||
Path directoryPath;
|
Path directoryPath;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user