Make State SimpleObjectProperty in ClientConnectionHandler.java
Add changelistener to stateproperty in ChatWindowController.java fixes Issue #21
This commit is contained in:
parent
029e099f26
commit
f2945b3075
|
@ -4,6 +4,8 @@ import ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State;
|
||||||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
|
import javafx.beans.value.ChangeListener;
|
||||||
|
import javafx.beans.value.ObservableValue;
|
||||||
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;
|
||||||
|
@ -40,7 +42,6 @@ public class ChatWindowController {
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName());
|
serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName());
|
||||||
serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT));
|
serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT));
|
||||||
stateChanged(NEW);
|
|
||||||
messages = new ClientMessageList(this);
|
messages = new ClientMessageList(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +51,7 @@ public class ChatWindowController {
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void toggleConnection () {
|
private void toggleConnection () {
|
||||||
if (connectionHandler == null || connectionHandler.getState() != CONNECTED) {
|
if (connectionHandler == null || connectionHandler.getStateObjectProperty().get() != CONNECTED) {
|
||||||
connect();
|
connect();
|
||||||
} else {
|
} else {
|
||||||
disconnect();
|
disconnect();
|
||||||
|
@ -117,6 +118,9 @@ public class ChatWindowController {
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
|
//register changelistener
|
||||||
|
startChangeListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void terminateConnectionHandler() {
|
private void terminateConnectionHandler() {
|
||||||
|
@ -141,6 +145,16 @@ public class ChatWindowController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void startChangeListener() {
|
||||||
|
connectionHandler.getStateObjectProperty().addListener(new ChangeListener<State>() {
|
||||||
|
@Override
|
||||||
|
public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
|
||||||
|
stateChanged(newValue);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
public void setUserName(String userName) {
|
||||||
Platform.runLater(new Runnable() {
|
Platform.runLater(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -211,8 +225,4 @@ public class ChatWindowController {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package ch.zhaw.pm2.multichat.client;
|
||||||
|
|
||||||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
|
import javafx.beans.property.ObjectProperty;
|
||||||
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -25,7 +27,7 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
public static final String USER_ALL = "*";
|
public static final String USER_ALL = "*";
|
||||||
|
|
||||||
private String userName = USER_NONE;
|
private String userName = USER_NONE;
|
||||||
private State state = NEW;
|
private ObjectProperty<State> stateObjectProperty;
|
||||||
|
|
||||||
enum State {
|
enum State {
|
||||||
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
|
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
|
||||||
|
@ -37,15 +39,16 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.userName = (userName == null || userName.isBlank())? USER_NONE : userName;
|
this.userName = (userName == null || userName.isBlank())? USER_NONE : userName;
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
|
this.stateObjectProperty = new SimpleObjectProperty<>(NEW);
|
||||||
}
|
}
|
||||||
|
|
||||||
public State getState() {
|
|
||||||
return this.state;
|
public ObjectProperty<State> getStateObjectProperty() {
|
||||||
|
return stateObjectProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setState (State newState) {
|
public void setState (State newState) {
|
||||||
this.state = newState;
|
this.stateObjectProperty.set(newState);
|
||||||
controller.stateChanged(newState);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run () {
|
public void run () {
|
||||||
|
@ -119,7 +122,7 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
if (type.equals(DATA_TYPE_CONNECT)) {
|
if (type.equals(DATA_TYPE_CONNECT)) {
|
||||||
System.err.println("Illegal connect request from server");
|
System.err.println("Illegal connect request from server");
|
||||||
} else if (type.equals(DATA_TYPE_CONFIRM)) {
|
} else if (type.equals(DATA_TYPE_CONFIRM)) {
|
||||||
if (state == CONFIRM_CONNECT) {
|
if (stateObjectProperty.get() == CONFIRM_CONNECT) {
|
||||||
this.userName = reciever;
|
this.userName = reciever;
|
||||||
controller.setUserName(userName);
|
controller.setUserName(userName);
|
||||||
controller.setServerPort(connection.getRemotePort());
|
controller.setServerPort(connection.getRemotePort());
|
||||||
|
@ -127,7 +130,7 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
controller.addInfo(payload);
|
controller.addInfo(payload);
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + payload);
|
||||||
this.setState(CONNECTED);
|
this.setState(CONNECTED);
|
||||||
} else if (state == CONFIRM_DISCONNECT) {
|
} else if (stateObjectProperty.get() == CONFIRM_DISCONNECT) {
|
||||||
controller.addInfo(payload);
|
controller.addInfo(payload);
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + payload);
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
|
@ -135,7 +138,7 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
System.err.println("Got unexpected confirm message: " + payload);
|
System.err.println("Got unexpected confirm message: " + payload);
|
||||||
}
|
}
|
||||||
} else if (type.equals(DATA_TYPE_DISCONNECT)) {
|
} else if (type.equals(DATA_TYPE_DISCONNECT)) {
|
||||||
if (state == DISCONNECTED) {
|
if (stateObjectProperty.get() == DISCONNECTED) {
|
||||||
System.out.println("DISCONNECT: Already in disconnected: " + payload);
|
System.out.println("DISCONNECT: Already in disconnected: " + payload);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -143,8 +146,8 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
System.out.println("DISCONNECT: " + payload);
|
System.out.println("DISCONNECT: " + payload);
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
} else if (type.equals(DATA_TYPE_MESSAGE)) {
|
} else if (type.equals(DATA_TYPE_MESSAGE)) {
|
||||||
if (state != CONNECTED) {
|
if (stateObjectProperty.get() != CONNECTED) {
|
||||||
System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload);
|
System.out.println("MESSAGE: Illegal state " + stateObjectProperty.get() + " for message: " + payload);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
controller.addMessage(sender, reciever, payload);
|
controller.addMessage(sender, reciever, payload);
|
||||||
|
@ -183,19 +186,19 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect() throws ChatProtocolException {
|
public void connect() throws ChatProtocolException {
|
||||||
if (state != NEW) throw new ChatProtocolException("Illegal state for connect: " + state);
|
if (stateObjectProperty.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + stateObjectProperty.get());
|
||||||
this.sendData(userName, USER_NONE, DATA_TYPE_CONNECT,null);
|
this.sendData(userName, USER_NONE, DATA_TYPE_CONNECT,null);
|
||||||
this.setState(CONFIRM_CONNECT);
|
this.setState(CONFIRM_CONNECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() throws ChatProtocolException {
|
public void disconnect() throws ChatProtocolException {
|
||||||
if (state != NEW && state != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + state);
|
if (stateObjectProperty.get() != NEW && stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + stateObjectProperty.get());
|
||||||
this.sendData(userName, USER_NONE, DATA_TYPE_DISCONNECT,null);
|
this.sendData(userName, USER_NONE, DATA_TYPE_DISCONNECT,null);
|
||||||
this.setState(CONFIRM_DISCONNECT);
|
this.setState(CONFIRM_DISCONNECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void message(String receiver, String message) throws ChatProtocolException {
|
public void message(String receiver, String message) throws ChatProtocolException {
|
||||||
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state);
|
if (stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + stateObjectProperty.get());
|
||||||
this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message);
|
this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue