comparison 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
comparison
equal deleted inserted replaced
2520:fe67f1345687 2521:fe177e7f61d1
1 package de.intevation.flys.client.client.ui;
2
3 import java.io.Serializable;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import com.google.gwt.core.client.GWT;
11 import com.google.gwt.user.client.ui.CheckBox;
12 import com.google.gwt.user.client.ui.Grid;
13 import com.google.gwt.user.client.ui.Widget;
14 import com.google.gwt.user.client.ui.ClickListener;
15
16 import com.smartgwt.client.widgets.Canvas;
17 import com.smartgwt.client.widgets.Label;
18 import com.smartgwt.client.widgets.layout.VLayout;
19
20 import de.intevation.flys.client.shared.model.DataItem;
21 import de.intevation.flys.client.shared.model.IntegerOptionsData;
22
23
24 public class ParameterMatrix {
25
26 public static class Column implements Serializable {
27 protected String name;
28 protected Map<String, String> values;
29
30 private Column() {
31 this.values = new HashMap<String, String>();
32 }
33
34 public Column(String name) {
35 this();
36 this.name = name;
37 }
38
39 public void addValue(String label, String value) {
40 values.put(label, value);
41 }
42
43 public String getValue(String label) {
44 return values.get(label);
45 }
46 } // end of class Column
47
48
49 public static final int CELL_HEIGHT = 25;
50
51 private Map<String, Column> columns;
52 private List<String> columnNames;
53 private List<String> valueNames;
54
55 private Map<String, List<String>> selected;
56
57 public ParameterMatrix() {
58 super();
59 this.columns = new HashMap<String, Column>();
60 this.columnNames = new ArrayList<String>();
61 this.valueNames = new ArrayList<String>();
62 this.selected = new HashMap<String, List<String>>();
63 }
64
65
66 public void addColumn(IntegerOptionsData group) {
67 String groupTitle = group.getLabel();
68
69 Column col = new Column(groupTitle);
70 for (DataItem item: group.getItems()) {
71 String title = item.getLabel();
72
73 if (valueNames.indexOf(title) < 0) {
74 valueNames.add(title);
75 }
76
77 col.addValue(item.getLabel(), item.getStringValue());
78 }
79
80 columnNames.add(groupTitle);
81 columns.put(groupTitle, col);
82 }
83
84
85 public Widget create() {
86 Grid grid = new Grid(valueNames.size()+1, columnNames.size() + 1);
87
88 for (int i = 0, n = columnNames.size(); i < n; i++) {
89 String columnName = columnNames.get(i);
90 Column col = columns.get(columnName);
91
92 selected.put(columnName, new ArrayList<String>());
93
94 grid.setWidget(0, i+1, createLabel(columnName));
95
96 for (int j = 0, o = valueNames.size(); j < o; j++) {
97 String valueName = valueNames.get(j);
98 String value = col.getValue(valueName);
99
100 if (i == 0) {
101 grid.setWidget(j+1, 0, createLabel(valueName));
102 }
103
104 if (value != null && value.length() > 0) {
105 grid.setWidget(j+1, i+1, createWidget(columnName, value));
106 }
107 }
108 }
109
110 return grid;
111 }
112
113
114 protected Label createLabel(String text) {
115 Label label = new Label(text);
116 label.setHeight(CELL_HEIGHT);
117
118 return label;
119 }
120
121
122 protected CheckBox createWidget(final String colName, final String value) {
123 CheckBox box = new CheckBox();
124 box.addClickListener(new ClickListener() {
125 @Override
126 public void onClick(Widget sender) {
127 CheckBox box = (CheckBox) sender;
128 Map<String, List<String>> selection = getSelection();
129
130 List<String> values = selection.get(colName);
131 if (values.indexOf(value) >= 0) {
132 values.remove(value);
133 }
134 else {
135 values.add(value);
136 }
137 }
138 });
139
140 return box;
141 }
142
143
144 public Map<String, List<String>> getSelection() {
145 return selected;
146 }
147 }
148 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org