diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 0662929..08e1501 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -197,6 +197,34 @@ public final class ChatScene { sceneContext.getController().initializeData(sceneContext, localDB); } + /** + * Checks the text length of the {@code messageTextArea}, adjusts the + * {@code remainingChars} - Label and checks whether to send the message + * automatically. + * + * @param e the keys that have been entered + * @since Envoy Client v0.1-beta + */ + @FXML + private void checkKeyCombination(KeyEvent e) { + // checks whether the text is too long + messageTextUpdated(); + // Automatic sending of messages via (ctrl +) enter + checkPostConditions(e); + } + + /** + * @param e the keys that have been pressed + * @since Envoy Client v0.1-beta + */ + @FXML + private void checkPostConditions(KeyEvent e) { + if (!postButton.isDisabled() && settings.isEnterToSend() && e.getCode() == KeyCode.ENTER + || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown()) + postMessage(); + postButton.setDisable(messageTextArea.getText().isBlank() || currentChat == null); + } + /** * Actions to perform when the text was updated in the messageTextArea. * @@ -210,29 +238,21 @@ public final class ChatScene { messageTextArea.positionCaret(MAX_MESSAGE_LENGTH); messageTextArea.setScrollTop(Double.MAX_VALUE); } + designRemainingCharsLabel(); + } - // Redesigning the remainingChars - Label + /** + * Sets the text and text-color of the {@code remainingChars} - Label. + * + * @since Envoy Client v0.1-beta + */ + private void designRemainingCharsLabel() { final int currentLength = messageTextArea.getText().length(); final int remainingLength = MAX_MESSAGE_LENGTH - currentLength; remainingChars.setText(String.format("remaining chars: %d/%d", remainingLength, MAX_MESSAGE_LENGTH)); remainingChars.setTextFill(Color.rgb(currentLength, remainingLength, 0, 1)); } - /** - * Actions to perform when a key has been entered. - * - * @param e the Keys that have been entered - * @since Envoy Client v0.1-beta - */ - @FXML - private void checkKeyCombination(KeyEvent e) { - // Automatic sending of messages via (ctrl +) enter - if (!postButton.isDisabled() && settings.isEnterToSend() && e.getCode() == KeyCode.ENTER - || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown()) - postMessage(); - postButton.setDisable(messageTextArea.getText().isBlank() || currentChat == null); - } - /** * Sends a new message to the server based on the text entered in the * messageTextArea. @@ -241,10 +261,12 @@ public final class ChatScene { */ @FXML private void postMessage() { + final var text = messageTextArea.getText().strip(); + if (text.isBlank()) throw new IllegalArgumentException("A message without visible text can not be sent."); try { // Create and send message final var message = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator()) - .setText(messageTextArea.getText().strip()) + .setText(text) .build(); // Send message @@ -264,5 +286,6 @@ public final class ChatScene { // Clear text field and disable post button messageTextArea.setText(""); postButton.setDisable(true); + designRemainingCharsLabel(); } } diff --git a/src/main/resources/fxml/ChatScene.fxml b/src/main/resources/fxml/ChatScene.fxml index aab77a6..5323930 100644 --- a/src/main/resources/fxml/ChatScene.fxml +++ b/src/main/resources/fxml/ChatScene.fxml @@ -27,7 +27,7 @@