I have two classes 1)MessageConfigurator and 2)MessageInfoManager. How can these two classes be moved to MainView class. I just need to remove these classes, but move their code to MainView.
MessageConfigurator
public class MessageConfigurator {
private static MessageConfigurator messageConfigurator = null;
private final MessagesInfoManager chatMessagesInfoManager = new MessagesInfoManager();
private MessageConfigurator() {
}
public static MessageConfigurator getInstance() {
if (messageConfigurator == null) {
messageConfigurator = new MessageConfigurator();
}
return messageConfigurator;
}
public MessagesInfoManager getChatMessagesInfoManager() {
return chatMessagesInfoManager;
}
}
MessageInfoManager
public class MessagesInfoManager {
private final List<MessageInfo> messagesInfoQueue = new ArrayList<>();
private final Map<Optional<UI>, MessageList> messageListMap = new HashMap<>();
public void updateMessageUIInfo(MessageInfo messageInfo) {
addMessageInfo(messageInfo);
Message message = messageInfo.getMessage();
for (Map.Entry<Optional<UI>, MessageList> entry : this.messageListMap.entrySet()) {
final Optional<UI> uiOptional = entry.getKey();
final MessageList messageList = entry.getValue();
uiOptional.ifPresent(ui ->
ui.access(() ->
messageList.add(new Paragraph(message.getFrom() + ": " + message.getMessage()))
));
}
}
private void addMessageInfo(MessageInfo messageInfo) {
if (!messagesInfoQueue.contains(messageInfo)) {
this.messagesInfoQueue.add(messageInfo);
this.messageListMap.putIfAbsent(messageInfo.getUI( ), messageInfo.getMessageList());
}
}
public List<Message> getMessagesByUI(Optional<UI> uiOptional) {
return messagesInfoQueue
.stream()
.filter(messageInfo -> !messageInfo.getUI().equals(uiOptional))
.map(MessageInfo::getMessage)
.collect(Collectors.toList());
}
}
It is necessary that the code of these two classes be in Mainview
MainView
@StyleSheet("frontend://styles/styles.css")
@Route
@PWA(name = "Vaadin MessagesInfoManager", shortName = "Vaadin MessagesInfoManager")
@Push
public class MainView extends VerticalLayout {
private final MessagesInfoManager messagesInfoManager;
private final RestService restService;
private String username;
@Autowired
public MainView(RestService restService) {
this.messagesInfoManager = MessageConfigurator.getInstance().getChatMessagesI nfoManager();
addClassName("main-view");
setSizeFull();
setDefaultHorizontalComponentAlignment(Alignment.C ENTER);
H1 header = new H1("Vaadin Chat");
header.getElement().getThemeList().add("dark");
add(header);
askUsername();
this.restService = restService;
}
private void askUsername() {
HorizontalLayout layout = new HorizontalLayout();
TextField usernameField = new TextField();
Button startButton = new Button("Start chat");
layout.add(usernameField, startButton);
startButton.addClickListener(click -> {
username = usernameField.getValue();
remove(layout);
showChat(username);
});
add(layout);
}
private void showChat(String username) {
MessageList messageList = new MessageList();
List<Message> lasts = restService.getLast();
for (Message message : lasts) {
messageList.add(new Paragraph(message.getFrom() + ": " + message.getMessage()));
}
add(messageList, createInputLayout(username, messageList));
expand(messageList);
}
private Component createInputLayout(String username, MessageList messageList) {
HorizontalLayout layout = new HorizontalLayout();
layout.setWidth("100%");
TextField messageField = new TextField();
messageField.addKeyDownListener(Key.ENTER, keyDownEvent -> sender(messageField, messageList));
Button sendButton = new Button("Send");
sendButton.addThemeVariants(ButtonVariant.LUMO_PRI MARY);
layout.add(messageField, sendButton);
layout.expand(messageField);
messageField.addFocusListener(event -> {
for (Message message : messagesInfoManager.getMessagesByUI(getUI())) {
if (!message.getFrom().equals(username)) {
message.setUnread(false);
this.restService.updateMessage(message.getId(), message);
}
}
});
sendButton.addClickListener(click -> sender(messageField, messageList));
messageField.focus();
return layout;
}
private void sender(TextField textField, MessageList messageList) {
Message message = new Message(username, textField.getValue());
message = restService.saveMessage(message);
messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, message, this));
textField.clear();
textField.focus();
}