Add message editing

Which constrains the message format and layout
This commit is contained in:
Nathan McRae 2024-12-29 12:42:34 -08:00
parent 55cec6be43
commit 52f7369a10
2 changed files with 59 additions and 3 deletions

View File

@ -82,7 +82,7 @@
<Insets bottom="10.0" /> <Insets bottom="10.0" />
</padding> </padding>
</VBox> </VBox>
<TextArea layoutY="110.0" prefHeight="270.0" prefWidth="640.0" text="0239 0480 2938 0928&#10;3093 2298 3923 8933" wrapText="true" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="110.0"> <TextArea fx:id="messageTextArea" layoutY="110.0" prefHeight="270.0" prefWidth="640.0" text="0239 0480 2938 0928&#10;3093 2298 3923 8933" wrapText="true" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="110.0">
<font> <font>
<Font size="30.0" /> <Font size="30.0" />
</font> </font>

View File

@ -2,16 +2,26 @@ package name.nathanmcrae;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import java.net.URL;
import java.util.ResourceBundle;
public class NumbersStationController implements Initializable {
private Stage configStage;
public class NumbersStationController {
@FXML @FXML
private Label lastRetrievedLabel; private Label lastRetrievedLabel;
private Stage configStage; @FXML
private TextArea messageTextArea;
@FXML @FXML
private void handleButtonPress() { private void handleButtonPress() {
@ -33,4 +43,50 @@ public class NumbersStationController {
configStage.toFront(); configStage.toFront();
} }
} }
@Override
public void initialize(URL location, ResourceBundle resources) {
messageTextArea.addEventFilter(KeyEvent.ANY, event -> {
int cursorPosition = messageTextArea.getCaretPosition();
String character = event.getCharacter();
KeyCode code = event.getCode();
String str = event.getText();
// Consume the event to block the default behavior
if (!(code == KeyCode.LEFT ||
code == KeyCode.RIGHT ||
code == KeyCode.UP ||
code == KeyCode.DOWN ||
code == KeyCode.HOME ||
code == KeyCode.END ||
code == KeyCode.PAGE_UP ||
code == KeyCode.PAGE_DOWN)) {
event.consume();
}
if (event.getEventType() == KeyEvent.KEY_PRESSED &&
(code == KeyCode.DIGIT0 ||
code == KeyCode.DIGIT1 ||
code == KeyCode.DIGIT2 ||
code == KeyCode.DIGIT3 ||
code == KeyCode.DIGIT4 ||
code == KeyCode.DIGIT5 ||
code == KeyCode.DIGIT6 ||
code == KeyCode.DIGIT7 ||
code == KeyCode.DIGIT8 ||
code == KeyCode.DIGIT9)) {
if (cursorPosition < messageTextArea.getLength()) {
char currentChar = messageTextArea.getText().charAt(cursorPosition);
if (Character.isWhitespace(currentChar)) {
messageTextArea.replaceText(cursorPosition + 1, cursorPosition + 2, str);
} else {
messageTextArea.replaceText(cursorPosition, cursorPosition + 1, str);
}
}
}
});
}
} }