package org.vaadin.elements.demo;

import java.util.Optional;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import org.vaadin.elements.Element;
import org.vaadin.elements.Elements;
import org.vaadin.elements.Root;
import org.vaadin.elements.TextNode;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
@Theme("valo")
public class Demo extends UI {

    @WebServlet(value = "/*", asyncSupported = true, loadOnStartup = 1)
    @VaadinServletConfiguration(productionMode = false, ui = Demo.class)
    public static class Servlet extends VaadinServlet {
        @Override
        protected void servletInitialized() throws ServletException {
            super.servletInitialized();
            // XXX Workaround until we can use beta2
            getService().addSessionInitListener(
                    e -> e.getSession().addBootstrapListener(
                            new BootstrapListenerImplementation()));
        }
    }

    @Override
    protected void init(VaadinRequest request) {
        TabSheet tabSheet = new TabSheet();
        tabSheet.setSizeFull();
        tabSheet.addTab(new Html5InputDemo(), "HTML5 inputs");
        // Does not work in Firefox
        // tabSheet.addTab(new GoogleMapDemo(), "Web components");
        tabSheet.addTab(new ExistingElementsDemo(), "Existing elements");
        tabSheet.addTab(new PaperElementsDemo(), "Paper compoments");

        VerticalLayout layout = new VerticalLayout(tabSheet);
        layout.setSizeFull();
        layout.setMargin(true);
        setContent(layout);
        // Root root = ElementIntegration.getRoot(this);

        // demoNewElements(root);

        // demoExistingElements(root);

        // demoPaperComponents(root);
    }

    private void demoExistingElements(Root root) {
        Button button = new Button("Test", c -> {
            root.fetchDom(() -> Notification.show(root.asHtml()));
        });

        Label label = new Label("Test");
        VerticalLayout layout = new VerticalLayout(button, label);

        setContent(layout);

        root.fetchDom(() -> {
            Optional<Element> span = root.querySelector("span > span");
            span.ifPresent(s -> s.setAttribute("style", "color: blue"));
        }, layout, button, label);
    }

    private void demoNewElements(Root root) {
        root.setInnerHtml("<h1>Hello world</h1>");
        TextNode text = Elements.createText("Hello");
        root.appendChild(text);

        Element h1 = root.querySelector("h1").get();

        h1.addEventListener("click", arguments -> {
            h1.setAttribute("style", "color: blue");
        });
    }
}
@@ -32,36 +32,8 @@
 o;%0A%0A
-import java.util.Optional;%0A%0A
 impo
@@ -116,152 +116,8 @@
 t;%0A%0A
-import org.vaadin.elements.Element;%0Aimport org.vaadin.elements.Elements;%0Aimport org.vaadin.elements.Root;%0Aimport org.vaadin.elements.TextNode;%0A%0A
 impo
@@ -291,100 +291,8 @@
 et;%0A
-import com.vaadin.ui.Button;%0Aimport com.vaadin.ui.Label;%0Aimport com.vaadin.ui.Notification;%0A
 impo
@@ -1641,1077 +1641,8 @@
 t);%0A
-        // Root root = ElementIntegration.getRoot(this);%0A%0A        // demoNewElements(root);%0A%0A        // demoExistingElements(root);%0A%0A        // demoPaperComponents(root);%0A    %7D%0A%0A    private void demoExistingElements(Root root) %7B%0A        Button button = new Button(%22Test%22, c -%3E %7B%0A            root.fetchDom(() -%3E Notification.show(root.asHtml()));%0A        %7D);%0A%0A        Label label = new Label(%22Test%22);%0A        VerticalLayout layout = new VerticalLayout(button, label);%0A%0A        setContent(layout);%0A%0A        root.fetchDom(() -%3E %7B%0A            Optional%3CElement%3E span = root.querySelector(%22span %3E span%22);%0A            span.ifPresent(s -%3E s.setAttribute(%22style%22, %22color: blue%22));%0A        %7D, layout, button, label);%0A    %7D%0A%0A    private void demoNewElements(Root root) %7B%0A        root.setInnerHtml(%22%3Ch1%3EHello world%3C/h1%3E%22);%0A        TextNode text = Elements.createText(%22Hello%22);%0A        root.appendChild(text);%0A%0A        Element h1 = root.querySelector(%22h1%22).get();%0A%0A        h1.addEventListener(%22click%22, arguments -%3E %7B%0A            h1.setAttribute(%22style%22, %22color: blue%22);%0A        %7D);%0A
     

Remove deprectated demo code