Show attachment status
This commit is contained in:
parent
9237649b60
commit
0cb5b34ff6
4 changed files with 35 additions and 19 deletions
|
@ -23,15 +23,15 @@ import java.util.ResourceBundle;
|
|||
* Controller for the chat input field and associated buttons
|
||||
*/
|
||||
public class ChatFooterController implements Initializable {
|
||||
|
||||
@FXML
|
||||
private HBox container;
|
||||
@FXML
|
||||
private JFXTextField textField;
|
||||
@FXML
|
||||
private JFXButton sendButton;
|
||||
@FXML
|
||||
private JFXButton attachButton;
|
||||
|
||||
// private ButtonPressEvent attachmentListeners;
|
||||
private ErrorCallback sendErrorListeners;
|
||||
|
||||
private PeerUser remoteUser;
|
||||
|
@ -40,21 +40,34 @@ public class ChatFooterController implements Initializable {
|
|||
FileChooser fileChooser = new FileChooser();
|
||||
File attachedFile;
|
||||
|
||||
// public void setAttachmentListener(ButtonPressEvent listener) {
|
||||
// attachmentListeners = listener;
|
||||
// }
|
||||
|
||||
public void setSendErrorListener(ErrorCallback listener) {
|
||||
sendErrorListeners = listener;
|
||||
}
|
||||
|
||||
public void onAttachmentPress() {
|
||||
// if (attachmentListeners != null) {
|
||||
// attachmentListeners.onPress();
|
||||
// }
|
||||
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) {
|
||||
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
|
||||
*/
|
||||
public void onSend() {
|
||||
if (!isTextFieldEmpty()) {
|
||||
if (canSend()) {
|
||||
if (remoteUser != null) {
|
||||
if (attachedFile == null) {
|
||||
remoteUser.sendTextMessage(textField.getText(), this::onSendError);
|
||||
} else {
|
||||
remoteUser.sendFileMessage(textField.getText(), attachedFile, this::onSendError);
|
||||
attachedFile = null;
|
||||
removeAttachment();
|
||||
}
|
||||
} else {
|
||||
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) {
|
||||
saveText(newText);
|
||||
sendButton.setDisable(isTextFieldEmpty());
|
||||
sendButton.setDisable(!canSend());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -133,8 +146,8 @@ public class ChatFooterController implements Initializable {
|
|||
*
|
||||
* @return True if empty, false otherwise
|
||||
*/
|
||||
private boolean isTextFieldEmpty() {
|
||||
return textField.getText().isEmpty();
|
||||
private boolean canSend() {
|
||||
return !textField.getText().isEmpty() || attachedFile != null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -164,7 +177,8 @@ public class ChatFooterController implements Initializable {
|
|||
this.remoteUser = user;
|
||||
user.addObserver(this::onUserStateChange);
|
||||
textField.setText(findSavedText());
|
||||
sendButton.setDisable(isTextFieldEmpty());
|
||||
removeAttachment();
|
||||
sendButton.setDisable(!canSend());
|
||||
setEnabled(user.isActive());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,9 +41,7 @@ public class MessageListItemController implements Initializable {
|
|||
if (message instanceof FileMessage) {
|
||||
FileMessage fileMessage = ((FileMessage) message);
|
||||
text += "\n<" + fileMessage.getFileName() + ">";
|
||||
button.setOnMouseClicked(event -> {
|
||||
openFile(fileMessage.getPath());
|
||||
});
|
||||
button.setOnMouseClicked(event -> openFile(fileMessage.getPath()));
|
||||
}
|
||||
button.setText(text);
|
||||
timestamp.setText(DateFormat.getTimeInstance().format(message.getDate()));
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
|
||||
</padding>
|
||||
<JFXTextField HBox.hgrow="ALWAYS" fx:id="textField"/>
|
||||
<JFXButton mnemonicParsing="false" onMouseClicked="#onAttachmentPress">
|
||||
<JFXButton mnemonicParsing="false" onMouseClicked="#onAttachmentPress" fx:id="attachButton">
|
||||
<graphic>
|
||||
<FontIcon iconLiteral="fas-paperclip" iconSize="24"/>
|
||||
</graphic>
|
||||
|
|
|
@ -96,6 +96,10 @@
|
|||
-fx-background-color: -primary;
|
||||
}
|
||||
|
||||
.attachment-ready {
|
||||
-fx-background-color: -success;
|
||||
}
|
||||
|
||||
.selected-user-item, .active-user-item, .inactive-user-item {
|
||||
-fx-background-radius: 0 20 20 0;
|
||||
-jfx-button-type: "FLAT";
|
||||
|
|
Loading…
Reference in a new issue