worked through a few warnings, changed a few anonymous classes to lambdas

This commit is contained in:
Sebastian Lenzlinger 2022-04-17 11:35:16 +02:00
parent 4bd9038146
commit 660883f732
3 changed files with 20 additions and 36 deletions

View File

@ -6,7 +6,6 @@ public class ClientModel {
private String username; private String username;
private Client client; private Client client;
private String incomingChatMsg;
public ClientModel(String username, Client client) { public ClientModel(String username, Client client) {
this.username = username; this.username = username;

View File

@ -20,6 +20,6 @@ public class GUI implements Runnable{
*/ */
@Override @Override
public void run() { public void run() {
Application.launch(this.chatApp.getClass()); Application.launch(ChatApp.class);
} }
} }

View File

@ -1,25 +1,21 @@
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.Client;
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.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
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.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.Node;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.SplitPane; import javafx.scene.control.SplitPane;
import javafx.scene.control.TextArea; import javafx.scene.control.TextArea;
@ -27,7 +23,6 @@ import javafx.scene.control.TextField;
import javafx.scene.layout.Background; import javafx.scene.layout.Background;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
public class ChatController implements Initializable { public class ChatController implements Initializable {
@ -56,6 +51,7 @@ public class ChatController implements Initializable {
whisperTargetChosen = new SimpleBooleanProperty(); whisperTargetChosen = new SimpleBooleanProperty();
cmd = ""; cmd = "";
} }
public ChatController(ClientModel client) { public ChatController(ClientModel client) {
this.client = client; this.client = client;
whisperTargetChosen = new SimpleBooleanProperty(); whisperTargetChosen = new SimpleBooleanProperty();
@ -74,12 +70,9 @@ public class ChatController implements Initializable {
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
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<Number>() {
/** /**
@ -97,34 +90,26 @@ public class ChatController implements Initializable {
}); });
/** /**
* Initialize what heppens when the sen button is pressed * Initialize what happens when the send button is pressed
*/ */
sendButton.setOnAction(new EventHandler<ActionEvent>() { sendButton.setOnAction(event -> {
@Override String msg = chatMsgField.getText();
public void handle(ActionEvent event) { if (!msg.isEmpty()) {
String msg = chatMsgField.getText(); client.getClient().sendMsgToServer(cmd.toString() + msg);
if (!msg.isEmpty()) { Label l = new Label(client.getUsername() + " (you): " + msg);
client.getClient().sendMsgToServer(cmd.toString() + msg); l.setBackground(Background.fill(Color.LAVENDER));
Label l = new Label(client.getUsername() + " (you): " + msg); vBoxChatMessages.getChildren().add(l);
l.setBackground(Background.fill(Color.LAVENDER)); chatMsgField.clear();
vBoxChatMessages.getChildren().add(l);
chatMsgField.clear();
}
} }
}); });
/** /**
* 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);
}
});
//Bind the the fact if the whisper field contains a name to a boolean //Bind the fact if the whisper field contains a name to a boolean
whisperTargetChosen.bind(whisperTargetSelectField.textProperty().isEmpty()); whisperTargetChosen.bind(whisperTargetSelectField.textProperty().isEmpty());
/** /**
@ -153,7 +138,7 @@ public class ChatController implements Initializable {
} }
/** /**
* @return the client who's chat controller this is * @return the client whose chat controller this is
*/ */
public ClientModel getClient() { public ClientModel getClient() {
return client; return client;
@ -171,7 +156,7 @@ public class ChatController implements Initializable {
} }
/** /**
* The client calls this method to foreward a chat message to the chat gui * The client calls this method to forward a chat message to the chat gui
* *
* @param msg the message to be displayed * @param msg the message to be displayed
*/ */