Java tutorial
/******************************************************************************* * SORMAS - Surveillance Outbreak Response Management & Analysis System * Copyright 2016-2018 Helmholtz-Zentrum fr Infektionsforschung GmbH (HZI) * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. *******************************************************************************/ package de.symeda.sormas.ui.user; import com.vaadin.icons.VaadinIcons; import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; import com.vaadin.ui.Button; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.themes.ValoTheme; import com.vaadin.v7.ui.ComboBox; import com.vaadin.v7.ui.TextField; import de.symeda.sormas.api.i18n.Captions; import de.symeda.sormas.api.i18n.I18nProperties; import de.symeda.sormas.api.i18n.Strings; import de.symeda.sormas.api.user.UserCriteria; import de.symeda.sormas.api.user.UserDto; import de.symeda.sormas.api.user.UserRight; import de.symeda.sormas.api.user.UserRole; import de.symeda.sormas.ui.ControllerProvider; import de.symeda.sormas.ui.UserProvider; import de.symeda.sormas.ui.ViewModelProviders; import de.symeda.sormas.ui.utils.AbstractView; import de.symeda.sormas.ui.utils.CssStyles; /** * A view for performing create-read-update-delete operations on products. * * See also {@link UserController} for fetching the data, the actual CRUD * operations and controlling the view based on events from outside. */ public class UsersView extends AbstractView { private static final long serialVersionUID = -3533557348144005469L; public static final String VIEW_NAME = "users"; public static final String ACTIVE_FILTER = I18nProperties.getString(Strings.active); public static final String INACTIVE_FILTER = I18nProperties.getString(Strings.inactive); private UserCriteria criteria; private UserGrid grid; private Button createButton; private VerticalLayout gridLayout; // Filters private ComboBox activeFilter; private ComboBox userRolesFilter; private TextField searchField; public UsersView() { super(VIEW_NAME); criteria = ViewModelProviders.of(UsersView.class).get(UserCriteria.class); grid = new UserGrid(); grid.setCriteria(criteria); gridLayout = new VerticalLayout(); gridLayout.addComponent(createFilterBar()); gridLayout.addComponent(grid); gridLayout.setMargin(true); gridLayout.setSpacing(false); gridLayout.setSizeFull(); gridLayout.setExpandRatio(grid, 1); gridLayout.setStyleName("crud-main-layout"); addComponent(gridLayout); if (UserProvider.getCurrent().hasUserRight(UserRight.USER_CREATE)) { createButton = new Button(I18nProperties.getCaption(Captions.userNewUser)); createButton.addStyleName(ValoTheme.BUTTON_PRIMARY); createButton.setIcon(VaadinIcons.PLUS_CIRCLE); createButton.addClickListener(e -> ControllerProvider.getUserController().create()); addHeaderComponent(createButton); } } public HorizontalLayout createFilterBar() { HorizontalLayout filterLayout = new HorizontalLayout(); filterLayout.setMargin(false); filterLayout.setSpacing(true); filterLayout.setSizeUndefined(); filterLayout.addStyleName(CssStyles.VSPACE_3); activeFilter = new ComboBox(); activeFilter.setWidth(200, Unit.PIXELS); activeFilter.setInputPrompt(I18nProperties.getPrefixCaption(UserDto.I18N_PREFIX, UserDto.ACTIVE)); activeFilter.addItems(ACTIVE_FILTER, INACTIVE_FILTER); activeFilter.addValueChangeListener(e -> { criteria.active(ACTIVE_FILTER.equals(e.getProperty().getValue()) ? Boolean.TRUE : INACTIVE_FILTER.equals(e.getProperty().getValue()) ? Boolean.FALSE : null); navigateTo(criteria); }); filterLayout.addComponent(activeFilter); userRolesFilter = new ComboBox(); userRolesFilter.setWidth(200, Unit.PIXELS); userRolesFilter.setInputPrompt(I18nProperties.getPrefixCaption(UserDto.I18N_PREFIX, UserDto.USER_ROLES)); userRolesFilter.addItems(UserRole.getAssignableRoles(UserProvider.getCurrent().getUserRoles())); userRolesFilter.addValueChangeListener(e -> { criteria.userRole((UserRole) e.getProperty().getValue()); navigateTo(criteria); }); filterLayout.addComponent(userRolesFilter); searchField = new TextField(); searchField.setWidth(200, Unit.PIXELS); searchField.setNullRepresentation(""); searchField.setInputPrompt(I18nProperties.getString(Strings.promptUserSearch)); searchField.setImmediate(true); searchField.addTextChangeListener(e -> { criteria.freeText(e.getText()); grid.reload(); }); filterLayout.addComponent(searchField); return filterLayout; } @Override public void enter(ViewChangeEvent event) { if (event != null) { String params = event.getParameters().trim(); if (params.startsWith("?")) { params = params.substring(1); criteria.fromUrlParams(params); } updateFilterComponents(); } grid.reload(); } public void updateFilterComponents() { applyingCriteria = true; activeFilter.setValue( criteria.getActive() == null ? null : criteria.getActive() ? ACTIVE_FILTER : INACTIVE_FILTER); userRolesFilter.setValue(criteria.getUserRole()); searchField.setValue(criteria.getFreeText()); applyingCriteria = false; } }