view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/map/MapThemePanel.java @ 9483:992c188b7330

wms uinfo.inundation
author gernotbelger
date Thu, 13 Sep 2018 18:22:05 +0200
parents 05405292a7ca
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.client.client.ui.map;

import org.dive4elements.river.client.client.FLYSConstants;
import org.dive4elements.river.client.client.ui.CollectionView;
import org.dive4elements.river.client.client.ui.NoopThemeRecordHandler;
import org.dive4elements.river.client.client.ui.ThemePanel;
import org.dive4elements.river.client.shared.model.AttributedTheme;
import org.dive4elements.river.client.shared.model.FacetRecord;
import org.dive4elements.river.client.shared.model.OutputMode;
import org.dive4elements.river.client.shared.model.Theme;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.HeaderDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.HeaderDoubleClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class MapThemePanel extends ThemePanel {

    public static final int CELL_HEIGHT = 25;

    public interface ActivateCallback {
        void activate(Theme theme, boolean activate);
    }

    public interface ThemeMovedCallback {
        void onThemeMoved(Theme theme, int oldIdx, int newIdx);
    }

    public interface LayerZoomCallback {
        void onLayerZoom(Theme theme, String extent);
    }

    private final FLYSConstants MSG = GWT.create(FLYSConstants.class);

    protected ActivateCallback activateCallback;
    protected ThemeMovedCallback themeMovedCallback;
    protected LayerZoomCallback layerZoomCallback;

    protected ListGridRecord[] oldRecords;

    public static final String GRID_FIELD_ACTIVE = "active";
    public static final String GRID_FIELD_NAME = "name";

    protected MapOutputTab mapOut;

    public MapThemePanel(final CollectionView view, final OutputMode mode, final MapOutputTab mapOut, final ActivateCallback activateCallback,
            final ThemeMovedCallback themeMovedCallback, final LayerZoomCallback layerZoomCallback) {
        super(mode, view, new NoopThemeRecordHandler());

        this.mapOut = mapOut;
        this.activateCallback = activateCallback;
        this.themeMovedCallback = themeMovedCallback;
        this.layerZoomCallback = layerZoomCallback;

        initGrid();
        initLayout();

        updateGrid();
    }

    protected void initLayout() {
        setWidth100();
        setHeight100();

        final VLayout layout = new VLayout();
        layout.setWidth100();
        layout.setHeight100();

        layout.addMember(this.list);
        layout.addMember(this.navigation);

        addChild(layout);
    }

    protected void initGrid() {
        this.list.setCanEdit(true);
        this.list.setCanSort(false);
        this.list.setShowRecordComponents(false);
        this.list.setShowRecordComponentsByCell(true);
        this.list.setShowHeader(true);
        this.list.setShowHeaderContextMenu(false);
        this.list.setCanReorderFields(false);
        this.list.setWidth100();
        this.list.setHeight100();

        this.list.addHeaderDoubleClickHandler(new HeaderDoubleClickHandler() {
            @Override
            public void onHeaderDoubleClick(final HeaderDoubleClickEvent event) {
                // cancel the event.
                return;
            }
        });

        this.list.setCellHeight(CELL_HEIGHT);
        this.list.setShowRecordComponents(true);
        this.list.setShowRecordComponentsByCell(true);
        this.list.setShowAllRecords(true);

        this.list.addEditCompleteHandler(this);

        final ListGridField active = new ListGridField(GRID_FIELD_ACTIVE, " ", 20);
        active.setType(ListGridFieldType.BOOLEAN);
        active.setCanDragResize(false);

        final ListGridField name = new ListGridField(GRID_FIELD_NAME, this.MSG.chart_themepanel_header_themes());
        name.setType(ListGridFieldType.TEXT);

        this.list.setFields(active, name);
    }

    @Override
    protected void clearGrid() {
        this.oldRecords = this.list.getRecords();
        super.clearGrid();
    }

    @Override
    protected void addFacetRecord(final FacetRecord rec) {
        final Theme newTheme = rec.getTheme();
        boolean isNew = true;

        for (final ListGridRecord old : getOldRecords()) {
            final FacetRecord fr = (FacetRecord) old;

            if (newTheme.equals(fr.getTheme())) {
                isNew = false;
                break;
            }
        }

        if (isNew && this.mapOut != null)
            this.mapOut.addLayer(newTheme);

        super.addFacetRecord(rec);
    }

    @Override
    protected Menu getSingleContextMenu(final ListGridRecord[] records) {
        final Menu menu = super.getSingleContextMenu(records);

        final MenuItem layerZoom = createLayerZoomItem(records);
        if (layerZoom != null) {
            menu.addItem(layerZoom);
        }
        menu.addItem(createMapURLItem(records));

        return menu;
    }

    protected MenuItem createMapURLItem(final ListGridRecord[] records) {
        final FacetRecord fr = (FacetRecord) records[0];
        final Theme theme = fr.getTheme();
        final AttributedTheme at = (AttributedTheme) theme;

        final MenuItem item = new MenuItem(this.MSG.wmsURLMenuItem());
        item.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(final MenuItemClickEvent evt) {
                final String url = getMapOutputTab().getWMSUrl(theme);

                SC.say(MapThemePanel.this.MSG.wmsURLBoxTitle(), url);
            }
        });

        return item;
    }

    @Override
    protected MenuItem createRemoveItem(final ListGridRecord[] records) {
        final MenuItem item = super.createRemoveItem(records);
        item.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(final MenuItemClickEvent evt) {
                SC.ask(MapThemePanel.this.MSG.askThemeRemove(), new BooleanCallback() {
                    @Override
                    public void execute(final Boolean value) {
                        if (value) {
                            for (final ListGridRecord record : records) {
                                final FacetRecord facet = (FacetRecord) record;

                                final Theme theme = facet.getTheme();
                                theme.setVisible(0);
                                theme.setActive(0);

                                getMapOutputTab().removeLayer(theme);
                            }

                            updateCollection();
                        }
                    }
                });
            }
        });

        return item;
    }

    protected MenuItem createLayerZoomItem(final ListGridRecord[] recs) {
        final FacetRecord fr = (FacetRecord) recs[0];
        final AttributedTheme at = (AttributedTheme) fr.getTheme();

        final String extent = at.getAttr("extent");

        if (extent == null || extent.length() == 0) {
            return null;
        }

        final MenuItem zoom = new MenuItem(this.MSG.zoomToLayer());
        zoom.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(final MenuItemClickEvent evt) {
                if (MapThemePanel.this.layerZoomCallback != null) {
                    MapThemePanel.this.layerZoomCallback.onLayerZoom(at, extent);
                }
            }
        });

        return zoom;
    }

    @Override
    public String getWidthAsString() {
        if (!isVisible()) {
            return "0";
        } else {
            return super.getWidthAsString();
        }
    }

    @Override
    public void activateTheme(final Theme theme, final boolean active) {
        if (this.activateCallback != null) {
            this.activateCallback.activate(theme, active);
        }

        theme.setActive(active ? 1 : 0);
    }

    @Override
    protected void fireThemeMoved(final Theme theme, final int oldIdx, final int newIdx) {
        if (this.themeMovedCallback != null) {
            this.themeMovedCallback.onThemeMoved(theme, oldIdx, newIdx);
        }
    }

    protected ListGridRecord[] getOldRecords() {
        return this.oldRecords != null ? this.oldRecords : new ListGridRecord[0];
    }

    protected MapOutputTab getMapOutputTab() {
        return this.mapOut;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org