Extending functionality to connect the incoming msgs to the gui via buffered readers.

This commit is contained in:
Sebastian Lenzlinger 2022-04-16 16:05:21 +02:00
parent 504aa9aef2
commit 0270202c3f
7 changed files with 105 additions and 49 deletions

View File

@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client; package ch.unibas.dmi.dbis.cs108.multiplayer.client;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatApp;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
@ -26,6 +27,9 @@ public class Client {
private BufferedWriter out; private BufferedWriter out;
public ClientPinger clientPinger; public ClientPinger clientPinger;
private BufferedWriter toChatGui;
private ChatApp chatApp;
/** /**
* Saves the position of the client, gets refreshed everytime the client gets a vote request. * Saves the position of the client, gets refreshed everytime the client gets a vote request.
*/ */
@ -242,4 +246,7 @@ public class Client {
public BufferedWriter getOut() { public BufferedWriter getOut() {
return out; return out;
} }
public void sendToChat(String substring) {
}
} }

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import java.io.OutputStreamWriter;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -41,7 +42,7 @@ public class JClientProtocolParser {
break; break;
case Protocol.printToClientChat: case Protocol.printToClientChat:
//todo: handle chat separately from console. //todo: handle chat separately from console.
System.out.println(msg.substring(6)); c.sendToChat(msg.substring(6));
break; break;
case Protocol.serverConfirmQuit: case Protocol.serverConfirmQuit:
c.disconnectFromServer(); c.disconnectFromServer();

View File

@ -6,6 +6,8 @@ public class ClientModel {
private String username; private String username;
private Client client; private Client client;
private String incomingChatMsg;
//private Number; //private Number;
public Client getClient() { public Client getClient() {

View File

@ -1,9 +1,19 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat; package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
import javafx.application.Application; import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage; import javafx.stage.Stage;
public class ChatApp extends Application { public class ChatApp extends Application {
ClientModel clientModel;
ChatController chatController;
public ChatApp(ClientModel clientModel) {
this.clientModel = clientModel;
this.chatController = new ChatController(clientModel);
}
/** /**
* The main entry point for all JavaFX applications. The start method is called after the init * The main entry point for all JavaFX applications. The start method is called after the init
@ -21,5 +31,15 @@ public class ChatApp extends Application {
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
ChatApp chatApp = new ChatApp(new ClientModel());
AnchorPane root = new AnchorPane(chatController.getChatPaneRoot());
// TODO bin chatController.getChatPaneRoot() border to root border for rezising
Scene scene = new Scene(root);
primaryStage.setResizable(true);
primaryStage.setTitle("Chat");
primaryStage.setScene(scene);
primaryStage.show();
} }
} }

View File

@ -10,10 +10,13 @@ import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker; import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label; import javafx.scene.control.Label;
@ -41,18 +44,17 @@ public class ChatController implements Initializable {
private ClientModel client; private ClientModel client;
private SimpleBooleanProperty whisperTargetChosen; private SimpleBooleanProperty whisperTargetChosen;
private SimpleStringProperty cmd; private String cmd;
private static final String whisper = Protocol.whisper; private static final String whisper = Protocol.whisper;
private static final String chatToAll = Protocol.chatMsgToAll; private static final String chatToAll = Protocol.chatMsgToAll;
private static final String chatToLobby = Protocol.chatMsgToLobby; private static final String chatToLobby = Protocol.chatMsgToLobby;
public ChatController(ClientModel client) { public ChatController(ClientModel client) {
this.client = client; this.client = client;
whisperTargetChosen = new SimpleBooleanProperty(); whisperTargetChosen = new SimpleBooleanProperty();
cmd = new SimpleStringProperty(); cmd = "";
} }
@ -68,6 +70,12 @@ public class ChatController implements Initializable {
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
vBoxChatMessages.getChildren().addListener(new ListChangeListener<Node>() {
@Override
public void onChanged(Change<? extends Node> c) {
vBoxChatMessages.setMaxHeight(vBoxChatMessages.getMaxHeight()*2);
}
});
vBoxChatMessages.heightProperty().addListener(new ChangeListener<Number>() { vBoxChatMessages.heightProperty().addListener(new ChangeListener<Number>() {
/** /**
@ -84,11 +92,11 @@ public class ChatController implements Initializable {
} }
}); });
/**
* Initialize what heppens when the sen button is pressed
*/
sendButton.setOnAction(new EventHandler<ActionEvent>() { sendButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* control what to do when the "SendButton" is pressed
* @param event
*/
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
String msg = chatMsgField.getText(); String msg = chatMsgField.getText();
@ -101,6 +109,10 @@ public class ChatController implements Initializable {
} }
} }
}); });
/**
* Initialize the change of the TextArea field holding potential chat messages
*/
chatMsgField.textProperty().addListener(new ChangeListener<String>() { chatMsgField.textProperty().addListener(new ChangeListener<String>() {
@Override @Override
public void changed(ObservableValue<? extends String> observable, String oldValue, public void changed(ObservableValue<? extends String> observable, String oldValue,
@ -109,8 +121,25 @@ public class ChatController implements Initializable {
} }
}); });
//Bind the the fact if the whisper field contains a name to a boolean
whisperTargetChosen.bind(whisperTargetSelectField.textProperty().isEmpty()); whisperTargetChosen.bind(whisperTargetSelectField.textProperty().isEmpty());
/**
* change the chat command based on the whisper text field.
*/
whisperTargetChosen.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue,
Boolean newValue) {
//is true if {@code whisperTargetSelectedField} has content
if(!newValue) {
cmd = whisper + "$";
} else {
cmd = chatToLobby + "$";
}
}
});
whisperTargetSelectField.textProperty().addListener(new ChangeListener<String>() { whisperTargetSelectField.textProperty().addListener(new ChangeListener<String>() {
@Override @Override
public void changed(ObservableValue<? extends String> observable, String oldValue, public void changed(ObservableValue<? extends String> observable, String oldValue,
@ -137,4 +166,11 @@ public class ChatController implements Initializable {
public SplitPane getChatPaneRoot() { public SplitPane getChatPaneRoot() {
return chatPaneRoot; return chatPaneRoot;
} }
public void addChatMsgToView(String msg) {
Label l = new Label(msg);
l.setBackground(Background.fill(Color.LIGHTSKYBLUE));
l.setTextFill(Color.BLACK);
vBoxChatMessages.getChildren().add(l);
}
} }

View File

@ -0,0 +1,31 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat;
import java.io.BufferedReader;
import java.io.BufferedWriter;
/**
* To get the chat messages to the chat window.
*/
public class ChatIncomingThread implements Runnable {
BufferedReader inputReader;
ChatApp chatApp;
public ChatIncomingThread(BufferedReader inputReader, ChatApp chatApp) {
this.inputReader = inputReader;
this.chatApp = chatApp;
}
/**
* When an object implementing interface {@code Runnable} is used to create a thread, starting the
* thread causes the object's {@code run} method to be called in that separately executing
* thread.
* <p>
* The general contract of the method {@code run} is that it may take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
}
}

View File

@ -1,41 +0,0 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
public class outChatCmd implements ChangeListener {
private String cmd;
private static final Protocol prtcl = new Protocol();
private static final String whisper = Protocol.whisper;
private static final String chatToAll = Protocol.chatMsgToAll;
private static final String chatToLobby = Protocol.chatMsgToLobby
public outChatCmd(String cmd, String parameters) {
this.cmd = cmd;
this.parameters = parameters;
}
public String getCmd() {
return cmd;
}
public String getParameters() {
return parameters;
}
/**
* Called when the value of an {@link ObservableValue} changes.
* <p>
* In general, it is considered bad practice to modify the observed value in this method.
*
* @param observable The {@code ObservableValue} which value changed
* @param oldValue The old value
* @param newValue
*/
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
}
}