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 */ |
|
40 public class TableHelper { |
|
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; |
|
51 private final DataList dataList; |
|
52 final FLYSConstants flys; |
|
53 private final List<String> keyColEntries = new ArrayList<String>(); |
9074
|
54 |
9075
|
55 public TableHelper(final DataList dataList, final String definitionStr, final FLYSConstants flys, final IColumnClickHandler fromHandler) { |
|
56 this.flys = flys; |
|
57 this.dataList = dataList; |
|
58 this.table = new ListGrid(); |
|
59 this.table.setShowHeaderContextMenu(false); |
|
60 this.table.setWidth100(); |
|
61 this.table.setShowRecordComponents(true); |
|
62 this.table.setShowRecordComponentsByCell(true); |
|
63 this.table.setHeight100(); |
|
64 this.table.setEmptyMessage(flys.empty_table()); |
|
65 this.table.setCanReorderFields(false); |
|
66 |
|
67 final MultiAttributeData mData = findDefinition(definitionStr); |
|
68 final Map<String, Map<String, String>> meta = mData.getMeta(); |
|
69 |
|
70 final Map<String, String> tableInfo = meta.get("meta_tableinfo"); |
|
71 final String keyColumnName = tableInfo.get("keycol"); |
|
72 |
|
73 final SortedMap<Integer, ListGridField> fields = new TreeMap<Integer, ListGridField>(); |
|
74 |
|
75 for (final Entry<String, Map<String, String>> entry : meta.entrySet()) { |
|
76 |
|
77 final String colName = entry.getKey(); |
|
78 if (colName.startsWith("meta_")) |
|
79 continue; |
|
80 |
|
81 final Map<String, String> values = entry.getValue(); |
|
82 |
|
83 final int order = Integer.parseInt(values.get("order")); |
|
84 |
|
85 final ListGridField field = createField(colName, keyColumnName, values, fromHandler); |
|
86 |
|
87 fields.put(order, field); |
|
88 } |
|
89 |
|
90 for (final DataItem dataItem : mData.opts) { |
|
91 final MultiDataItem item = (MultiDataItem) dataItem; |
|
92 |
|
93 final Map<String, String> valueMap = item.getValue(); |
|
94 // final String columnName = mData.getLabel(); |
|
95 final Record newRecord = new Record(); |
|
96 for (final Entry<String, String> entry : valueMap.entrySet()) { |
|
97 final String key = entry.getKey(); |
|
98 final String value = entry.getValue(); |
|
99 newRecord.setAttribute(key, value); |
|
100 |
|
101 if (key.equals(keyColumnName)) |
|
102 this.keyColEntries.add(value); |
|
103 } |
|
104 |
|
105 this.table.addData(newRecord); |
|
106 } |
|
107 |
|
108 this.table.setFields(fields.values().toArray(new ListGridField[fields.size()])); |
9074
|
109 |
|
110 } |
|
111 |
9075
|
112 public Canvas getTable() { |
|
113 return this.table; |
|
114 } |
|
115 |
|
116 private MultiAttributeData findDefinition(final String paranemterName) { |
|
117 |
|
118 for (final Data data : this.dataList.getAll()) { |
|
119 |
|
120 final String label = data.getLabel(); |
|
121 if (label.equals(paranemterName) && data instanceof MultiAttributeData) |
|
122 return (MultiAttributeData) data; |
|
123 } |
|
124 |
|
125 return null; |
|
126 } |
|
127 |
|
128 private ListGridField createField(final String colName, final String keyColumnName, final Map<String, String> values, |
|
129 final IColumnClickHandler fromHandler) { |
|
130 |
|
131 final String translation = getMeta(values, "translation", colName); |
|
132 |
|
133 final ListGridField field = new ListGridField(colName, translation); |
|
134 |
|
135 final String width = getMeta(values, "width", "99"); |
|
136 field.setWidth(width); |
|
137 |
|
138 final ListGridFieldType type = getMeta(values, "type", ListGridFieldType.TEXT, ListGridFieldType.class); |
|
139 field.setType(type); |
|
140 |
|
141 final Alignment alignment = getMeta(values, "alignment", Alignment.LEFT, Alignment.class); |
|
142 field.setAlign(alignment); |
|
143 |
|
144 final PinType pinType = getMeta(values, "pin", PinType.none, PinType.class); |
|
145 switch (pinType) { |
|
146 case from: { |
|
147 final String baseUrl = GWT.getHostPageBaseURL(); |
|
148 field.setCellIcon(baseUrl + this.flys.markerGreen()); |
|
149 field.addRecordClickHandler(new RecordClickHandler() { |
|
150 @Override |
|
151 public void onRecordClick(final RecordClickEvent e) { |
|
152 final Record r = e.getRecord(); |
|
153 fromHandler.columnClicked(r.getAttribute(keyColumnName)); |
|
154 } |
|
155 }); |
|
156 } |
|
157 break; |
|
158 |
|
159 case to: |
|
160 break; |
|
161 |
|
162 case none: |
|
163 default: |
|
164 break; |
|
165 } |
|
166 |
|
167 return field; |
|
168 } |
|
169 |
|
170 private String getMeta(final Map<String, String> values, final String key, final String defaultValue) { |
|
171 |
|
172 final String value = values.get(key); |
|
173 if (value == null || value.isEmpty()) |
|
174 return defaultValue; |
|
175 |
|
176 return value; |
|
177 } |
|
178 |
|
179 private <TYPE extends Enum<TYPE>> TYPE getMeta(final Map<String, String> values, final String key, final TYPE defaultValue, final Class<TYPE> enumType) { |
|
180 |
|
181 final String value = values.get(key); |
|
182 if (value == null || value.isEmpty()) |
|
183 return defaultValue; |
|
184 |
|
185 try { |
|
186 return Enum.valueOf(enumType, value); |
|
187 } |
|
188 catch (final Exception e) { |
|
189 e.printStackTrace(); |
|
190 return defaultValue; |
|
191 } |
|
192 } |
|
193 |
|
194 public List<String> getKeycolEntries() { |
|
195 |
|
196 return this.keyColEntries; |
|
197 } |
|
198 |
9074
|
199 } |