com.esofthead.mycollab.module.project.view.message.MessageReadViewImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.esofthead.mycollab.module.project.view.message.MessageReadViewImpl.java

Source

/**
 * This file is part of mycollab-web.
 *
 * mycollab-web is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * mycollab-web is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with mycollab-web.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.esofthead.mycollab.module.project.view.message;

import com.esofthead.mycollab.common.CommentType;
import com.esofthead.mycollab.common.i18n.GenericI18Enum;
import com.esofthead.mycollab.configuration.SiteConfiguration;
import com.esofthead.mycollab.core.utils.DateTimeUtils;
import com.esofthead.mycollab.core.utils.StringUtils;
import com.esofthead.mycollab.eventmanager.EventBusFactory;
import com.esofthead.mycollab.module.ecm.domain.Content;
import com.esofthead.mycollab.module.ecm.service.ResourceService;
import com.esofthead.mycollab.module.file.AttachmentType;
import com.esofthead.mycollab.module.file.AttachmentUtils;
import com.esofthead.mycollab.module.project.CurrentProjectVariables;
import com.esofthead.mycollab.module.project.ProjectRolePermissionCollections;
import com.esofthead.mycollab.module.project.ProjectTypeConstants;
import com.esofthead.mycollab.module.project.domain.SimpleMessage;
import com.esofthead.mycollab.module.project.events.ProjectMemberEvent;
import com.esofthead.mycollab.module.project.i18n.MessageI18nEnum;
import com.esofthead.mycollab.module.project.service.MessageService;
import com.esofthead.mycollab.module.project.ui.components.CommentDisplay;
import com.esofthead.mycollab.module.project.ui.components.ProjectViewHeader;
import com.esofthead.mycollab.module.project.ui.components.ProjectAttachmentDisplayComponentFactory;
import com.esofthead.mycollab.schedule.email.project.MessageRelayEmailNotificationAction;
import com.esofthead.mycollab.spring.ApplicationContextUtil;
import com.esofthead.mycollab.vaadin.AppContext;
import com.esofthead.mycollab.vaadin.events.HasPreviewFormHandlers;
import com.esofthead.mycollab.vaadin.mvp.AbstractPageView;
import com.esofthead.mycollab.vaadin.mvp.ViewComponent;
import com.esofthead.mycollab.vaadin.mvp.ViewScope;
import com.esofthead.mycollab.vaadin.ui.*;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.server.FontAwesome;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import org.apache.commons.collections.CollectionUtils;
import org.vaadin.dialogs.ConfirmDialog;
import org.vaadin.maddon.layouts.MHorizontalLayout;
import org.vaadin.maddon.layouts.MVerticalLayout;

import java.util.List;

/**
 * @author MyCollab Ltd.
 * @since 1.0
 */
@ViewComponent(scope = ViewScope.PROTOTYPE)
public class MessageReadViewImpl extends AbstractPageView implements MessageReadView {
    private static final long serialVersionUID = 1L;

    private final AdvancedPreviewBeanForm<SimpleMessage> previewForm;
    private SimpleMessage message;
    private CssLayout contentWrapper;
    private MHorizontalLayout header;
    private CommentDisplay commentDisplay;
    private CheckBox stickyCheck;

    public MessageReadViewImpl() {
        super();

        this.header = new MHorizontalLayout().withStyleName("hdr-view").withWidth("100%").withSpacing(true)
                .withMargin(true);

        this.addComponent(header);
        previewForm = new AdvancedPreviewBeanForm<>();

        contentWrapper = new CssLayout();
        contentWrapper.setStyleName("content-wrapper");
        contentWrapper.addComponent(previewForm);
        contentWrapper.setWidth("900px");
        this.addComponent(contentWrapper);
        this.setMargin(false);
    }

    @Override
    public HasPreviewFormHandlers<SimpleMessage> getPreviewFormHandlers() {
        return previewForm;
    }

    @Override
    public void previewItem(SimpleMessage item) {
        this.message = item;
        previewForm.setFormLayoutFactory(new FormLayoutFactory());
        previewForm.setBeanFormFieldFactory(new AbstractBeanFieldGroupViewFieldFactory<SimpleMessage>(previewForm) {
            private static final long serialVersionUID = 1L;

            @Override
            protected Field<?> onCreateField(Object propertyId) {
                return null;
            }

        });
        previewForm.setBean(item);
    }

    @Override
    public SimpleMessage getItem() {
        return message;
    }

    class FormLayoutFactory implements IFormLayoutFactory {
        private static final long serialVersionUID = 1L;

        @Override
        public ComponentContainer getLayout() {
            header.removeAllComponents();

            MVerticalLayout messageAddLayout = new MVerticalLayout().withSpacing(true).withMargin(false)
                    .withWidth("100%");

            Button deleteBtn = new Button(AppContext.getMessage(GenericI18Enum.BUTTON_DELETE),
                    new Button.ClickListener() {
                        private static final long serialVersionUID = 1L;

                        @Override
                        public void buttonClick(ClickEvent event) {
                            ConfirmDialogExt.show(UI.getCurrent(),
                                    AppContext.getMessage(GenericI18Enum.DIALOG_DELETE_TITLE,
                                            SiteConfiguration.getSiteName()),
                                    AppContext.getMessage(GenericI18Enum.DIALOG_DELETE_SINGLE_ITEM_MESSAGE),
                                    AppContext.getMessage(GenericI18Enum.BUTTON_YES),
                                    AppContext.getMessage(GenericI18Enum.BUTTON_NO), new ConfirmDialog.Listener() {
                                        private static final long serialVersionUID = 1L;

                                        @Override
                                        public void onClose(final ConfirmDialog dialog) {
                                            if (dialog.isConfirmed()) {
                                                final MessageService messageService = ApplicationContextUtil
                                                        .getSpringBean(MessageService.class);
                                                messageService.removeWithSession(message.getId(),
                                                        AppContext.getUsername(), AppContext.getAccountId());
                                                previewForm.fireCancelForm(message);
                                            }
                                        }
                                    });
                        }
                    });
            deleteBtn.setIcon(FontAwesome.TRASH_O);
            deleteBtn.addStyleName(UIConstants.THEME_RED_LINK);
            deleteBtn.setEnabled(CurrentProjectVariables.canAccess(ProjectRolePermissionCollections.MESSAGES));

            stickyCheck = new CheckBox(AppContext.getMessage(MessageI18nEnum.FORM_IS_STICK), message.getIsstick());
            stickyCheck.addValueChangeListener(new ValueChangeListener() {
                private static final long serialVersionUID = 1L;

                @Override
                public void valueChange(ValueChangeEvent event) {
                    if (CurrentProjectVariables.canWrite(ProjectRolePermissionCollections.MESSAGES)) {
                        message.setIsstick(stickyCheck.getValue());
                        message.setSaccountid(AppContext.getAccountId());
                        final MessageService messageService = ApplicationContextUtil
                                .getSpringBean(MessageService.class);
                        messageService.updateWithSession(message, AppContext.getUsername());
                    } else {
                        NotificationUtil.showMessagePermissionAlert();
                    }

                }
            });

            Label headerText = new ProjectViewHeader(ProjectTypeConstants.MESSAGE, message.getTitle());

            header.with(headerText, stickyCheck, deleteBtn).withAlign(headerText, Alignment.MIDDLE_LEFT)
                    .withAlign(stickyCheck, Alignment.MIDDLE_RIGHT).withAlign(deleteBtn, Alignment.MIDDLE_RIGHT)
                    .expand(headerText);

            MHorizontalLayout messageLayout = new MHorizontalLayout().withStyleName("message").withWidth("100%")
                    .withSpacing(true);

            if (message.getIsstick() != null && message.getIsstick()) {
                messageLayout.addStyleName("important-message");
            }

            MVerticalLayout userBlock = new MVerticalLayout().withWidth("80px").withSpacing(true).withMargin(false);
            userBlock.setDefaultComponentAlignment(Alignment.TOP_CENTER);
            ClickListener gotoUser = new ClickListener() {
                private static final long serialVersionUID = 1L;

                @Override
                public void buttonClick(ClickEvent event) {
                    EventBusFactory.getInstance().post(
                            new ProjectMemberEvent.GotoRead(MessageReadViewImpl.this, message.getPosteduser()));
                }
            };
            Button userAvatarBtn = UserAvatarControlFactory
                    .createUserAvatarButtonLink(message.getPostedUserAvatarId(), message.getFullPostedUserName());
            userAvatarBtn.addClickListener(gotoUser);
            userBlock.addComponent(userAvatarBtn);

            Button userName = new Button(message.getFullPostedUserName());
            userName.setStyleName("user-name");
            userName.addStyleName("link");
            userName.addStyleName(UIConstants.WORD_WRAP);
            userName.addClickListener(gotoUser);
            userBlock.addComponent(userName);

            messageLayout.addComponent(userBlock);

            final CssLayout rowLayout = new CssLayout();
            rowLayout.setStyleName("message-container");
            rowLayout.setWidth("100%");

            final MHorizontalLayout messageHeader = new MHorizontalLayout().withStyleName("message-header")
                    .withSpacing(true).withMargin(new MarginInfo(true, true, false, true)).withWidth("100%");
            messageHeader.setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);

            final Label timePostLbl = new Label(
                    AppContext.getMessage(MessageI18nEnum.USER_COMMENT_ADD, message.getFullPostedUserName(),
                            DateTimeUtils.getPrettyDateValue(message.getPosteddate(), AppContext.getUserLocale())),
                    ContentMode.HTML);
            timePostLbl.setSizeUndefined();
            timePostLbl.setStyleName("time-post");

            messageHeader.addComponent(timePostLbl);
            messageHeader.setExpandRatio(timePostLbl, 1.0f);

            rowLayout.addComponent(messageHeader);

            final Label messageContent = new Label(StringUtils.formatRichText(message.getMessage()),
                    ContentMode.HTML);
            messageContent.setStyleName("message-body");
            rowLayout.addComponent(messageContent);

            ResourceService attachmentService = ApplicationContextUtil.getSpringBean(ResourceService.class);
            List<Content> attachments = attachmentService
                    .getContents(AttachmentUtils.getProjectEntityAttachmentPath(AppContext.getAccountId(),
                            message.getProjectid(), AttachmentType.PROJECT_MESSAGE, message.getId()));
            if (CollectionUtils.isNotEmpty(attachments)) {
                HorizontalLayout attachmentField = new HorizontalLayout();
                Button attachmentIcon = new Button(null, FontAwesome.PAPERCLIP);
                attachmentIcon.addStyleName(UIConstants.BUTTON_ICON_ONLY);
                attachmentField.addComponent(attachmentIcon);

                Label lbAttachment = new Label(AppContext.getMessage(MessageI18nEnum.FORM_ATTACHMENT_FIELD));
                attachmentField.addComponent(lbAttachment);

                Component attachmentDisplayComp = ProjectAttachmentDisplayComponentFactory
                        .getAttachmentDisplayComponent(message.getProjectid(), AttachmentType.PROJECT_MESSAGE,
                                message.getId());

                MVerticalLayout messageFooter = new MVerticalLayout().withWidth("100%")
                        .withStyleName("message-footer").withMargin(true).withSpacing(true)
                        .with(attachmentField, attachmentDisplayComp);

                rowLayout.addComponent(messageFooter);
            }

            messageLayout.addComponent(rowLayout);
            messageLayout.setExpandRatio(rowLayout, 1.0f);

            messageAddLayout.addComponent(messageLayout);

            if (commentDisplay != null && commentDisplay.getParent() == contentWrapper) {
                contentWrapper.removeComponent(commentDisplay);
            }
            commentDisplay = createCommentPanel();
            contentWrapper.addComponent(commentDisplay);

            return messageAddLayout;
        }

        protected CommentDisplay createCommentPanel() {
            CommentDisplay commentDisplay = new CommentDisplay(CommentType.PRJ_MESSAGE,
                    CurrentProjectVariables.getProjectId(), true, true, MessageRelayEmailNotificationAction.class);
            commentDisplay.loadComments("" + message.getId());
            return commentDisplay;
        }

        @Override
        public void attachField(Object propertyId, Field<?> field) {

        }
    }
}