serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatClient.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 04 May 2013 07:21:05 +0200
changeset 5 f0a52c72ef74
child 9 045360776205
permissions -rw-r--r--
Skeleton of client part of the chat application
jtulach@5
     1
/**
jtulach@5
     2
 * The MIT License (MIT)
jtulach@5
     3
 *
jtulach@5
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@5
     5
 *
jtulach@5
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jtulach@5
     7
 * of this software and associated documentation files (the "Software"), to deal
jtulach@5
     8
 * in the Software without restriction, including without limitation the rights
jtulach@5
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jtulach@5
    10
 * copies of the Software, and to permit persons to whom the Software is
jtulach@5
    11
 * furnished to do so, subject to the following conditions:
jtulach@5
    12
 *
jtulach@5
    13
 * The above copyright notice and this permission notice shall be included in
jtulach@5
    14
 * all copies or substantial portions of the Software.
jtulach@5
    15
 *
jtulach@5
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jtulach@5
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jtulach@5
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jtulach@5
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jtulach@5
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jtulach@5
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jtulach@5
    22
 * THE SOFTWARE.
jtulach@5
    23
 */
jtulach@5
    24
package org.apidesign.bck2brwsr.demo.serverside;
jtulach@5
    25
jtulach@5
    26
import net.java.html.json.ComputedProperty;
jtulach@5
    27
import net.java.html.json.Context;
jtulach@5
    28
import net.java.html.json.Function;
jtulach@5
    29
import net.java.html.json.Model;
jtulach@5
    30
import net.java.html.json.Property;
jtulach@5
    31
jtulach@5
    32
/**
jtulach@5
    33
 *
jtulach@5
    34
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@5
    35
 */
jtulach@5
    36
@Model(className = "ChatModel", properties = {
jtulach@5
    37
    @Property(name = "user", type = String.class),
jtulach@5
    38
    @Property(name = "text", type = String.class),
jtulach@5
    39
    @Property(name = "msgs", type = Message.class, array = true)
jtulach@5
    40
})
jtulach@5
    41
class ChatClient {
jtulach@5
    42
    @ComputedProperty
jtulach@5
    43
    static boolean sendEnabled(String user, String text) {
jtulach@5
    44
        boolean res = user != null && text != null && !user.isEmpty() && !text.isEmpty();
jtulach@5
    45
        try {
jtulach@5
    46
            if (true) throw new IllegalStateException("Query for msg '" + text + "' by user " + user + " res: " + res);
jtulach@5
    47
        } catch (Exception ex) {
jtulach@5
    48
            
jtulach@5
    49
        }
jtulach@5
    50
        return res;
jtulach@5
    51
    }
jtulach@5
    52
    
jtulach@5
    53
    @Function
jtulach@5
    54
    static void submit(ChatModel m) {
jtulach@5
    55
        if (!sendEnabled(m.getUser(), m.getText())) {
jtulach@5
    56
            return;
jtulach@5
    57
        }
jtulach@5
    58
        Message msg = new Message(CNTX);
jtulach@5
    59
        msg.setComment(m.getText());
jtulach@5
    60
        msg.setUser(m.getUser());
jtulach@5
    61
        m.getMsgs().add(msg);
jtulach@5
    62
    }
jtulach@5
    63
    
jtulach@5
    64
    static final Context CNTX = Context.findDefault(ChatClient.class);
jtulach@5
    65
    static {
jtulach@5
    66
        ChatModel chm = new ChatModel(CNTX);
jtulach@5
    67
        Message m = new Message(CNTX);
jtulach@5
    68
        m.setComment("Hello World!");
jtulach@5
    69
        m.setUser("Jarda");
jtulach@5
    70
        chm.getMsgs().add(m);
jtulach@5
    71
        chm.applyBindings();
jtulach@5
    72
    }
jtulach@5
    73
}