2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.kosample;
20 import java.util.Timer;
21 import java.util.TimerTask;
22 import net.java.html.json.ComputedProperty;
23 import net.java.html.json.Function;
24 import net.java.html.json.Model;
25 import net.java.html.json.ModelOperation;
26 import net.java.html.json.Property;
27 import org.apidesign.bck2brwsr.kosample.js.Dialogs;
29 /** Model annotation generates class Data with
30 * one message property, boolean property and read only words property
32 @Model(className = "Data", targetId="", instance = true, properties = {
33 @Property(name = "message", type = String.class),
34 @Property(name = "rotating", type = boolean.class)
36 final class DataModel {
37 @ComputedProperty static java.util.List<String> words(String message) {
38 String[] arr = new String[6];
39 String[] words = message == null ? new String[0] : message.split(" ", 6);
40 for (int i = 0; i < 6; i++) {
41 arr[i] = words.length > i ? words[i] : "!";
43 return java.util.Arrays.asList(arr);
46 @Function static void turnAnimationOn(Data model) {
47 model.setRotating(true);
50 @Function static void turnAnimationOff(final Data model) {
51 Dialogs.confirmByUser("Really turn off?", new Runnable() {
54 model.setRotating(false);
59 private static final Timer TIMER = new Timer("Pending tasks");
60 private static void schedule(Runnable run, long delay) {
61 TIMER.schedule(new TimerTask() {
69 @Function static void rotate5s(final Data model) {
70 model.setRotating(true);
71 schedule(() -> model.setRotating(false), 5000);
74 @Function static void showScreenSize(Data model) {
75 model.setMessage(Dialogs.screenSize());
77 private static Data ui;
79 * Called when the page is ready.
81 static void onPageLoad() throws Exception {
83 ui.setMessage("Hello World from HTML and Java!");
86 schedule(() -> ui.startTest(), 1000);
94 static void startTest(Data model) {
95 Dialogs.triggerClick("beginTest");
98 private boolean inTesting;
101 void beginTest(Data model) {
103 model.setRotating(false);
104 model.setMessage("Hello World from HTML and Java!");
110 model.setMessage("In testing mode stop Automatic testing?");
111 model.setRotating(true);
114 model.setMessage("In testing mode count down 3s");
119 model.setMessage("In testing mode count down 2s");
124 model.setMessage("In testing mode count down 1s");
129 model.setMessage("Finished testing mode close the browser");
130 model.setRotating(false);