view flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/OverviewOutputTab.java @ 2982:d2a54ae0016b

Switched from 2 columns and 3 rows to 3 columns and 2 rows in sq chart overview. flys-client/trunk@4981 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 13 Jul 2012 10:09:20 +0000
parents 5161e25392ea
children e746e57c65c0
line wrap: on
line source
package de.intevation.flys.client.client.ui.chart;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.events.ResizedEvent;
import com.smartgwt.client.widgets.events.ResizedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import de.intevation.flys.client.client.Config;
import de.intevation.flys.client.client.ui.CollectionView;
import de.intevation.flys.client.client.ui.OutputTab;

import de.intevation.flys.client.shared.model.Collection;
import de.intevation.flys.client.shared.model.OutputMode;


/**
 * An overview output tab. This tab displays an overview of all charts
 * integrated in the current calculation mode.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class OverviewOutputTab
extends OutputTab
implements   ResizedHandler
{
    /** The collection view.*/
    protected CollectionView view;

    /** The root layout. */
    protected HLayout root;

    /** The columns */
    protected VLayout[] columns;

    /** The chart container.*/
    protected Canvas[][] charts;

    /** All relevant output modes. */
    protected Map<String, OutputMode> relModes;


    public OverviewOutputTab(
        String title,
        Collection collection,
        OutputMode mode,
        CollectionView collectionView)
    {
        super(title, collection, collectionView, mode);

        relModes = new HashMap<String, OutputMode>();

        Map<String, OutputMode> modes = collection.getOutputModes();
        Set<String> keys = modes.keySet();

        // Get all relevant outputs.
        for (String key: keys) {
            OutputMode m = modes.get(key);
            if (m.getType().equals("chart")) {
                relModes.put(key, m);
            }
        }

        root = new HLayout();
        setPane(root);
        root.addResizedHandler(this);

        // Get the column and row count and initialize the grid.
        int width = getColumnCount(relModes.size());
        int height = getRowCount(relModes.size());
        charts = new Canvas[width][height];
        columns = new VLayout[width];

        for (int i = 0; i < width; i++) {
            columns[i] = new VLayout();
            root.addMember(columns[i]);

            for (int j = 0; j < height; j++) {
                charts[i][j] = new Canvas();

                // This is for 3, 6 or 9 charts only! 
                // TODO: Calculate the height.
                charts[i][j].setHeight("50%");

                String type =
                    ((OutputMode)relModes.values()
                        .toArray()[j + i * height]).getName();
                columns[i].addMember(charts[i][j]);
                charts[i][j].addChild(
                    new Img(getImgUrl(
                        charts[i][j].getWidth(),
                        charts[i][j].getHeight(), type)));

            }
        }
    }


    /**
     * Resize handler.
     * 
     * @param event The resize event.
     */
    @Override
    public void onResized(ResizedEvent event) {
        for (int i = 0; i < charts.length; i++) {
            // This is for 3, 6 or 9 charts only! 
            // TODO: Calculate the width.
            columns[i].setWidth(root.getWidth()/3);

            for (int j = 0; j < charts[i].length; j++) {
                String type =
                    ((OutputMode)relModes.values()
                        .toArray()[j + i * charts[i].length]).getName();
                Canvas[] children = charts[i][j].getChildren();
                for (int k = 0; k < children.length; k++) {
                    charts[i][j].removeChild(children[k]);
                }
                charts[i][j].addChild(new Img(
                        getImgUrl(
                            charts[i][j].getWidth(),
                            charts[i][j].getHeight(),
                            type),
                        charts[i][j].getWidth(),
                        charts[i][j].getHeight()
                        ));
            }
        }
    }


    /**
     * Returns the column count for the grid.
     *
     * @param count all fields
     * @return the column count
     */
    protected int getRowCount(int count) {
        if (count <= 3) {
            return 1;
        }
        else if (count > 3 && count < 9) {
            return 2;
        }
        else {
            return 3;
        }
    }


    /**
     * Returns the row count for the grid.
     *
     * @param count all fields
     * @return the row count
     */
    protected int getColumnCount(int count) {
        if(count <= 3) {
            return count;
        }
        else if(count > 3 && count < 9) {
            return ((count + (count % 2))/getRowCount(count));
        }
        else {
            return (count + (3 - (count % 3))/getRowCount(count));
        }
    }


    /**
     * Builds the URL that points to the chart image.
     *
     * @param width The width of the requested chart.
     * @param height The height of the requested chart.
     * @param xr Optional x range (used for zooming).
     * @param yr Optional y range (used for zooming).
     *
     * @return the URL to the chart image.
     */
    protected String getImgUrl(int width, int height, String type) {
        Config config = Config.getInstance();

        String imgUrl = GWT.getModuleBaseURL();
        imgUrl += "chart";
        imgUrl += "?uuid=" + collection.identifier();
        imgUrl += "&type=" + type;
        imgUrl += "&locale=" + config.getLocale();
        imgUrl += "&timestamp=" + new Date().getTime();
        imgUrl += "&width=" + Integer.toString(width);
        imgUrl += "&height=" + Integer.toString(height);

        return imgUrl;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org