com.esofthead.mycollab.module.user.accountsettings.team.view.RoleSearchPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.esofthead.mycollab.module.user.accountsettings.team.view.RoleSearchPanel.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.user.accountsettings.team.view;

import com.esofthead.mycollab.common.i18n.GenericI18Enum;
import com.esofthead.mycollab.core.arguments.SearchCriteria;
import com.esofthead.mycollab.core.arguments.SearchField;
import com.esofthead.mycollab.core.arguments.StringSearchField;
import com.esofthead.mycollab.eventmanager.EventBusFactory;
import com.esofthead.mycollab.module.user.accountsettings.localization.RoleI18nEnum;
import com.esofthead.mycollab.module.user.domain.criteria.RoleSearchCriteria;
import com.esofthead.mycollab.module.user.events.RoleEvent;
import com.esofthead.mycollab.security.RolePermissionCollections;
import com.esofthead.mycollab.vaadin.AppContext;
import com.esofthead.mycollab.vaadin.ui.GenericSearchPanel;
import com.esofthead.mycollab.vaadin.ui.MyCollabResource;
import com.esofthead.mycollab.vaadin.ui.UIConstants;
import com.vaadin.server.FontAwesome;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.*;
import org.apache.commons.lang3.StringUtils;
import org.vaadin.maddon.layouts.MHorizontalLayout;

/**
 * 
 * @author MyCollab Ltd.
 * @since 1.0
 */
public class RoleSearchPanel extends GenericSearchPanel<RoleSearchCriteria> {
    private static final long serialVersionUID = 1L;
    private RoleSearchCriteria searchCriteria;

    public RoleSearchPanel() {
        this.setCompositionRoot(new RoleBasicSearchLayout());
    }

    private HorizontalLayout createSearchTopPanel() {
        final MHorizontalLayout layout = new MHorizontalLayout().withStyleName(UIConstants.HEADER_VIEW)
                .withWidth("100%").withSpacing(true).withMargin(new MarginInfo(true, false, true, false));
        layout.setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);

        final Label searchtitle = new Label(
                FontAwesome.USERS.getHtml() + " " + AppContext.getMessage(RoleI18nEnum.VIEW_LIST_TITLE),
                ContentMode.HTML);
        searchtitle.setStyleName(UIConstants.HEADER_TEXT);
        layout.addComponent(searchtitle);
        layout.setExpandRatio(searchtitle, 1.0f);

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

                    @Override
                    public void buttonClick(final Button.ClickEvent event) {
                        EventBusFactory.getInstance().post(new RoleEvent.GotoAdd(this, null));
                    }
                });
        createBtn.setStyleName(UIConstants.THEME_GREEN_LINK);
        createBtn.setIcon(FontAwesome.PLUS);
        createBtn.setEnabled(AppContext.canWrite(RolePermissionCollections.ACCOUNT_ROLE));

        layout.with(createBtn).withAlign(createBtn, Alignment.MIDDLE_RIGHT);

        return layout;
    }

    @SuppressWarnings("rawtypes")
    private class RoleBasicSearchLayout extends GenericSearchPanel.BasicSearchLayout {

        @SuppressWarnings("unchecked")
        public RoleBasicSearchLayout() {
            super(RoleSearchPanel.this);
        }

        private static final long serialVersionUID = 1L;
        private TextField nameField;

        @Override
        public ComponentContainer constructHeader() {
            return RoleSearchPanel.this.createSearchTopPanel();
        }

        @Override
        public ComponentContainer constructBody() {
            final MHorizontalLayout basicSearchBody = new MHorizontalLayout().withSpacing(true).withMargin(true)
                    .with(new Label("Name"));

            this.nameField = new TextField();
            this.nameField.setWidth(UIConstants.DEFAULT_CONTROL_WIDTH);
            basicSearchBody.addComponent(this.nameField);

            final Button searchBtn = new Button(AppContext.getMessage(GenericI18Enum.BUTTON_SEARCH));
            searchBtn.setIcon(FontAwesome.SEARCH);
            searchBtn.setStyleName(UIConstants.THEME_GREEN_LINK);
            searchBtn.addClickListener(new Button.ClickListener() {
                private static final long serialVersionUID = 1L;

                @Override
                public void buttonClick(final Button.ClickEvent event) {
                    RoleBasicSearchLayout.this.callSearchAction();
                }
            });
            basicSearchBody.addComponent(searchBtn);

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

                        @Override
                        public void buttonClick(final Button.ClickEvent event) {
                            RoleBasicSearchLayout.this.nameField.setValue("");
                        }
                    });
            clearBtn.setStyleName(UIConstants.THEME_GRAY_LINK);
            clearBtn.addStyleName("cancel-button");
            basicSearchBody.addComponent(clearBtn);
            basicSearchBody.setComponentAlignment(clearBtn, Alignment.MIDDLE_LEFT);
            return basicSearchBody;
        }

        @Override
        protected SearchCriteria fillUpSearchCriteria() {
            RoleSearchPanel.this.searchCriteria = new RoleSearchCriteria();
            if (StringUtils.isNotBlank(this.nameField.getValue())) {
                RoleSearchPanel.this.searchCriteria
                        .setRoleName(new StringSearchField(SearchField.AND, this.nameField.getValue()));
            }
            return RoleSearchPanel.this.searchCriteria;
        }
    }
}