view flys-client/src/main/java/de/intevation/flys/client/client/ui/fixation/FixationPanel.java @ 2917:be99bf1aa59b

Create simplified column filter. flys-client/trunk@4739 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 21 Jun 2012 11:55:37 +0000
parents 7d69e570e79b
children 71db63f67d31
line wrap: on
line source
package de.intevation.flys.client.client.ui.fixation;

import java.util.HashMap;
import java.util.Date;

import java.io.Serializable;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.i18n.client.DateTimeFormat;

import com.smartgwt.client.util.SC;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ResizedHandler;
import com.smartgwt.client.widgets.events.ResizedEvent;
import com.smartgwt.client.widgets.form.validator.IsFloatValidator;
import com.smartgwt.client.types.Alignment;

import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.Img;

import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.client.ui.AbstractUIProvider;

import de.intevation.flys.client.client.Config;

import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataList;
import de.intevation.flys.client.shared.model.FixingsOverviewInfo;
import de.intevation.flys.client.shared.model.FixAnalysisArtifact;

import de.intevation.flys.client.client.services.FixingsOverviewService;
import de.intevation.flys.client.client.services.FixingsOverviewServiceAsync;


/**
 * This UIProvider creates helper panel for fixation analysis without input
 * elements.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public abstract class FixationPanel
extends               AbstractUIProvider
implements            ResizedHandler
{
    protected static HashMap<String, FixationPanel> instances = new HashMap<String, FixationPanel>();

    /** The message class that provides i18n strings. */
    protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);

    protected FixingsOverviewServiceAsync overviewService =
        GWT.create(FixingsOverviewService.class);

    protected String   htmlOverview;
    protected FixingsOverviewInfo fixInfo;
    protected TabSet tabs;
    protected Tab events;
    protected Tab chart;
    protected VLayout chartContainer;
    protected Img chartImg;

    public FixationPanel() {
        chartImg = new Img();
        htmlOverview = "";
    }

    protected String getArtifactUuid() {
        return this.artifact.getUuid();
    }

    protected void init() {

    }

    public Data[] getData() {
        return null;
    }

    public Canvas create(DataList list) {
        VLayout layout = new VLayout();

        Canvas helper = createHelper();
        this.helperContainer.addMember(helper);

        Canvas submit = getNextButton();
        Canvas widget = createWidget(list);

        layout.addMember(widget);
        layout.addMember(submit);
        return layout;
    }

    public Canvas createOld(DataList list) {
        return new DynamicForm();
    }

    protected Canvas createHelper() {
        Config config    = Config.getInstance();
        String locale    = config.getLocale ();

        tabs = new TabSet();
        events = new Tab(MESSAGES.events());
        chart = new Tab(MESSAGES.kmchart());

        chartContainer = new VLayout();
        Canvas scroll = createChartHelper();

        VLayout layout = new VLayout();
        layout.addResizedHandler(this);
        layout.addMember(chartContainer);
        layout.addMember(scroll);
        layout.setAlign(Alignment.CENTER);
        chart.setPane(layout);

        final HTMLPane eventPane = new HTMLPane();

        String river = artifact.getArtifactDescription().getRiver();
        createCallback();

        String callBack = "fixationCallback(this.checked, this.name)";
        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;

        overviewService.generateOverview(
            locale,
            artifact.getUuid(),
            art.getFilter().getOverviewFilter(),
            renderCheckboxes(),
            callBack,
            new AsyncCallback<FixingsOverviewInfo>() {
                public void onFailure(Throwable caught) {
                    GWT.log("Could not receive overview.");
                    SC.warn(caught.getMessage());
                }
                public void onSuccess(FixingsOverviewInfo info) {
                    GWT.log("Successfully loaded overview.");
                    fixInfo = info;
                    htmlOverview = info.getHTML();
                    FixAnalysisArtifact art = (FixAnalysisArtifact)artifact;
                    art.getFilter().setRiver(info.getRiver());
                    art.getFilter().setCurrentKm(1d);
                    art.getFilter().setFromKm(info.getFrom());
                    art.getFilter().setToKm(info.getTo());
                    eventPane.setContents(htmlOverview);
                    updateChartTab(fixInfo.getFrom());
                    events.setPane(eventPane);
                    success();
                }
            });

        tabs.addTab(events);
        tabs.addTab(chart);

        return tabs;
    }


    protected Canvas createChartHelper() {


        DynamicForm form = new DynamicForm();
        Button lower = new Button("<<");
        lower.setWidth(30);
        Button upper = new Button(">>");
        upper.setWidth(30);
        final TextItem currentkm = new TextItem();
        currentkm.setWidth(60);
        currentkm.setShowTitle(false);
        currentkm.setValidators(new IsFloatValidator());

        form.setFields(currentkm);
        form.setWidth(60);
        lower.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent ce) {
                updateChartTabLow();
                FixAnalysisArtifact art = (FixAnalysisArtifact) artifact;
                currentkm.setValue(art.getFilter().getCurrentKm());
            }
        });

        upper.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent ce) {
                updateChartTabUp();
                FixAnalysisArtifact art = (FixAnalysisArtifact) artifact;
                currentkm.setValue(art.getFilter().getCurrentKm());
            }
        });

        currentkm.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent ce) {
                //TODO: get current value.
                if(ce.getForm().validate() && ce.getItem().getValue() != null) {
                    try {
                        String s = ce.getItem().getValue().toString();
                        Double d = new Double(s);
                        updateChartTab(d.doubleValue());
                    }
                    catch(NumberFormatException nfe) {
                        // Do nothing.
                    }
                }
            }
        });

        HLayout layout = new HLayout();
        layout.setAlign(Alignment.CENTER);

        layout.addMember(lower);
        layout.addMember(form);
        layout.addMember(upper);
        return layout;
    }

    protected void updateChartTab(double km) {
        Config config    = Config.getInstance();
        String locale    = config.getLocale ();

        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;
        if (km < 0) {
            km = 0;
        }
        art.getFilter().setCurrentKm(km);
        if (chartContainer.hasMember(chartImg)) {
            chartContainer.removeMember(chartImg);
        }

        int hWidth = helperContainer.getWidth() - 12;
        int hHeight = helperContainer.getHeight() - 62;

        if ((int)(hHeight *4/3) < hWidth) {
            hWidth = (int)hHeight * 4/3;
        }
        else {
            hHeight = (int)hWidth *3/4;
        }

        String imgUrl = GWT.getModuleBaseURL();
        imgUrl += "fixings-km-chart";
        imgUrl += "?locale=" + locale;
        imgUrl += "&filter=" + art.getFilter().getChartFilter(hWidth, hHeight);

        chartImg = new Img(imgUrl, hWidth, hHeight);
        chartContainer.addMember(chartImg);

    }


    protected void updateChartTabLow() {
        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;

        double curr = art.getFilter().getCurrentKm();
        if (curr > art.getFilter().getFromKm()) {
            double newVal = (curr - 0.1) * 10;
            long round = Math.round(newVal);
            updateChartTab(((double)round) / 10);
        }
        return;
    }


    protected void updateChartTabUp() {
        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;

        double curr = art.getFilter().getCurrentKm();
        if (curr < art.getFilter().getToKm()) {
            double newVal = (curr + 0.1) * 10;
            long round = Math.round(newVal);
            updateChartTab(((double)round) / 10);
        }
        return;
    }


    public void onResized(ResizedEvent re) {
        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;

        updateChartTab(art.getFilter().getCurrentKm());
    }


    private native void createCallback() /*-{
        $wnd.fixationCallback = @de.intevation.flys.client.client.ui.fixation.FixationPanel::helperCallback(ZLjava/lang/String;);
    }-*/;

    private static void helperCallback(boolean checked, String name) {
        String[] parts = name.split(":");
        String uuid = parts[0];
        String cid = parts[1];
        FixationPanel p = FixationPanel.getInstance(uuid);
        if (p != null) {
            p.setValues(cid, checked);
        }
    }

    private static FixationPanel getInstance(String uuid) {
        for (int i = 0; i < instances.size(); i++) {
            if (instances.get(uuid) != null) {
                return instances.get(uuid);
            }
        }
        return null;
    }

    public abstract Canvas createWidget(DataList data);
    public abstract void setValues(String cid, boolean checked);
    public abstract boolean renderCheckboxes();
    public abstract void success();

    public static class FixFilter implements Serializable{
        protected String river;
        protected double fromKm;
        protected double toKm;
        protected double currentKm;
        protected int fromClass;
        protected int toClass;
        protected long fromDate;
        protected long toDate;
        protected boolean hasDate;
        protected int[] events;

        public FixFilter() {
            this.river = "";
            this.fromKm = -1d;
            this.toKm = -1;
            this.currentKm = -1;
            this.fromClass = -1;
            this.toClass = -1;
            this.fromDate = -1;
            this.toDate = -1;
            this.hasDate = false;
            this.events = new int[0];
        }

        public void setRiver(String river) {
            this.river = river;
        }

        public void setFromKm(double from) {
            this.fromKm = from;
        }

        public void setToKm(double to) {
            this.toKm = to;
        }

        public void setCurrentKm(double km) {
            this.currentKm = km;
        }

        public void setFromClass(int from) {
            this.fromClass = from;
        }

        public void setToClass(int to) {
            this.toClass = to;
        }

        public void setFromDate(long from) {
            this.hasDate = true;
            this.fromDate = from;
        }

        public void setToDate(long to) {
            this.hasDate = true;
            this.toDate = to;
        }

        public void setEvents(int[] ev) {
            this.events = ev;
        }

        public String getRiver() {
            return this.river;
        }

        public double getFromKm() {
            return this.fromKm;
        }

        public double getToKm() {
            return this.toKm;
        }

        public double getCurrentKm() {
            return this.currentKm;
        }

        public int getFromClass() {
            return this.fromClass;
        }

        public int getToClass() {
            return this.toClass;
        }

        public long getFromDate() {
            return this.fromDate;
        }

        public long getToDate() {
            return this.toDate;
        }

        public int[] getEvents() {
            return this.events;
        }

        public String getOverviewFilter() {
            if (river != null && river.length() > 0) {
                JSONObject jfix = new JSONObject();
                JSONObject jfilter = new JSONObject();
                JSONObject jrName = new JSONObject();
                JSONString jrValue = new JSONString(river);
                jrName.put("name", jrValue);
                jfilter.put("river", jrName);
                jfix.put("fixings", createFilter(jfilter));
                return jfix.toString();
            }
            return "";
        }

        public String getChartFilter(int width, int height) {
            if (river != null && river.length() > 0 &&
                currentKm >= fromKm && currentKm <= toKm)
            {
                JSONObject jfix = new JSONObject();
                JSONObject jfilter = new JSONObject();
                JSONObject jrName = new JSONObject();
                JSONString jrValue = new JSONString(river);
                JSONObject jkm = new JSONObject();
                JSONNumber jkmValue = new JSONNumber(currentKm);
                JSONObject jextent = new JSONObject();
                JSONNumber jwidth = new JSONNumber(width);
                JSONNumber jheight = new JSONNumber(height);

                jkm.put("value", jkmValue);
                jrName.put("name", jrValue);
                jfilter.put("river", jrName);
                jfilter.put("km", jkm);
                jextent.put("width", jwidth);
                jextent.put("height", jheight);
                jfilter.put("extent", jextent);
                jfix.put("fixings", createFilter(jfilter));
                return jfix.toString();
            }
            return "";
        }

        protected JSONObject createFilter(JSONObject root) {
            if (this.fromKm >= 0 && this.toKm >= 0 && this.fromKm <=this.toKm) {
                JSONObject range = new JSONObject();
                JSONObject fromtokm = new JSONObject();
                JSONNumber f = new JSONNumber(this.fromKm);
                JSONNumber t = new JSONNumber(this.toKm);
                fromtokm.put("from", f);
                fromtokm.put("to", t);
                root.put("range", fromtokm);
            }
            JSONObject and = new JSONObject();
            if (this.hasDate) {
                JSONObject daterange = new JSONObject();
                Date df = new Date(this.fromDate);
                Date dt = new Date(this.toDate);
                DateTimeFormat dtf = DateTimeFormat.getFormat("dd.MM.yyyy");
                JSONString f = new JSONString(dtf.format(df));
                JSONString t = new JSONString(dtf.format(dt));
                daterange.put("from", f);
                daterange.put("to", t);
                and.put("date-range", daterange);
            }
            if (this.fromClass >= 0 &&
                this.toClass >= 0 &&
                this.fromClass <= this.toClass) {
                JSONObject classrange = new JSONObject();
                JSONNumber f = new JSONNumber(this.fromClass);
                JSONNumber t = new JSONNumber(this.toClass);
                classrange.put("from", f);
                classrange.put("to", t);
                and.put("sector-range", classrange);
            }
            if (this.events.length > 0) {
                StringBuilder cids = new StringBuilder();

                for (int i = 0; i < events.length; i++) {
                    if (i > 0) cids.append(' ');
                    cids.append(events[i]);
                }
                JSONObject columns = new JSONObject();
                columns.put("cids", new JSONString(cids.toString()));
                and.put("columns", columns);
            }
            if (and.size() > 0) {
                JSONObject filter = new JSONObject();
                filter.put("and", and);
                root.put("filter", filter);
            }
            return root;
        }
    }
}

http://dive4elements.wald.intevation.org