Show attachment status

This commit is contained in:
Arnaud Vergnet 2021-01-06 11:05:26 +01:00
parent 9237649b60
commit 0cb5b34ff6
4 changed files with 35 additions and 19 deletions

View file

@ -23,15 +23,15 @@ import java.util.ResourceBundle;
* Controller for the chat input field and associated buttons * Controller for the chat input field and associated buttons
*/ */
public class ChatFooterController implements Initializable { public class ChatFooterController implements Initializable {
@FXML @FXML
private HBox container; private HBox container;
@FXML @FXML
private JFXTextField textField; private JFXTextField textField;
@FXML @FXML
private JFXButton sendButton; private JFXButton sendButton;
@FXML
private JFXButton attachButton;
// private ButtonPressEvent attachmentListeners;
private ErrorCallback sendErrorListeners; private ErrorCallback sendErrorListeners;
private PeerUser remoteUser; private PeerUser remoteUser;
@ -40,21 +40,34 @@ public class ChatFooterController implements Initializable {
FileChooser fileChooser = new FileChooser(); FileChooser fileChooser = new FileChooser();
File attachedFile; File attachedFile;
// public void setAttachmentListener(ButtonPressEvent listener) {
// attachmentListeners = listener;
// }
public void setSendErrorListener(ErrorCallback listener) { public void setSendErrorListener(ErrorCallback listener) {
sendErrorListeners = listener; sendErrorListeners = listener;
} }
public void onAttachmentPress() { public void onAttachmentPress() {
// if (attachmentListeners != null) {
// attachmentListeners.onPress();
// }
attachedFile = fileChooser.showOpenDialog(container.getScene().getWindow()); attachedFile = fileChooser.showOpenDialog(container.getScene().getWindow());
sendButton.setDisable(!canSend());
System.out.println(sendButton.getStyleClass());
if (attachedFile != null) {
setSendButtonActive();
} else {
removeAttachment();
}
} }
public void clearSendButton() {
attachButton.getStyleClass().remove("attachment-ready");
}
public void setSendButtonActive() {
clearSendButton();
attachButton.getStyleClass().add("attachment-ready");
}
public void removeAttachment() {
attachedFile = null;
clearSendButton();
}
public void onSendError(Exception e) { public void onSendError(Exception e) {
Log.e(this.getClass().getSimpleName(), "Error: Could not send message", e); Log.e(this.getClass().getSimpleName(), "Error: Could not send message", e);
@ -67,13 +80,13 @@ public class ChatFooterController implements Initializable {
* If the input text is not empty and the remote user set, send the message * If the input text is not empty and the remote user set, send the message
*/ */
public void onSend() { public void onSend() {
if (!isTextFieldEmpty()) { if (canSend()) {
if (remoteUser != null) { if (remoteUser != null) {
if (attachedFile == null) { if (attachedFile == null) {
remoteUser.sendTextMessage(textField.getText(), this::onSendError); remoteUser.sendTextMessage(textField.getText(), this::onSendError);
} else { } else {
remoteUser.sendFileMessage(textField.getText(), attachedFile, this::onSendError); remoteUser.sendFileMessage(textField.getText(), attachedFile, this::onSendError);
attachedFile = null; removeAttachment();
} }
} else { } else {
Log.e(this.getClass().getSimpleName(), "Error: remote user not set"); Log.e(this.getClass().getSimpleName(), "Error: remote user not set");
@ -125,7 +138,7 @@ public class ChatFooterController implements Initializable {
*/ */
public void onTextChange(ObservableValue<? extends String> observable, String oldText, String newText) { public void onTextChange(ObservableValue<? extends String> observable, String oldText, String newText) {
saveText(newText); saveText(newText);
sendButton.setDisable(isTextFieldEmpty()); sendButton.setDisable(!canSend());
} }
/** /**
@ -133,8 +146,8 @@ public class ChatFooterController implements Initializable {
* *
* @return True if empty, false otherwise * @return True if empty, false otherwise
*/ */
private boolean isTextFieldEmpty() { private boolean canSend() {
return textField.getText().isEmpty(); return !textField.getText().isEmpty() || attachedFile != null;
} }
/** /**
@ -164,7 +177,8 @@ public class ChatFooterController implements Initializable {
this.remoteUser = user; this.remoteUser = user;
user.addObserver(this::onUserStateChange); user.addObserver(this::onUserStateChange);
textField.setText(findSavedText()); textField.setText(findSavedText());
sendButton.setDisable(isTextFieldEmpty()); removeAttachment();
sendButton.setDisable(!canSend());
setEnabled(user.isActive()); setEnabled(user.isActive());
} }
} }

View file

@ -41,9 +41,7 @@ public class MessageListItemController implements Initializable {
if (message instanceof FileMessage) { if (message instanceof FileMessage) {
FileMessage fileMessage = ((FileMessage) message); FileMessage fileMessage = ((FileMessage) message);
text += "\n<" + fileMessage.getFileName() + ">"; text += "\n<" + fileMessage.getFileName() + ">";
button.setOnMouseClicked(event -> { button.setOnMouseClicked(event -> openFile(fileMessage.getPath()));
openFile(fileMessage.getPath());
});
} }
button.setText(text); button.setText(text);
timestamp.setText(DateFormat.getTimeInstance().format(message.getDate())); timestamp.setText(DateFormat.getTimeInstance().format(message.getDate()));

View file

@ -13,7 +13,7 @@
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/> <Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding> </padding>
<JFXTextField HBox.hgrow="ALWAYS" fx:id="textField"/> <JFXTextField HBox.hgrow="ALWAYS" fx:id="textField"/>
<JFXButton mnemonicParsing="false" onMouseClicked="#onAttachmentPress"> <JFXButton mnemonicParsing="false" onMouseClicked="#onAttachmentPress" fx:id="attachButton">
<graphic> <graphic>
<FontIcon iconLiteral="fas-paperclip" iconSize="24"/> <FontIcon iconLiteral="fas-paperclip" iconSize="24"/>
</graphic> </graphic>

View file

@ -96,6 +96,10 @@
-fx-background-color: -primary; -fx-background-color: -primary;
} }
.attachment-ready {
-fx-background-color: -success;
}
.selected-user-item, .active-user-item, .inactive-user-item { .selected-user-item, .active-user-item, .inactive-user-item {
-fx-background-radius: 0 20 20 0; -fx-background-radius: 0 20 20 0;
-jfx-button-type: "FLAT"; -jfx-button-type: "FLAT";