be.progs.routeshare.client.ResizableMapLayout.java Source code

Java tutorial

Introduction

Here is the source code for be.progs.routeshare.client.ResizableMapLayout.java

Source

/*
 * This file is part routeshare.
 * Copyright PROGS bvba, http://www.progs.be/
 * For licensing details, see LICENSE.txt in the project root.
 */

package be.progs.routeshare.client;

import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import org.geomajas.gwt.client.util.Log;
import org.geomajas.puregwt.client.event.MapInitializationEvent;
import org.geomajas.puregwt.client.event.MapInitializationHandler;
import org.geomajas.puregwt.client.map.MapPresenter;

/**
 * <p>
 * A layout widget for automatic resizing of a map. This widget must be created with a {@link MapPresenter}. This given
 * {@link MapPresenter} will then be added to the inner layout within this class. Don't add the map anywhere else.
 * </p>
 * <p>
 * One warning though, the size of the map is calculated through the offsetWidth and offsetHeight of the containing
 * layout. It is not smart enough to keep padding, margin or border into account, so never use any of those on this
 * layout.
 * </p>
 *
 * @author Joachim Van der Auwera
 */
public class ResizableMapLayout implements IsWidget {

    private final MapPresenter mapPresenter;

    private final ResizeLayoutPanel layout;

    /**
     * Create a resizable map layout for the given map. The map will be added to this layout.
     *
     * @param mapPresenter The map to add.
     */
    public ResizableMapLayout(final MapPresenter mapPresenter) {
        this.mapPresenter = mapPresenter;
        mapPresenter.getEventBus().addHandler(MapInitializationHandler.TYPE, new RedrawMapInitializationHandler());

        layout = new ResizeLayoutPanel();
        layout.setSize("100%", "100%");
        layout.add(mapPresenter.asWidget());

        // Add an automatic resize handler to set the correct size when the window resizes:
        Window.addResizeHandler(new ResizeHandler() {

            @Override
            public void onResize(ResizeEvent event) {
                applySize();
            }
        });

        // Calculate the correct size on load:
        layout.addAttachHandler(new AttachEvent.Handler() {

            public void onAttachOrDetach(AttachEvent event) {
                Timer timer = new Timer() {

                    @Override
                    public void run() {
                        if (!applySize()) {
                            schedule(50);
                        }
                    }
                };
                timer.run();
            }
        });
    }

    /** Returns the layout containing the map. */
    public Widget asWidget() {
        return layout;
    }

    /**
     * Get the map that's within this layout.
     *
     * @return The map that's within this layout.
     */
    public MapPresenter getMapPresenter() {
        return mapPresenter;
    }

    // ------------------------------------------------------------------------
    // Private methods and classes:
    // ------------------------------------------------------------------------

    private boolean applySize() {
        if (mapPresenter.getViewPort().isInitialized()) {
            int width = layout.getOffsetWidth();
            int height = layout.getOffsetHeight();
            if (width > 50 && height > 50) {
                mapPresenter.setSize(width, height);
                try {
                    mapPresenter.getViewPort().applyBounds(mapPresenter.getViewPort().getBounds());
                } catch (Throwable t) {
                    Log.logWarn("Could not center map..." + t.getMessage());
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    /**
     * Map initialization handler that zooms in.
     *
     * @author Joachim Van der Auwera
     */
    private class RedrawMapInitializationHandler implements MapInitializationHandler {

        @Override
        public void onMapInitialized(MapInitializationEvent event) {
            applySize();
        }
    }
}