feat: handle message send event
This commit is contained in:
parent
0fe3b666dd
commit
32dc8d719c
4 changed files with 64 additions and 8 deletions
|
@ -94,7 +94,7 @@ public class MainController implements Initializable {
|
||||||
userListController.addRefreshUserListener(() -> System.out.println("refresh event"));
|
userListController.addRefreshUserListener(() -> System.out.println("refresh event"));
|
||||||
userListController.addUserSelectedListener((user) -> chatController.setRemoteUser(user));
|
userListController.addUserSelectedListener((user) -> chatController.setRemoteUser(user));
|
||||||
chatController.addAttachmentListener(() -> System.out.println("attach event"));
|
chatController.addAttachmentListener(() -> System.out.println("attach event"));
|
||||||
chatController.addSendListener(() -> System.out.println("send event"));
|
chatController.addSendListener((text) -> System.out.println("sent : " + text));
|
||||||
toolbarController.addEditListener(() -> {
|
toolbarController.addEditListener(() -> {
|
||||||
try {
|
try {
|
||||||
openEditUsernameDialog();
|
openEditUsernameDialog();
|
||||||
|
|
|
@ -39,12 +39,13 @@ public class ChatController implements Initializable {
|
||||||
chatFooterController.addAttachmentListener(listener);
|
chatFooterController.addAttachmentListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSendListener(ButtonPressEvent listener) {
|
public void addSendListener(ChatFooterController.SendMessageEvent listener) {
|
||||||
chatFooterController.addSendListener(listener);
|
chatFooterController.addSendListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRemoteUser(PeerUser remoteUser) {
|
public void setRemoteUser(PeerUser remoteUser) {
|
||||||
this.remoteUser = remoteUser;
|
this.remoteUser = remoteUser;
|
||||||
|
this.chatFooterController.setRemoteUser(remoteUser);
|
||||||
this.chatHeaderController.setRemoteUser(remoteUser);
|
this.chatHeaderController.setRemoteUser(remoteUser);
|
||||||
setState(State.LOADING);
|
setState(State.LOADING);
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,19 @@
|
||||||
package fr.insa.clavardator.ui.chat;
|
package fr.insa.clavardator.ui.chat;
|
||||||
|
|
||||||
|
import com.jfoenix.controls.JFXTextField;
|
||||||
import fr.insa.clavardator.ui.ButtonPressEvent;
|
import fr.insa.clavardator.ui.ButtonPressEvent;
|
||||||
|
import fr.insa.clavardator.users.PeerUser;
|
||||||
|
import javafx.beans.value.ObservableValue;
|
||||||
|
import javafx.event.EventHandler;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.Initializable;
|
import javafx.fxml.Initializable;
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
import javafx.scene.input.KeyEvent;
|
||||||
import javafx.scene.layout.HBox;
|
import javafx.scene.layout.HBox;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
@ -14,31 +21,79 @@ public class ChatFooterController implements Initializable {
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private HBox container;
|
private HBox container;
|
||||||
|
@FXML
|
||||||
|
private JFXTextField textField;
|
||||||
|
|
||||||
private List<ButtonPressEvent> attachmentListeners;
|
private List<ButtonPressEvent> attachmentListeners;
|
||||||
private List<ButtonPressEvent> sendListeners;
|
private List<SendMessageEvent> sendListeners;
|
||||||
|
|
||||||
|
private PeerUser remoteUser;
|
||||||
|
private HashMap<PeerUser, String> savedText;
|
||||||
|
|
||||||
|
|
||||||
public void addAttachmentListener(ButtonPressEvent listener) {
|
public void addAttachmentListener(ButtonPressEvent listener) {
|
||||||
attachmentListeners.add(listener);
|
attachmentListeners.add(listener);
|
||||||
}
|
}
|
||||||
public void addSendListener(ButtonPressEvent listener) {
|
public void addSendListener(SendMessageEvent listener) {
|
||||||
sendListeners.add(listener);
|
sendListeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onAttachmentPress() {
|
public void onAttachmentPress() {
|
||||||
attachmentListeners.forEach(ButtonPressEvent::onPress);
|
attachmentListeners.forEach(ButtonPressEvent::onPress);
|
||||||
}
|
}
|
||||||
public void onSendPress() {
|
public void onSend() {
|
||||||
sendListeners.forEach(ButtonPressEvent::onPress);
|
sendListeners.forEach((l) -> l.onSend(findSavedText()));
|
||||||
|
textField.setText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEnabled(boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
container.setDisable(!enabled);
|
container.setDisable(!enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String findSavedText() {
|
||||||
|
String text = null;
|
||||||
|
if (remoteUser != null) {
|
||||||
|
text = savedText.get(remoteUser);
|
||||||
|
}
|
||||||
|
return text != null ? text : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveText(String text) {
|
||||||
|
if (remoteUser != null) {
|
||||||
|
savedText.put(remoteUser, text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onTextChange(ObservableValue<? extends String> observable, String oldText, String newText) {
|
||||||
|
saveText(newText);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(URL location, ResourceBundle resources) {
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
|
savedText = new HashMap<>();
|
||||||
attachmentListeners = new ArrayList<>();
|
attachmentListeners = new ArrayList<>();
|
||||||
sendListeners = new ArrayList<>();
|
sendListeners = new ArrayList<>();
|
||||||
|
textField.textProperty().addListener(this::onTextChange);
|
||||||
|
textField.setOnKeyPressed(event -> {
|
||||||
|
if (event.getCode() == KeyCode.ENTER) {
|
||||||
|
event.consume();
|
||||||
|
if (event.isShiftDown()) {
|
||||||
|
textField.appendText(System.getProperty("line.separator"));
|
||||||
|
} else {
|
||||||
|
if(!textField.getText().isEmpty()){
|
||||||
|
onSend();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRemoteUser(PeerUser remoteUser) {
|
||||||
|
this.remoteUser = remoteUser;
|
||||||
|
textField.setText(findSavedText());
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface SendMessageEvent {
|
||||||
|
void onSend(String text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,13 +12,13 @@
|
||||||
<padding>
|
<padding>
|
||||||
<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"/>
|
<JFXTextField HBox.hgrow="ALWAYS" fx:id="textField"/>
|
||||||
<JFXButton mnemonicParsing="false" onMouseClicked="#onAttachmentPress">
|
<JFXButton mnemonicParsing="false" onMouseClicked="#onAttachmentPress">
|
||||||
<graphic>
|
<graphic>
|
||||||
<FontIcon iconLiteral="fas-paperclip" iconSize="24"/>
|
<FontIcon iconLiteral="fas-paperclip" iconSize="24"/>
|
||||||
</graphic>
|
</graphic>
|
||||||
</JFXButton>
|
</JFXButton>
|
||||||
<JFXButton mnemonicParsing="false" text="Envoyer" onMouseClicked="#onSendPress">
|
<JFXButton mnemonicParsing="false" text="Envoyer" onMouseClicked="#onSend">
|
||||||
<graphic>
|
<graphic>
|
||||||
<FontIcon iconLiteral="fas-paper-plane" iconSize="24"/>
|
<FontIcon iconLiteral="fas-paper-plane" iconSize="24"/>
|
||||||
</graphic>
|
</graphic>
|
||||||
|
|
Loading…
Reference in a new issue