com.squadd.views.ChatView.java Source code

Java tutorial

Introduction

Here is the source code for com.squadd.views.ChatView.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.squadd.views;

import com.squadd.chat.DialogController;
import com.squadd.chat.MessageBroadcaster;
import com.squadd.chat.MessageBroadcaster.BroadcastListener;
import com.squadd.chat.UserInfoFace;
import com.squadd.entities.UserInfo;
import com.squadd.javaBeans.ChatMessage;
import com.squadd.javaBeans.UserInfoBean;
import com.squadd.managers.DBManager;
import com.squadd.managers.SearchEngine;
import com.vaadin.annotations.Push;
import com.vaadin.spring.annotation.SpringView;
import com.vaadin.spring.annotation.UIScope;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import java.util.Date;

/**
 *
 * @author Glebao
 */
@SpringView(name = ChatView.NAME)
@UIScope()
@Push
public class ChatView extends MainPageView implements BroadcastListener {
    public static final String NAME = "Chat";
    DBManager man = new DBManager();
    SearchEngine searchEngine = new SearchEngine(man);

    @Override
    protected Panel fillContentPanel(Layout layout) {
        Panel result = new Panel();
        result.setContent(configureMainPageLayout());
        return result;
    }

    @Override
    public void fillFromView() {
        contact.setFromView(ChatView.NAME);
    }

    private Layout configureMainPageLayout() {
        setUser();
        configureComponents();
        buildLayout();
        MessageBroadcaster.register(this);
        mainLayout.setId(("" + MessageBroadcaster.size()));
        chatPanel.setId(mainLayout.getId());
        return mainLayout;
    }

    HorizontalLayout mainLayout;

    public UserInfoBean mainUser;
    Label header;
    public DialogController control;
    VerticalLayout contactsLayout;
    FormLayout contactsContent;
    VerticalLayout chatLayout;
    Button send;
    Button clear;
    Button update;
    public HorizontalLayout footer;
    TextField chatInput;
    Panel contactsPanel;
    Panel chatPanel;
    FormLayout content;
    UserInfoBean userTo1;

    private void setUser() {
        UserInfoBean user = new UserInfoBean();
        UserInfoBean user2 = new UserInfoBean();
        user.setId(13);
        user.setName("Pasza");
        user.setSurname("Yrmom");
        mainUser = user;//}  
    }

    public void setAnotherUser(UserInfoBean user) {
        anotherUser = user;
    }

    private void configureComponents() {
        mainLayout = new HorizontalLayout();
        mainUser = man.get(contact.getUserInfo().getId(), UserInfo.class, UserInfoBean.class);
        contactsLayout = new VerticalLayout();
        contactsPanel = new Panel();
        chatPanel = new Panel();
        footer = new HorizontalLayout();
        control = new DialogController(mainUser, null, chatPanel, contactsPanel, footer, this);
        content = new FormLayout();
        contactsContent = new FormLayout();
        //this.getUI().getPushConfiguration().setPushMode(PushMode.MANUAL);

        ClickListener sendListener = new ClickListener() {
            @Override
            public void buttonClick(final ClickEvent event) {
                ChatMessage mess = new ChatMessage(control.getUserFromId(), control.getUserToId(), new Date(),
                        chatInput.getValue());
                String body = mess.getBody();
                if (body != null && !body.isEmpty()) {
                    control.manager.add(mess);
                    MessageBroadcaster.broadcast(mess);
                    chatInput.clear();
                    chatInput.focus();
                }
            }
        };

        ClickListener clearListener = (ClickEvent event) -> {
            clearChat();
            content = new FormLayout();
        };

        ClickListener updateListener = new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                clearChat();
                content = new FormLayout();
                control.updateChatLog(4);
            }
        };

        send = new Button("Send", sendListener);
        clear = new Button("Clear", clearListener);
        update = new Button("Update", updateListener);
    }

    private void buildLayout() {

        VerticalLayout chatAndFooter = new VerticalLayout();
        chatAndFooter.setHeight("90%");
        VerticalLayout contacts = new VerticalLayout();
        contacts.setSizeFull();
        contactsPanel.setHeight("800px");
        contactsPanel.setWidth("300px");
        contactsPanel.setContent(contactsLayout);
        contacts.addComponent(contactsPanel);
        contacts.setHeight("90%");

        TextField idTo = new TextField("idTo");
        idTo.setWidth("200px");
        Button setIdTo = new Button("set");
        setIdTo.setWidth("100px");
        HorizontalLayout setUserToId = new HorizontalLayout();

        Button.ClickListener st = new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                if (!idTo.getValue().equals("")) {
                    UserInfoBean newUserTo = new UserInfoBean();
                    newUserTo.setId(Integer.parseInt(idTo.getValue()));
                    newUserTo.setName("id" + idTo.getValue());
                    control.setUserTo(newUserTo);
                    if (footer.isVisible() == false) {
                        footer.setVisible(true);
                    }
                    UserInfoFace look = new UserInfoFace(newUserTo, control, footer);
                    Panel panel = look.getUserPanel();

                    boolean exists = false;
                    for (int i = 0; i < contactsLayout.getComponentCount(); i++) {
                        if (contactsLayout.getComponent(i).getClass() == Panel.class) {
                            Panel pan = (Panel) contactsLayout.getComponent(i);
                            if ((!(pan.getId() == null)) && pan.getId().equals(idTo.getValue())) {
                                exists = true;
                            }
                        }
                    }
                    if (exists == false) {
                        contactsLayout.addComponent(panel);
                    }
                    control.clearChat();
                    control.updateChatLog(10);
                }
                idTo.clear();
            }
        };

        setIdTo.addClickListener(st);
        setUserToId.addComponents(idTo, setIdTo);
        setUserToId.setComponentAlignment(setIdTo, Alignment.BOTTOM_CENTER);
        contacts.addComponent(setUserToId);
        mainLayout.addComponents(contacts);
        footer.setVisible(false);/////////
        chatAndFooter.addComponents(chatPanel, footer);
        chatLayout = new VerticalLayout();
        chatPanel.setHeight("750px");
        chatPanel.setWidth("900px");
        chatPanel.setContent(chatLayout);
        chatInput = new TextField();
        chatInput.setWidthUndefined();
        footer.addComponent(chatInput);
        chatInput.focus();
        send.setWidth("120px");
        footer.addComponent(send);
        clear.setWidth("120px");
        footer.addComponent(clear);
        update.setWidth("120px");
        footer.addComponent(update);
        footer.setHeight("50px");
        footer.setWidth("900px");
        chatInput.setWidth("100%");
        footer.setExpandRatio(chatInput, 1f);
        chatAndFooter.setExpandRatio(chatPanel, 1f);
        mainLayout.addComponents(chatAndFooter);
        mainLayout.setExpandRatio(chatAndFooter, 1f);
        control.loadFriends();
    }

    private void clearChat() {
        chatInput.clear();
        chatInput.focus();
        control.clearChat();
    }

    @Override
    public void receiveBroadcast(ChatMessage message) {
        this.getUI().access(new Runnable() {
            @Override
            public void run() {
                System.out.println("Gotcha " + control.getUserFromId());
                System.out.println(message.getBody());
                control.print(message);
                //chatPanel.setContent(new Label("Done!"));
                System.out.println(mainLayout.getId() + " " + chatPanel.getId());
            }
        });
    }

}