|
@@ -3,12 +3,12 @@ package fr.insa.clavardator.ui.chat;
|
3
|
3
|
import com.jfoenix.controls.JFXTextField;
|
4
|
4
|
import fr.insa.clavardator.ui.ButtonPressEvent;
|
5
|
5
|
import fr.insa.clavardator.users.PeerUser;
|
|
6
|
+import fr.insa.clavardator.util.ErrorCallback;
|
|
7
|
+import fr.insa.clavardator.util.Log;
|
6
|
8
|
import javafx.beans.value.ObservableValue;
|
7
|
|
-import javafx.event.EventHandler;
|
8
|
9
|
import javafx.fxml.FXML;
|
9
|
10
|
import javafx.fxml.Initializable;
|
10
|
11
|
import javafx.scene.input.KeyCode;
|
11
|
|
-import javafx.scene.input.KeyEvent;
|
12
|
12
|
import javafx.scene.layout.HBox;
|
13
|
13
|
|
14
|
14
|
import java.net.URL;
|
|
@@ -25,7 +25,7 @@ public class ChatFooterController implements Initializable {
|
25
|
25
|
private JFXTextField textField;
|
26
|
26
|
|
27
|
27
|
private List<ButtonPressEvent> attachmentListeners;
|
28
|
|
- private List<SendMessageEvent> sendListeners;
|
|
28
|
+ private List<ErrorCallback> sendErrorListeners;
|
29
|
29
|
|
30
|
30
|
private PeerUser remoteUser;
|
31
|
31
|
private HashMap<PeerUser, String> savedText;
|
|
@@ -34,18 +34,27 @@ public class ChatFooterController implements Initializable {
|
34
|
34
|
public void addAttachmentListener(ButtonPressEvent listener) {
|
35
|
35
|
attachmentListeners.add(listener);
|
36
|
36
|
}
|
37
|
|
- public void addSendListener(SendMessageEvent listener) {
|
38
|
|
- sendListeners.add(listener);
|
|
37
|
+ public void addSendErrorListener(ErrorCallback listener) {
|
|
38
|
+ sendErrorListeners.add(listener);
|
39
|
39
|
}
|
40
|
40
|
|
41
|
41
|
public void onAttachmentPress() {
|
42
|
42
|
attachmentListeners.forEach(ButtonPressEvent::onPress);
|
43
|
43
|
}
|
44
|
44
|
public void onSend() {
|
45
|
|
- sendListeners.forEach((l) -> l.onSend(findSavedText()));
|
|
45
|
+ if (remoteUser != null) {
|
|
46
|
+ remoteUser.sendTextMessage(textField.getText(), this::onSendError);
|
|
47
|
+ } else {
|
|
48
|
+ Log.e(this.getClass().getSimpleName(), "Error: remote user not set");
|
|
49
|
+ }
|
46
|
50
|
textField.setText("");
|
47
|
51
|
}
|
48
|
52
|
|
|
53
|
+ public void onSendError(Exception e) {
|
|
54
|
+ Log.e(this.getClass().getSimpleName(), "Error: Could not send message", e);
|
|
55
|
+ sendErrorListeners.forEach((l) -> l.onError(e));
|
|
56
|
+ }
|
|
57
|
+
|
49
|
58
|
public void setEnabled(boolean enabled) {
|
50
|
59
|
container.setDisable(!enabled);
|
51
|
60
|
}
|
|
@@ -72,7 +81,7 @@ public class ChatFooterController implements Initializable {
|
72
|
81
|
public void initialize(URL location, ResourceBundle resources) {
|
73
|
82
|
savedText = new HashMap<>();
|
74
|
83
|
attachmentListeners = new ArrayList<>();
|
75
|
|
- sendListeners = new ArrayList<>();
|
|
84
|
+ sendErrorListeners = new ArrayList<>();
|
76
|
85
|
textField.textProperty().addListener(this::onTextChange);
|
77
|
86
|
textField.setOnKeyPressed(event -> {
|
78
|
87
|
if (event.getCode() == KeyCode.ENTER) {
|