1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatClient.java Sat May 04 07:21:05 2013 +0200
1.3 @@ -0,0 +1,73 @@
1.4 +/**
1.5 + * The MIT License (MIT)
1.6 + *
1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.8 + *
1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1.10 + * of this software and associated documentation files (the "Software"), to deal
1.11 + * in the Software without restriction, including without limitation the rights
1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1.13 + * copies of the Software, and to permit persons to whom the Software is
1.14 + * furnished to do so, subject to the following conditions:
1.15 + *
1.16 + * The above copyright notice and this permission notice shall be included in
1.17 + * all copies or substantial portions of the Software.
1.18 + *
1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1.25 + * THE SOFTWARE.
1.26 + */
1.27 +package org.apidesign.bck2brwsr.demo.serverside;
1.28 +
1.29 +import net.java.html.json.ComputedProperty;
1.30 +import net.java.html.json.Context;
1.31 +import net.java.html.json.Function;
1.32 +import net.java.html.json.Model;
1.33 +import net.java.html.json.Property;
1.34 +
1.35 +/**
1.36 + *
1.37 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.38 + */
1.39 +@Model(className = "ChatModel", properties = {
1.40 + @Property(name = "user", type = String.class),
1.41 + @Property(name = "text", type = String.class),
1.42 + @Property(name = "msgs", type = Message.class, array = true)
1.43 +})
1.44 +class ChatClient {
1.45 + @ComputedProperty
1.46 + static boolean sendEnabled(String user, String text) {
1.47 + boolean res = user != null && text != null && !user.isEmpty() && !text.isEmpty();
1.48 + try {
1.49 + if (true) throw new IllegalStateException("Query for msg '" + text + "' by user " + user + " res: " + res);
1.50 + } catch (Exception ex) {
1.51 +
1.52 + }
1.53 + return res;
1.54 + }
1.55 +
1.56 + @Function
1.57 + static void submit(ChatModel m) {
1.58 + if (!sendEnabled(m.getUser(), m.getText())) {
1.59 + return;
1.60 + }
1.61 + Message msg = new Message(CNTX);
1.62 + msg.setComment(m.getText());
1.63 + msg.setUser(m.getUser());
1.64 + m.getMsgs().add(msg);
1.65 + }
1.66 +
1.67 + static final Context CNTX = Context.findDefault(ChatClient.class);
1.68 + static {
1.69 + ChatModel chm = new ChatModel(CNTX);
1.70 + Message m = new Message(CNTX);
1.71 + m.setComment("Hello World!");
1.72 + m.setUser("Jarda");
1.73 + chm.getMsgs().add(m);
1.74 + chm.applyBindings();
1.75 + }
1.76 +}