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

Java tutorial

Introduction

Here is the source code for com.zklogtool.web.components.SnapshotView.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.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;
import com.zklogtool.data.DataState;
import com.zklogtool.printer.DataNodePrinter;
import com.zklogtool.printer.UnicodeDecoder;
import com.zklogtool.util.Util;

public class SnapshotView extends CustomComponent {

    @AutoGenerated
    private AbsoluteLayout mainLayout;
    @AutoGenerated
    private HorizontalSplitPanel horizontalSplitPanel_1;
    @AutoGenerated
    private VerticalLayout verticalLayout_2;
    @AutoGenerated
    private Label description;
    @AutoGenerated
    private VerticalLayout verticalLayout_1;
    @AutoGenerated
    private Tree sessionTree;
    @AutoGenerated
    private Label label_5;
    @AutoGenerated
    private Tree dataTree;
    @AutoGenerated
    private Label label_4;
    private DataState dataState;

    public SnapshotView(DataState dataState) {

        buildMainLayout();
        setCompositionRoot(mainLayout);

        dataTree.setImmediate(true);
        sessionTree.setImmediate(true);
        description.setContentMode(ContentMode.PREFORMATTED);

        this.dataState = dataState;

        TreeNode parent = new TreeNode("/", "");

        dataTree.addItem(parent);

        dataTree.setChildrenAllowed(parent, true);

        createDataTree(parent);

        createSessionTree();

    }

    private void createSessionTree() {

        final String parent = "sessions";

        final Map<String, Integer> sessionMap = new HashMap<String, Integer>();

        Iterator i = dataState.getSessions().entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry pairs = (Map.Entry) i.next();
            sessionMap.put(Util.longToHexString((Long) pairs.getKey()), (Integer) pairs.getValue());
        }

        sessionTree.addItem(parent);

        if (sessionMap.isEmpty()) {

            sessionTree.setChildrenAllowed(parent, false);

        } else {

            sessionTree.setChildrenAllowed(parent, true);

            Iterator it = sessionMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry) it.next();
                sessionTree.addItem(pairs.getKey());
                sessionTree.setChildrenAllowed(pairs.getKey(), false);
                sessionTree.setParent(pairs.getKey(), parent);
            }

        }

        sessionTree.addValueChangeListener(new Property.ValueChangeListener() {

            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                // TODO Auto-generated method stub

                if (sessionTree.getValue() == null)
                    return;

                StringBuilder descriptionStringBuilder = new StringBuilder();

                String session = sessionTree.getValue().toString();

                if (session == null ? parent != null : !session.equals(parent)) {

                    descriptionStringBuilder.append("Session id:\t").append(session).append("\n");
                    descriptionStringBuilder.append("Timeout:\t").append(sessionMap.get(session)).append("\n");
                    description.setValue(descriptionStringBuilder.toString());

                }

            }

        });

    }

    private void createDataTree(TreeNode parent) {

        Set<String> children = dataState.getChildren(parent.getFullPath());

        for (String child : children) {

            TreeNode item = new TreeNode(child, parent.getFullPath() + "/" + child);

            dataTree.addItem(item);
            dataTree.setParent(item, parent);

            if (dataState.getChildren(item.getFullPath()) != null
                    && dataState.getChildren(item.getFullPath()).size() != 0) {

                dataTree.setChildrenAllowed(item, true);
                createDataTree(item);

            } else {

                dataTree.setChildrenAllowed(item, false);
            }

        }

        dataTree.addValueChangeListener(new Property.ValueChangeListener() {

            @Override
            public void valueChange(Property.ValueChangeEvent event) {

                Object value = dataTree.getValue();

                if (value == null) {
                    return;
                }

                String selectedValue = ((TreeNode) value).getFullPath();

                if (!selectedValue.contentEquals("/")) {

                    StringBuilder descriptionStringBuilder = new StringBuilder();

                    descriptionStringBuilder.append("Path:\t\t").append(selectedValue).append("\n");

                    DataNodePrinter printer = new DataNodePrinter(descriptionStringBuilder, new UnicodeDecoder());

                    printer.printDataNode(dataState.getNode(selectedValue), dataState);

                    description.setValue(descriptionStringBuilder.toString());

                }

            }

        });

    }

    class TreeNode {

        String name;
        String fullPath;

        public TreeNode(String name, String fullPath) {
            this.name = name;
            this.fullPath = fullPath;
        }

        @Override
        public String toString() {
            return name;
        }

        public String getFullPath() {
            return fullPath;
        }

    }

    @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%");

        // horizontalSplitPanel_1
        horizontalSplitPanel_1 = buildHorizontalSplitPanel_1();
        mainLayout.addComponent(horizontalSplitPanel_1, "top:0.0px;right:0.0px;bottom:8.0px;left:0.0px;");

        return mainLayout;
    }

    @AutoGenerated
    private HorizontalSplitPanel buildHorizontalSplitPanel_1() {
        // common part: create layout
        horizontalSplitPanel_1 = new HorizontalSplitPanel();
        horizontalSplitPanel_1.setImmediate(false);
        horizontalSplitPanel_1.setWidth("100.0%");
        horizontalSplitPanel_1.setHeight("100.0%");

        // verticalLayout_1
        verticalLayout_1 = buildVerticalLayout_1();
        horizontalSplitPanel_1.addComponent(verticalLayout_1);

        // verticalLayout_2
        verticalLayout_2 = buildVerticalLayout_2();
        horizontalSplitPanel_1.addComponent(verticalLayout_2);

        return horizontalSplitPanel_1;
    }

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

        // label_4
        label_4 = new Label();
        label_4.setImmediate(false);
        label_4.setWidth("-1px");
        label_4.setHeight("-1px");
        label_4.setValue("Data Tree:");
        verticalLayout_1.addComponent(label_4);

        // dataTree
        dataTree = new Tree();
        dataTree.setImmediate(false);
        dataTree.setWidth("-1px");
        dataTree.setHeight("-1px");
        verticalLayout_1.addComponent(dataTree);

        // label_5
        label_5 = new Label();
        label_5.setImmediate(false);
        label_5.setWidth("-1px");
        label_5.setHeight("-1px");
        label_5.setValue("Sessions:");
        verticalLayout_1.addComponent(label_5);

        // sessionTree
        sessionTree = new Tree();
        sessionTree.setImmediate(false);
        sessionTree.setWidth("-1px");
        sessionTree.setHeight("-1px");
        verticalLayout_1.addComponent(sessionTree);

        return verticalLayout_1;
    }

    @AutoGenerated
    private VerticalLayout buildVerticalLayout_2() {
        // common part: create layout
        verticalLayout_2 = new VerticalLayout();
        verticalLayout_2.setImmediate(false);
        verticalLayout_2.setWidth("100.0%");
        verticalLayout_2.setHeight("100.0%");
        verticalLayout_2.setMargin(true);

        // description
        description = new Label();
        description.setImmediate(false);
        description.setWidth("-1px");
        description.setHeight("-1px");
        description.setValue("Select znode or session");
        verticalLayout_2.addComponent(description);

        return verticalLayout_2;
    }

}