view flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationPicker.java @ 4221:480de0dbca8e

Extended location input helper. The locationpicker has now an attribute whether the input is distance or location to display one or two clickable columns. Replaced the record click handler with cell click handler.
author Raimund Renkert <rrenkert@intevation.de>
date Tue, 23 Oct 2012 13:17:20 +0200
parents 99bd77501188
children b195fede1c3b
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.AdvancedCriteria;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Criterion;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.CellFormatter;
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.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;

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

/**
 * 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;

    CellClickHandler handler;

    boolean isDistance = false;

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


    public LocationPicker(CellClickHandler 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.
     */
    public 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.setCellIcon (baseUrl + MSG.markerGreen());
        ListGridField addTo = new ListGridField ("", "");
        addTo.setType (ListGridFieldType.ICON);
        addTo.setWidth (20);
        addTo.setCellIcon (baseUrl + MSG.markerRed());
 
        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.addCellClickHandler(handler);
        if (isDistance) {
            locationTable.setFields(
                addLocation, addTo, ldescr, loc, lside, bottom, top);
        }
        else {
            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);
    }

    public void setIsDistance(boolean value) {
        this.isDistance = value;
    }

    public boolean isDistance() {
        return this.isDistance;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org