com.squadd.chat.DialogController.java Source code

Java tutorial

Introduction

Here is the source code for com.squadd.chat.DialogController.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.chat;

import com.squadd.entities.UserInfo;
import com.squadd.javaBeans.ChatMessage;
import com.squadd.javaBeans.File;
import com.squadd.javaBeans.UserInfoBean;
import com.squadd.managers.ResourceManager;
import com.squadd.views.ChatView;
import com.vaadin.event.MouseEvents;
import com.vaadin.server.FileResource;
import com.vaadin.server.VaadinService;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Glebao
 */
public class DialogController extends ChatController {

    private HorizontalLayout footer;
    ChatView view;

    public DialogController(UserInfoBean userFrom, UserInfoBean userTo, Panel chatPanel, Panel contactsPanel,
            HorizontalLayout footer, ChatView view) {
        super(chatPanel, contactsPanel, view);
        setUserFrom(userFrom);
        setUserTo(userTo);
        this.footer = footer;
        this.view = view;
    }

    public MouseEvents.ClickListener more = new MouseEvents.ClickListener() {
        @Override
        public void click(MouseEvents.ClickEvent event) {
            Integer numb = new Integer(firstMessage);
            clearChat();
            updateChatLog(50 + numb);
        }
    };

    public ArrayList<ChatMessage> getLastMessagesFromDB(Integer amount) {
        Integer from = userFrom.getId();
        Integer to = userTo.getId();
        ArrayList<ChatMessage> mess = manager.getMessagesFromTo(from, to, amount);
        return mess;
    }

    public void updateChatLog(Integer amount) {
        ArrayList mess = getLastMessagesFromDB(amount);

        if (mess.size() > 0) {
            Panel add = new Panel("add 50 messages");
            add.setId("add");
            add.addClickListener(more);
            content.addComponent(add);

            for (int i = mess.size(); i > 0; i--) {
                print((ChatMessage) mess.get(mess.size() - i));
            }
        }
    }

    public void loadFriends() {
        List<Integer> friends = manager.getThoseWhoTalkToUser(userFrom.getId());
        if (friends.size() > 0) {
            System.out.println(friends);
            for (int i = 0; i < friends.size(); i++) {

                UserInfoBean friend = manager.get(friends.get(i), UserInfo.class, UserInfoBean.class);
                if (friend == null) {
                    friend = new UserInfoBean();
                    friend.setId(friends.get(i));
                    friend.setName("loadedFriend id" + friends.get(i));
                }
                UserInfoFace look = new UserInfoFace(friend, this, footer);
                Panel friendFace = look.getUserPanel();
                //Panel friendFace = look.getUserPanel(friends.get(i));
                // HorizontalLayout fri = new HorizontalLayout();
                // fri.addComponent(friendFace);
                ((VerticalLayout) (contactsPanel.getContent())).addComponent(friendFace);
            }
        }
    }

}