From 8bc65d96b56b3db36445c3ffd847eb3afc378748 Mon Sep 17 00:00:00 2001 From: Junji Takakura Date: Sun, 25 Jan 2015 11:25:07 +0900 Subject: [PATCH] Adds Web Storage Demo Application --- teavm-samples/pom.xml | 1 + teavm-samples/teavm-samples-storage/pom.xml | 110 ++++++++++++++++++ .../teavm/samples/storage/Application.java | 102 ++++++++++++++++ .../src/main/webapp/WEB-INF/web.xml | 21 ++++ .../src/main/webapp/index.html | 39 +++++++ 5 files changed, 273 insertions(+) create mode 100644 teavm-samples/teavm-samples-storage/pom.xml create mode 100644 teavm-samples/teavm-samples-storage/src/main/java/org/teavm/samples/storage/Application.java create mode 100644 teavm-samples/teavm-samples-storage/src/main/webapp/WEB-INF/web.xml create mode 100644 teavm-samples/teavm-samples-storage/src/main/webapp/index.html diff --git a/teavm-samples/pom.xml b/teavm-samples/pom.xml index 1b9dc40c4..6795fb884 100644 --- a/teavm-samples/pom.xml +++ b/teavm-samples/pom.xml @@ -32,5 +32,6 @@ teavm-samples-hello teavm-samples-benchmark + teavm-samples-storage \ No newline at end of file diff --git a/teavm-samples/teavm-samples-storage/pom.xml b/teavm-samples/teavm-samples-storage/pom.xml new file mode 100644 index 000000000..edfa3d4e1 --- /dev/null +++ b/teavm-samples/teavm-samples-storage/pom.xml @@ -0,0 +1,110 @@ + + + 4.0.0 + + + org.teavm + teavm-samples + 0.3.0-SNAPSHOT + + teavm-samples-storage + + war + + TeaVM Web Storage web application + A sample application that demonstrate how to use Web Storage API. + + + + org.teavm + teavm-classlib + ${project.version} + + + org.teavm + teavm-jso + ${project.version} + + + org.teavm + teavm-dom + ${project.version} + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + + + + maven-war-plugin + 2.4 + + + + ${project.build.directory}/generated/js + + + + + + org.teavm + teavm-maven-plugin + ${project.version} + + + web-client + prepare-package + + build-javascript + + + ${project.build.directory}/generated/js/teavm + org.teavm.samples.storage.Application + SEPARATE + false + true + true + true + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + ../../checkstyle.xml + config_loc=${basedir}/../.. + + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + \ No newline at end of file diff --git a/teavm-samples/teavm-samples-storage/src/main/java/org/teavm/samples/storage/Application.java b/teavm-samples/teavm-samples-storage/src/main/java/org/teavm/samples/storage/Application.java new file mode 100644 index 000000000..f75803c9d --- /dev/null +++ b/teavm-samples/teavm-samples-storage/src/main/java/org/teavm/samples/storage/Application.java @@ -0,0 +1,102 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * 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 org.teavm.samples.storage; + +import org.teavm.dom.browser.Storage; +import org.teavm.dom.browser.Window; +import org.teavm.dom.events.Event; +import org.teavm.dom.events.EventListener; +import org.teavm.dom.html.HTMLButtonElement; +import org.teavm.dom.html.HTMLDocument; +import org.teavm.dom.html.HTMLElement; +import org.teavm.dom.html.HTMLInputElement; +import org.teavm.jso.JS; + +public final class Application { + + private static Window window = (Window)JS.getGlobal(); + private static HTMLDocument document = window.getDocument(); + private static Storage storage = window.getSessionStorage(); + + private Application() { + } + + public static void main(String[] args) { + if (storage == null) { + window.alert("storage is not supported."); + } + + HTMLButtonElement saveButton = (HTMLButtonElement)document.getElementById("save-button"); + saveButton.addEventListener("click", new EventListener() { + @Override + public void handleEvent(Event evt) { + String key = ((HTMLInputElement)document.getElementById("key")).getValue(); + String value = ((HTMLInputElement)document.getElementById("value")).getValue(); + + if (key != null && key.length() > 0 && value != null && value.length() > 0) { + storage.setItem(key, value); + draw(); + } + } + }); + HTMLButtonElement deleteButton = (HTMLButtonElement)document.getElementById("delete-button"); + deleteButton.addEventListener("click", new EventListener() { + @Override + public void handleEvent(Event evt) { + String key = ((HTMLInputElement)document.getElementById("key")).getValue(); + + if (key != null && key.length() > 0) { + storage.removeItem(key); + draw(); + } + } + }); + HTMLButtonElement deleteAllButton = (HTMLButtonElement)document.getElementById("delete-all-button"); + deleteAllButton.addEventListener("click", new EventListener() { + @Override + public void handleEvent(Event evt) { + storage.clear(); + draw(); + } + }); + draw(); + } + + private static void draw() { + HTMLElement tbody = document.getElementById("list"); + + while (tbody.getFirstChild() != null) { + tbody.removeChild(tbody.getFirstChild()); + } + + for (int i = 0; i < storage.getLength(); i++) { + String key = storage.key(i); + String value = storage.getItem(key); + + HTMLElement tdKey = document.createElement("td"); + tdKey.appendChild(document.createTextNode(key)); + + HTMLElement tdValue = document.createElement("td"); + tdValue.appendChild(document.createTextNode(value)); + + HTMLElement tr = document.createElement("tr"); + tr.appendChild(tdKey); + tr.appendChild(tdValue); + + tbody.appendChild(tr); + } + } +} diff --git a/teavm-samples/teavm-samples-storage/src/main/webapp/WEB-INF/web.xml b/teavm-samples/teavm-samples-storage/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..bfc410b12 --- /dev/null +++ b/teavm-samples/teavm-samples-storage/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/teavm-samples/teavm-samples-storage/src/main/webapp/index.html b/teavm-samples/teavm-samples-storage/src/main/webapp/index.html new file mode 100644 index 000000000..a961da445 --- /dev/null +++ b/teavm-samples/teavm-samples-storage/src/main/webapp/index.html @@ -0,0 +1,39 @@ + + + + + Web Storage web application + + + + + +

Web Storage web application

+ + + + + + + + + + + +
KeyValue
+ + \ No newline at end of file