Implement Set as next message button

This commit is contained in:
Nathan McRae 2025-02-01 12:17:26 -08:00
parent a7169b49cc
commit f7314290db
3 changed files with 46 additions and 1 deletions

View File

@ -116,6 +116,29 @@ public class MainController implements Initializable {
}
}
@FXML
private void handleSetAsNextMessageButtonPress() {
Path stationDirPath = selectedStation.stationPath();
try {
if (!Files.exists(stationDirPath)) {
Files.createDirectories(stationDirPath);
}
Path nextMessagePath = stationDirPath.resolve("next-message.txt");
Files.write(nextMessagePath, messageTextArea.getText().getBytes());
} catch (IOException e) {
String message = "Failed to write next message to " + stationDirPath.toString();
logger.log(Level.SEVERE, message, e);
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Exception");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
stationNameField.textProperty().bindBidirectional(selectedStationName);

View File

@ -86,7 +86,7 @@
</TextArea>
<FlowPane alignment="CENTER_RIGHT" prefHeight="34.0" prefWidth="620.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<Button mnemonicParsing="false" text="Set as next message" />
<Button mnemonicParsing="false" onMousePressed="#handleSetAsNextMessageButtonPress" text="Set as next message" />
</children>
</FlowPane>
</children>

View File

@ -1,5 +1,8 @@
package name.nathanmcrae.numbersstation;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
@ -40,6 +43,25 @@ public class StationSettings {
name = newName;
}
public Path stationPath() {
String configHome = System.getenv("XDG_CONFIG_HOME");
Path directoryPath;
if (configHome != null && !configHome.isEmpty() && Paths.get(configHome).isAbsolute()) {
directoryPath = Paths.get(configHome, "numbers-station");
} else {
String userHome = System.getProperty("user.home");
directoryPath = Paths.get(userHome, ".config", "numbers-station");
}
String stationDirName = null;
try {
stationDirName = java.net.URLEncoder.encode(name, "UTF-8");
} catch(UnsupportedEncodingException e) {} // We know UTF-8 is supported
return directoryPath.resolve(stationDirName);
}
public String getAddress() {
return address;
}