1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java Tue Apr 26 08:04:12 2016 +0200
1.3 @@ -0,0 +1,79 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.kosample;
1.22 +
1.23 +import net.java.html.json.ComputedProperty;
1.24 +import net.java.html.json.Function;
1.25 +import net.java.html.json.Model;
1.26 +import net.java.html.json.Property;
1.27 +import org.apidesign.bck2brwsr.kosample.js.Dialogs;
1.28 +
1.29 +/** Model annotation generates class Data with
1.30 + * one message property, boolean property and read only words property
1.31 + */
1.32 +@Model(className = "Data", targetId="", properties = {
1.33 + @Property(name = "message", type = String.class),
1.34 + @Property(name = "rotating", type = boolean.class)
1.35 +})
1.36 +final class DataModel {
1.37 + @ComputedProperty static java.util.List<String> words(String message) {
1.38 + String[] arr = new String[6];
1.39 + String[] words = message == null ? new String[0] : message.split(" ", 6);
1.40 + for (int i = 0; i < 6; i++) {
1.41 + arr[i] = words.length > i ? words[i] : "!";
1.42 + }
1.43 + return java.util.Arrays.asList(arr);
1.44 + }
1.45 +
1.46 + @Function static void turnAnimationOn(Data model) {
1.47 + model.setRotating(true);
1.48 + }
1.49 +
1.50 + @Function static void turnAnimationOff(final Data model) {
1.51 + Dialogs.confirmByUser("Really turn off?", new Runnable() {
1.52 + @Override
1.53 + public void run() {
1.54 + model.setRotating(false);
1.55 + }
1.56 + });
1.57 + }
1.58 +
1.59 + @Function static void rotate5s(final Data model) {
1.60 + model.setRotating(true);
1.61 + java.util.Timer timer = new java.util.Timer("Rotates a while");
1.62 + timer.schedule(new java.util.TimerTask() {
1.63 + @Override
1.64 + public void run() {
1.65 + model.setRotating(false);
1.66 + }
1.67 + }, 5000);
1.68 + }
1.69 +
1.70 + @Function static void showScreenSize(Data model) {
1.71 + model.setMessage(Dialogs.screenSize());
1.72 + }
1.73 + private static Data ui;
1.74 + /**
1.75 + * Called when the page is ready.
1.76 + */
1.77 + static void onPageLoad() throws Exception {
1.78 + ui = new Data();
1.79 + ui.setMessage("Hello World from HTML and Java!");
1.80 + ui.applyBindings();
1.81 + }
1.82 +}