jaroslav@1941
|
1 |
/**
|
jaroslav@1941
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1941
|
3 |
* Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1941
|
4 |
*
|
jaroslav@1941
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1941
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1941
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1941
|
8 |
*
|
jaroslav@1941
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1941
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1941
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1941
|
12 |
* GNU General Public License for more details.
|
jaroslav@1941
|
13 |
*
|
jaroslav@1941
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1941
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1941
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1941
|
17 |
*/
|
jaroslav@1941
|
18 |
package org.apidesign.bck2brwsr.kosample;
|
jaroslav@1941
|
19 |
|
jaroslav@1947
|
20 |
import java.util.Timer;
|
jaroslav@1944
|
21 |
import java.util.TimerTask;
|
jaroslav@1941
|
22 |
import net.java.html.json.ComputedProperty;
|
jaroslav@1941
|
23 |
import net.java.html.json.Function;
|
jaroslav@1941
|
24 |
import net.java.html.json.Model;
|
jaroslav@1947
|
25 |
import net.java.html.json.ModelOperation;
|
jaroslav@1941
|
26 |
import net.java.html.json.Property;
|
jaroslav@1941
|
27 |
import org.apidesign.bck2brwsr.kosample.js.Dialogs;
|
jaroslav@1941
|
28 |
|
jaroslav@1941
|
29 |
/** Model annotation generates class Data with
|
jaroslav@1941
|
30 |
* one message property, boolean property and read only words property
|
jaroslav@1941
|
31 |
*/
|
jaroslav@1947
|
32 |
@Model(className = "Data", targetId="", instance = true, properties = {
|
jaroslav@1941
|
33 |
@Property(name = "message", type = String.class),
|
jaroslav@1941
|
34 |
@Property(name = "rotating", type = boolean.class)
|
jaroslav@1941
|
35 |
})
|
jaroslav@1941
|
36 |
final class DataModel {
|
jaroslav@1941
|
37 |
@ComputedProperty static java.util.List<String> words(String message) {
|
jaroslav@1941
|
38 |
String[] arr = new String[6];
|
jaroslav@1941
|
39 |
String[] words = message == null ? new String[0] : message.split(" ", 6);
|
jaroslav@1941
|
40 |
for (int i = 0; i < 6; i++) {
|
jaroslav@1941
|
41 |
arr[i] = words.length > i ? words[i] : "!";
|
jaroslav@1941
|
42 |
}
|
jaroslav@1941
|
43 |
return java.util.Arrays.asList(arr);
|
jaroslav@1941
|
44 |
}
|
jaroslav@1941
|
45 |
|
jaroslav@1941
|
46 |
@Function static void turnAnimationOn(Data model) {
|
jaroslav@1941
|
47 |
model.setRotating(true);
|
jaroslav@1941
|
48 |
}
|
jaroslav@1941
|
49 |
|
jaroslav@1941
|
50 |
@Function static void turnAnimationOff(final Data model) {
|
jaroslav@1941
|
51 |
Dialogs.confirmByUser("Really turn off?", new Runnable() {
|
jaroslav@1941
|
52 |
@Override
|
jaroslav@1941
|
53 |
public void run() {
|
jaroslav@1941
|
54 |
model.setRotating(false);
|
jaroslav@1941
|
55 |
}
|
jaroslav@1941
|
56 |
});
|
jaroslav@1941
|
57 |
}
|
jaroslav@1941
|
58 |
|
jaroslav@1947
|
59 |
private static final Timer TIMER = new Timer("Pending tasks");
|
jaroslav@1944
|
60 |
private static void schedule(Runnable run, long delay) {
|
jaroslav@1944
|
61 |
TIMER.schedule(new TimerTask() {
|
jaroslav@1944
|
62 |
@Override
|
jaroslav@1944
|
63 |
public void run() {
|
jaroslav@1944
|
64 |
run.run();
|
jaroslav@1944
|
65 |
}
|
jaroslav@1944
|
66 |
}, delay);
|
jaroslav@1944
|
67 |
}
|
jaroslav@1944
|
68 |
|
jaroslav@1941
|
69 |
@Function static void rotate5s(final Data model) {
|
jaroslav@1941
|
70 |
model.setRotating(true);
|
jaroslav@1944
|
71 |
schedule(() -> model.setRotating(false), 5000);
|
jaroslav@1941
|
72 |
}
|
jaroslav@1941
|
73 |
|
jaroslav@1941
|
74 |
@Function static void showScreenSize(Data model) {
|
jaroslav@1941
|
75 |
model.setMessage(Dialogs.screenSize());
|
jaroslav@1941
|
76 |
}
|
jaroslav@1941
|
77 |
private static Data ui;
|
jaroslav@1941
|
78 |
/**
|
jaroslav@1941
|
79 |
* Called when the page is ready.
|
jaroslav@1941
|
80 |
*/
|
jaroslav@1941
|
81 |
static void onPageLoad() throws Exception {
|
jaroslav@1941
|
82 |
ui = new Data();
|
jaroslav@1941
|
83 |
ui.setMessage("Hello World from HTML and Java!");
|
jaroslav@1941
|
84 |
ui.applyBindings();
|
jaroslav@1947
|
85 |
|
jaroslav@1947
|
86 |
schedule(() -> ui.startTest(), 1000);
|
jaroslav@1947
|
87 |
}
|
jaroslav@1947
|
88 |
|
jaroslav@1947
|
89 |
//
|
jaroslav@1947
|
90 |
// testing
|
jaroslav@1947
|
91 |
//
|
jaroslav@1947
|
92 |
|
jaroslav@1947
|
93 |
@ModelOperation
|
jaroslav@1947
|
94 |
static void startTest(Data model) {
|
jaroslav@1947
|
95 |
Dialogs.triggerClick("beginTest");
|
jaroslav@1947
|
96 |
}
|
jaroslav@1947
|
97 |
|
jaroslav@1947
|
98 |
private boolean inTesting;
|
jaroslav@1947
|
99 |
|
jaroslav@1947
|
100 |
@Function
|
jaroslav@1947
|
101 |
void beginTest(Data model) {
|
jaroslav@1947
|
102 |
if (inTesting) {
|
jaroslav@1947
|
103 |
model.setRotating(false);
|
jaroslav@1947
|
104 |
model.setMessage("Hello World from HTML and Java!");
|
jaroslav@1947
|
105 |
inTesting = false;
|
jaroslav@1947
|
106 |
return;
|
jaroslav@1947
|
107 |
}
|
jaroslav@1947
|
108 |
|
jaroslav@1947
|
109 |
inTesting = true;
|
jaroslav@1947
|
110 |
model.setMessage("In testing mode stop Automatic testing?");
|
jaroslav@1947
|
111 |
model.setRotating(true);
|
jaroslav@1947
|
112 |
schedule(() -> {
|
jaroslav@1947
|
113 |
if (inTesting) {
|
jaroslav@1947
|
114 |
model.setMessage("In testing mode count down 3s");
|
jaroslav@1947
|
115 |
}
|
jaroslav@1947
|
116 |
}, 3000);
|
jaroslav@1947
|
117 |
schedule(() -> {
|
jaroslav@1947
|
118 |
if (inTesting) {
|
jaroslav@1947
|
119 |
model.setMessage("In testing mode count down 2s");
|
jaroslav@1947
|
120 |
}
|
jaroslav@1947
|
121 |
}, 4000);
|
jaroslav@1947
|
122 |
schedule(() -> {
|
jaroslav@1947
|
123 |
if (inTesting) {
|
jaroslav@1947
|
124 |
model.setMessage("In testing mode count down 1s");
|
jaroslav@1947
|
125 |
}
|
jaroslav@1947
|
126 |
}, 5000);
|
jaroslav@1947
|
127 |
schedule(() -> {
|
jaroslav@1947
|
128 |
if (inTesting) {
|
jaroslav@1947
|
129 |
model.setMessage("Finished testing mode close the browser");
|
jaroslav@1947
|
130 |
model.setRotating(false);
|
jaroslav@1947
|
131 |
System.exit(0);
|
jaroslav@1947
|
132 |
}
|
jaroslav@1947
|
133 |
}, 6000);
|
jaroslav@1941
|
134 |
}
|
jaroslav@1941
|
135 |
}
|