9074
|
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by |
|
3 * Björnsen Beratende Ingenieure GmbH |
|
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt |
|
5 * |
|
6 * This file is Free Software under the GNU AGPL (>=v3) |
|
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
8 * documentation coming with Dive4Elements River for details. |
|
9 */ |
|
10 package org.dive4elements.river.client.client.ui; |
|
11 |
9075
|
12 import java.util.ArrayList; |
|
13 import java.util.List; |
|
14 import java.util.Map; |
|
15 import java.util.Map.Entry; |
|
16 import java.util.SortedMap; |
|
17 import java.util.TreeMap; |
|
18 |
|
19 import org.dive4elements.river.client.client.FLYSConstants; |
|
20 import org.dive4elements.river.client.shared.model.Data; |
|
21 import org.dive4elements.river.client.shared.model.DataItem; |
|
22 import org.dive4elements.river.client.shared.model.DataList; |
|
23 import org.dive4elements.river.client.shared.model.MultiAttributeData; |
|
24 import org.dive4elements.river.client.shared.model.MultiDataItem; |
|
25 |
|
26 import com.google.gwt.core.client.GWT; |
|
27 import com.smartgwt.client.data.Record; |
|
28 import com.smartgwt.client.types.Alignment; |
|
29 import com.smartgwt.client.types.ListGridFieldType; |
|
30 import com.smartgwt.client.widgets.Canvas; |
9074
|
31 import com.smartgwt.client.widgets.grid.ListGrid; |
9075
|
32 import com.smartgwt.client.widgets.grid.ListGridField; |
|
33 import com.smartgwt.client.widgets.grid.events.RecordClickEvent; |
|
34 import com.smartgwt.client.widgets.grid.events.RecordClickHandler; |
9074
|
35 |
|
36 /** |
|
37 * @author Domenico Nardi Tironi |
|
38 * |
|
39 */ |
9076
|
40 public class FromToTableHelperPanel { |
9074
|
41 |
9075
|
42 public interface IColumnClickHandler { |
|
43 void columnClicked(String value); |
|
44 } |
|
45 |
9074
|
46 private enum PinType { |
|
47 none, from, to |
|
48 } |
|
49 |
9075
|
50 private final ListGrid table; |
9076
|
51 |
9075
|
52 private final DataList dataList; |
9076
|
53 |
|
54 private final FLYSConstants flys; |
|
55 |
9075
|
56 private final List<String> keyColEntries = new ArrayList<String>(); |
9074
|
57 |
9076
|
58 public FromToTableHelperPanel(final DataList dataList, final String definitionStr, final FLYSConstants flys, final IColumnClickHandler fromHandler, |
|
59 final IColumnClickHandler toHandler) { |
9075
|
60 this.flys = flys; |
|
61 this.dataList = dataList; |
|
62 this.table = new ListGrid(); |
|
63 this.table.setShowHeaderContextMenu(false); |
|
64 this.table.setWidth100(); |
|
65 this.table.setShowRecordComponents(true); |
|
66 this.table.setShowRecordComponentsByCell(true); |
|
67 this.table.setHeight100(); |
|
68 this.table.setEmptyMessage(flys.empty_table()); |
|
69 this.table.setCanReorderFields(false); |
|
70 |
|
71 final MultiAttributeData mData = findDefinition(definitionStr); |
|
72 final Map<String, Map<String, String>> meta = mData.getMeta(); |
|
73 |
|
74 final Map<String, String> tableInfo = meta.get("meta_tableinfo"); |
|
75 final String keyColumnName = tableInfo.get("keycol"); |
|
76 |
|
77 final SortedMap<Integer, ListGridField> fields = new TreeMap<Integer, ListGridField>(); |
|
78 |
|
79 for (final Entry<String, Map<String, String>> entry : meta.entrySet()) { |
|
80 |
|
81 final String colName = entry.getKey(); |
|
82 if (colName.startsWith("meta_")) |
|
83 continue; |
|
84 |
|
85 final Map<String, String> values = entry.getValue(); |
|
86 |
|
87 final int order = Integer.parseInt(values.get("order")); |
|
88 |
9076
|
89 final ListGridField field = createField(colName, keyColumnName, values, fromHandler, toHandler); |
9075
|
90 |
|
91 fields.put(order, field); |
|
92 } |
|
93 |
9169
|
94 if (mData == null || mData.opts == null) { |
|
95 this.table.setEmptyMessage(flys.error_feed_no_data()); |
|
96 return; |
|
97 } |
9222
|
98 |
9075
|
99 for (final DataItem dataItem : mData.opts) { |
|
100 final MultiDataItem item = (MultiDataItem) dataItem; |
|
101 |
|
102 final Map<String, String> valueMap = item.getValue(); |
9076
|
103 |
9075
|
104 final Record newRecord = new Record(); |
|
105 for (final Entry<String, String> entry : valueMap.entrySet()) { |
|
106 final String key = entry.getKey(); |
|
107 final String value = entry.getValue(); |
|
108 newRecord.setAttribute(key, value); |
|
109 |
|
110 if (key.equals(keyColumnName)) |
|
111 this.keyColEntries.add(value); |
|
112 } |
|
113 |
|
114 this.table.addData(newRecord); |
|
115 } |
9222
|
116 this.table.setEmptyMessage(""); |
9075
|
117 this.table.setFields(fields.values().toArray(new ListGridField[fields.size()])); |
9074
|
118 } |
|
119 |
9075
|
120 public Canvas getTable() { |
|
121 return this.table; |
|
122 } |
|
123 |
|
124 private MultiAttributeData findDefinition(final String paranemterName) { |
|
125 |
|
126 for (final Data data : this.dataList.getAll()) { |
|
127 |
|
128 final String label = data.getLabel(); |
|
129 if (label.equals(paranemterName) && data instanceof MultiAttributeData) |
|
130 return (MultiAttributeData) data; |
|
131 } |
|
132 |
|
133 return null; |
|
134 } |
|
135 |
9076
|
136 private ListGridField createField(final String colName, final String keyColumnName, final Map<String, String> values, final IColumnClickHandler fromHandler, |
|
137 final IColumnClickHandler toHandler) { |
9075
|
138 |
|
139 final String translation = getMeta(values, "translation", colName); |
|
140 |
|
141 final ListGridField field = new ListGridField(colName, translation); |
|
142 |
9076
|
143 final String width = getMeta(values, "colwidth", "99"); |
9075
|
144 field.setWidth(width); |
|
145 |
|
146 final ListGridFieldType type = getMeta(values, "type", ListGridFieldType.TEXT, ListGridFieldType.class); |
|
147 field.setType(type); |
|
148 |
|
149 final Alignment alignment = getMeta(values, "alignment", Alignment.LEFT, Alignment.class); |
|
150 field.setAlign(alignment); |
|
151 |
|
152 final PinType pinType = getMeta(values, "pin", PinType.none, PinType.class); |
|
153 switch (pinType) { |
|
154 case from: { |
9076
|
155 makePin(keyColumnName, fromHandler, field, this.flys.markerGreen()); |
9075
|
156 } |
|
157 break; |
|
158 |
9076
|
159 case to: { |
|
160 makePin(keyColumnName, toHandler, field, this.flys.markerRed()); |
|
161 } |
9075
|
162 break; |
|
163 case none: |
|
164 default: |
|
165 break; |
|
166 } |
|
167 |
|
168 return field; |
|
169 } |
|
170 |
9076
|
171 private void makePin(final String keyColumnName, final IColumnClickHandler handler, final ListGridField field, final String markerStr) { |
|
172 final String baseUrl = GWT.getHostPageBaseURL(); |
|
173 field.setCellIcon(baseUrl + markerStr); |
|
174 field.addRecordClickHandler(new RecordClickHandler() { |
|
175 @Override |
|
176 public void onRecordClick(final RecordClickEvent e) { |
|
177 final Record r = e.getRecord(); |
|
178 handler.columnClicked(r.getAttribute(keyColumnName)); |
|
179 } |
|
180 }); |
|
181 } |
|
182 |
9075
|
183 private String getMeta(final Map<String, String> values, final String key, final String defaultValue) { |
|
184 |
|
185 final String value = values.get(key); |
|
186 if (value == null || value.isEmpty()) |
|
187 return defaultValue; |
|
188 |
|
189 return value; |
|
190 } |
|
191 |
|
192 private <TYPE extends Enum<TYPE>> TYPE getMeta(final Map<String, String> values, final String key, final TYPE defaultValue, final Class<TYPE> enumType) { |
|
193 |
|
194 final String value = values.get(key); |
|
195 if (value == null || value.isEmpty()) |
|
196 return defaultValue; |
|
197 |
|
198 try { |
|
199 return Enum.valueOf(enumType, value); |
|
200 } |
|
201 catch (final Exception e) { |
|
202 e.printStackTrace(); |
|
203 return defaultValue; |
|
204 } |
|
205 } |
|
206 |
|
207 public List<String> getKeycolEntries() { |
|
208 return this.keyColEntries; |
|
209 } |
9076
|
210 } |