1.1 --- a/twitter/src/main/java/org/apidesign/html/demo/twitter/TwitterClient.java Fri Dec 20 08:24:57 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,205 +0,0 @@
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.html.demo.twitter;
1.28 -
1.29 -import java.util.Arrays;
1.30 -import java.util.List;
1.31 -import net.java.html.json.ComputedProperty;
1.32 -import net.java.html.json.Function;
1.33 -import net.java.html.json.Model;
1.34 -import net.java.html.json.OnPropertyChange;
1.35 -import net.java.html.json.OnReceive;
1.36 -import net.java.html.json.Property;
1.37 -
1.38 -/** Controller class for access to Twitter.
1.39 - *
1.40 - * @author Jaroslav Tulach
1.41 - */
1.42 -@Model(className="TwitterModel", properties={
1.43 - @Property(name="savedLists", type=Tweeters.class, array = true),
1.44 - @Property(name="activeTweetersName", type=String.class),
1.45 - @Property(name="activeTweeters", type=String.class, array = true),
1.46 - @Property(name="userNameToAdd", type=String.class),
1.47 - @Property(name="currentTweets", type=Tweet.class, array = true),
1.48 - @Property(name="loading", type=boolean.class)
1.49 -})
1.50 -final class TwitterClient {
1.51 - @Model(className = "Tweeters", properties = {
1.52 - @Property(name="name", type = String.class),
1.53 - @Property(name="userNames", type = String.class, array = true)
1.54 - })
1.55 - static class Twttrs {
1.56 - }
1.57 - @Model(className = "Tweet", properties = {
1.58 - @Property(name = "from_user", type = String.class),
1.59 - @Property(name = "from_user_id", type = int.class),
1.60 - @Property(name = "profile_image_url", type = String.class),
1.61 - @Property(name = "text", type = String.class),
1.62 - @Property(name = "created_at", type = String.class),
1.63 - })
1.64 - static final class Twt {
1.65 - @ComputedProperty static String html(String text) {
1.66 - StringBuilder sb = new StringBuilder(320);
1.67 - for (int pos = 0;;) {
1.68 - int http = text.indexOf("http", pos);
1.69 - if (http == -1) {
1.70 - sb.append(text.substring(pos));
1.71 - return sb.toString();
1.72 - }
1.73 - int spc = text.indexOf(' ', http);
1.74 - if (spc == -1) {
1.75 - spc = text.length();
1.76 - }
1.77 - sb.append(text.substring(pos, http));
1.78 - String url = text.substring(http, spc);
1.79 - sb.append("<a href='").append(url).append("'>").append(url).append("</a>");
1.80 - pos = spc;
1.81 - }
1.82 - }
1.83 -
1.84 - @ComputedProperty static String userUrl(String from_user) {
1.85 - return "http://twitter.com/" + from_user;
1.86 - }
1.87 - }
1.88 - @Model(className = "TwitterQuery", properties = {
1.89 - @Property(array = true, name = "results", type = Twt.class)
1.90 - })
1.91 - public static final class TwttrQr {
1.92 - }
1.93 -
1.94 - @OnReceive(url="{root}/search.json?{query}&callback={me}", jsonp="me")
1.95 - static void queryTweets(TwitterModel page, TwitterQuery q) {
1.96 - page.getCurrentTweets().clear();
1.97 - page.getCurrentTweets().addAll(q.getResults());
1.98 - page.setLoading(false);
1.99 - }
1.100 -
1.101 - @OnPropertyChange("activeTweetersName")
1.102 - static void changeTweetersList(TwitterModel model) {
1.103 - Tweeters people = findByName(model.getSavedLists(), model.getActiveTweetersName());
1.104 - model.getActiveTweeters().clear();
1.105 - model.getActiveTweeters().addAll(people.getUserNames());
1.106 - }
1.107 -
1.108 - @OnPropertyChange({ "activeTweeters", "activeTweetersCount" })
1.109 - static void refreshTweets(TwitterModel model) {
1.110 - StringBuilder sb = new StringBuilder();
1.111 - sb.append("rpp=25&q=");
1.112 - String sep = "";
1.113 - for (String p : model.getActiveTweeters()) {
1.114 - sb.append(sep);
1.115 - sb.append("from:");
1.116 - sb.append(p);
1.117 - sep = " OR ";
1.118 - }
1.119 - model.setLoading(true);
1.120 - model.queryTweets("http://search.twitter.com", sb.toString());
1.121 - }
1.122 -
1.123 - static void init() {
1.124 - final TwitterModel model = new TwitterModel();
1.125 - final List<Tweeters> svdLst = model.getSavedLists();
1.126 - svdLst.add(newTweeters("API Design", "JaroslavTulach"));
1.127 - svdLst.add(newTweeters("Celebrities", "JohnCleese", "MCHammer", "StephenFry", "algore", "StevenSanderson"));
1.128 - svdLst.add(newTweeters("Microsoft people", "BillGates", "shanselman", "ScottGu"));
1.129 - svdLst.add(newTweeters("NetBeans", "GeertjanW","monacotoni", "NetBeans", "petrjiricka"));
1.130 - svdLst.add(newTweeters("Tech pundits", "Scobleizer", "LeoLaporte", "techcrunch", "BoingBoing", "timoreilly", "codinghorror"));
1.131 -
1.132 - model.setActiveTweetersName("NetBeans");
1.133 -
1.134 - model.applyBindings();
1.135 - }
1.136 -
1.137 - @ComputedProperty
1.138 - static boolean hasUnsavedChanges(List<String> activeTweeters, List<Tweeters> savedLists, String activeTweetersName) {
1.139 - Tweeters tw = findByName(savedLists, activeTweetersName);
1.140 - if (activeTweeters == null) {
1.141 - return false;
1.142 - }
1.143 - return !tw.getUserNames().equals(activeTweeters);
1.144 - }
1.145 -
1.146 - @ComputedProperty
1.147 - static int activeTweetersCount(List<String> activeTweeters) {
1.148 - return activeTweeters.size();
1.149 - }
1.150 -
1.151 - @ComputedProperty
1.152 - static boolean userNameToAddIsValid(
1.153 - String userNameToAdd, String activeTweetersName, List<Tweeters> savedLists, List<String> activeTweeters
1.154 - ) {
1.155 - return userNameToAdd != null &&
1.156 - userNameToAdd.matches("[a-zA-Z0-9_]{1,15}") &&
1.157 - !activeTweeters.contains(userNameToAdd);
1.158 - }
1.159 -
1.160 - @Function
1.161 - static void deleteList(TwitterModel model) {
1.162 - final List<Tweeters> sl = model.getSavedLists();
1.163 - sl.remove(findByName(sl, model.getActiveTweetersName()));
1.164 - if (sl.isEmpty()) {
1.165 - final Tweeters t = new Tweeters();
1.166 - t.setName("New");
1.167 - sl.add(t);
1.168 - }
1.169 - model.setActiveTweetersName(sl.get(0).getName());
1.170 - }
1.171 -
1.172 - @Function
1.173 - static void saveChanges(TwitterModel model) {
1.174 - Tweeters t = findByName(model.getSavedLists(), model.getActiveTweetersName());
1.175 - int indx = model.getSavedLists().indexOf(t);
1.176 - if (indx != -1) {
1.177 - t.setName(model.getActiveTweetersName());
1.178 - t.getUserNames().clear();
1.179 - t.getUserNames().addAll(model.getActiveTweeters());
1.180 - }
1.181 - }
1.182 -
1.183 - @Function
1.184 - static void addUser(TwitterModel model) {
1.185 - String n = model.getUserNameToAdd();
1.186 - model.getActiveTweeters().add(n);
1.187 - }
1.188 - @Function
1.189 - static void removeUser(String data, TwitterModel model) {
1.190 - model.getActiveTweeters().remove(data);
1.191 - }
1.192 -
1.193 - private static Tweeters findByName(List<Tweeters> list, String name) {
1.194 - for (Tweeters l : list) {
1.195 - if (l.getName() != null && l.getName().equals(name)) {
1.196 - return l;
1.197 - }
1.198 - }
1.199 - return list.isEmpty() ? new Tweeters() : list.get(0);
1.200 - }
1.201 -
1.202 - private static Tweeters newTweeters(String listName, String... userNames) {
1.203 - Tweeters t = new Tweeters();
1.204 - t.setName(listName);
1.205 - t.getUserNames().addAll(Arrays.asList(userNames));
1.206 - return t;
1.207 - }
1.208 -}