Mercurial > dive4elements > river
comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/ParameterMatrix.java @ 5834:f507086aa94b
Repaired internal references.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:31:32 +0200 |
parents | flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterMatrix.java@d5821c6f0ab0 |
children | 821a02bbfb4e |
comparison
equal
deleted
inserted
replaced
5833:a2bdc0f524e8 | 5834:f507086aa94b |
---|---|
1 package de.intevation.flys.client.client.ui; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 import com.google.gwt.event.dom.client.ClickEvent; | |
5 import com.google.gwt.event.dom.client.ClickHandler; | |
6 import com.google.gwt.user.client.ui.CheckBox; | |
7 import com.google.gwt.user.client.ui.Grid; | |
8 import com.google.gwt.user.client.ui.Widget; | |
9 | |
10 import com.smartgwt.client.types.ListGridFieldType; | |
11 import com.smartgwt.client.widgets.Canvas; | |
12 import com.smartgwt.client.widgets.Label; | |
13 import com.smartgwt.client.widgets.grid.ListGrid; | |
14 import com.smartgwt.client.widgets.grid.ListGridField; | |
15 import com.smartgwt.client.widgets.grid.ListGridRecord; | |
16 | |
17 import de.intevation.flys.client.client.FLYSConstants; | |
18 import de.intevation.flys.client.shared.model.DataItem; | |
19 import de.intevation.flys.client.shared.model.IntegerOptionsData; | |
20 import de.intevation.flys.client.shared.model.StringOptionsData; | |
21 | |
22 import java.io.Serializable; | |
23 import java.util.ArrayList; | |
24 import java.util.HashMap; | |
25 import java.util.List; | |
26 import java.util.Map; | |
27 | |
28 | |
29 /** | |
30 * Some parameters take the form of on/off options that can also be seen | |
31 * as a matrix. | |
32 * | |
33 * This class helps to survive the underlying objects and create a visual | |
34 * representation of this matrix. Later can happen in two ways to overcome | |
35 * shortcomings of GWT/SmartGWT combination. | |
36 */ | |
37 public class ParameterMatrix { | |
38 | |
39 protected ListGrid listGrid = null; | |
40 | |
41 public static class Column implements Serializable { | |
42 | |
43 private static final long serialVersionUID = -3493426383086860118L; | |
44 | |
45 protected String name; | |
46 protected Map<String, String> values; | |
47 | |
48 private Column() { | |
49 this.values = new HashMap<String, String>(); | |
50 } | |
51 | |
52 public Column(String name) { | |
53 this(); | |
54 this.name = name; | |
55 } | |
56 | |
57 public void addValue(String label, String value) { | |
58 values.put(label, value); | |
59 } | |
60 | |
61 public String getValue(String label) { | |
62 return values.get(label); | |
63 } | |
64 } // end of class Column | |
65 | |
66 /** The message class that provides i18n strings.*/ | |
67 protected FLYSConstants MESSAGE = GWT.create(FLYSConstants.class); | |
68 | |
69 public static final int CELL_HEIGHT = 25; | |
70 | |
71 private Map<String, Column> columns; | |
72 private List<String> columnNames; | |
73 private List<String> valueNames; | |
74 | |
75 /** Maps column names to list of rows' first fields. */ | |
76 private Map<String, List<String>> selected; | |
77 | |
78 public ParameterMatrix() { | |
79 super(); | |
80 this.columns = new HashMap<String, Column>(); | |
81 this.columnNames = new ArrayList<String>(); | |
82 this.valueNames = new ArrayList<String>(); | |
83 this.selected = new HashMap<String, List<String>>(); | |
84 } | |
85 | |
86 | |
87 public void addColumn(IntegerOptionsData group) { | |
88 String groupTitle = group.getLabel(); | |
89 | |
90 Column col = new Column(groupTitle); | |
91 DataItem[] items = group.getItems(); | |
92 | |
93 if (items == null) { | |
94 GWT.log("No items found in StringOptionsData '" + groupTitle + "'"); | |
95 return; | |
96 } | |
97 | |
98 for (DataItem item: items) { | |
99 String title = item.getLabel(); | |
100 | |
101 if (valueNames.indexOf(title) < 0) { | |
102 valueNames.add(title); | |
103 } | |
104 | |
105 col.addValue(item.getLabel(), item.getStringValue()); | |
106 } | |
107 | |
108 columnNames.add(groupTitle); | |
109 columns.put(groupTitle, col); | |
110 } | |
111 | |
112 | |
113 public void addColumn(StringOptionsData options) { | |
114 String groupTitle = options.getLabel(); | |
115 | |
116 Column col = new Column(groupTitle); | |
117 DataItem[] items = options.getItems(); | |
118 | |
119 if (items == null) { | |
120 GWT.log("No items found in StringOptionsData '" + groupTitle + "'"); | |
121 return; | |
122 } | |
123 | |
124 for (DataItem item: items) { | |
125 String title = item.getLabel(); | |
126 | |
127 if (valueNames.indexOf(title) < 0) { | |
128 valueNames.add(title); | |
129 } | |
130 | |
131 col.addValue(item.getLabel(), item.getStringValue()); | |
132 } | |
133 | |
134 columnNames.add(groupTitle); | |
135 columns.put(groupTitle, col); | |
136 } | |
137 | |
138 | |
139 public Widget createParameterGrid() { | |
140 listGrid = new ListGrid(); | |
141 listGrid.setShowAllRecords(true); | |
142 listGrid.setWrapCells(true); | |
143 listGrid.setShowHeaderContextMenu(false); | |
144 listGrid.setCanReorderFields(false); | |
145 listGrid.setCanSort(false); | |
146 //listGrid.setAutoFitData(Autofit.VERTICAL); | |
147 listGrid.setFixedRecordHeights(false); | |
148 // TODO: Then also need "autofit" (when wrapping) | |
149 | |
150 ListGridField itemNameField = new ListGridField("itemname", " "); | |
151 ArrayList<ListGridField> fields = new ArrayList<ListGridField>(); | |
152 fields.add(itemNameField); | |
153 | |
154 for (int i = 0, n = columnNames.size(); i < n; i++) { | |
155 ListGridField field = new ListGridField(columnNames.get(i), MESSAGE.getString(columnNames.get(i))); | |
156 field.setType(ListGridFieldType.BOOLEAN); | |
157 field.setCanEdit(true); | |
158 fields.add(field); | |
159 selected.put(columnNames.get(i), new ArrayList<String>()); | |
160 } | |
161 | |
162 ListGridField[] fieldsArray = fields.toArray(new ListGridField[fields.size()]); | |
163 listGrid.setFields(fieldsArray); | |
164 | |
165 int nVals = valueNames.size(); | |
166 | |
167 ArrayList<ListGridRecord> records = new ArrayList<ListGridRecord>(); | |
168 for (int j = 0; j < nVals; j++) { | |
169 String valueName = valueNames.get(j); | |
170 ListGridRecord record = new ListGridRecord(); | |
171 record.setAttribute("itemname", valueName); | |
172 for (int i = 0, n = columnNames.size(); i < n; i++) { | |
173 String columnName = columnNames.get(i); | |
174 Column col = columns.get(columnName); | |
175 String value = col.getValue(valueName); | |
176 record.setAttribute(columnName, false); | |
177 record.setAttribute(columnName+"-value", value); | |
178 } | |
179 records.add(record); | |
180 } | |
181 | |
182 listGrid.setData(records.toArray(new ListGridRecord[records.size()])); | |
183 | |
184 return listGrid; | |
185 | |
186 } | |
187 | |
188 | |
189 /** | |
190 * Returns a widget with matrix of checkboxes and labels. | |
191 * @param asListGrid if true, use a ListGrid (for inclusion in SmartGWT | |
192 * containers, avoiding scrollbar-issues. | |
193 */ | |
194 public Widget create(boolean asListGrid) { | |
195 if (asListGrid) { | |
196 return createParameterGrid(); | |
197 } | |
198 Grid grid = new Grid(valueNames.size() + 1, columnNames.size() + 1); | |
199 | |
200 for (int i = 0, n = columnNames.size(); i < n; i++) { | |
201 String columnName = columnNames.get(i); | |
202 Column col = columns.get(columnName); | |
203 | |
204 selected.put(columnName, new ArrayList<String>()); | |
205 | |
206 grid.setWidget(0, i+1, createLabel(MESSAGE.getString(columnName))); | |
207 | |
208 for (int j = 0, o = valueNames.size(); j < o; j++) { | |
209 String valueName = valueNames.get(j); | |
210 String value = col.getValue(valueName); | |
211 | |
212 if (i == 0) { | |
213 grid.setWidget(j+1, 0, createLabel(valueName)); | |
214 } | |
215 | |
216 if (value != null && value.length() > 0) { | |
217 grid.setWidget(j+1, i+1, createCheckBox(columnName, value)); | |
218 } | |
219 } | |
220 } | |
221 | |
222 return grid; | |
223 } | |
224 | |
225 | |
226 /** Creates label with given text. */ | |
227 protected Label createLabel(String text) { | |
228 Label label = new Label(text); | |
229 label.setHeight(CELL_HEIGHT); | |
230 | |
231 return label; | |
232 } | |
233 | |
234 | |
235 /** Create Checkbox for column/value. */ | |
236 protected Canvas createCheckBox(final String colName, final String value) { | |
237 CheckBox box = new CheckBox(); | |
238 box.addClickHandler(new ClickHandler() { | |
239 | |
240 @Override | |
241 public void onClick(ClickEvent event) { | |
242 Map<String, List<String>> selection = getSelection(); | |
243 | |
244 List<String> values = selection.get(colName); | |
245 if (values.indexOf(value) >= 0) { | |
246 values.remove(value); | |
247 } | |
248 else { | |
249 values.add(value); | |
250 } | |
251 } | |
252 }); | |
253 | |
254 Canvas c = new Canvas(); | |
255 c.addChild(box); | |
256 return c; | |
257 } | |
258 | |
259 | |
260 public Map<String, List<String>> getSelection() { | |
261 if (listGrid == null) { | |
262 return selected; | |
263 } | |
264 | |
265 ListGridRecord[] records = listGrid.getRecords(); | |
266 Map<String, List<String>> result = new HashMap<String, List<String>>(); | |
267 for (ListGridRecord record : records) { | |
268 for (int i = 0, n = columnNames.size(); i < n; i++) { | |
269 String columnName = columnNames.get(i); | |
270 if (Boolean.valueOf(record.getAttribute(columnName)) == true) { | |
271 if (result.containsKey(columnName)) { | |
272 result.get(columnName).add(record.getAttribute(columnName + "-value")); | |
273 } | |
274 else { | |
275 List<String> items = new ArrayList<String>(); | |
276 items.add(record.getAttribute(columnName + "-value")); | |
277 result.put(columnName, items); | |
278 } | |
279 } | |
280 } | |
281 } | |
282 return result; | |
283 } | |
284 } | |
285 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |