javaquery/demo-twitter/src/main/java/org/apidesign/bck2brwsr/demo/twitter/TwitterClient.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/javaquery/demo-twitter/src/main/java/org/apidesign/bck2brwsr/demo/twitter/TwitterClient.java Thu Apr 04 13:12:17 2013 +0200
1.3 @@ -0,0 +1,137 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 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.demo.twitter;
1.22 +
1.23 +import java.util.Arrays;
1.24 +import java.util.List;
1.25 +import org.apidesign.bck2brwsr.htmlpage.api.*;
1.26 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
1.27 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
1.28 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
1.29 +
1.30 +/** Controller class for access to Twitter.
1.31 + *
1.32 + * @author Jaroslav Tulach
1.33 + */
1.34 +@Page(xhtml="index.html", className="TwitterModel", properties={
1.35 + @Property(name="savedLists", type=TwitterClient.Twttrs.class, array = true),
1.36 + @Property(name="activeTweetersName", type=String.class),
1.37 + @Property(name="modifiedList", type=TwitterClient.Twttrs.class),
1.38 + @Property(name="userNameToAdd", type=String.class),
1.39 + @Property(name="currentTweets", type=String.class, array = true)
1.40 +})
1.41 +public class TwitterClient {
1.42 + @Model(className = "Tweeters", properties = {
1.43 + @Property(name="name", type = String.class),
1.44 + @Property(name="userNames", type = String.class, array = true)
1.45 + })
1.46 + static class Twttrs {
1.47 + }
1.48 +
1.49 + private static Tweeters tweeters(String listName, String... userNames) {
1.50 + Tweeters t = new Tweeters();
1.51 + t.setName(listName);
1.52 + t.getUserNames().addAll(Arrays.asList(userNames));
1.53 + return t;
1.54 + }
1.55 +
1.56 + static {
1.57 + final TwitterModel model = new TwitterModel();
1.58 + final List<Tweeters> svdLst = model.getSavedLists();
1.59 + svdLst.add(tweeters("API Design", "JaroslavTulach"));
1.60 + svdLst.add(tweeters("Celebrities", "JohnCleese", "MCHammer", "StephenFry", "algore", "StevenSanderson"));
1.61 + svdLst.add(tweeters("Microsoft people", "BillGates", "shanselman", "ScottGu"));
1.62 + svdLst.add(tweeters("NetBeans", "GeertjanW","monacotoni", "NetBeans"));
1.63 + svdLst.add(tweeters("Tech pundits", "Scobleizer", "LeoLaporte", "techcrunch", "BoingBoing", "timoreilly", "codinghorror"));
1.64 +
1.65 + model.setActiveTweetersName("NetBeans");
1.66 +
1.67 + model.applyBindings();
1.68 + }
1.69 +
1.70 + @ComputedProperty
1.71 + static Tweeters activeTweeters(String activeTweetersName, List<Tweeters> savedLists, Tweeters modifiedList) {
1.72 + if (modifiedList != null && modifiedList.getName() != null) {
1.73 + if (modifiedList.getName().equals(activeTweetersName)) {
1.74 + return modifiedList;
1.75 + } else {
1.76 + modifiedList.setName(null);
1.77 + }
1.78 + }
1.79 + return findByName(savedLists, activeTweetersName);
1.80 + }
1.81 +
1.82 + @ComputedProperty
1.83 + static boolean hasUnsavedChanges(Tweeters modifiedList) {
1.84 + return modifiedList != null;
1.85 + }
1.86 +
1.87 + @ComputedProperty
1.88 + static boolean userNameToAddIsValid(String userNameToAdd, String activeTweetersName, List<Tweeters> savedLists, Tweeters modifiedList) {
1.89 + Tweeters editingList = activeTweeters(activeTweetersName, savedLists, modifiedList);
1.90 + return editingList != null && userNameToAdd != null &&
1.91 + userNameToAdd.matches("[a-zA-Z0-9_]{1,15}") &&
1.92 + !editingList.getUserNames().contains(userNameToAdd);
1.93 + }
1.94 +
1.95 + @OnFunction
1.96 + static void deleteList(TwitterModel model) {
1.97 + final List<Tweeters> sl = model.getSavedLists();
1.98 + sl.remove(model.getActiveTweeters());
1.99 + if (sl.isEmpty()) {
1.100 + final Tweeters t = new Tweeters();
1.101 + t.setName("New");
1.102 + sl.add(t);
1.103 + }
1.104 + model.setActiveTweetersName(sl.get(0).getName());
1.105 + }
1.106 +
1.107 + @OnFunction
1.108 + static void saveChanges(TwitterModel model) {
1.109 + Tweeters t = findByName(model.getSavedLists(), model.getActiveTweetersName());
1.110 + int indx = model.getSavedLists().indexOf(t);
1.111 + assert indx != -1;
1.112 + model.getSavedLists().set(indx, model.getModifiedList());
1.113 + model.setModifiedList(null);
1.114 + }
1.115 +
1.116 + @OnFunction
1.117 + static void addUser(TwitterModel model) {
1.118 + String n = model.getUserNameToAdd();
1.119 + findModifiedList(model).getUserNames().add(n);
1.120 + }
1.121 + @OnFunction
1.122 + static void removeUser(String data, TwitterModel model) {
1.123 + findModifiedList(model).getUserNames().remove(data);
1.124 + }
1.125 +
1.126 + private static Tweeters findModifiedList(TwitterModel model) {
1.127 + if (model.getModifiedList() == null || model.getModifiedList().getName() == null) {
1.128 + model.setModifiedList(model.getActiveTweeters().clone());
1.129 + }
1.130 + return model.getModifiedList();
1.131 + }
1.132 + private static Tweeters findByName(List<Tweeters> list, String name) {
1.133 + for (Tweeters l : list) {
1.134 + if (l.getName() != null && l.getName().equals(name)) {
1.135 + return l;
1.136 + }
1.137 + }
1.138 + return list.isEmpty() ? null : list.get(0);
1.139 + }
1.140 +}