trying to fix MVC

This commit is contained in:
Leonardo Brandenberger 2022-04-15 04:57:46 +02:00
parent a75d3466ef
commit 28376f1242
2 changed files with 60 additions and 18 deletions

View File

@ -7,6 +7,7 @@ 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;
import javafx.collections.FXCollections;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
@ -25,21 +26,33 @@ public class ChatWindowController {
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler(); private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
@FXML private Pane rootPane; @FXML
@FXML private TextField serverAddressField; private Pane rootPane;
@FXML private TextField serverPortField; @FXML
@FXML private TextField userNameField; private TextField serverAddressField;
@FXML private TextField messageField; @FXML
@FXML private TextArea messageArea; private TextField serverPortField;
@FXML private Button connectButton; @FXML
@FXML private Button sendButton; private TextField userNameField;
@FXML private TextField filterValue; @FXML
private TextField messageField;
@FXML
private TextArea messageArea;
@FXML
private Button connectButton;
@FXML
private Button sendButton;
@FXML
private TextField filterValue;
private ChatWindowModel model = new ChatWindowModel();
@FXML @FXML
public void initialize() { public void initialize() {
serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverAddressField.setText(model.getServerAddress()); // binding server address to view
serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); model.setServerAddress(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); // sets modell adress
serverPortField.setText(model.getServerPort());
model.setServerPort(String.valueOf(NetworkHandler.DEFAULT_PORT));
} }
public void setMessages(ClientMessageList messages) { public void setMessages(ClientMessageList messages) {
@ -47,7 +60,7 @@ public class ChatWindowController {
messageListener(); messageListener();
} }
public void setConnectionHandler(ClientConnectionHandler connectionHandler){ public void setConnectionHandler(ClientConnectionHandler connectionHandler) {
this.connectionHandler = connectionHandler; this.connectionHandler = connectionHandler;
} }
@ -56,7 +69,7 @@ public class ChatWindowController {
} }
@FXML @FXML
private void toggleConnection () { private void toggleConnection() {
if (connectionHandler == null || connectionHandler.getStateProperty().get() != CONNECTED) { if (connectionHandler == null || connectionHandler.getStateProperty().get() != CONNECTED) {
connect(); connect();
} else { } else {
@ -69,7 +82,7 @@ public class ChatWindowController {
messages.clear(); // clear message list messages.clear(); // clear message list
startConnectionHandler(); startConnectionHandler();
connectionHandler.connect(); connectionHandler.connect();
} catch(ChatProtocolException | IOException e) { } catch (ChatProtocolException | IOException e) {
addError(e.getMessage()); addError(e.getMessage());
} }
} }
@ -103,13 +116,13 @@ public class ChatWindowController {
} }
@FXML @FXML
private void applyFilter( ) { private void applyFilter() {
this.redrawMessageList(); this.redrawMessageList();
} }
private void startConnectionHandler() throws IOException { private void startConnectionHandler() throws IOException {
String userName = userNameField.getText(); String userName = userNameField.getText();
if(!userName.contains(" ")) { if (!userName.contains(" ")) {
String serverAddress = serverAddressField.getText(); String serverAddress = serverAddressField.getText();
int serverPort = Integer.parseInt(serverPortField.getText()); int serverPort = Integer.parseInt(serverPortField.getText());
connectionHandler.initialize(serverAddress, serverPort, userName); connectionHandler.initialize(serverAddress, serverPort, userName);
@ -133,7 +146,7 @@ public class ChatWindowController {
connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect"); connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect");
} }
}); });
if(newState == DISCONNECTED){ if (newState == DISCONNECTED) {
connectionHandler.stopReceiving(); connectionHandler.stopReceiving();
} }
} }

View File

@ -0,0 +1,29 @@
package ch.zhaw.pm2.multichat.client;
import javafx.beans.value.ObservableStringValue;
public class ChatWindowModel {
private ClientConnectionHandler connectionHandler;
private ClientMessageList messages;
ObservableStringValue serverAddress;
ObservableStringValue serverPort;
public String getServerAddress() {
return serverAddress.get();
}
public void setServerAddress(String address) {
setServerAddress(address);
}
public String getServerPort() {
return getServerPort();
}
public void setServerPort(String address) {
setServerPort(address);
}
}