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