com.squadd.chat.ChatController.java Source code

Java tutorial

Introduction

Here is the source code for com.squadd.chat.ChatController.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.DBManager;
import com.squadd.managers.ResourceManager;
import com.squadd.technical.ImageGetter;
import com.squadd.views.ChatView;
import com.squadd.views.UserPageView;
import com.vaadin.event.MouseEvents;
import com.vaadin.server.FileResource;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.shared.ui.AlignmentInfo;
import com.vaadin.shared.ui.AlignmentInfo.Bits;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Glebao
 */
abstract class ChatController {

    Integer firstMessage = 0;

    public DBManager manager = new DBManager();
    UserInfoBean userFrom;
    UserInfoBean userTo;
    Panel chatPanel;
    Panel contactsPanel;
    FormLayout content;
    //private Image userFromImage;
    //private Image userToImage;
    private File userFromImageFile;
    private File userToImageFile;
    private Embedded userFromImageEmbedded;
    private Embedded userToImageEmbedded;
    DBManager man = new DBManager();
    ImageGetter ig = new ImageGetter();
    private ChatView view;

    public ChatController(Panel chatPanel, Panel contactsPanel, ChatView view) {
        this.chatPanel = chatPanel;
        this.contactsPanel = contactsPanel;
        this.content = new FormLayout();
        content.setSizeFull();
        configureImages();
    }

    private File findUserImage(UserInfoBean user) {
        List fileList = man.getFilesOrUsersByUserOrFile(user);
        if (fileList != null)
            return (File) fileList.get(0);
        else
            return null;
    }

    private void configureImages() {
        if (userFromImageFile == null) {
            String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
            FileResource resource = new FileResource(new java.io.File(basepath + "/VAADIN/images/user_icon.png"));
            userFromImageEmbedded = new Embedded("", resource);
        } else {
            FileResource imageResource = ResourceManager.open(userFromImageFile);
            userFromImageEmbedded = new Embedded("", imageResource);
        }
        if (userToImageFile == null) {
            String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
            FileResource resource = new FileResource(new java.io.File(basepath + "/VAADIN/images/user_icon.png"));
            userToImageEmbedded = new Embedded("", resource);
        } else {
            FileResource imageResource = ResourceManager.open(userToImageFile);
            userToImageEmbedded = new Embedded("", imageResource);
        }
        userFromImageEmbedded.setHeight("50px");
        userFromImageEmbedded.setWidth("50px");
        userToImageEmbedded.setHeight("50px");
        userToImageEmbedded.setWidth("50px");
    }

    /* private void configureComponents() {
    userFromImageFile = findUserImage(userFrom);
    userToImageFile = findUserImage(userTo);
    userFromImage = ig.get(userFrom);
    userToImage = ig.get(userTo);
    userFromImage.setHeight ("50px");
    userFromImage.setWidth("50px");
    userToImage.setHeight ("50px");
    userToImage.setWidth("50px");
    }
    */

    public void setUserFrom(UserInfoBean userFrom) {
        this.userFrom = userFrom;
    }

    public void setUserTo(UserInfoBean userTo) {
        this.userTo = userTo;
        this.firstMessage = 0;
    }

    public Integer getUserToId() {
        return userTo.getId();
    }

    public Integer getUserFromId() {
        return userFrom.getId();
    }

    public void addNewChatLine(ChatMessage message) {

        print(message);
    }

    public void clearChat() {
        content = new FormLayout();
        chatPanel.setContent(content);
        firstMessage = 0;
    }

    public HorizontalLayout createChatLine(ChatMessage mess) {
        if (mess.getIdFrom().equals(this.getUserFromId())) {
            return createChatLineFrom(mess);
        } else {
            return createChatLineTo(mess);
        }
    }

    public void print(ChatMessage mess) {
        HorizontalLayout chatLine = createChatLine(mess);
        content.addComponent(chatLine);
        chatPanel.setContent(content);
        firstMessage = firstMessage + 1;
        chatPanel.getUI().scrollIntoView(chatLine);
        //UI.getCurrent().scrollIntoView(chatLine);
    }

    public HorizontalLayout createChatLineFrom(ChatMessage message) {
        HorizontalLayout chatLine = new HorizontalLayout();
        Panel messagePanel = createMessagePanel(message);
        Panel photoPanel = createPhotoPanel(userFromImageEmbedded);
        //FormLayout photoPanel = createPhotoLayout(userFromImageEmbedded);
        HorizontalLayout dateLayout = createDateLayout(message);
        chatLine.addComponents(photoPanel, messagePanel, dateLayout);
        chatLine.setExpandRatio(messagePanel, 1);
        chatLine.setComponentAlignment(dateLayout, Alignment.BOTTOM_CENTER);
        //chatLine.setWidth("900px");
        //chatLine.setMargin(true);
        return chatLine;
    }

    public HorizontalLayout createChatLineTo(ChatMessage message) {
        HorizontalLayout chatLine = new HorizontalLayout();
        Panel messagePanel = createMessagePanel(message);
        Panel photoPanel = createPhotoPanel(userToImageEmbedded);
        //FormLayout photoPanel = createPhotoLayout(userToImageEmbedded);
        HorizontalLayout dateLayout = createDateLayout(message);
        chatLine.addComponents(dateLayout, messagePanel, photoPanel);
        chatLine.setExpandRatio(messagePanel, 1);
        //chatLine.setWidth("900px");
        chatLine.setComponentAlignment(dateLayout, Alignment.BOTTOM_CENTER);
        //chatLine.setMargin(false);
        return chatLine;
    }

    public Panel createPhotoPanel(Embedded image) {
        Panel photoPanel = new Panel();
        photoPanel.setContent(image);
        //photoPanel.setWidth("50px");
        //photoPanel.setHeight("50px");

        UserInfoBean use = man.get(userTo.getId(), UserInfo.class, UserInfoBean.class);

        MouseEvents.ClickListener showUserButtonListener = new MouseEvents.ClickListener() {
            @Override
            public void click(MouseEvents.ClickEvent event) {
                if (use != null) {
                    System.out.println(use.getId());
                    view.setAnotherUser(use);
                    UI.getCurrent().getNavigator().navigateTo(UserPageView.NAME);
                }
            }
        };
        if (use != null) {
            photoPanel.addClickListener(showUserButtonListener);
        }

        return photoPanel;
    }

    public FormLayout createPhotoLayout(Embedded image) {
        FormLayout photoLayout = new FormLayout();
        photoLayout.addComponent(image);
        photoLayout.setWidth("50px");
        photoLayout.setHeight("50px");

        UserInfoBean use = man.get(userTo.getId(), UserInfo.class, UserInfoBean.class);

        MouseEvents.ClickListener showUserButtonListener = new MouseEvents.ClickListener() {
            @Override
            public void click(MouseEvents.ClickEvent event) {
                if (use != null) {
                    System.out.println(use.getId());
                    view.setAnotherUser(use);
                    UI.getCurrent().getNavigator().navigateTo(UserPageView.NAME);
                }
            }
        };
        //if(use!=null){
        //photoLayout.addClickListener(showUserButtonListener);}

        return photoLayout;
    }

    public static Panel createDatePanel(ChatMessage mess) {
        Panel datePanel = new Panel();
        //FormLayout form = new FormLayout();
        //form.setMargin(false);
        Label dateLabel = new Label(mess.getFormattedDate());
        dateLabel.addStyleName("dateLabel");
        dateLabel.setValue("YrMOOOm");
        //form.addComponent(dateLabel);
        //datePanel.setContent(form);
        datePanel.setContent(dateLabel);
        datePanel.setWidth("100px");
        datePanel.setHeight("50px");
        return datePanel;
    }

    public static HorizontalLayout createDateLayout(ChatMessage mess) {
        HorizontalLayout dateLayout = new HorizontalLayout();
        Label dateLabel = new Label(mess.getFormattedDate());
        dateLabel.addStyleName("dateLabel");
        dateLayout.addComponent(dateLabel);
        dateLayout.setWidth("100px");
        dateLayout.setHeight("50px");
        return dateLayout;
    }

    public static Panel createMessagePanel(ChatMessage message) {
        Panel chatLine = new Panel();
        Integer from = message.getIdFrom();
        Integer to = message.getIdTo();
        String body = message.getBody();

        VerticalLayout messageLayout = new VerticalLayout();
        Label nameLabel = new Label(from.toString() + " said:");//!!! ?
        nameLabel.setStyleName("messageHeader");
        Label messageLabel = new Label(body);
        messageLabel.setStyleName("messageBody");
        messageLayout.addComponents(nameLabel, messageLabel);
        chatLine.setContent(messageLayout);
        chatLine.setWidth("500px");
        return chatLine;
    }

}