General Codestyle and higlighted todos

This commit is contained in:
Leonardo Brandenberger
2022-04-16 08:19:44 +02:00
parent 2acfa708e9
commit c732e82260
7 changed files with 145 additions and 106 deletions
@@ -20,7 +20,7 @@ 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.
* To Contact the Model Elements needed it also holds references to message and the Connectionhandler.
*/
public class ChatWindowController {
private ClientConnectionHandler connectionHandler;
@@ -56,7 +56,7 @@ public class ChatWindowController {
}
/**
* Takes a Connectionhandler object and stores it used as Model and also starts a Listener for it.
* Takes a Connection handler object and stores it used as Model and also starts a Listener for it.
*
* @param connectionHandler that will be set and used as Model.
*/
@@ -241,7 +241,7 @@ public class ChatWindowController {
*/
class WindowCloseHandler implements EventHandler<WindowEvent> {
/**
* TODO
* //TODO
*
* @param event the event which occurred
*/
@@ -251,7 +251,7 @@ public class ChatWindowController {
}
/**
* TODO missing
* //TODO missing
*/
public void startConnectionHandlerListener() {
@@ -285,7 +285,7 @@ public class ChatWindowController {
}
/**
* TODO
* //TODO
*/
private void messageListener() {
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
@@ -25,12 +25,18 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
private final Pattern messagePattern = Pattern.compile("^(?:@(\\S*))?\\s*(.*)$");
private SimpleStringProperty userName;
private SimpleObjectProperty<State> state;
private ClientMessageList messages;
private SimpleStringProperty serverAddress;
private SimpleIntegerProperty serverPort;
private final SimpleStringProperty userName;
private final SimpleObjectProperty<State> state;
private final ClientMessageList
messages;
private final SimpleStringProperty serverAddress;
private final SimpleIntegerProperty serverPort;
/**
* Constructor initializes ConnectionHandler by Setting default values for the fields and stores the given messages itself.
*
* @param messages
*/
public ClientConnectionHandler(ClientMessageList messages) {
super();
this.messages = messages;
@@ -40,6 +46,14 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
this.userName = new SimpleStringProperty(null);
}
/**
* //TODO complete javadoc
*
* @param serverAddress
* @param serverPort
* @param userName
* @throws IOException
*/
public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
state.set(NEW);
this.serverAddress.set(serverAddress);
@@ -102,7 +116,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
/**
* Method that is started by the run method, starts a connection handler.
* Figures out if connection is avaible if not determines the error cause and gives an error acordingly.
* Figures out if connection is available if not determines the error cause and gives an error accordingly.
*/
private void startReceiving() {
System.out.println("Starting Connection Handler");
@@ -112,7 +126,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
String data = getConnection().receive();
processData(data);
}
System.out.println("Stopped recieving data");
System.out.println("Stopped receiving data");
} catch (SocketException e) {
System.out.println("Connection terminated locally");
this.setState(DISCONNECTED);
@@ -145,7 +159,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
}
/**
* Method which processes data and determines its type then uses the corresponding method to proccess it.
* Method which processes data and determines its type then uses the corresponding method to process it.
*
* @param data that is received in a form of a String and then used depending on its determined cause.
*/
@@ -153,22 +167,22 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
try {
Scanner scanner = new Scanner(data);
StringBuilder sender = new StringBuilder();
StringBuilder reciever = new StringBuilder();
StringBuilder receiver = new StringBuilder();
StringBuilder type = new StringBuilder();
StringBuilder payload = new StringBuilder();
super.processData(scanner, sender, reciever, type, payload);
super.processData(scanner, sender, receiver, type, payload);
// dispatch operation based on type parameter
if (type.toString().equals(getDataTypeConnect())) {
System.err.println("Illegal connect request from server");
} else if (type.toString().equals(getDataTypeConfirm())) {
caseConfirm(sender.toString(), reciever.toString(), payload.toString());
caseConfirm(sender.toString(), receiver.toString(), payload.toString());
} else if (type.toString().equals(getDataTypeDisconnect())) {
caseDisconnect(sender.toString(), reciever.toString(), payload.toString());
caseDisconnect(sender.toString(), receiver.toString(), payload.toString());
} else if (type.toString().equals(getDataTypeMessage())) {
caseMessage(sender.toString(), reciever.toString(), payload.toString());
caseMessage(sender.toString(), receiver.toString(), payload.toString());
} else if (type.toString().equals(getDataTypeError())) {
caseError(sender.toString(), reciever.toString(), payload.toString());
caseError(sender.toString(), receiver.toString(), payload.toString());
} else {
System.out.println("Unknown data type received: " + type);
}
@@ -178,16 +192,16 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
}
}
private void caseConfirm(String sender, String reciever, String payload) {
private void caseConfirm(String sender, String receiver, String payload) {
if (state.get() == CONFIRM_CONNECT) {
this.userName.set(reciever);
this.userName.set(receiver);
this.serverPort.set(getConnection().getRemotePort());
this.serverAddress.set(getConnection().getRemoteHost());
messages.addMessage(new Message(Message.MessageType.INFO, sender, reciever, payload));
messages.addMessage(new Message(Message.MessageType.INFO, sender, receiver, payload));
System.out.println("CONFIRM: " + payload);
this.setState(CONNECTED);
} else if (state.get() == CONFIRM_DISCONNECT) {
messages.addMessage(new Message(Message.MessageType.INFO, sender, reciever, payload));
messages.addMessage(new Message(Message.MessageType.INFO, sender, receiver, payload));
System.out.println("CONFIRM: " + payload);
this.setState(DISCONNECTED);
} else {
@@ -229,7 +243,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
}
/**
* Stores the message as an error message and displays it as such aswell.
* Stores the message as an error message and displays it as such as well.
*
* @param sender the sender name of the message
* @param receiver the receiver name of the message
@@ -241,7 +255,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
}
/**
* Connects TODO
* //Connects TODO
*
* @throws ChatProtocolException Error that is thrown if the state is not set to NEW
*/
@@ -252,7 +266,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
}
/**
* TODO
* //TODO
*
* @throws ChatProtocolException
*/
public void disconnect() throws ChatProtocolException {
@@ -263,7 +278,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
}
/**
* TODO
* //TODO
*
* @param messageString
* @return
* @throws ChatProtocolException
@@ -6,17 +6,29 @@ import java.util.ArrayList;
import java.util.List;
/**
* Class that is use
* Class that is used to store the messages in an ArrayList
* And also as a Model that informs the controller via Listeners that can be initialized
*/
public class ClientMessageList {
private List<Message> messages = new ArrayList<>();
private final SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
/**
* Adds a new message to ArrayList and also informs Listener.
* @param message that should be added
*/
public void addMessage(Message message) {
messages.add(message);
changed.set(!changed.get());
}
/**
* Applies a given filter over all messages and returns the result as a new String.
* Method is also in charge of applying the correct prefix to the message according to its type.
*
* @param filter is the applied filter on all the messages that are stored.
* @return String that matches the given filter.
*/
public String getFilteredMessages(String filter) {
StringBuilder result = new StringBuilder();
boolean showAll = filter == null || filter.isBlank();
@@ -35,11 +47,19 @@ public class ClientMessageList {
return result.toString();
}
/**
* Overwrites the Arraylist of messages, clearing it, also informs all Listeners.
*/
public void clear() {
messages = new ArrayList<>();
changed.set(!changed.get());
}
/**
* Getter Method to check the current value of SimpleBooleanProperty changed.
*
* @return the current value of changed
*/
public SimpleBooleanProperty getChangedProperty() {
return changed;
}
@@ -14,7 +14,7 @@ public class Message {
*
* @param type Message (if it's a message typed by a user), Error or Information (if it is generated automatically, in this case sender and reciever will be null)
* @param sender The User who has sent the message.
* @param receiver The User who should recieve the message.
* @param receiver The User who should receive the message.
* @param text The Text of the message.
*/
public Message(MessageType type, String sender, String receiver, String text) {
@@ -25,10 +25,10 @@ public class Message {
}
/**
* Checks if the Filter String is contained in one of the datafields sender, receiver or text
* Checks if the Filter String is contained in one of the data fields sender, receiver or text
*
* @param filter The Filter String
* @return true if it the Filter String is contained in a datafield.
* @return true if it is the Filter String is contained in a data field.
*/
public boolean matchesFilter(String filter) {
return (sender != null && sender.contains(filter)) ||
@@ -51,7 +51,7 @@ public class Message {
}
/**
* @return The Reciever who recieves the Message.
* @return The Receiver who receives the Message.
*/
public String getReceiver() {
return receiver;