diff --git a/src/main/java/name/nathanmcrae/numbersstation/MainController.java b/src/main/java/name/nathanmcrae/numbersstation/MainController.java index 900133a..73763a7 100644 --- a/src/main/java/name/nathanmcrae/numbersstation/MainController.java +++ b/src/main/java/name/nathanmcrae/numbersstation/MainController.java @@ -5,6 +5,7 @@ import com.leakyabstractions.result.core.Results; import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -121,18 +122,14 @@ public class MainController implements Initializable { @FXML private void handleSetAsNextMessageButtonPress() { - Path stationDirPath = selectedStation.stationPath(); - try { - if (!Files.exists(stationDirPath)) { - Files.createDirectories(stationDirPath); + if (!Files.exists(selectedStation.stationPath())) { + Files.createDirectories(selectedStation.stationPath()); } - Path nextMessagePath = stationDirPath.resolve("next-message.txt"); - - Files.write(nextMessagePath, messageTextArea.getText().getBytes()); + Files.write(selectedStation.nextMessagePath(), messageTextArea.getText().getBytes()); } 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); Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Exception"); @@ -180,6 +177,8 @@ public class MainController implements Initializable { selectedStationName.set(selectedStation.getName()); } + updateStationSettings(selectedStation); + messageTextArea.addEventFilter(KeyEvent.ANY, event -> { int cursorPosition = messageTextArea.getCaretPosition(); @@ -250,17 +249,53 @@ public class MainController implements Initializable { // TODO: Load message from file // 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(); - StringBuilder messageBuilder = new StringBuilder(); - - for (int i = 0; i < selectedStation.getMessageLength(); i++) { - if (i > 0 && i % selectedStation.getDigitsPerGroup() == 0) { - messageBuilder.append(" "); + String messageText; + if (!Files.exists(nextMessagePath)) { + try { + messageText = selectedStation.generateMessage(settings.getMessageGenerationAttempts()); + } catch (StationSettings.MessageGenerationException e) { + 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); } } diff --git a/src/main/java/name/nathanmcrae/numbersstation/StationSettings.java b/src/main/java/name/nathanmcrae/numbersstation/StationSettings.java index 45a93b6..ae431b1 100644 --- a/src/main/java/name/nathanmcrae/numbersstation/StationSettings.java +++ b/src/main/java/name/nathanmcrae/numbersstation/StationSettings.java @@ -81,6 +81,10 @@ public class StationSettings { }); } + public Path nextMessagePath() { + return stationPath().resolve("next-message.txt"); + } + public Path stationPath() { String configHome = System.getenv("XDG_CONFIG_HOME"); Path directoryPath;