Mercurial > dive4elements > river
diff flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterMatrix.java @ 2521:fe177e7f61d1
Added a new UIProvider that renders a matrix of checkboxes for user input.
flys-client/trunk@4401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 14 May 2012 14:45:02 +0000 |
parents | |
children | 261347ea60b8 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterMatrix.java Mon May 14 14:45:02 2012 +0000 @@ -0,0 +1,148 @@ +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.layout.VLayout; + +import de.intevation.flys.client.shared.model.DataItem; +import de.intevation.flys.client.shared.model.IntegerOptionsData; + + +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 + + + 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); + for (DataItem item: group.getItems()) { + 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 create() { + 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(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, createWidget(columnName, value)); + } + } + } + + return grid; + } + + + protected Label createLabel(String text) { + Label label = new Label(text); + label.setHeight(CELL_HEIGHT); + + return label; + } + + + protected CheckBox createWidget(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); + } + } + }); + + return box; + } + + + public Map<String, List<String>> getSelection() { + return selected; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :