refactoring of ClientConnectionHandler
Instance is now created in ClientUI.java solved #24 and #15
This commit is contained in:
		
							parent
							
								
									8c588ee75c
								
							
						
					
					
						commit
						615b3844e3
					
				| 
						 | 
					@ -48,6 +48,10 @@ public class ChatWindowController {
 | 
				
			||||||
        messageListener();
 | 
					        messageListener();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setConnectionHandler(ClientConnectionHandler connectionHandler){
 | 
				
			||||||
 | 
					        this.connectionHandler = connectionHandler;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void applicationClose() {
 | 
					    private void applicationClose() {
 | 
				
			||||||
        disconnect();
 | 
					        disconnect();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -108,9 +112,7 @@ public class ChatWindowController {
 | 
				
			||||||
        String userName = userNameField.getText();
 | 
					        String userName = userNameField.getText();
 | 
				
			||||||
        String serverAddress = serverAddressField.getText();
 | 
					        String serverAddress = serverAddressField.getText();
 | 
				
			||||||
        int serverPort = Integer.parseInt(serverPortField.getText());
 | 
					        int serverPort = Integer.parseInt(serverPortField.getText());
 | 
				
			||||||
        connectionHandler = new ClientConnectionHandler(
 | 
					        connectionHandler.initialize(serverAddress, serverPort, userName);
 | 
				
			||||||
            NetworkHandler.openConnection(serverAddress, serverPort), userName,
 | 
					 | 
				
			||||||
            messages);
 | 
					 | 
				
			||||||
        new Thread(connectionHandler).start();
 | 
					        new Thread(connectionHandler).start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //register Listener
 | 
					        //register Listener
 | 
				
			||||||
| 
						 | 
					@ -120,15 +122,6 @@ public class ChatWindowController {
 | 
				
			||||||
        rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
 | 
					        rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void terminateConnectionHandler() {
 | 
					 | 
				
			||||||
        // unregister window close handler
 | 
					 | 
				
			||||||
        rootPane.getScene().getWindow().removeEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
 | 
					 | 
				
			||||||
        if (connectionHandler != null) {
 | 
					 | 
				
			||||||
            connectionHandler.stopReceiving();
 | 
					 | 
				
			||||||
            connectionHandler = null;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void stateChanged(State newState) {
 | 
					    public void stateChanged(State newState) {
 | 
				
			||||||
        // update UI (need to be run in UI thread: see Platform.runLater())
 | 
					        // update UI (need to be run in UI thread: see Platform.runLater())
 | 
				
			||||||
        Platform.runLater(new Runnable() {
 | 
					        Platform.runLater(new Runnable() {
 | 
				
			||||||
| 
						 | 
					@ -137,8 +130,8 @@ 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){
 | 
				
			||||||
            terminateConnectionHandler();
 | 
					            connectionHandler.stopReceiving();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,7 +16,7 @@ import java.util.regex.Pattern;
 | 
				
			||||||
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
 | 
					import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ClientConnectionHandler implements Runnable {
 | 
					public class ClientConnectionHandler implements Runnable {
 | 
				
			||||||
    private final NetworkHandler.NetworkConnection<String> connection;
 | 
					    private NetworkHandler.NetworkConnection<String> connection;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Data types used for the Chat Protocol
 | 
					    // Data types used for the Chat Protocol
 | 
				
			||||||
    private static final String DATA_TYPE_CONNECT = "CONNECT";
 | 
					    private static final String DATA_TYPE_CONNECT = "CONNECT";
 | 
				
			||||||
| 
						 | 
					@ -40,17 +40,19 @@ public class ClientConnectionHandler implements Runnable {
 | 
				
			||||||
        NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
 | 
					        NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public ClientConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
 | 
					    public ClientConnectionHandler(ClientMessageList messages)  {
 | 
				
			||||||
                                   String userName,
 | 
					 | 
				
			||||||
                                   ClientMessageList messages)  {
 | 
					 | 
				
			||||||
        this.connection = connection;
 | 
					 | 
				
			||||||
        this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
 | 
					 | 
				
			||||||
        this.messages = messages;
 | 
					        this.messages = messages;
 | 
				
			||||||
        state = new SimpleObjectProperty<>(NEW);
 | 
					        state = new SimpleObjectProperty<>(NEW);
 | 
				
			||||||
        serverAddress = new SimpleStringProperty();
 | 
					        serverAddress = new SimpleStringProperty();
 | 
				
			||||||
        serverPort = new SimpleIntegerProperty();
 | 
					        serverPort = new SimpleIntegerProperty();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
 | 
				
			||||||
 | 
					        state = new SimpleObjectProperty<>(NEW);
 | 
				
			||||||
 | 
					        this.connection = NetworkHandler.openConnection(serverAddress, serverPort);
 | 
				
			||||||
 | 
					        this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
 | 
					    public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public SimpleIntegerProperty getServerPortProperty() { return serverPort; }
 | 
					    public SimpleIntegerProperty getServerPortProperty() { return serverPort; }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,6 +8,7 @@ import javafx.stage.Stage;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ClientUI extends Application {
 | 
					public class ClientUI extends Application {
 | 
				
			||||||
    private ClientMessageList clientMessageList = new ClientMessageList();
 | 
					    private ClientMessageList clientMessageList = new ClientMessageList();
 | 
				
			||||||
 | 
					    private ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void start(Stage primaryStage) {
 | 
					    public void start(Stage primaryStage) {
 | 
				
			||||||
| 
						 | 
					@ -21,6 +22,7 @@ public class ClientUI extends Application {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            ChatWindowController chatWindowController = loader.getController();
 | 
					            ChatWindowController chatWindowController = loader.getController();
 | 
				
			||||||
            chatWindowController.setMessages(clientMessageList);
 | 
					            chatWindowController.setMessages(clientMessageList);
 | 
				
			||||||
 | 
					            chatWindowController.setConnectionHandler(connectionHandler);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // fill in scene and stage setup
 | 
					            // fill in scene and stage setup
 | 
				
			||||||
            Scene scene = new Scene(rootPane);
 | 
					            Scene scene = new Scene(rootPane);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue