created Class Message

This commit is contained in:
schrom01 2022-04-12 07:36:14 +02:00
parent e0ca1c77bb
commit e11fb136cf
3 changed files with 50 additions and 28 deletions

View File

@ -170,17 +170,17 @@ public class ChatWindowController {
} }
public void addMessage(String sender, String receiver, String message) { 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(); this.redrawMessageList();
} }
public void addInfo(String message) { 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(); this.redrawMessageList();
} }
public void addError(String message) { 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(); this.redrawMessageList();
} }

View File

@ -5,47 +5,34 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class ClientMessageList { public class ClientMessageList {
private final List<MessageType> typeList = new ArrayList<>(); private final List<Message> messages = new ArrayList<>();
private final List<String> senderList = new ArrayList<>();
private final List<String> receiverList = new ArrayList<>();
private final List<String> messageList = new ArrayList<>();
private final ChatWindowController gui; private final ChatWindowController gui;
public ClientMessageList(ChatWindowController gui) { public ClientMessageList(ChatWindowController gui) {
this.gui = gui; this.gui = gui;
} }
public void addMessage(MessageType type, String sender, String receiver, String message) { public void addMessage(Message message) {
typeList.add(type); messages.add(message);
senderList.add(sender);
receiverList.add(receiver);
messageList.add(message);
} }
public void writeFilteredMessages(String filter) { public void writeFilteredMessages(String filter) {
boolean showAll = filter == null || filter.isBlank(); boolean showAll = filter == null || filter.isBlank();
gui.clearMessageArea(); gui.clearMessageArea();
for(int i=0; i<senderList.size(); i++) { for(Message message : messages) {
String sender = Objects.requireNonNullElse(senderList.get(i),"");
String receiver = Objects.requireNonNullElse(receiverList.get(i),"");
String message = Objects.requireNonNull(messageList.get(i), "");
if(showAll || if(showAll ||
sender.contains(filter) || message.getSender().contains(filter) ||
receiver.contains(filter) || message.getReceiver().contains(filter) ||
message.contains(filter)) message.getText().contains(filter))
{ {
switch (typeList.get(i)) { switch (message.getType()) {
case MESSAGE: gui.writeMessage(senderList.get(i), receiverList.get(i), messageList.get(i)); break; case MESSAGE: gui.writeMessage(message.getSender(), message.getReceiver(), message.getText()); break;
case ERROR: gui.writeError(messageList.get(i)); break; case ERROR: gui.writeError(message.getText()); break;
case INFO: gui.writeInfo(messageList.get(i)); break; case INFO: gui.writeInfo(message.getText()); break;
default: gui.writeError("Unexpected message type: " + typeList.get(i)); default: gui.writeError("Unexpected message type: " + message.getType());
} }
} }
} }
} }
public enum MessageType {
INFO, MESSAGE, ERROR;
}
} }

View File

@ -0,0 +1,35 @@
package ch.zhaw.pm2.multichat.client;
public class Message {
private MessageType type;
private String sender;
private String receiver;
private String text;
public Message(MessageType type, String sender, String receiver, String text) {
this.type = type;
this.sender = sender;
this.receiver = receiver;
this.text = text;
}
public MessageType getType() {
return type;
}
public String getSender() {
return sender;
}
public String getReceiver() {
return receiver;
}
public String getText() {
return text;
}
public enum MessageType {
INFO, MESSAGE, ERROR;
}
}