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