ru.codeinside.adm.ui.TableGroup.java Source code

Java tutorial

Introduction

Here is the source code for ru.codeinside.adm.ui.TableGroup.java

Source

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * Copyright (c) 2013, MPL CodeInside http://codeinside.ru
 */

package ru.codeinside.adm.ui;

import java.util.List;
import java.util.Set;

import org.tepi.filtertable.FilterTable;
import ru.codeinside.adm.AdminServiceProvider;
import ru.codeinside.adm.database.Group;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

public class TableGroup extends VerticalLayout implements Property.ValueChangeListener {

    private static final long serialVersionUID = 1L;
    private String typeGroup;
    private final FilterTable table;
    private final Panel panel = new Panel();

    TableGroup(String typeGroup) {
        setSizeFull();
        this.typeGroup = typeGroup;
        setMargin(false, false, false, true);
        table = new FilterTable();
        table.setSizeFull();
        table.setSelectable(true);
        table.setMultiSelect(false);
        table.addListener(this);
        table.setImmediate(true);
        table.addContainerProperty("", String.class, "");
        table.addContainerProperty("?", String.class, "");
        table.setFilterBarVisible(true);
        table.setFilterDecorator(new FilterDecorator_());
        Set<String> groupNames = null;
        if (typeGroup.equals(GroupTab.ORGANIZATION)) {
            groupNames = AdminServiceProvider.get().getOrgGroupNames();
        } else if (typeGroup.equals(GroupTab.EMPLOYEE)) {
            groupNames = AdminServiceProvider.get().getEmpGroupNames();
        }
        for (String groupName : groupNames) {
            List<Group> groups = AdminServiceProvider.get().findGroupByName(groupName);
            for (Group group : groups) {
                table.addItem(new Object[] { groupName, group.getTitle() }, groupName);
            }
        }

        panel.setStyleName(Reindeer.PANEL_LIGHT);
        panel.setSizeFull();

        final HorizontalSplitPanel horiz = new HorizontalSplitPanel();
        horiz.setSplitPosition(35); // percent
        horiz.setSizeFull();
        addComponent(horiz);
        horiz.addComponent(table);
        horiz.addComponent(panel);
    }

    public void addItem(String group, String title) {
        table.addItem(new Object[] { group, title }, group);
    }

    public void valueChange(ValueChangeEvent event) {
        Object valuePropertyEvent = event.getProperty().getValue();
        if (valuePropertyEvent != null) {
            showGroupEditor(valuePropertyEvent.toString());
        } else {
            panel.removeAllComponents();
        }
    }

    public void setValue(Object value) {
        table.setValue(value);
        getWindow().showNotification(String.valueOf(table.size()));
        table.setCurrentPageFirstItemIndex(table.size());
    }

    private void showGroupEditor(String group) {
        panel.removeAllComponents();
        panel.addComponent(new GroupEditor(typeGroup, group, table));
    }
}