feat: use Message class in chat and improve modularity
This commit is contained in:
parent
93266eb429
commit
811537dec8
8 changed files with 175 additions and 74 deletions
|
@ -1,5 +1,6 @@
|
||||||
package fr.insa.clavardator.chat;
|
package fr.insa.clavardator.chat;
|
||||||
|
|
||||||
|
import fr.insa.clavardator.users.CurrentUser;
|
||||||
import fr.insa.clavardator.users.User;
|
import fr.insa.clavardator.users.User;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
@ -22,4 +23,8 @@ public class Message implements Serializable {
|
||||||
public String getText() {
|
public String getText() {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User getSender() {
|
||||||
|
return sender;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package fr.insa.clavardator.ui.chat;
|
package fr.insa.clavardator.ui.chat;
|
||||||
|
|
||||||
|
import fr.insa.clavardator.chat.Message;
|
||||||
import fr.insa.clavardator.ui.ButtonPressEvent;
|
import fr.insa.clavardator.ui.ButtonPressEvent;
|
||||||
import fr.insa.clavardator.ui.NoSelectionModel;
|
import fr.insa.clavardator.ui.NoSelectionModel;
|
||||||
|
import fr.insa.clavardator.users.CurrentUser;
|
||||||
|
import fr.insa.clavardator.users.PeerUser;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
|
@ -9,54 +12,57 @@ import javafx.fxml.Initializable;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
public class ChatController implements Initializable {
|
public class ChatController implements Initializable {
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private ListView<String> messageList;
|
private ListView<Message> messageList;
|
||||||
|
@FXML
|
||||||
|
private ChatFooterController chatFooterController;
|
||||||
|
@FXML
|
||||||
|
private ChatHeaderController chatHeaderController;
|
||||||
|
|
||||||
private List<ButtonPressEvent> attachmentListeners;
|
private CurrentUser currentUser;
|
||||||
private List<ButtonPressEvent> sendListeners;
|
private PeerUser remoteUser;
|
||||||
private List<ButtonPressEvent> editListeners;
|
|
||||||
|
|
||||||
public void addAttachmentListener(ButtonPressEvent listener) {
|
public void addAttachmentListener(ButtonPressEvent listener) {
|
||||||
attachmentListeners.add(listener);
|
chatFooterController.addAttachmentListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSendListener(ButtonPressEvent listener) {
|
public void addSendListener(ButtonPressEvent listener) {
|
||||||
sendListeners.add(listener);
|
chatFooterController.addSendListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEditListener(ButtonPressEvent listener) {
|
public void addEditListener(ButtonPressEvent listener) {
|
||||||
editListeners.add(listener);
|
chatHeaderController.addEditListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onAttachmentPress() {
|
|
||||||
attachmentListeners.forEach(ButtonPressEvent::onPress);
|
public void setCurrentUser(CurrentUser currentUser) {
|
||||||
|
this.currentUser = currentUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onSendPress() {
|
public void setRemoteUser(PeerUser remoteUser) {
|
||||||
sendListeners.forEach(ButtonPressEvent::onPress);
|
this.remoteUser = remoteUser;
|
||||||
|
loadMessageList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onEditPress() {
|
private void loadMessageList() {
|
||||||
editListeners.forEach(ButtonPressEvent::onPress);
|
ObservableList<Message> messages = FXCollections.observableArrayList(
|
||||||
|
new Message(remoteUser, currentUser, "Messsssage 1"),
|
||||||
|
new Message(remoteUser, currentUser, "Messsssage 2"),
|
||||||
|
new Message(currentUser, remoteUser, "Messsssage 3"),
|
||||||
|
new Message(remoteUser, currentUser, "Messsssage 4")
|
||||||
|
);
|
||||||
|
messageList.setItems(messages);
|
||||||
|
messageList.scrollTo(messageList.getItems().size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(URL url, ResourceBundle rb) {
|
public void initialize(URL url, ResourceBundle rb) {
|
||||||
attachmentListeners = new ArrayList<>();
|
messageList.setItems(null);
|
||||||
sendListeners = new ArrayList<>();
|
messageList.setSelectionModel(new NoSelectionModel<Message>());
|
||||||
editListeners = new ArrayList<>();
|
|
||||||
|
|
||||||
ObservableList<String> messages = FXCollections.observableArrayList(
|
|
||||||
"test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "no", "no", "test", "no", "no", "test", "no", "test");
|
|
||||||
messageList.setItems(messages);
|
|
||||||
messageList.scrollTo(messageList.getItems().size() - 1);
|
|
||||||
messageList.setSelectionModel(new NoSelectionModel());
|
|
||||||
messageList.setCellFactory(listView -> new MessageListItemCell());
|
messageList.setCellFactory(listView -> new MessageListItemCell());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package fr.insa.clavardator.ui.chat;
|
||||||
|
|
||||||
|
import fr.insa.clavardator.ui.ButtonPressEvent;
|
||||||
|
import javafx.fxml.Initializable;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public class ChatFooterController implements Initializable {
|
||||||
|
|
||||||
|
private List<ButtonPressEvent> attachmentListeners;
|
||||||
|
private List<ButtonPressEvent> sendListeners;
|
||||||
|
|
||||||
|
public void addAttachmentListener(ButtonPressEvent listener) {
|
||||||
|
attachmentListeners.add(listener);
|
||||||
|
}
|
||||||
|
public void addSendListener(ButtonPressEvent listener) {
|
||||||
|
sendListeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onAttachmentPress() {
|
||||||
|
attachmentListeners.forEach(ButtonPressEvent::onPress);
|
||||||
|
}
|
||||||
|
public void onSendPress() {
|
||||||
|
sendListeners.forEach(ButtonPressEvent::onPress);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
|
attachmentListeners = new ArrayList<>();
|
||||||
|
sendListeners = new ArrayList<>();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package fr.insa.clavardator.ui.chat;
|
||||||
|
|
||||||
|
import fr.insa.clavardator.ui.ButtonPressEvent;
|
||||||
|
import javafx.fxml.Initializable;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public class ChatHeaderController implements Initializable {
|
||||||
|
|
||||||
|
private List<ButtonPressEvent> editListeners;
|
||||||
|
|
||||||
|
public void addEditListener(ButtonPressEvent listener) {
|
||||||
|
editListeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onEditPress() {
|
||||||
|
editListeners.forEach(ButtonPressEvent::onPress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onAboutPress() {
|
||||||
|
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
|
editListeners = new ArrayList<>();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,22 +1,26 @@
|
||||||
package fr.insa.clavardator.ui.chat;
|
package fr.insa.clavardator.ui.chat;
|
||||||
|
|
||||||
|
import fr.insa.clavardator.chat.Message;
|
||||||
|
import fr.insa.clavardator.users.CurrentUser;
|
||||||
|
import fr.insa.clavardator.users.User;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.control.ListCell;
|
import javafx.scene.control.ListCell;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class MessageListItemCell extends ListCell<String> {
|
public class MessageListItemCell extends ListCell<Message> {
|
||||||
|
|
||||||
public MessageListItemCell() {
|
public MessageListItemCell() {
|
||||||
setStyle("-fx-padding: 0px");
|
setStyle("-fx-padding: 0px");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void updateItem(String item, boolean empty) {
|
protected void updateItem(Message item, boolean empty) {
|
||||||
super.updateItem(item, empty);
|
super.updateItem(item, empty);
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
FXMLLoader cellLoader;
|
FXMLLoader cellLoader;
|
||||||
if (item.equals("test")) {
|
final User sender = item.getSender();
|
||||||
|
if (sender instanceof CurrentUser) {
|
||||||
cellLoader = new FXMLLoader(getClass().getResource("messageListItemSelf.fxml"));
|
cellLoader = new FXMLLoader(getClass().getResource("messageListItemSelf.fxml"));
|
||||||
} else {
|
} else {
|
||||||
cellLoader = new FXMLLoader(getClass().getResource("messageListItemOther.fxml"));
|
cellLoader = new FXMLLoader(getClass().getResource("messageListItemOther.fxml"));
|
||||||
|
@ -24,7 +28,7 @@ public class MessageListItemCell extends ListCell<String> {
|
||||||
try {
|
try {
|
||||||
setGraphic(cellLoader.load());
|
setGraphic(cellLoader.load());
|
||||||
final MessageListItemController userListItemController = cellLoader.getController();
|
final MessageListItemController userListItemController = cellLoader.getController();
|
||||||
userListItemController.setMessage(item);
|
userListItemController.setMessage(item.getText());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,58 +1,15 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<?import com.jfoenix.controls.*?>
|
|
||||||
<?import javafx.geometry.Insets?>
|
|
||||||
<?import javafx.scene.control.Label?>
|
|
||||||
<?import javafx.scene.control.ListView?>
|
<?import javafx.scene.control.ListView?>
|
||||||
<?import javafx.scene.layout.*?>
|
<?import javafx.scene.layout.*?>
|
||||||
<?import javafx.scene.shape.Circle?>
|
<!--suppress JavaFxUnresolvedFxIdReference -->
|
||||||
<?import org.kordamp.ikonli.javafx.FontIcon?>
|
|
||||||
<AnchorPane xmlns="http://javafx.com/javafx/11.0.1"
|
<AnchorPane xmlns="http://javafx.com/javafx/11.0.1"
|
||||||
xmlns:fx="http://javafx.com/fxml/1" fx:controller="fr.insa.clavardator.ui.chat.ChatController"
|
xmlns:fx="http://javafx.com/fxml/1" fx:controller="fr.insa.clavardator.ui.chat.ChatController"
|
||||||
stylesheets="@../styles.css" styleClass="container">
|
stylesheets="@../styles.css" styleClass="container">
|
||||||
<VBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
|
<VBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
|
||||||
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
|
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
|
||||||
<HBox alignment="CENTER_LEFT" prefHeight="64.0" spacing="10">
|
<fx:include source="chatHeader.fxml" fx:id="chatHeader"/>
|
||||||
<padding>
|
|
||||||
<Insets left="20" right="20"/>
|
|
||||||
</padding>
|
|
||||||
<Label text="Connecté en tant que : "/>
|
|
||||||
<Label text="<USERNAME>"/>
|
|
||||||
<JFXButton mnemonicParsing="false" onMouseClicked="#onEditPress">
|
|
||||||
<graphic>
|
|
||||||
<FontIcon iconLiteral="fas-user-edit" iconSize="24"/>
|
|
||||||
</graphic>
|
|
||||||
</JFXButton>
|
|
||||||
<Pane HBox.hgrow="ALWAYS"/>
|
|
||||||
<JFXButton mnemonicParsing="false" onMouseClicked="#onEditPress">
|
|
||||||
<graphic>
|
|
||||||
<FontIcon iconLiteral="fas-info" iconSize="24"/>
|
|
||||||
</graphic>
|
|
||||||
</JFXButton>
|
|
||||||
</HBox>
|
|
||||||
<HBox alignment="CENTER_LEFT" spacing="5" prefHeight="45">
|
|
||||||
<padding>
|
|
||||||
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
|
|
||||||
</padding>
|
|
||||||
<Circle radius="7.0" styleClass="active-user-dot"/>
|
|
||||||
<Label text="USER NAME" styleClass="current-user"/>
|
|
||||||
</HBox>
|
|
||||||
<ListView fx:id="messageList" VBox.vgrow="ALWAYS"/>
|
<ListView fx:id="messageList" VBox.vgrow="ALWAYS"/>
|
||||||
<HBox alignment="CENTER" spacing="10.0">
|
<fx:include source="chatFooter.fxml" fx:id="chatFooter"/>
|
||||||
<padding>
|
|
||||||
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
|
|
||||||
</padding>
|
|
||||||
<JFXTextField HBox.hgrow="ALWAYS"/>
|
|
||||||
<JFXButton mnemonicParsing="false" onMouseClicked="#onAttachmentPress">
|
|
||||||
<graphic>
|
|
||||||
<FontIcon iconLiteral="fas-paperclip" iconSize="24"/>
|
|
||||||
</graphic>
|
|
||||||
</JFXButton>
|
|
||||||
<JFXButton mnemonicParsing="false" text="Envoyer" onMouseClicked="#onSendPress">
|
|
||||||
<graphic>
|
|
||||||
<FontIcon iconLiteral="fas-paper-plane" iconSize="24"/>
|
|
||||||
</graphic>
|
|
||||||
</JFXButton>
|
|
||||||
</HBox>
|
|
||||||
</VBox>
|
</VBox>
|
||||||
</AnchorPane>
|
</AnchorPane>
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import com.jfoenix.controls.JFXButton?>
|
||||||
|
<?import com.jfoenix.controls.JFXTextField?>
|
||||||
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.layout.HBox?>
|
||||||
|
<?import org.kordamp.ikonli.javafx.FontIcon?>
|
||||||
|
<HBox xmlns="http://javafx.com/javafx"
|
||||||
|
xmlns:fx="http://javafx.com/fxml"
|
||||||
|
fx:controller="fr.insa.clavardator.ui.chat.ChatFooterController"
|
||||||
|
stylesheets="@../styles.css" styleClass="container" alignment="CENTER" spacing="10.0">
|
||||||
|
<padding>
|
||||||
|
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
|
||||||
|
</padding>
|
||||||
|
<JFXTextField HBox.hgrow="ALWAYS"/>
|
||||||
|
<JFXButton mnemonicParsing="false" onMouseClicked="#onAttachmentPress">
|
||||||
|
<graphic>
|
||||||
|
<FontIcon iconLiteral="fas-paperclip" iconSize="24"/>
|
||||||
|
</graphic>
|
||||||
|
</JFXButton>
|
||||||
|
<JFXButton mnemonicParsing="false" text="Envoyer" onMouseClicked="#onSendPress">
|
||||||
|
<graphic>
|
||||||
|
<FontIcon iconLiteral="fas-paper-plane" iconSize="24"/>
|
||||||
|
</graphic>
|
||||||
|
</JFXButton>
|
||||||
|
</HBox>
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import com.jfoenix.controls.JFXButton?>
|
||||||
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.control.Label?>
|
||||||
|
<?import javafx.scene.layout.*?>
|
||||||
|
<?import javafx.scene.shape.Circle?>
|
||||||
|
<?import org.kordamp.ikonli.javafx.FontIcon?>
|
||||||
|
<VBox xmlns="http://javafx.com/javafx"
|
||||||
|
xmlns:fx="http://javafx.com/fxml"
|
||||||
|
fx:controller="fr.insa.clavardator.ui.chat.ChatHeaderController"
|
||||||
|
stylesheets="@../styles.css" styleClass="container">
|
||||||
|
<HBox alignment="CENTER_LEFT" prefHeight="64.0" spacing="10">
|
||||||
|
<padding>
|
||||||
|
<Insets left="20" right="20"/>
|
||||||
|
</padding>
|
||||||
|
<Label text="Connecté en tant que : "/>
|
||||||
|
<Label text="<USERNAME>"/>
|
||||||
|
<JFXButton mnemonicParsing="false" onMouseClicked="#onEditPress">
|
||||||
|
<graphic>
|
||||||
|
<FontIcon iconLiteral="fas-user-edit" iconSize="24"/>
|
||||||
|
</graphic>
|
||||||
|
</JFXButton>
|
||||||
|
<Pane HBox.hgrow="ALWAYS"/>
|
||||||
|
<JFXButton mnemonicParsing="false" onMouseClicked="#onAboutPress">
|
||||||
|
<graphic>
|
||||||
|
<FontIcon iconLiteral="fas-info" iconSize="24"/>
|
||||||
|
</graphic>
|
||||||
|
</JFXButton>
|
||||||
|
</HBox>
|
||||||
|
<HBox alignment="CENTER_LEFT" spacing="5" prefHeight="45">
|
||||||
|
<padding>
|
||||||
|
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
|
||||||
|
</padding>
|
||||||
|
<Circle radius="7.0" styleClass="active-user-dot"/>
|
||||||
|
<Label text="USER NAME" styleClass="current-user"/>
|
||||||
|
</HBox>
|
||||||
|
</VBox>
|
Loading…
Reference in a new issue