next.celebs.controller.PhotosController.java Source code

Java tutorial

Introduction

Here is the source code for next.celebs.controller.PhotosController.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.controller;

import next.celebs.Context;
import next.celebs.DemoUtils;
import next.celebs.di.UiGinjector;
import next.celebs.model.API.Response;
import next.celebs.model.Key;
import next.celebs.model.Photo;
import next.celebs.model.PhotoMap;
import next.i.controller.XController;
import next.i.mobile.SelectionChangedEvent;
import next.i.mobile.SelectionChangedHandler;
import next.i.view.XBarItem.Type;
import next.i.view.widgets.XRadioButton;
import next.i.view.widgets.XRadioButtonGroup;

import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.IsWidget;

public class PhotosController extends XController {

    private final static Context ctx = UiGinjector.INSTANCE.getCtx();
    private PhotoDragController dragController;

    private boolean isCelebrity;

    public PhotosController(final String title, boolean isCelebrity) {

        if ("Feel Lucky".equals(title)) {
            setTitle(ctx.getNamesDao().getRandomName());
        } else {
            setTitle(title);
        }

        this.isCelebrity = isCelebrity;

        getNavigationBar().setLeftTitle("Back", Type.BACK_BUTTON);
        getNavigationBar().getLeftButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                getNavigationController().popController(true);
            }
        });

        if (isCelebrity) {
            getNavigationBar().setRightTitle("Wiki");
            getNavigationBar().getRightButton().addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    DemoUtils.openWiki("http://en.wikipedia.org/wiki/" + title);
                }
            });
        }
    }

    private void doGetPhotos(final FlowPanel imageWrapper, String title) {

        // XLog.info("before search " + getTitle());

        String suffux = isCelebrity ? "" : " girl";
        ctx.getPhotoDao().getPhotos(new Key(title + suffux, title), 8, new Response<PhotoMap>() {
            @Override
            public void read(PhotoMap data) {

                for (String id : data.keySet()) {
                    final Photo p = data.get(id);

                    Image img = new Image(p.getThumbUrl());
                    img.setStyleName("yThumb");

                    img.addClickHandler(new ClickHandler() {
                        @Override
                        public void onClick(ClickEvent event) {
                            dragController = new PhotoDragController(getTitle(), p, false);
                            getNavigationController().pushController(dragController, true);
                        }
                    });
                    imageWrapper.add(img);
                }
            }
        });
    }

    @Override
    public IsWidget getViewContent() {
        final FlowPanel panel = new FlowPanel();
        Style s = panel.getElement().getStyle();
        s.setProperty("backgroundColor", "#fff");
        s.setProperty("padding", "1em");

        final FlowPanel imageWrapper = new FlowPanel();

        final XRadioButtonGroup group = new XRadioButtonGroup();
        XRadioButton btn0 = new XRadioButton("Wallpaper", "Wallpaper");
        XRadioButton btn1 = new XRadioButton("Beach", "Beach");
        XRadioButton btn2 = new XRadioButton("Bride", "Bride");
        group.add(btn0, btn1, btn2);
        group.getElement().getStyle().setProperty("marginBottom", "1em");

        final String celebName = getTitle();

        group.addSelectionChangedHandler(new SelectionChangedHandler() {
            public void onSelectionChanged(SelectionChangedEvent e) {

                imageWrapper.clear();
                imageWrapper.removeFromParent();
                panel.add(imageWrapper);

                String selected = group.getCheckedWidget().getName();
                if (selected.equals("Beach")) {
                    selected = "swimwear";
                }

                doGetPhotos(imageWrapper, celebName + " " + selected);
            }
        });

        panel.add(group);
        panel.add(imageWrapper);

        doGetPhotos(imageWrapper, celebName);

        return panel;
    }

    @Override
    public Scroll getScrollOrientation() {
        return XController.Scroll.VERTICAL;
    }

}