next.celebs.event.EventAppController.java Source code

Java tutorial

Introduction

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

import java.util.ArrayList;

import next.celebs.Context;
import next.celebs.di.UiEventBus;
import next.celebs.model.Photo;
import next.i.XLog;
import next.i.view.widgets.XButton;
import next.i.view.widgets.XButton.XButtonType;
import next.i.view.widgets.XFlexTable;
import next.i.view.widgets.XLabel;
import next.i.view.widgets.XPopup;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;

public class EventAppController {

    /**
     * Event listener channel
     */
    public void register(final Context ctx) {

        UiEventBus eventBus = ctx.getEventBus();

        eventBus.on(MagazineEvent._, new Do() {
            void _(E e) {
                XLog.info("MagazineClickedEvent: ...");
            }
        });

        eventBus.on(CelebAddedEvent._, new CelebAddedHandler() {
            void _(CelebAddedEvent e) {
                ctx.getFavorites().put(e.getPhoto().getUrl(), e.getPhoto());
                showPopup("The picture was added to your favorites.");
            }
        });

        eventBus.on(CelebRemovedEvent._, new CelebRemovedHandler() {
            void _(CelebRemovedEvent e) {
                ctx.getFavorites().remove(e.getPhoto().getUrl());
                showPopup("The pictue was removed from your favorites.");
            }
        });

        Window.addCloseHandler(new CloseHandler<Window>() {
            @Override
            public void onClose(CloseEvent<Window> event) {
                XLog.info("persisiting favorites");

                ArrayList<Photo> favs = new ArrayList<Photo>();

                String sss = "[";

                boolean isFirst = true;

                for (String key : ctx.getFavorites().keySet()) {
                    Photo p = ctx.getFavorites().get(key);
                    favs.add(p);
                    if (!isFirst) {
                        sss += ",";
                    }
                    sss += p.json();

                    isFirst = false;
                }

                sss += "]";

                ctx.getStorage().setItem("photos", sss);
            }
        });

    }

    void showPopup(String message) {

        final XPopup popup = new XPopup();
        XFlexTable wrapper = new XFlexTable(null, null);
        XButton btn = new XButton("Close", XButtonType.NavigationBlue);
        btn.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                popup.hide();
            }
        });
        wrapper.addWidgets(new HTML("&nbsp;"), new XLabel(message), new HTML("&nbsp;"), btn, new HTML("&nbsp;"));
        popup.setWidget(wrapper);
        popup.setTop("35px");
        popup.setRight("10%");
        popup.setLeft("10%");
        popup.setBottom("10%");
        popup.show();
    }

}