diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java index e9a704e..9eac20a 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java @@ -19,7 +19,6 @@ import java.util.regex.Pattern; import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; public class ChatWindowController { - private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" ); private ClientConnectionHandler connectionHandler; private ClientMessageList messages; @@ -41,7 +40,10 @@ public class ChatWindowController { serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); stateChanged(NEW); - messages = new ClientMessageList(this); + } + + public void setMessages(ClientMessageList messages) { + this.messages = messages; } private void applicationClose() { @@ -59,46 +61,40 @@ public class ChatWindowController { private void connect() { try { - messages = new ClientMessageList(this); // clear message list + messages.clear(); // clear message list + redrawMessageList(); startConnectionHandler(); connectionHandler.connect(); } catch(ChatProtocolException | IOException e) { - writeError(e.getMessage()); + addError(e.getMessage()); } } private void disconnect() { if (connectionHandler == null) { - writeError("No connection handler"); + addError("No connection handler"); return; } try { connectionHandler.disconnect(); } catch (ChatProtocolException e) { - writeError(e.getMessage()); + addError(e.getMessage()); } } @FXML private void message() { - if (connectionHandler == null) { - writeError("No connection handler"); - return; - } String messageString = messageField.getText().strip(); - messageField.clear(); - Matcher matcher = messagePattern.matcher(messageString); - if (matcher.find()) { - String receiver = matcher.group(1); - String message = matcher.group(2); - if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; - try { - connectionHandler.message(receiver, message); - } catch (ChatProtocolException e) { - writeError(e.getMessage()); + try { + if (connectionHandler == null) { + addError("No connection handler"); + } else if (!connectionHandler.message(messageString)) { + addError("Not a valid message format."); + } else { + messageField.clear(); } - } else { - writeError("Not a valid message format."); + } catch (ChatProtocolException e) { + addError(e.getMessage()); } } @@ -170,42 +166,25 @@ public class ChatWindowController { } public void addMessage(String sender, String receiver, String message) { - messages.addMessage(ClientMessageList.MessageType.MESSAGE, sender, receiver, message); + messages.addMessage(new Message(Message.MessageType.MESSAGE, sender, receiver, message)); this.redrawMessageList(); } public void addInfo(String message) { - messages.addMessage(ClientMessageList.MessageType.INFO, null, null, message); + messages.addMessage(new Message(Message.MessageType.INFO, null, null, message)); this.redrawMessageList(); } public void addError(String message) { - messages.addMessage(ClientMessageList.MessageType.ERROR, null, null, message); + messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message)); this.redrawMessageList(); } - public void clearMessageArea() { - this.messageArea.clear(); - } - - private void redrawMessageList() { - Platform.runLater(() -> messages.writeFilteredMessages(filterValue.getText().strip())); + this.messageArea.clear(); + Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip()))); } - public void writeError(String message) { - this.messageArea.appendText(String.format("[ERROR] %s\n", message)); - } - - public void writeInfo(String message) { - this.messageArea.appendText(String.format("[INFO] %s\n", message)); - } - - public void writeMessage(String sender, String reciever, String message) { - this.messageArea.appendText(String.format("[%s -> %s] %s\n", sender, reciever, message)); - } - - class WindowCloseHandler implements EventHandler { public void handle(WindowEvent event) { applicationClose(); @@ -213,7 +192,4 @@ public class ChatWindowController { } - - - } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java index 156010d..22e4ce2 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java @@ -7,6 +7,8 @@ import java.io.EOFException; import java.io.IOException; import java.net.SocketException; import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; @@ -24,6 +26,8 @@ public class ClientConnectionHandler implements Runnable { public static final String USER_NONE = ""; public static final String USER_ALL = "*"; + private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" ); + private String userName = USER_NONE; private State state = NEW; @@ -194,9 +198,22 @@ public class ClientConnectionHandler implements Runnable { this.setState(CONFIRM_DISCONNECT); } - public void message(String receiver, String message) throws ChatProtocolException { + public boolean message(String messageString) throws ChatProtocolException { if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); - this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); + + Matcher matcher = messagePattern.matcher(messageString); + if (matcher.find()) { + String receiver = matcher.group(1); + String message = matcher.group(2); + if(message.length() < 1){ + return false; + } + if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; + this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); + return true; + } else { + return false; + } } } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java index 100cb67..a811406 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java @@ -2,50 +2,34 @@ package ch.zhaw.pm2.multichat.client; import java.util.ArrayList; import java.util.List; -import java.util.Objects; public class ClientMessageList { - private final List typeList = new ArrayList<>(); - private final List senderList = new ArrayList<>(); - private final List receiverList = new ArrayList<>(); - private final List messageList = new ArrayList<>(); - private final ChatWindowController gui; + private List messages = new ArrayList<>(); - public ClientMessageList(ChatWindowController gui) { - this.gui = gui; - } - public void addMessage(MessageType type, String sender, String receiver, String message) { - typeList.add(type); - senderList.add(sender); - receiverList.add(receiver); - messageList.add(message); + public void addMessage(Message message) { + messages.add(message); } - public void writeFilteredMessages(String filter) { + public String getFilteredMessages(String filter) { + StringBuilder result = new StringBuilder(); boolean showAll = filter == null || filter.isBlank(); - gui.clearMessageArea(); - for(int i=0; i result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText())); + case ERROR -> result.append(String.format("[ERROR] %s\n", message.getText())); + case INFO -> result.append(String.format("[INFO] %s\n", message.getText())); + default -> result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType())); } } } + return result.toString(); } - public enum MessageType { - INFO, MESSAGE, ERROR; + public void clear() { + messages = new ArrayList<>(); } } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java index 56ae368..bde3d1d 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java @@ -7,6 +7,7 @@ import javafx.scene.layout.Pane; import javafx.stage.Stage; public class ClientUI extends Application { + private ClientMessageList clientMessageList = new ClientMessageList(); @Override public void start(Stage primaryStage) { @@ -17,6 +18,10 @@ public class ClientUI extends Application { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatWindow.fxml")); Pane rootPane = loader.load(); + + ChatWindowController chatWindowController = loader.getController(); + chatWindowController.setMessages(clientMessageList); + // fill in scene and stage setup Scene scene = new Scene(rootPane); //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java new file mode 100644 index 0000000..d805f68 --- /dev/null +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java @@ -0,0 +1,71 @@ +package ch.zhaw.pm2.multichat.client; + +/** +A Message object represents one Message of a client. Can be stored in ClientMessageList. + */ +public class Message { + private MessageType type; + private String sender; + private String receiver; + private String text; + + /** + * Constructor of Message. Needs all Information about a Message to save them. + * @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 text The Text of the message. + */ + public Message(MessageType type, String sender, String receiver, String text) { + this.type = type; + this.sender = sender; + this.receiver = receiver; + this.text = text; + } + + /** + * Checks if the Filter String is contained in one of the datafields sender, receiver or text + * @param filter The Filter String + * @return true if it the Filter String is contained in a datafield. + */ + public boolean matchesFilter(String filter){ + return (sender != null && sender.contains(filter)) || + (receiver != null && receiver.contains(filter)) || + (text != null && text.contains(filter)); + } + + /** + * @return The type of the message. + */ + public MessageType getType() { + return type; + } + + /** + * @return The User who has sent the Message. + */ + public String getSender() { + return sender; + } + + /** + * @return The Reciever who recieves the Message. + */ + public String getReceiver() { + return receiver; + } + + /** + * @return The Text of the Message. + */ + public String getText() { + return text; + } + + /** + * Enummeration of Message Types. + */ + public enum MessageType { + INFO, MESSAGE, ERROR; + } +}