ch.wscr.management.ui.view.MemberView.java Source code

Java tutorial

Introduction

Here is the source code for ch.wscr.management.ui.view.MemberView.java

Source

package ch.wscr.management.ui.view;

import ch.wscr.management.db.model.Member;
import ch.wscr.management.db.repository.MemberRepository;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.server.FontAwesome;
import com.vaadin.spring.annotation.SpringView;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.HeaderRow;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.renderers.ClickableRenderer;
import com.vaadin.ui.renderers.DateRenderer;
import com.vaadin.ui.themes.ValoTheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.gridutil.GridUtil;
import org.vaadin.gridutil.renderer.BooleanRenderer;
import org.vaadin.gridutil.renderer.EditDeleteButtonValueRenderer;

import javax.annotation.PostConstruct;
import java.text.DateFormat;

/**
 * Created by marco on 09.07.15.
 */
@SpringView(name = MemberView.VIEW_NAME)
public class MemberView extends VerticalLayout implements View {

    public static final String VIEW_NAME = "members";

    BeanItemContainer<Member> memberBeanItemContainer;

    @Autowired
    private MemberRepository memberRepository;

    public MemberView() {
        setSizeFull();
    }

    /**
     * Die View erstellen, Header und Grid hinzufgen
     */
    @PostConstruct
    void init() {
        // Datenquelle setzen
        memberBeanItemContainer = new BeanItemContainer<Member>(Member.class, memberRepository.findAll());

        addComponent(buidViewHeader());
        Grid grid = buildGrid();
        addComponent(grid);
        setExpandRatio(grid, 1);

    }

    /**
     * Erstellt das Grid fr die Anwendung, befllt es mit Daten aus dem BeanItemContainer
     *
     * @return das Grid fr die Mitgliederverwaltung
     */
    private Grid buildGrid() {
        final Grid grid = new Grid();
        grid.setSizeFull();

        //        propertyContainer = new GeneratedPropertyContainer(memberBeanItemContainer);
        //        propertyContainer.addGeneratedProperty("edit", getPropertyValueGenerator("."));

        grid.setContainerDataSource(memberBeanItemContainer);

        setColumnHeaders(grid);
        setColumnRenderers(grid);
        setHeaderRow(grid);
        setEditorFields(grid);
        setCommitHandler(grid);

        return grid;
    }

    private void setCommitHandler(final Grid grid) {
        grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler() {
            @Override
            public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
                // Do nothing
            }

            @Override
            public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
                BeanItem<Member> item = (BeanItem<Member>) commitEvent.getFieldBinder().getItemDataSource();

                memberRepository.saveAndFlush(item.getBean());

                Notification.show("Daten gespeichert");
            }
        });
    }

    /**
     * Methode zum Hinzufgen individueller Spalten zum Grid
     *
     * @param columnName der Spaltenname
     * @return ein PropertyValueGenerator
     */
    //    private PropertyValueGenerator<String> getPropertyValueGenerator(final String columnName) {
    //        return new PropertyValueGenerator<String>() {
    //            @Override
    //            public String getValue(Item item, Object o, Object o1) {
    //                return columnName;
    //            }
    //
    //            @Override
    //            public Class<String> getType() {
    //                return String.class;
    //            }
    //        };
    //    }

    /**
     * Felder zum Bearbeiten der Daten im Grid anpassen
     *
     * @param grid das Grid fr die Mitgliederverwaltung
     */
    private void setEditorFields(final Grid grid) {
        grid.getColumn("driverLicense").getEditorField().setCaption("");
    }

    /**
     * Die Spalten im Grid anpassen
     *
     * @param grid das Grid fr die Mitgliederverwaltung
     */
    private void setColumnHeaders(final Grid grid) {

        grid.setColumnOrder("memberId", "lastName", "firstName", "adrStreet", "adrPostalCode", "adrCity",
                "adrCountry", "birthDate", "driverLicense");

        grid.getColumn("memberId").setHeaderCaption("ID");
        grid.getColumn("firstName").setHeaderCaption("Vorname");
        grid.getColumn("lastName").setHeaderCaption("Nachname");
        grid.getColumn("adrStreet").setHeaderCaption("Strasse");
        grid.getColumn("adrPostalCode").setHeaderCaption("PLZ");
        grid.getColumn("adrCity").setHeaderCaption("Stadt");
        grid.getColumn("adrCountry").setHeaderCaption("Land");
        grid.getColumn("birthDate").setHeaderCaption("Geburtsdatum");
        grid.getColumn("driverLicense").setHeaderCaption("Bootsdriver");

        //        grid.setCellStyleGenerator(new Grid.CellStyleGenerator() {
        //            @Override
        //            public String getStyle(Grid.CellReference cellReference) {
        //                if (cellReference.getPropertyId().equals("memberId")) {
        //                    return GridUtil.ALIGN_CELL_RIGHT;
        //                }
        //                return null;
        //            }
        //        });
    }

    /**
     * Die Darstellung der Daten im Grid mit Renderern anpassen
     *
     * @param grid das Grid fr die Mitgliederverwaltung
     */
    private void setColumnRenderers(final Grid grid) {
        grid.getColumn("memberId").setRenderer(new EditDeleteButtonValueRenderer(
                new EditDeleteButtonValueRenderer.EditDeleteButtonClickListener() {
                    @Override
                    public void onEdit(ClickableRenderer.RendererClickEvent rendererClickEvent) {
                        grid.setEditorEnabled(true);
                        grid.editItem(rendererClickEvent.getItemId());
                    }

                    @Override
                    public void onDelete(ClickableRenderer.RendererClickEvent rendererClickEvent) {

                    }
                })).setWidth(120);

        grid.getColumn("driverLicense").setRenderer(new BooleanRenderer()).setWidth(120);
        grid.getColumn("birthDate").setRenderer(new DateRenderer(DateFormat.getDateInstance(DateFormat.SHORT)))
                .setWidth(120);
    }

    /**
     * Kopfzeile fr das Grid erstellen
     *
     * @param grid das Grid dem die Kopfzeile hinzugefgt werden soll
     */
    private void setHeaderRow(final Grid grid) {
        // TODO Erstellung neuer Mitglieder muss noch optimiert werden

        final Button addMember = new Button("Neu", clickEvent -> {
            BeanItem<Member> beanItem = memberBeanItemContainer.addItem(new Member());
            grid.setEditorEnabled(true);
            //grid.editItem(beanItem);

        });

        addMember.setStyleName(ValoTheme.BUTTON_BORDERLESS_COLORED);
        addMember.setIcon(FontAwesome.PLUS_CIRCLE);

        HorizontalLayout buttonLayout = new HorizontalLayout();
        buttonLayout.setSizeFull();
        buttonLayout.setSpacing(true);
        buttonLayout.setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);
        buttonLayout.addComponent(addMember);

        HeaderRow headerRow = grid.prependHeaderRow();
        headerRow.join("memberId", "lastName", "firstName", "adrStreet", "adrPostalCode", "adrCity", "adrCountry",
                "birthDate", "driverLicense");
        headerRow.getCell("lastName").setComponent(buttonLayout);
    }

    /**
     * Header der View erstellen
     *
     * @return der Header
     */
    private Component buidViewHeader() {
        HorizontalLayout header = new HorizontalLayout();
        header.setWidth(100f, Unit.PERCENTAGE);
        header.setDefaultComponentAlignment(Alignment.MIDDLE_RIGHT);
        header.setSpacing(true);

        Label title = new Label("Mitgliederverwaltung");
        title.setSizeUndefined();
        title.addStyleName(ValoTheme.LABEL_H1);

        header.addComponent(title);

        Label gap = new Label();
        gap.setWidth(10, Unit.PIXELS);

        header.addComponent(gap);
        header.setExpandRatio(title, 1);
        return header;
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {

    }

}