Java tutorial
/* * 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.UI; import com.squadd.javaBeans.Group; import com.squadd.javaBeans.GroupChat; import com.squadd.managers.DBManager; import com.squadd.technical.Display; import com.squadd.technical.ImageGetter; import com.vaadin.event.MouseEvents; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Image; import com.vaadin.ui.Label; import com.vaadin.ui.Panel; import com.vaadin.ui.TextArea; import com.vaadin.ui.VerticalLayout; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; /** * * @author SharkNado */ public class GroupChatLayout extends HorizontalLayout { private TextArea message; private Panel name; private Button send; private List<MessageLayout> msg; private VerticalLayout content; private VerticalLayout cont; private static Comparator dateComparator; private boolean isClicked = false; public GroupChatLayout(Group group) { name = new Panel(); Label label = new Label("Group Chat"); label.setSizeUndefined(); HorizontalLayout hor = new HorizontalLayout(); hor.setWidth(0.67 * Display.width + "px"); hor.addComponent(label); hor.setComponentAlignment(label, Alignment.TOP_CENTER); name.setWidth(0.68 * Display.width + "px"); name.setContent(hor); name.setStyleName("backColorGrey"); message = new TextArea(); msg = new ArrayList(); send = new Button("send"); content = new VerticalLayout(); dateComparator = new Comparator<GroupChat>() { @Override public int compare(GroupChat o1, GroupChat o2) { return -o1.getDate().compareTo(o2.getDate()); } }; configureLayout(group); configureActions(group); } private void configureLayout(Group group) { HorizontalLayout sendL = new HorizontalLayout(message, send); sendL.setWidth(Display.width * 0.68 + "px"); send.setSizeUndefined(); message.setWidth(0.61 * Display.width - send.getWidth() + "px"); sendL.setComponentAlignment(send, Alignment.MIDDLE_RIGHT); DBManager db = new DBManager(); List<GroupChat> jb = (ArrayList) db.getAllGroupMessages(group.getGroupId()); Collections.sort(jb, dateComparator); msg = new ArrayList<MessageLayout>(); for (int i = 0; i < jb.size(); ++i) { Label when = new Label(jb.get(i).getDate().toString()); Image photo = new ImageGetter().get(group); Label text = new Label(jb.get(i).getMessage()); msg.add(new MessageLayout(when, photo, text)); } for (int i = 0; i < msg.size(); ++i) { content.addComponent(msg.get(i)); } cont = new VerticalLayout(sendL, content); cont.setVisible(false); VerticalLayout all = new VerticalLayout(name, cont); addComponent(all); } private void configureActions(Group group) { Button.ClickListener click = new Button.ClickListener() { @Override public void buttonClick(Button.ClickEvent event) { if (!message.getValue().isEmpty()) { DBManager db = new DBManager(); db.addMessage(10, message.getValue()); //change 10->group.getId(); cont.removeComponent(content); List<GroupChat> jb = (ArrayList) db.getAllGroupMessages(group.getGroupId()); Collections.sort(jb, dateComparator); makeNewContent(jb, group); cont.addComponent(content); } } }; send.addClickListener(click); name.addClickListener(new MouseEvents.ClickListener() { @Override public void click(MouseEvents.ClickEvent event) { if (!isClicked) { isClicked = true; cont.setVisible(isClicked); } else { isClicked = false; cont.setVisible(isClicked); } } }); } private void makeNewContent(List<GroupChat> jb, Group group) { content = new VerticalLayout(); msg = new ArrayList<MessageLayout>(); for (int i = 0; i < jb.size(); ++i) { Label when = new Label(jb.get(i).getDate().toString()); Image photo = new ImageGetter().get(group); Label text = new Label(jb.get(i).getMessage()); msg.add(new MessageLayout(when, photo, text)); } for (int i = 0; i < msg.size(); ++i) { content.addComponent(msg.get(i)); } } public TextArea getMessage() { return message; } public Button getSend() { return send; } public List<MessageLayout> getMsg() { return msg; } public VerticalLayout getContent() { return content; } public VerticalLayout getCont() { return cont; } public void setMessage(TextArea message) { this.message = message; } public void setSend(Button send) { this.send = send; } public void setMsg(List<MessageLayout> msg) { this.msg = msg; } public void setContent(VerticalLayout content) { this.content = content; } public void setCont(VerticalLayout cont) { this.cont = cont; } }