view flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterMatrix.java @ 3858:87e7571970e6

Removed trailing whitespace flys-client/trunk@5609 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 27 Sep 2012 09:39:12 +0000
parents 3228d65b0db9
children f9729662f1be
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ClickListener;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.tile.TileLayout;

import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.IntegerOptionsData;
import de.intevation.flys.client.shared.model.StringOptionsData;


/**
 * Some parameters take the form of on/off options that can also be seen
 * as a matrix.
 *
 * This class helps to survive the underlying objects and create a visual
 * representation of this matrix. Later can happen in two ways to overcome
 * shortcomings of GWT/SmartGWT combination.
 */
public class ParameterMatrix {

    public static class Column implements Serializable {
        protected String              name;
        protected Map<String, String> values;

        private Column() {
            this.values = new HashMap<String, String>();
        }

        public Column(String name) {
            this();
            this.name = name;
        }

        public void addValue(String label, String value) {
            values.put(label, value);
        }

        public String getValue(String label) {
            return values.get(label);
        }
    } // end of class Column

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

    public static final int CELL_HEIGHT = 25;

    private Map<String, Column> columns;
    private List<String>        columnNames;
    private List<String>        valueNames;

    private Map<String, List<String>> selected;

    public ParameterMatrix() {
        super();
        this.columns     = new HashMap<String, Column>();
        this.columnNames = new ArrayList<String>();
        this.valueNames  = new ArrayList<String>();
        this.selected    = new HashMap<String, List<String>>();
    }


    public void addColumn(IntegerOptionsData group) {
        String groupTitle = group.getLabel();

        Column     col   = new Column(groupTitle);
        DataItem[] items = group.getItems();

        if (items == null) {
            GWT.log("No items found in StringOptionsData '" + groupTitle + "'");
            return;
        }

        for (DataItem item: items) {
            String title = item.getLabel();

            if (valueNames.indexOf(title) < 0) {
                valueNames.add(title);
            }

            col.addValue(item.getLabel(), item.getStringValue());
        }

        columnNames.add(groupTitle);
        columns.put(groupTitle, col);
    }


    public void addColumn(StringOptionsData options) {
        String groupTitle = options.getLabel();

        Column     col   = new Column(groupTitle);
        DataItem[] items = options.getItems();

        if (items == null) {
            GWT.log("No items found in StringOptionsData '" + groupTitle + "'");
            return;
        }

        for (DataItem item: items) {
            String title = item.getLabel();

            if (valueNames.indexOf(title) < 0) {
                valueNames.add(title);
            }

            col.addValue(item.getLabel(), item.getStringValue());
        }

        columnNames.add(groupTitle);
        columns.put(groupTitle, col);
    }


    public Widget createTileLayout() {
        TileLayout tileLayout = new TileLayout();
        tileLayout.setTilesPerLine (columnNames.size() + 1);
        tileLayout.setAutoWrapLines(false);
        tileLayout.setTileMargin(1);

        tileLayout.addTile(new Label(""));
        for (int i = 0, n = columnNames.size(); i < n; i++) {
            String columnName = columnNames.get(i);
            Column col        = columns.get(columnName);

            selected.put(columnName, new ArrayList<String>());

            tileLayout.addTile(createLabel(MESSAGE.getString(columnName)));
        }

        int nVals = valueNames.size();

        for (int j = 0; j < nVals; j++) {
            for (int i = 0, n = columnNames.size(); i < n; i++) {
                String valueName  = valueNames.get(j);
                String columnName = columnNames.get(i);
                Column col        = columns.get(columnName);
                String value      = col.getValue(valueName);

                if (i == 0) {
                    tileLayout.addTile(createLabel(valueName));
                }

                if (value != null && value.length() > 0) {
                    tileLayout.addTile(createCheckBox(columnName, value));
                }
            }
        }

        return tileLayout;
    }


    /**
     * Returns a widget with matrix of checkboxes and labels.
     * @param tileLayouted if true, use a TileLayout (for inclusion in SmartGWT
     *                     containers, avoiding scrollbar-issues.
     */
    public Widget create(boolean tileLayouted) {
        if (tileLayouted) {
            return createTileLayout();
        }
        Grid grid = new Grid(valueNames.size() + 1, columnNames.size() + 1);

        for (int i = 0, n = columnNames.size(); i < n; i++) {
            String columnName = columnNames.get(i);
            Column col        = columns.get(columnName);

            selected.put(columnName, new ArrayList<String>());

            grid.setWidget(0, i+1, createLabel(MESSAGE.getString(columnName)));

            for (int j = 0, o = valueNames.size(); j < o; j++) {
                String valueName = valueNames.get(j);
                String value     = col.getValue(valueName);

                if (i == 0) {
                    grid.setWidget(j+1, 0, createLabel(valueName));
                }

                if (value != null && value.length() > 0) {
                    grid.setWidget(j+1, i+1, createCheckBox(columnName, value));
                }
            }
        }

        return grid;
    }


    /** Creates label with given text. */
    protected Label createLabel(String text) {
        Label label = new Label(text);
        label.setHeight(CELL_HEIGHT);

        return label;
    }


    /** Create Checkbox for column/value. */
    protected Canvas createCheckBox(final String colName, final String value) {
        CheckBox box = new CheckBox();
        box.addClickListener(new ClickListener() {
            @Override
            public void onClick(Widget sender) {
                CheckBox box = (CheckBox) sender;
                Map<String, List<String>> selection = getSelection();

                List<String> values = selection.get(colName);
                if (values.indexOf(value) >= 0) {
                    values.remove(value);
                }
                else {
                    values.add(value);
                }
            }
        });

        Canvas c = new Canvas();
        c.addChild(box);
        return c;
    }


    public Map<String, List<String>> getSelection() {
        return selected;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org