ru.codeinside.adm.ui.employee.EmployeeWidget.java Source code

Java tutorial

Introduction

Here is the source code for ru.codeinside.adm.ui.employee.EmployeeWidget.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.employee;

import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TreeTable;
import com.vaadin.ui.Upload;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import org.apache.tika.mime.MimeTypes;
import ru.codeinside.adm.AdminServiceProvider;
import ru.codeinside.adm.ui.TreeTableOrganization;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

final public class EmployeeWidget extends VerticalLayout implements Upload.Receiver, Upload.SucceededListener {

    private TableAllEmployee tableEmployee;
    private ByteArrayOutputStream outputStream;
    private TreeTable table;

    public EmployeeWidget(boolean lockedFilterValue, TreeTable table) {
        this.table = table;
        Upload upload = new Upload();
        upload.setImmediate(false);
        upload.setButtonCaption("  ");
        upload.setReceiver(this);
        upload.addListener(this);
        /*    Panel panel1 = new Panel();
            Button button = new Button("??? ", new Button.ClickListener() {
              @Override
              public void buttonClick(Button.ClickEvent event) {
                try {
                  URL url = new URL(getApplication().getURL(), "/registry/structures");
                  loadEmployeeData(url.openStream());
                } catch (MalformedURLException e) {
                  getWindow().showNotification(" " + e.getMessage());
                } catch (IOException e) {
                  getWindow().showNotification(" " + e.getMessage());
                }
              }
            });
            panel1.addComponent(button);*/
        tableEmployee = new TableAllEmployee(lockedFilterValue);
        tableEmployee.setSizeFull();
        HorizontalLayout hr = new HorizontalLayout();
        hr.setSizeFull();
        hr.setHeight("70px");
        hr.setSpacing(true);
        hr.setMargin(true);
        hr.addComponent(upload);
        addComponent(hr);
        addComponent(tableEmployee);
        tableEmployee.addButtonToLayout(hr);
        setExpandRatio(hr, 0.01f);
        setExpandRatio(tableEmployee, 0.99f);
        setSizeFull();
    }

    public void refreshList() {
        tableEmployee.refreshList();
    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        this.outputStream = new ByteArrayOutputStream();
        return outputStream;
    }

    @Override
    public void uploadSucceeded(Upload.SucceededEvent event) {
        byte[] buffer = outputStream.toByteArray();
        boolean isPlainText = MimeTypes.PLAIN_TEXT.equals(new MimeTypes().getMimeType(buffer).getName());
        if (isPlainText) {
            loadEmployeeData(new ByteArrayInputStream(buffer));
        } else {
            getWindow().showNotification(" ",
                    "? ?  .",
                    Window.Notification.TYPE_ERROR_MESSAGE);
        }
    }

    private void loadEmployeeData(InputStream data) {
        String currentUserName = getApplication().getUser().toString();
        try {
            AdminServiceProvider.get().loadEmployeeData(data, currentUserName);
            getWindow().showNotification("? ");
            if (table != null) {
                table.getContainerDataSource().removeAllItems();
                TreeTableOrganization.fillTable(table);
            }
        } catch (Exception e) {
            getWindow().showNotification(" ", e.getCause().getMessage(),
                    Window.Notification.TYPE_ERROR_MESSAGE);
        }
    }
}