Merge pull request #43 from PM2-IT21bWIN-ruiz-mach-krea/refactoring_chatwindowcontroller

Refactoring chatwindowcontroller to improve MVC
This commit is contained in:
Leonardo Brandenberger 2022-04-15 15:39:47 +02:00 committed by GitHub Enterprise
commit 313a4f5861
2 changed files with 13 additions and 21 deletions

View File

@ -2,8 +2,6 @@ package ch.zhaw.pm2.multichat.client;
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State; import ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State;
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
@ -36,12 +34,6 @@ public class ChatWindowController {
@FXML private TextField filterValue; @FXML private TextField filterValue;
@FXML
public void initialize() {
serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName());
serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT));
}
public void setMessages(ClientMessageList messages) { public void setMessages(ClientMessageList messages) {
this.messages = messages; this.messages = messages;
messageListener(); messageListener();
@ -49,6 +41,9 @@ public class ChatWindowController {
public void setConnectionHandler(ClientConnectionHandler connectionHandler){ public void setConnectionHandler(ClientConnectionHandler connectionHandler){
this.connectionHandler = connectionHandler; this.connectionHandler = connectionHandler;
startConnectionHandlerListener();
serverAddressField.setText(connectionHandler.getServerAddressProperty().get());
serverPortField.setText(String.valueOf(connectionHandler.getServerPortProperty().get()));
} }
private void applicationClose() { private void applicationClose() {
@ -104,7 +99,7 @@ public class ChatWindowController {
@FXML @FXML
private void applyFilter( ) { private void applyFilter( ) {
this.redrawMessageList(); Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip())));
} }
private void startConnectionHandler() throws IOException { private void startConnectionHandler() throws IOException {
@ -116,7 +111,7 @@ public class ChatWindowController {
new Thread(connectionHandler).start(); new Thread(connectionHandler).start();
//register Listener //register Listener
startListener(); //startConnectionHandlerListener();
// register window close handler // register window close handler
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
@ -169,19 +164,13 @@ public class ChatWindowController {
messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message)); messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message));
} }
private void redrawMessageList() {
this.messageArea.clear();
Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip())));
}
class WindowCloseHandler implements EventHandler<WindowEvent> { class WindowCloseHandler implements EventHandler<WindowEvent> {
public void handle(WindowEvent event) { public void handle(WindowEvent event) {
applicationClose(); applicationClose();
} }
} }
public void startListener() { public void startConnectionHandlerListener() {
connectionHandler.getStateProperty().addListener(new ChangeListener<State>() { connectionHandler.getStateProperty().addListener(new ChangeListener<State>() {
@Override @Override
public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) { public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
@ -215,7 +204,7 @@ public class ChatWindowController {
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() { messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
@Override @Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
redrawMessageList(); Platform.runLater(() -> messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip())));
} }
}); });
} }

View File

@ -29,12 +29,15 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
super(); super();
this.messages = messages; this.messages = messages;
state = new SimpleObjectProperty<>(State.NEW); state = new SimpleObjectProperty<>(State.NEW);
serverAddress = new SimpleStringProperty(); serverAddress = new SimpleStringProperty(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName());
serverPort = new SimpleIntegerProperty(); serverPort = new SimpleIntegerProperty(NetworkHandler.DEFAULT_PORT);
this.userName = new SimpleStringProperty(null);
} }
public void initialize(String serverAddress, int serverPort, String userName) throws IOException { public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
state = new SimpleObjectProperty<>(NEW); state.set(NEW);
this.serverAddress.set(serverAddress);
this.serverPort.set(serverPort);
setConnection(NetworkHandler.openConnection(serverAddress, serverPort)); setConnection(NetworkHandler.openConnection(serverAddress, serverPort));
this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName); this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
} }