view flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationPicker.java @ 3847:f3b821735e39

Calculate the info url via i18n Don't fetch the info url from the artifact service and use i18n to calculate the url by using the official gauge and river number. flys-client/trunk@5582 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Mon, 24 Sep 2012 08:39:22 +0000
parents 99bd77501188
children 480de0dbca8e
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import java.util.LinkedHashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Criterion;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.types.OperatorId;

import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;

import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;

import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.Alignment;

import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.client.event.FilterHandler;
import de.intevation.flys.client.client.event.StringFilterEvent;
import de.intevation.flys.client.client.event.RangeFilterEvent;

/**
 * Bundle widgets and handler for a lacation input helper.
 *
 * Note that the construction is weird and driven by issues that arose due to
 * reasons not understood.
 */
public class LocationPicker
implements   FilterHandler
{
    /** The message class that provides i18n strings.*/
    protected FLYSConstants MSG = GWT.create(FLYSConstants.class);

    /** The locations table. */
    protected ListGrid locationTable;

    protected HLayout filterLayout;

    DynamicForm resultCountForm;

    RecordClickHandler handler;

    /** Text to show number of matched items when filtered. */
    protected StaticTextItem filterResultCount;


    public LocationPicker(RecordClickHandler handler) {
        locationTable = new ListGrid();
        locationTable.setShowHeaderContextMenu(false);
        this.handler = handler;
    }

    public void prepareFilter() {

        filterResultCount = new StaticTextItem(MSG.resultCount());
        filterResultCount.setTitleAlign(Alignment.LEFT);
        filterResultCount.setTitleStyle("color: #000");

        final TableFilter filter = new TableFilter();
        filter.setHeight("30px");
        filter.addFilterHandler(this);

        final RangeTableFilter filterRange = new RangeTableFilter();
        filterRange.setHeight("30px");
        filterRange.addFilterHandler(this);
        filterRange.setVisible(false);

        SelectItem filterCriteria = new SelectItem();
        filterCriteria.setShowTitle(false);
        filterCriteria.setWidth(100);
        filterCriteria.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent e) {
                if(e.getValue().toString().equals("range")) {
                    filterRange.setVisible(true);
                    filter.setVisible(false);
                    filter.clear();
                    filterResultCount.setValue("");
                }
                else {
                    filterRange.setVisible(false);
                    filterRange.clear();
                    filter.setVisible(true);
                    filterResultCount.setValue("");
                }
            }
        });

        LinkedHashMap<String, String> filterMap =
            new LinkedHashMap<String, String>();
        filterMap.put("description", MSG.description());
        filterMap.put("range", MSG.range());
        filterCriteria.setValueMap(filterMap);
        filterCriteria.setValue("description");

        DynamicForm form = new DynamicForm();
        form.setFields(filterCriteria);

        resultCountForm = new DynamicForm();
        resultCountForm.setFields(filterResultCount);

        filterLayout = new HLayout();
        filterLayout.addMember(form);
        filterLayout.addMember(filter);
        filterLayout.addMember(filterRange);
    }


    /** Access the main widget, a table in which locations can be chosen. */
    public ListGrid getLocationTable() {
        return locationTable;
    }


    /** Access the 'form' that shows the filter result count. */
    public DynamicForm getResultCountForm() {
        return resultCountForm;
    }


    /** Access the layout containing filter stuff. */
    public HLayout getFilterLayout() {
        return filterLayout;
    }


    /**
     * This method creates a table that contains the location values.
     */
    protected void createLocationTable(/*RecordClickHandler handler*/) {
        GWT.log("Create Location Table in LocationPicker");

        String baseUrl = GWT.getHostPageBaseURL();

        locationTable.setWidth100();
        locationTable.setShowRecordComponents(true);
        locationTable.setShowRecordComponentsByCell(true);
        locationTable.setHeight100();
        locationTable.setEmptyMessage(MSG.empty_filter());
        locationTable.setCanReorderFields(false);

        ListGridField addLocation = new ListGridField ("", "");
        addLocation.setType (ListGridFieldType.ICON);
        addLocation.setWidth (20);
        addLocation.addRecordClickHandler (handler);
        addLocation.setCellIcon (baseUrl + MSG.markerGreen());
        GWT.log ("i18n: ..... "  + MSG.description());

        ListGridField ldescr = new ListGridField("description",
                MSG.description());
        ldescr.setType(ListGridFieldType.TEXT);
        ldescr.setWidth("*");
        ListGridField lside = new ListGridField("riverside",
                MSG.riverside());
        lside.setType(ListGridFieldType.TEXT);
        lside.setWidth("10%");

        ListGridField loc = new ListGridField("from", MSG.location());
        loc.setCellFormatter(new CellFormatter() {
            public String format(
                Object value,
                ListGridRecord record,
                int rowNum, int colNum) {
                    if (value == null) return null;
                    try {
                        NumberFormat nf;
                        double v = Double.parseDouble((String)value);
                        nf = NumberFormat.getFormat("###0.00##");
                        return nf.format(v);
                    }
                    catch (Exception e) {
                        return value.toString();
                    }
                }
            }
        );
        loc.setType(ListGridFieldType.FLOAT);

        loc.setWidth("10%");

        ListGridField bottom =
            new ListGridField("bottom", MSG.bottom_edge());
        bottom.setType(ListGridFieldType.TEXT);
        bottom.setWidth("10%");

        ListGridField top =
            new ListGridField("top", MSG.top_edge());
        top.setType(ListGridFieldType.TEXT);
        top.setWidth("10%");


        locationTable.setFields(
            addLocation, ldescr, loc, lside, bottom, top);
    }


    @Override
    public void onFilterCriteriaChanged(StringFilterEvent event) {
        String search = event.getFilter();

        if (search != null && search.length() > 0) {
            Criteria c = new Criteria("description", search);
            locationTable.filterData(c);
            filterResultCount.setValue(locationTable.getRecords().length);
        }
        else {
            locationTable.clearCriteria();
            filterResultCount.setValue("");
        }
    }


    @Override
    public void onFilterCriteriaChanged(RangeFilterEvent event) {
        Float from = event.getFrom() - 0.001f;
        Float to = event.getTo() + 0.001f;

        Criterion combinedFilter = null;
        if (from.equals(Float.NaN) && to.equals(Float.NaN)) {
            locationTable.clearCriteria();
            filterResultCount.setValue("");
            return;
        }
        else if (from.equals(Float.NaN)) {
            combinedFilter =
                new Criterion("from", OperatorId.LESS_OR_EQUAL, to);
        }
        else if (to.equals(Float.NaN)) {
            combinedFilter =
                new Criterion("from", OperatorId.GREATER_OR_EQUAL, from);
        }
        else {
            combinedFilter =
                new AdvancedCriteria(OperatorId.AND, new Criterion[] {
                    new Criterion("from", OperatorId.GREATER_OR_EQUAL, from),
                    new Criterion("from", OperatorId.LESS_OR_EQUAL, to)
                });
        }
        locationTable.filterData(combinedFilter);
        filterResultCount.setValue(locationTable.getRecords().length);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org