de.kaiserpfalzEdv.vaadin.auth.list.UserListViewImpl.java Source code

Java tutorial

Introduction

Here is the source code for de.kaiserpfalzEdv.vaadin.auth.list.UserListViewImpl.java

Source

/*
 * Copyright 2015 Kaiserpfalz EDV-Service, Roland T. Lichti
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.kaiserpfalzEdv.vaadin.auth.list;

import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.spring.annotation.SpringView;
import com.vaadin.spring.annotation.UIScope;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Grid;
import com.vaadin.ui.GridLayout;
import de.kaiserpfalzEdv.infopir.backend.db.auth.User;
import de.kaiserpfalzEdv.vaadin.auth.Access;
import de.kaiserpfalzEdv.vaadin.menu.MenuEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

/**
 * @author klenkes
 * @version 2015Q1
 * @since 09.09.15 21:22
 */
@Named
@UIScope
@Access({ "admin" })
@MenuEntry(name = UserListViewImpl.VIEW_NAME, caption = "List Users", i18nKey = "users.list.menu-entry", icon = UserListViewImpl.VIEW_ICON, access = {
        "admin" }, order = 90, separator = true)
@SpringView(name = UserListViewImpl.VIEW_NAME)
public class UserListViewImpl extends CustomComponent implements UserListView, View {
    public static final String VIEW_NAME = "users";
    public static final String VIEW_ICON = "USERS";
    private static final Logger LOG = LoggerFactory.getLogger(UserListViewImpl.class);
    private UserListPresenter presenter;
    private EntityManager em;

    private GridLayout layout;
    private JPAContainer<User> data;
    private Grid users;

    @Inject
    public UserListViewImpl(final UserListPresenter presenter,
            @SuppressWarnings("SpringJavaAutowiringInspection") final EntityManager em) {
        this.presenter = presenter;
        this.presenter.setView(this);
        this.em = em;

        setSizeFull();
        setResponsive(true);
    }

    @Override
    public void refresh() {
        LOG.debug("Refreshing user list view.");

        data.refresh();
        users.setCellStyleGenerator(users.getCellStyleGenerator());
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        LOG.debug("Opened view: {}", event);

        if (layout == null) {
            layout = new GridLayout(5, 2);
            layout.setSizeFull();
            layout.setResponsive(true);
            layout.setMargin(true);
            layout.setSpacing(true);
            setCompositionRoot(layout);

            data = JPAContainerFactory.makeReadOnly(User.class, em);
            users = new Grid(presenter.translate("users.list.title"));
            users.setContainerDataSource(data);

            users.setColumns("id", "login", "surName", "givenName", "commonName");
            users.getColumn("id").setHeaderCaption(presenter.translate("user.id"));
            users.getColumn("login").setHeaderCaption(presenter.translate("user.login"));
            users.getColumn("login").setLastFrozenColumn();
            users.getColumn("surName").setHeaderCaption(presenter.translate("user.sur-name"));
            users.getColumn("givenName").setHeaderCaption(presenter.translate("user.given-name"));
            users.getColumn("commonName").setHeaderCaption(presenter.translate("user.common-name"));

            layout.addComponent(users, 0, 0, 4, 0);
        } else {
            refresh();
        }
    }
}