Started Java Doc in ChatWindowController

This commit is contained in:
Leonardo Brandenberger 2022-04-16 04:38:28 +02:00
parent 6d6b9f1564
commit dcee52cc1b
1 changed files with 80 additions and 16 deletions

View File

@ -17,7 +17,11 @@ import java.io.IOException;
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
/**
* Class Representing the Controller Element of the Window, also Contains the Elements that contacts the View Elements via
* Listeners and Observable Objects.
* To Contact the Model Elements needed it also holds references to messages and the Connectionhandler.
*/
public class ChatWindowController {
private ClientConnectionHandler connectionHandler;
private ClientMessageList messages;
@ -39,21 +43,24 @@ public class ChatWindowController {
@FXML
private Button connectButton;
@FXML
private Button sendButton;
@FXML
private TextField filterValue;
@FXML
public void initialize() {
}
/**
* Takes a message object and stores it used as Model and also starts message Listener via messageListener method.
*
* @param messages Object that will be set for use as Model
*/
public void setMessages(ClientMessageList messages) {
this.messages = messages;
messageListener();
}
/**
* Takes a Connectionhandler object and stores it used as Model and also starts a Listener for it.
*
* @param connectionHandler that will be set and used as Model.
*/
public void setConnectionHandler(ClientConnectionHandler connectionHandler) {
this.connectionHandler = connectionHandler;
startConnectionHandlerListener();
@ -61,10 +68,17 @@ public class ChatWindowController {
serverPortField.setText(String.valueOf(connectionHandler.getServerPortProperty().get()));
}
/**
* Method which closes the Application via use of the disconnect Method. //TODO evtl Löschen?
*/
private void applicationClose() {
disconnect();
}
/**
* Method that handles the Connect Button and Initiates connect when connectionHandler is not Connected or
* a disconnect when it is connected.
*/
@FXML
private void toggleConnection() {
if (connectionHandler == null || connectionHandler.getStateProperty().get() != CONNECTED) {
@ -74,19 +88,21 @@ public class ChatWindowController {
}
}
/**
* Initiates a connection by starting Connection Handler and telling, the Handler to start a connection.
*/
private void connect() {
try {
messages.clear(); // clear message list
startConnectionHandler();
connectionHandler.connect();
} catch (ChatProtocolException | IOException e) {
addError(e.getMessage());
addError("Error while starting Connection Handler and connect" + e);
}
}
/**
* Initiates disconnecting of the connectionHandler, also checks if connectionHandler is avaible.
* Initiates disconnecting of the connectionHandler, also checks if connectionHandler is available.
*/
private void disconnect() {
if (connectionHandler == null) {
@ -101,11 +117,11 @@ public class ChatWindowController {
}
/**
*
* Method which is used when the send button is pressed and handing over a message to the Connection Handler
*/
@FXML
private void message() {
String messageString = messageField.getText().strip();
String messageString = messageField.getText().strip(); //TODO MVC ok?
try {
if (connectionHandler == null) {
addError("No connection handler");
@ -119,11 +135,20 @@ public class ChatWindowController {
}
}
/**
* Method which is used when a Filter is applied
* Setting the Text in the message area after sending it through the filter.
*/
@FXML
private void applyFilter() {
Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip())));
}
/**
* Starts the ConnectionHandler setting the username and Checking if the name follows the valid format of no spaces.
*
* @throws IOException for error that may occur during initialization of connectionHandler.
*/
private void startConnectionHandler() throws IOException {
String userName = userNameField.getText();
if (!userName.contains(" ")) {
@ -132,7 +157,7 @@ public class ChatWindowController {
connectionHandler.initialize(serverAddress, serverPort, userName);
new Thread(connectionHandler).start();
//register Listener
//register Listener //TODO what todo with methods?
//startConnectionHandlerListener();
// register window close handler
@ -142,6 +167,12 @@ public class ChatWindowController {
}
}
/**
* Sets the state shown according to the state the method receives, if state indicates disconnected it will also inform the
* Connection Handler and tell it to stop Receiving more messages.
*
* @param newState is the state that it should be set to.
*/
public void stateChanged(State newState) {
// update UI (need to be run in UI thread: see Platform.runLater())
Platform.runLater(new Runnable() {
@ -155,15 +186,25 @@ public class ChatWindowController {
}
}
/**
* Sets displayed username according to the String provided.
*
* @param userName provided String that is set as name.
*/
public void setUserName(String userName) {
Platform.runLater(new Runnable() {
@Override
public void run() {
public void run() { //TODO MVC ok??
userNameField.setText(userName);
}
});
}
/**
* Sets displayed Server Address.
*
* @param serverAddress provided String that is set as server address.
*/
public void setServerAddress(String serverAddress) {
Platform.runLater(new Runnable() {
@Override
@ -173,6 +214,11 @@ public class ChatWindowController {
});
}
/**
* Sets displayed Server port.
*
* @param serverPort provided String that is set as server port.
*/
public void setServerPort(int serverPort) {
Platform.runLater(new Runnable() {
@Override
@ -182,16 +228,32 @@ public class ChatWindowController {
});
}
/**
* Method which adds an incoming String as an Error Message.
*
* @param message String to be added as Error
*/
public void addError(String message) {
messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message));
}
/**
* Nested Class in charge of Closing the wind
*/
class WindowCloseHandler implements EventHandler<WindowEvent> {
/**
* TODO
* @param event the event which occurred
*/
public void handle(WindowEvent event) {
applicationClose();
}
}
/**
* TODO missing
*/
public void startConnectionHandlerListener() {
connectionHandler.getStateProperty().addListener(new ChangeListener<State>() {
@Override
@ -222,7 +284,9 @@ public class ChatWindowController {
});
}
/**
* TODO
*/
private void messageListener() {
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
@Override