ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Apr 2016 04:01:39 +0200
changeset 1944 644d4f4bc6e0
parent 1941 621825e167d7
child 1947 12a252145892
permissions -rw-r--r--
Using lamdas to schedule tasks
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.kosample;
    19 
    20 import java.util.TimerTask;
    21 import net.java.html.json.ComputedProperty;
    22 import net.java.html.json.Function;
    23 import net.java.html.json.Model;
    24 import net.java.html.json.Property;
    25 import org.apidesign.bck2brwsr.kosample.js.Dialogs;
    26 
    27 /** Model annotation generates class Data with
    28  * one message property, boolean property and read only words property
    29  */
    30 @Model(className = "Data", targetId="", properties = {
    31     @Property(name = "message", type = String.class),
    32     @Property(name = "rotating", type = boolean.class)
    33 })
    34 final class DataModel {
    35     @ComputedProperty static java.util.List<String> words(String message) {
    36         String[] arr = new String[6];
    37         String[] words = message == null ? new String[0] : message.split(" ", 6);
    38         for (int i = 0; i < 6; i++) {
    39             arr[i] = words.length > i ? words[i] : "!";
    40         }
    41         return java.util.Arrays.asList(arr);
    42     }
    43 
    44     @Function static void turnAnimationOn(Data model) {
    45         model.setRotating(true);
    46     }
    47 
    48     @Function static void turnAnimationOff(final Data model) {
    49         Dialogs.confirmByUser("Really turn off?", new Runnable() {
    50             @Override
    51             public void run() {
    52                 model.setRotating(false);
    53             }
    54         });
    55     }
    56 
    57     private static java.util.Timer TIMER = new java.util.Timer("Pending tasks");
    58     private static void schedule(Runnable run, long delay) {
    59         TIMER.schedule(new TimerTask() {
    60             @Override
    61             public void run() {
    62                 run.run();
    63             }
    64         }, delay);
    65     }
    66 
    67     @Function static void rotate5s(final Data model) {
    68         model.setRotating(true);
    69         schedule(() -> model.setRotating(false), 5000);
    70     }
    71 
    72     @Function static void showScreenSize(Data model) {
    73         model.setMessage(Dialogs.screenSize());
    74     }
    75     private static Data ui;
    76     /**
    77      * Called when the page is ready.
    78      */
    79     static void onPageLoad() throws Exception {
    80         ui = new Data();
    81         ui.setMessage("Hello World from HTML and Java!");
    82         ui.applyBindings();
    83     }
    84 }