ro.jtonic.handson.HandsonVaadinApplication.java Source code

Java tutorial

Introduction

Here is the source code for ro.jtonic.handson.HandsonVaadinApplication.java

Source

/*
 * Copyright 2009 IT Mill Ltd.
 *
 * 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 ro.jtonic.handson;

import com.google.gwt.thirdparty.guava.common.collect.Lists;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PopupView;
import com.vaadin.ui.Table;
import com.vaadin.ui.Tree;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

import javax.servlet.annotation.WebServlet;
import java.util.List;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class HandsonVaadinApplication extends UI {

    private Button btn;

    private NativeSelect sel1;

    private Table tbl;

    private final Panel p = new Panel("Persons");

    private final Tree tree = new Tree();

    @Override
    protected void init(VaadinRequest vaadinRequest) {

        setTheme("runo");

        btn = new Button("Click me!!!");
        final VerticalLayout layout = new VerticalLayout(btn);
        layout.addComponent(btn);

        sel1 = new NativeSelect("Country: ");
        sel1.setNullSelectionAllowed(false);
        sel1.setMultiSelect(false);
        sel1.addItems(Lists.asList("Romania", new String[] { "USA" }));
        sel1.select("Romania");
        layout.addComponent(sel1);

        BeanContainer<String, Person> persBeans = new BeanContainer<>(Person.class);
        persBeans.setBeanIdProperty("id");

        final List<Person> persons = Lists.newArrayList();
        persons.add(new Person("1", "Antonel", "Pazargic", 45));
        persons.add(new Person("2", "Liviu", "Pazargic", 32));
        persBeans.addAll(persons);

        tbl = new Table("Persons", persBeans);
        layout.addComponent(tbl);

        tree.setCaption("PM tree");
        tree.setMultiSelect(true);

        List<PmcVo> pms = PmcVo.getPms();
        fillInTree(pms);
        layout.addComponent(tree);

        p.setHeightUndefined();
        p.setWidth("300px");
        p.setContent(layout);

        setContent(p);

        // events
        setupEvents();
    }

    private void fillInTree(List<PmcVo> pmcs) {
        for (PmcVo pmc : pmcs) {
            tree.addItem(pmc);
        }
    }

    private void setupEvents() {
        btn.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent clickEvent) {
                sel1.setEnabled(false);
                VerticalLayout vl = new VerticalLayout();
                final Label fNameLbl = new Label("First name: ");
                final Label lNameLbl = new Label("Last name: ");
                vl.addComponents(fNameLbl, lNameLbl);

                PopupView view = new PopupView("Pop it up!!!", fNameLbl);
                ((AbstractLayout) HandsonVaadinApplication.this.p.getContent()).addComponent(view);
                view.setVisible(true);
            }
        });

        tree.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                System.err.println("Selected value: " + event.getProperty().getValue());
            }
        });

        tree.addExpandListener(new Tree.ExpandListener() {
            @Override
            public void nodeExpand(Tree.ExpandEvent event) {
                final Object selectedObj = event.getItemId();
                System.out.println("selectedObj = " + selectedObj);

                if (selectedObj instanceof Visitable) {
                    Visitable visitable = (Visitable) selectedObj;
                    if (visitable.isVisited()) {
                        System.out.println(String.format("The node %s was already visited.", selectedObj));
                        return;
                    }
                    if (selectedObj instanceof PmcVo) {
                        final long crtTime = System.currentTimeMillis();
                        final PmcVo.PmcyVo pmcyVo1 = new PmcVo.PmcyVo("PmcyVo1_" + crtTime, "2014 " + crtTime);
                        tree.addItem(pmcyVo1);
                        tree.setChildrenAllowed(pmcyVo1, true);
                        tree.setParent(pmcyVo1, selectedObj);

                        final PmcVo.PmcyVo pmcyVo2 = new PmcVo.PmcyVo("Pmcy2Vo_" + crtTime, "2015 " + crtTime);
                        tree.setChildrenAllowed(pmcyVo2, true);
                        tree.addItem(pmcyVo2);
                        tree.setParent(pmcyVo2, selectedObj);
                    } else if (selectedObj instanceof PmcVo.PmcyVo) {
                        final PmcVo.PmVo pmVo1 = new PmcVo.PmVo("PmVo1_" + System.currentTimeMillis(),
                                "PmVo1_" + System.currentTimeMillis());
                        tree.setChildrenAllowed(pmVo1, false);
                        tree.addItem(pmVo1);
                        tree.setParent(pmVo1, selectedObj);

                        final PmcVo.PmVo pmVo2 = new PmcVo.PmVo("PmVo2_" + System.currentTimeMillis(),
                                "PmVo2_" + System.currentTimeMillis());
                        tree.setChildrenAllowed(pmVo2, false);
                        tree.addItem(pmVo2);
                        tree.setParent(pmVo2, selectedObj);
                    }
                    visitable.setVisited(true);
                }
            }
        });
    }

    @WebServlet(urlPatterns = "/*")
    @VaadinServletConfiguration(productionMode = false, ui = HandsonVaadinApplication.class)
    public static class MyUIServlet extends VaadinServlet {

    }

}