sk.stefan.mvps.view.components.hlasovani.V6s_VotesView.java Source code

Java tutorial

Introduction

Here is the source code for sk.stefan.mvps.view.components.hlasovani.V6s_VotesView.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 sk.stefan.mvps.view.components.hlasovani;

import com.vaadin.annotations.DesignRoot;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.Page;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.declarative.Design;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import sk.stefan.annotations.MenuButton;
import sk.stefan.annotations.ViewTab;
import sk.stefan.enums.UserType;
import sk.stefan.interfaces.TabEntity;
import sk.stefan.mvps.model.entity.PublicBody;
import sk.stefan.mvps.model.entity.Vote;
import sk.stefan.mvps.model.service.LinkService;
import sk.stefan.mvps.model.service.SecurityService;
import sk.stefan.mvps.model.service.VoteService;
import sk.stefan.mvps.view.tabs.TabComponent;
import sk.stefan.utils.Localizator;

/**
 * Zloka se seznamem hlasovn. Me zobrazovat vechna hlasovn nebo jen ta, souvisejc s danou entitou.
 *
 * @author stefan
 */
@MenuButton(name = "voteTab", position = 3, icon = FontAwesome.HAND_O_UP)
@ViewTab("hlasovaniTab")
@SpringComponent
@Scope("prototype")
@DesignRoot
public class V6s_VotesView extends VerticalLayout implements TabComponent {

    private static final long serialVersionUID = 12L;

    @Autowired
    private VoteService voteService;

    @Autowired
    private LinkService linkService;

    @Autowired
    private SecurityService securityService;

    //Design
    private Label lblRelatedEntity;
    private TextField searchFd;
    private Grid grid;
    private Button addVoteBt;

    //data
    private TabEntity tabEntity;
    private BeanItemContainer<Vote> container;

    public V6s_VotesView() {
        Design.read(this);
        Localizator.localizeDesign(this);

        container = new BeanItemContainer<>(Vote.class);

        grid.setContainerDataSource(container);
        grid.setHeightMode(HeightMode.ROW);
        grid.addSelectionListener(event -> Page.getCurrent()
                .open(linkService.getUriFragmentForEntity((TabEntity) grid.getSelectedRow()), null));

        addVoteBt.addClickListener(event -> Page.getCurrent()
                .open(tabEntity != null
                        ? linkService.getUriFragmentForTabWithParentEntity(NewVoteForm.class,
                                tabEntity.getEntityName(), tabEntity.getId())
                        : linkService.getUriFragmentForTab(NewVoteForm.class), null));

    }

    @Override
    public void setEntity(TabEntity tabEntity) {
        this.tabEntity = tabEntity;
        lblRelatedEntity.setValue(tabEntity.getPresentationName());
        lblRelatedEntity.setVisible(true);

        addVoteBt.setVisible(tabEntity instanceof PublicBody && (securityService.currentUserHasRole(UserType.ADMIN)
                || securityService.currentUserHasRole(UserType.VOLUNTEER)));
    }

    @Override
    public TabEntity getParentEntity() {
        return tabEntity;
    }

    @Override
    public String getTabCaption() {
        String relatedCaption = tabEntity != null ? tabEntity.getPresentationName() : "ve";
        return "Hlasovn - " + relatedCaption;
    }

    @Override
    public void show() {
        container.removeAllItems();
        container.addAll(
                tabEntity != null ? voteService.findAllVotesForTabEntity(tabEntity) : voteService.findAll());
        grid.setHeightByRows(container.size() > 7 ? 7 : container.size() == 0 ? 1 : container.size());

        if (addVoteBt.isVisible()) {
            addVoteBt.setVisible(securityService.currentUserHasRole(UserType.ADMIN)
                    || securityService.currentUserHasRole(UserType.VOLUNTEER));
        }
    }

    @Override
    public String getTabId() {
        Integer relatedId = tabEntity != null ? tabEntity.getId() : null;
        return "hlasovaniTab" + (relatedId != null ? String.valueOf(relatedId) : "");
    }
}