next.celebs.model.API.java Source code

Java tutorial

Introduction

Here is the source code for next.celebs.model.API.java

Source

/*
 * Copyright 2011 Vancouver Ywebb Consulting Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package next.celebs.model;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.search.client.ImageResult;
import com.google.gwt.search.client.ImageSearch;
import com.google.gwt.search.client.ImageTypeValue;
import com.google.gwt.search.client.ResultSetSize;
import com.google.gwt.search.client.SearchResultsHandler;
import com.google.gwt.search.client.SearchUtils;
import com.google.gwt.user.client.Timer;

public class API {

    public static abstract class Response<T> {

        public abstract void read(T data);

        public void afterRead(String jsonData) {
            // Override if jsonData is required
        }

        public void onError(int statusCode, String response) {
            // handler code for error capture
        }
    }

    public void getPhotos(final Key searchKey, final int numberPages,
            final Response<JsArray<ImageResult>> response) {

        final SearchResultsHandler resultHandler = new SearchResultsHandler() {
            @Override
            public void onSearchResults(SearchResultsEvent event) {
                @SuppressWarnings("unchecked")
                JsArray<ImageResult> data = (JsArray<ImageResult>) event.getResults();
                response.read(data);
            }
        };

        SearchUtils.loadSearchApi(new Runnable() {
            public void run() {
                ImageSearch search = initImageSearch(resultHandler);
                search.execute(searchKey.getSearchQuery());
                int[] page = { 1 };
                gotoPage(page, numberPages, search);
            }
        });
    }

    private void gotoPage(final int[] page, final int maxPage, final ImageSearch search) {
        if (page[0] > maxPage) {
            return;
        }
        Timer t = new Timer() {
            public void run() {
                search.gotoPage(page[0]);
                page[0] = page[0] + 1;
                gotoPage(page, maxPage, search);
            }
        };

        t.schedule(200);
    }

    public ImageSearch initImageSearch(SearchResultsHandler resultsHandler) {
        final ImageSearch is = new ImageSearch();
        is.setNoHtmlGeneration();
        is.setImageType(ImageTypeValue.FACES);

        // disabled We love revealed pics ;)
        // is.setSafeSearch(ctx.getSafeSearcrh());
        is.addSearchResultsHandler(resultsHandler);
        is.setResultSetSize(ResultSetSize.LARGE);
        // is.setQueryAddition("tbs=isch:1,iur:fc");
        // is.setSiteRestriction("wikimedia.org");
        // is.setSiteRestriction("wikipedia.org");
        // is.setColorization(ColorizationValue.BLACK_AND_WHITE);
        // is.setImageSize(ImageSizeValue.SMALL);
        return is;
    }

}