Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/TableHelper.java @ 9075:89740fe82196
Extract Table from Panel
author | gernotbelger |
---|---|
date | Fri, 18 May 2018 17:42:34 +0200 |
parents | 766890addcb2 |
children |
comparison
equal
deleted
inserted
replaced
9074:766890addcb2 | 9075:89740fe82196 |
---|---|
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the | 7 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
8 * documentation coming with Dive4Elements River for details. | 8 * documentation coming with Dive4Elements River for details. |
9 */ | 9 */ |
10 package org.dive4elements.river.client.client.ui; | 10 package org.dive4elements.river.client.client.ui; |
11 | 11 |
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; | |
12 import com.smartgwt.client.widgets.grid.ListGrid; | 31 import com.smartgwt.client.widgets.grid.ListGrid; |
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; | |
13 | 35 |
14 /** | 36 /** |
15 * @author Domenico Nardi Tironi | 37 * @author Domenico Nardi Tironi |
16 * | 38 * |
17 */ | 39 */ |
18 public class TableHelper { | 40 public class TableHelper { |
19 | 41 |
42 public interface IColumnClickHandler { | |
43 void columnClicked(String value); | |
44 } | |
45 | |
20 private enum PinType { | 46 private enum PinType { |
21 none, from, to | 47 none, from, to |
22 } | 48 } |
23 | 49 |
24 private ListGrid table; | 50 private final ListGrid table; |
51 private final DataList dataList; | |
52 final FLYSConstants flys; | |
53 private final List<String> keyColEntries = new ArrayList<String>(); | |
25 | 54 |
26 public TableHelper() { | 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()])); | |
27 | 109 |
28 } | 110 } |
29 | 111 |
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 | |
30 } | 199 } |