next.celebs.controller.PhotoDragController.java Source code

Java tutorial

Introduction

Here is the source code for next.celebs.controller.PhotoDragController.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.di.UiGinjector;
import next.celebs.event.CelebAddedEvent;
import next.celebs.event.CelebRemovedEvent;
import next.celebs.model.Photo;
import next.i.controller.XController;
import next.i.view.XBarItem.Type;

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.Window;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.SimplePanel;

public class PhotoDragController extends XController {

    private final static Context ctx = UiGinjector.INSTANCE.getCtx();
    private Photo photo;

    public PhotoDragController(final String title, final Photo p, boolean isFavorites) {

        this.photo = p;
        setTitle(title);

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

        if (isFavorites) {
            getNavigationBar().setRightTitle("Remove");
            getNavigationBar().getRightButton().addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {

                    ctx.fireEvent(new CelebRemovedEvent(p));

                    ((FavoritesController) getNavigationController().getTopController()).reload();
                    getNavigationController().popController(true);
                }
            });

        } else {

            getNavigationBar().setRightTitle("Add");
            getNavigationBar().getRightButton().addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    ctx.fireEvent(new CelebAddedEvent(p));
                }
            });

        }
    }

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

    @Override
    public IsWidget getViewContent() {

        Photo p = getPhoto();

        SimplePanel widg = new SimplePanel();
        Style s = widg.getElement().getStyle();

        widg.setWidth(p.getWidth() + "px");
        widg.setHeight(p.getHeight() + "px");
        s.setProperty("backgroundImage", "url(" + p.getUrl() + ")");
        s.setProperty("backgroundRepeat", "no-repeat");

        int h = p.getHeight();
        int w = p.getWidth();

        int ofh = getView().asWidget().getOffsetHeight();
        int ofw = Window.getClientWidth();

        if (h < ofh) {
            s.setProperty("marginTop", (ofh - h) / 2 + "px");
        }

        if (w < ofw) {
            s.setProperty("marginLeft", (ofw - w) / 2 + "px");
        }

        return widg;
    }

    public Photo getPhoto() {
        return photo;
    }

}