com.zklogtool.web.components.OpenSnapshotFileDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.zklogtool.web.components.OpenSnapshotFileDialog.java

Source

/* 
 * Copyright 2014 Alen Caljkusic.
 *
 * 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 com.zklogtool.web.components;

import java.io.File;
import java.io.IOException;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.server.UserError;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.zklogtool.data.DataState;
import com.zklogtool.reader.CRCValidationException;
import com.zklogtool.reader.SnapshotFileReader;

public class OpenSnapshotFileDialog extends CustomComponent {

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
    @AutoGenerated
    private AbsoluteLayout mainLayout;
    @AutoGenerated
    private VerticalLayout verticalLayout_1;
    @AutoGenerated
    private Button openButton;
    @AutoGenerated
    private TextField snapshotFileLabel;
    @AutoGenerated
    private TextField tabNameLabel;

    public OpenSnapshotFileDialog(final TabSheet displayTabSheet, final Window windowHandle) {

        buildMainLayout();
        setCompositionRoot(mainLayout);

        openButton.addClickListener(new ClickListener() {

            DataState dataState;

            @Override
            public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {

                File snapshot = new File(snapshotFileLabel.getValue());

                SnapshotFileReader snapReader = new SnapshotFileReader(snapshot, 0);

                SnapshotView snapshotView;

                if (!validateInputs()) {
                    return;
                }

                try {

                    dataState = snapReader.readFuzzySnapshot();

                    snapshotView = new SnapshotView(dataState);

                } catch (CRCValidationException e) {

                    snapshotFileLabel
                            .setComponentError(new UserError("CRC validation problem. File is probably corrupted"));
                    return;

                } catch (IOException e) {

                    snapshotFileLabel.setComponentError(new UserError("IO problem with file"));
                    return;

                }

                HorizontalLayout horizontalLayout = new HorizontalLayout();
                horizontalLayout.setCaption(tabNameLabel.getValue());
                horizontalLayout.addComponent(snapshotView);
                horizontalLayout.setWidth("100%");
                horizontalLayout.setHeight("100%");
                Tab snapshotTab = displayTabSheet.addTab(horizontalLayout);
                snapshotTab.setClosable(true);
                displayTabSheet.setSelectedTab(snapshotTab);

                windowHandle.close();

            }

        });

    }

    boolean validateInputs() {

        boolean isOk = true;

        if (snapshotFileLabel.getValue().isEmpty()) {

            snapshotFileLabel.setComponentError(new UserError("File path must be provided"));
            isOk = false;

        } else {

            snapshotFileLabel.setComponentError(null);

        }

        if (tabNameLabel.getValue().isEmpty()) {

            tabNameLabel.setComponentError(new UserError("Name must be provided"));
            isOk = false;

        } else {

            tabNameLabel.setComponentError(null);

        }

        return isOk;

    }

    @AutoGenerated
    private AbsoluteLayout buildMainLayout() {
        // common part: create layout
        mainLayout = new AbsoluteLayout();
        mainLayout.setImmediate(false);
        mainLayout.setWidth("100%");
        mainLayout.setHeight("100%");

        // top-level component properties
        setWidth("100.0%");
        setHeight("100.0%");

        // verticalLayout_1
        verticalLayout_1 = buildVerticalLayout_1();
        mainLayout.addComponent(verticalLayout_1, "top:0.0px;left:0.0px;");

        return mainLayout;
    }

    @AutoGenerated
    private VerticalLayout buildVerticalLayout_1() {
        // common part: create layout
        verticalLayout_1 = new VerticalLayout();
        verticalLayout_1.setImmediate(false);
        verticalLayout_1.setWidth("-1px");
        verticalLayout_1.setHeight("-1px");
        verticalLayout_1.setMargin(true);
        verticalLayout_1.setSpacing(true);

        // textField_2
        tabNameLabel = new TextField();
        tabNameLabel.setCaption("Name");
        tabNameLabel.setImmediate(false);
        tabNameLabel.setWidth("-1px");
        tabNameLabel.setHeight("-1px");
        tabNameLabel.setRequired(true);
        verticalLayout_1.addComponent(tabNameLabel);

        // textField_1
        snapshotFileLabel = new TextField();
        snapshotFileLabel.setCaption("Snapshot file to open");
        snapshotFileLabel.setImmediate(false);
        snapshotFileLabel.setWidth("540px");
        snapshotFileLabel.setHeight("-1px");
        snapshotFileLabel.setRequired(true);
        verticalLayout_1.addComponent(snapshotFileLabel);

        // button_1
        openButton = new Button();
        openButton.setCaption("Open");
        openButton.setImmediate(true);
        openButton.setWidth("-1px");
        openButton.setHeight("-1px");
        verticalLayout_1.addComponent(openButton);

        return verticalLayout_1;
    }

}