com.roughindustries.commonwealthcocktails.client.application.ApplicationPresenter.java Source code

Java tutorial

Introduction

Here is the source code for com.roughindustries.commonwealthcocktails.client.application.ApplicationPresenter.java

Source

/**
 * Copyright 2012 ArcBees Inc.
 *
 * 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 com.roughindustries.commonwealthcocktails.client.application;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.gwtbootstrap3.client.ui.Container;
import org.gwtbootstrap3.client.ui.NavbarBrand;

import com.google.gwt.cell.client.SafeHtmlCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.NativeVerticalScrollbar;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.Range;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.presenter.slots.NestedSlot;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
import com.gwtplatform.mvp.shared.proxy.PlaceRequest;
import com.roughindustries.commonwealthcocktails.client.CocktailService;
import com.roughindustries.commonwealthcocktails.client.CocktailServiceAsync;
import com.roughindustries.commonwealthcocktails.client.place.NameTokens;
import com.roughindustries.commonwealthcocktails.model.ccCocktail;
import com.roughindustries.commonwealthcocktails.shared.MutableInt;

public class ApplicationPresenter extends Presenter<ApplicationPresenter.MyView, ApplicationPresenter.MyProxy> {

    private final CocktailServiceAsync cocktailService = GWT.create(CocktailService.class);
    final MutableInt queryResultsCount = new MutableInt();
    final MutableInt step = new MutableInt();
    final MutableInt current = new MutableInt();
    final MutableInt next = new MutableInt();
    final MutableInt prev = new MutableInt();
    final MutableInt lastScrollPos = new MutableInt();
    final MutableInt lastMaxScrollTop = new MutableInt();

    final List<ccCocktail> data = new ArrayList<ccCocktail>();
    final List<ccCocktail> list = new ArrayList<ccCocktail>();

    public interface MyView extends View {
        public ScrollPanel getScroll();

        public CellTable<ccCocktail> getCocktailGrid();

        public HTMLPanel getLeftBuffer();

        public HTMLPanel getRightBuffer();

        public HTMLPanel getTopBuffer();

        public HTMLPanel getBottomBuffer();

        public Container getHeaderContainer();

        public NavbarBrand getBrand();
    }

    public static final NestedSlot SLOT_COCKTAILS = new NestedSlot();

    //@NameToken(NameTokens.cocktails)
    @ProxyStandard
    public interface MyProxy extends Proxy<ApplicationPresenter> {
    }

    @Inject
    public ApplicationPresenter(final EventBus eventBus, final MyView view, final MyProxy proxy) {
        super(eventBus, view, proxy, RevealType.Root);
    }

    @Override
    public void prepareFromRequest(PlaceRequest request) {
        super.prepareFromRequest(request);
    }

    @Override
    protected void onBind() {

        step.setValue(25);
        current.setValue(25);
        next.setValue(current.intValue() + step.intValue());
        prev.setValue(0);

        lastScrollPos.setValue(0);
        lastMaxScrollTop.setValue(0);

        list.addAll(Collections.nCopies(current.intValue(), new ccCocktail()));

        final HTML f = new HTML("");
        Column<ccCocktail, SafeHtml> text = new Column<ccCocktail, SafeHtml>(new SafeHtmlCell()) {

            public SafeHtml getValue(ccCocktail cocktail) {
                SafeHtmlBuilder sb = new SafeHtmlBuilder();
                if (cocktail == null || cocktail.getCocktailName() == null) {
                    sb.appendHtmlConstant("Loading ...");
                    return sb.toSafeHtml();
                } else {
                    sb.appendHtmlConstant(
                            "<table border='0' cellpadding='1' cellspacing='1' style='width: 100%;'>");
                    sb.appendHtmlConstant("<tbody>");
                    sb.appendHtmlConstant("<tr>");
                    sb.appendHtmlConstant(
                            "<td><img alt='Cocktail' src='https://s3.amazonaws.com/commonwealthcocktailbucket/noun_320760_cc.png' style='border-width: 0px; border-style: solid; margin: 0px; width: 100px; height: 100px;' /></td>");
                    sb.appendHtmlConstant(
                            "<td border='0' cellpadding='1' cellspacing='1' style='width: 100%;' table=''>");
                    sb.appendHtmlConstant(
                            "<table border='0' cellpadding='1' cellspacing='1' style='width: 100%;'>");
                    sb.appendHtmlConstant("<tbody>");
                    sb.appendHtmlConstant("<tr>");
                    sb.appendHtmlConstant("<td><b>" + cocktail.getCocktailName() + "</b></td>");
                    sb.appendHtmlConstant("</tr>");
                    sb.appendHtmlConstant("<tr>");
                    sb.appendHtmlConstant(
                            "<td><b>Origin: </b>"
                                    + ((cocktail.getCocktailOrigin() == null)
                                            || (cocktail.getCocktailOrigin().length() == 0) ? ""
                                                    : (cocktail.getCocktailOrigin().substring(0, 150) + "..."))
                                    + "</td>");
                    sb.appendHtmlConstant("</tr>");
                    sb.appendHtmlConstant("<tr>");
                    sb.appendHtmlConstant("<td><b>Method: </b>"
                            + ((cocktail.getCocktailMethod() == null) || (cocktail.getCocktailMethod() == null) ? ""
                                    : (cocktail.getCocktailMethod().substring(0, 150) + "..."))
                            + "</td>");
                    sb.appendHtmlConstant("</tr>");
                    sb.appendHtmlConstant("</tbody>");
                    sb.appendHtmlConstant("</table>");
                    sb.appendHtmlConstant("</td>");
                    sb.appendHtmlConstant("</tr>");
                    sb.appendHtmlConstant("</tbody>");
                    sb.appendHtmlConstant("</table>");
                    return sb.toSafeHtml();
                }
            }

        };
        getView().getCocktailGrid().addColumn(text);
        data.addAll(list);
        getView().getCocktailGrid().setRowData(0, data);
        getView().getCocktailGrid().setVisibleRange(new Range(0, current.intValue()));
        getView().getCocktailGrid().setHeight(Integer.toString(Window.getClientHeight()) + "px");
        getView().getScroll().setHeight(Integer.toString(Window.getClientHeight()) + "px");
        getView().getScroll().setWidth(Integer.toString(Window.getClientWidth()) + "px");
        if (Window.getClientWidth() <= 768) {
            getView().getBrand().setMarginLeft((Window.getClientWidth() / 2) - 125);
        } else {
            getView().getBrand().setMarginLeft(0);
        }
        if (Window.getClientWidth() > (800)
                && (Window.getClientWidth() <= (800 + NativeVerticalScrollbar.getNativeScrollbarWidth()))) {
            getView().getHeaderContainer()
                    .setWidth((800 - NativeVerticalScrollbar.getNativeScrollbarWidth()) + "px");
            getView().getCocktailGrid().setWidth((800 - NativeVerticalScrollbar.getNativeScrollbarWidth()) + "px");
            if (getView().getLeftBuffer().isVisible()) {
                getView().getLeftBuffer().setVisible(false);
                getView().getRightBuffer().setVisible(false);
            }
        } else if (Window.getClientWidth() <= (800 - NativeVerticalScrollbar.getNativeScrollbarWidth())) {
            getView().getHeaderContainer().setWidth(
                    Integer.toString(Window.getClientWidth() - NativeVerticalScrollbar.getNativeScrollbarWidth())
                            + "px");
            getView().getCocktailGrid().setWidth(
                    Integer.toString(Window.getClientWidth() - NativeVerticalScrollbar.getNativeScrollbarWidth())
                            + "px");

            if (getView().getLeftBuffer().isVisible()) {
                getView().getLeftBuffer().setVisible(false);
                getView().getRightBuffer().setVisible(false);
            }
        } else {
            getView().getHeaderContainer().setWidth("800px");
            getView().getCocktailGrid().setWidth("800px");
            if (!getView().getLeftBuffer().isVisible()) {
                getView().getLeftBuffer().setVisible(true);
                getView().getRightBuffer().setVisible(true);
            }
            getView().getLeftBuffer()
                    .setWidth(Integer.toString(
                            (Window.getClientWidth() - 800 - NativeVerticalScrollbar.getNativeScrollbarWidth()) / 2)
                            + "px");
            getView().getRightBuffer()
                    .setWidth(Integer.toString(
                            (Window.getClientWidth() - 800 - NativeVerticalScrollbar.getNativeScrollbarWidth()) / 2)
                            + "px");
        }
        getView().getScroll().addScrollHandler(new ScrollHandler() {

            @Override
            public void onScroll(ScrollEvent ev) {
                int oldScrollPos = lastScrollPos.intValue();
                lastScrollPos.setValue(getView().getScroll().getVerticalScrollPosition());

                // If scrolling up, ignore the event.
                if (oldScrollPos >= lastScrollPos.intValue()) {
                    return;
                }

                // Height of grid contents (including outside the viewable area)
                // - height of the scroll panel
                int maxScrollTop = getView().getScroll().getWidget().getOffsetHeight()
                        - getView().getScroll().getOffsetHeight();
                if (lastScrollPos.intValue() == maxScrollTop) {
                    if ((data.size() + step.intValue()) <= queryResultsCount.intValue()) {
                        prev.setValue(current);
                        current.setValue(next);
                        next.setValue(next.intValue() + step.intValue());
                        data.addAll(
                                new ArrayList<ccCocktail>(Collections.nCopies(step.intValue(), new ccCocktail())));
                    } else {
                        int diff = queryResultsCount.intValue() - data.size();
                        if (diff > 0) {
                            step.setValue(diff);
                            prev.setValue(current);
                            current.setValue(next);
                            next.setValue(next.intValue() + step.intValue());
                            data.addAll(new ArrayList<ccCocktail>(Collections.nCopies(diff, new ccCocktail())));
                        } else {
                            step.setValue(0);
                        }
                    }
                    getView().getCocktailGrid().setRowData(0, data);
                    getView().getCocktailGrid().setVisibleRange(new Range(0, current.intValue()));
                    getView().getCocktailGrid().redraw();
                    if (step.intValue() != 0) {
                        cocktailService.cocktailServer("", prev.intValue(), step.intValue(),
                                new AsyncCallback<ccCocktail[]>() {
                                    public void onFailure(Throwable caught) {

                                    }

                                    public void onSuccess(ccCocktail[] result) {
                                        if (result != null) {
                                            for (ccCocktail item : result) {
                                                data.set(item.getPageRefID(), item);
                                            }
                                            getView().getCocktailGrid().setRowData(0, data);
                                            getView().getCocktailGrid()
                                                    .setVisibleRange(new Range(0, current.intValue()));
                                            getView().getCocktailGrid().redraw();

                                        }
                                    }
                                });
                    }
                }
            }
        });
        Window.addWindowScrollHandler(new Window.ScrollHandler() {
            @Override
            public void onWindowScroll(com.google.gwt.user.client.Window.ScrollEvent event) {
                // Window.alert("Scrolling Window");
            }
        });
        cocktailService.getCount("", new AsyncCallback<Integer>() {
            public void onFailure(Throwable caught) {

            }

            public void onSuccess(Integer result) {
                queryResultsCount.setValue(result.intValue());
                cocktailService.cocktailServer("", prev.intValue(), current.intValue(),
                        new AsyncCallback<ccCocktail[]>() {
                            public void onFailure(Throwable caught) {

                            }

                            public void onSuccess(ccCocktail[] result) {
                                ArrayList<ccCocktail> removed = new ArrayList<ccCocktail>();
                                for (ccCocktail item : result) {
                                    if (item != null) {
                                        removed.add(item);
                                    }
                                }
                                result = removed.toArray(new ccCocktail[0]);
                                if (result != null) {
                                    for (ccCocktail item : result) {
                                        data.set(item.getPageRefID(), item);
                                    }
                                    getView().getCocktailGrid().setRowData(0, data);
                                    getView().getCocktailGrid().setVisibleRange(new Range(0, current.intValue()));
                                    getView().getCocktailGrid().redraw();
                                }
                            }
                        });
            }
        });
        Window.addResizeHandler(new ResizeHandler() {

            @Override
            public void onResize(ResizeEvent event) {
                getView().getCocktailGrid().setHeight(Integer.toString(Window.getClientHeight()) + "px");
                getView().getScroll().setHeight(Integer.toString(Window.getClientHeight()) + "px");
                getView().getScroll().setWidth(Integer.toString(Window.getClientWidth()) + "px");

                if (Window.getClientWidth() <= 768) {
                    getView().getBrand().setMarginLeft((Window.getClientWidth() / 2) - 125);
                } else {
                    getView().getBrand().setMarginLeft(0);
                }

                if (Window.getClientWidth() > (800)
                        && (Window.getClientWidth() <= (800 + NativeVerticalScrollbar.getNativeScrollbarWidth()))) {
                    getView().getHeaderContainer()
                            .setWidth((800 - NativeVerticalScrollbar.getNativeScrollbarWidth()) + "px");
                    getView().getCocktailGrid()
                            .setWidth((800 - NativeVerticalScrollbar.getNativeScrollbarWidth()) + "px");
                    if (getView().getLeftBuffer().isVisible()) {
                        getView().getLeftBuffer().setVisible(false);
                        getView().getRightBuffer().setVisible(false);
                    }
                } else if (Window.getClientWidth() <= (800 + NativeVerticalScrollbar.getNativeScrollbarWidth())) {
                    getView().getHeaderContainer()
                            .setWidth(Integer.toString(
                                    Window.getClientWidth() - NativeVerticalScrollbar.getNativeScrollbarWidth())
                                    + "px");
                    getView().getCocktailGrid()
                            .setWidth(Integer.toString(
                                    Window.getClientWidth() - NativeVerticalScrollbar.getNativeScrollbarWidth())
                                    + "px");
                    if (getView().getLeftBuffer().isVisible()) {
                        getView().getLeftBuffer().setVisible(false);
                        getView().getRightBuffer().setVisible(false);
                    }
                } else {
                    getView().getHeaderContainer().setWidth("800px");
                    getView().getCocktailGrid().setWidth("800px");
                    if (!getView().getLeftBuffer().isVisible()) {
                        getView().getLeftBuffer().setVisible(true);
                        getView().getRightBuffer().setVisible(true);
                    }
                    getView().getLeftBuffer().setWidth(Integer.toString(
                            (Window.getClientWidth() - 800 - NativeVerticalScrollbar.getNativeScrollbarWidth()) / 2)
                            + "px");
                    getView().getRightBuffer().setWidth(Integer.toString(
                            (Window.getClientWidth() - 800 - NativeVerticalScrollbar.getNativeScrollbarWidth()) / 2)
                            + "px");
                }
            }

        });
    }
}