Cleaned up ChatController a bit and added a chat label configurator classed used by the chat controller.

This commit is contained in:
Sebastian Lenzlinger 2022-04-30 13:48:21 +02:00
parent fc737012c7
commit d2907aa8ac
2 changed files with 51 additions and 51 deletions

View File

@ -3,9 +3,8 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.utils.ChatLabelConfigurator;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import com.sun.javafx.scene.control.Properties;
import com.sun.javafx.scene.control.inputmap.KeyBinding;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.application.Platform; import javafx.application.Platform;
@ -22,18 +21,13 @@ import javafx.scene.Node;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background; import javafx.scene.layout.Background;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -99,20 +93,14 @@ public class ChatController implements Initializable {
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
setClient(ChatApp.getClientModel()); setClient(ChatApp.getClientModel());
ChatApp.setChatController(this); ChatApp.setChatController(this);
vBoxChatMessages.getChildren().addListener(new ListChangeListener<Node>() { vBoxChatMessages.getChildren().addListener(
@Override (ListChangeListener<Node>) c -> vBoxChatMessages.setMaxHeight(
public void onChanged(Change<? extends Node> c) { vBoxChatMessages.getMaxHeight() * 2));
vBoxChatMessages.setMaxHeight(vBoxChatMessages.getMaxHeight() * 2);
}
});
vBoxChatMessages.heightProperty().addListener(new ChangeListener<Number>() { vBoxChatMessages.heightProperty().addListener(new ChangeListener<>() {
/** /**
* TODO: implement * TODO: implement
* Adjust the hight when new messages come in. * Adjust the height when new messages come in.
* @param observable
* @param oldValue
* @param newValue
*/ */
@Override @Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, public void changed(ObservableValue<? extends Number> observable, Number oldValue,
@ -124,30 +112,15 @@ public class ChatController implements Initializable {
/** /**
* Initialize what happens when the send button is pressed * Initialize what happens when the send button is pressed
*/ */
sendButton.setOnAction(new EventHandler<ActionEvent>() { sendButton.setOnAction(event -> sendChatMsg());
@Override
public void handle(ActionEvent event) {
sendChatMsg();
}
});
chatMsgField.setOnAction(new EventHandler<ActionEvent>() { chatMsgField.setOnAction(event -> sendChatMsg());
@Override
public void handle(ActionEvent event) {
sendChatMsg();
}
});
/** /**
* Initialize the change of the TextArea field holding potential chat messages * Initialize the change of the TextArea field holding potential chat messages
*/ */
chatMsgField.textProperty().addListener(new ChangeListener<String>() { chatMsgField.textProperty().addListener(
@Override (observable, oldValue, newValue) -> chatMsgField.setText(newValue));
public void changed(ObservableValue<? extends String> observable, String oldValue,
String newValue) {
chatMsgField.setText(newValue);
}
});
//Possibly now the whisperTargetChosenProperty is obsolete //Possibly now the whisperTargetChosenProperty is obsolete
whisperTargetSelectField.textProperty().addListener(new ChangeListener<String>() { whisperTargetSelectField.textProperty().addListener(new ChangeListener<String>() {
@ -171,19 +144,10 @@ public class ChatController implements Initializable {
client.getClient().sendMsgToServer(cmd.toString() + msg); client.getClient().sendMsgToServer(cmd.toString() + msg);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg); LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Text t; Text t;
Label l;
if (cmd.startsWith(whisper)) { //Configure what to put in the Users ChatView
t = new Text("You whispered to " + whisperTargetSelectField.getText() + ": " + msg); Label l = ChatLabelConfigurator.configure(cmd,msg,whisperTargetSelectField,client);
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
t = new Text(client.getUsername() + " (you): " + msg);
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER));
l.setWrapText(true);
l.setMaxHeight(Double.MAX_VALUE);
l.setScaleShape(true);
}
vBoxChatMessages.getChildren().add(l); vBoxChatMessages.getChildren().add(l);
chatMsgField.clear(); chatMsgField.clear();
} else { } else {
@ -204,7 +168,7 @@ public class ChatController implements Initializable {
* @param client who's gui controller this should be * @param client who's gui controller this should be
*/ */
public void setClient(ClientModel client) { public void setClient(ClientModel client) {
this.client = client; ChatController.client = client;
} }
public Pane getChatPaneRoot() { public Pane getChatPaneRoot() {

View File

@ -0,0 +1,36 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.utils;
import static ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol.whisper;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
/**
* Provides utilities to configure Labels for the ChatView {@see ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatView.fxml}
* within {@link ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatController}
*/
public class ChatLabelConfigurator {
public static Label configure(String cmd, String msg, TextField whisperTargetSelectField, ClientModel client) {
Label l;
if (cmd.startsWith(whisper)) {
//t = new Text("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
//t = new Text(client.getUsername() + " (you): " + msg);
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER));
l.setWrapText(true);
l.setMaxHeight(Double.MAX_VALUE);
l.setScaleShape(true);
}
return l;
}
}