Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/BedHeightsDatacagePanel.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/BedHeightsDatacagePanel.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client.ui.minfo; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 | |
5 import com.google.gwt.user.client.rpc.AsyncCallback; | |
6 | |
7 import com.smartgwt.client.data.Record; | |
8 | |
9 import com.smartgwt.client.widgets.Canvas; | |
10 | |
11 import com.smartgwt.client.widgets.events.ClickEvent; | |
12 | |
13 import com.smartgwt.client.widgets.grid.ListGridRecord; | |
14 | |
15 import com.smartgwt.client.widgets.layout.HLayout; | |
16 import com.smartgwt.client.widgets.layout.VLayout; | |
17 | |
18 import org.dive4elements.river.client.client.Config; | |
19 import org.dive4elements.river.client.client.FLYSConstants; | |
20 | |
21 import org.dive4elements.river.client.client.event.StepForwardEvent; | |
22 | |
23 import org.dive4elements.river.client.client.services.LoadArtifactService; | |
24 import org.dive4elements.river.client.client.services.LoadArtifactServiceAsync; | |
25 import org.dive4elements.river.client.client.services.RemoveArtifactService; | |
26 import org.dive4elements.river.client.client.services.RemoveArtifactServiceAsync; | |
27 | |
28 import org.dive4elements.river.client.client.ui.DatacagePairWidget; | |
29 import org.dive4elements.river.client.client.ui.DatacageTwinPanel; | |
30 import org.dive4elements.river.client.client.ui.RecommendationPairRecord; | |
31 | |
32 import org.dive4elements.river.client.shared.model.Artifact; | |
33 import org.dive4elements.river.client.shared.model.Collection; | |
34 import org.dive4elements.river.client.shared.model.Data; | |
35 import org.dive4elements.river.client.shared.model.DataItem; | |
36 import org.dive4elements.river.client.shared.model.DataList; | |
37 | |
38 import org.dive4elements.river.client.shared.model.Recommendation.Facet; | |
39 import org.dive4elements.river.client.shared.model.Recommendation.Filter; | |
40 | |
41 import org.dive4elements.river.client.shared.model.Recommendation; | |
42 import org.dive4elements.river.client.shared.model.User; | |
43 | |
44 import java.util.ArrayList; | |
45 import java.util.List; | |
46 import java.util.Map; | |
47 import java.util.Set; | |
48 | |
49 // TODO Probably better to branch off AbstractUIProvider. | |
50 // TODO Merge with other datacage-widget impls. | |
51 /** | |
52 * Panel containing a Grid and a "next" button. The Grid is fed by a | |
53 * DatacagePairWidget which is put in the input-helper area. | |
54 */ | |
55 public class BedHeightsDatacagePanel | |
56 extends DatacageTwinPanel { | |
57 | |
58 protected static FLYSConstants MSG = GWT.create(FLYSConstants.class); | |
59 | |
60 /** | |
61 * List to track previously selected but now removed pairs. (Needed to | |
62 * be able to identify artifacts that can be removed from the collection. | |
63 */ | |
64 protected List<RecommendationPairRecord> removedPairs = | |
65 new ArrayList<RecommendationPairRecord>(); | |
66 | |
67 /** Service handle to clone and add artifacts to collection. */ | |
68 LoadArtifactServiceAsync loadArtifactService = GWT.create( | |
69 org.dive4elements.river.client.client.services.LoadArtifactService.class); | |
70 | |
71 /** Service to remove artifacts from collection. */ | |
72 RemoveArtifactServiceAsync removeArtifactService = GWT.create( | |
73 org.dive4elements.river.client.client.services.RemoveArtifactService.class); | |
74 | |
75 | |
76 public BedHeightsDatacagePanel(User user) { | |
77 super(user); | |
78 } | |
79 | |
80 | |
81 /** | |
82 * Create a recommendation from a string representation of it. | |
83 * @TODO describe format of input string | |
84 * @param from string in format as shown above. | |
85 * @return recommendation from input string. | |
86 */ | |
87 public Recommendation createRecommendationFromString(String from) { | |
88 // TODO Construct "real" filter. | |
89 String[] parts = unbracket(from).split(";"); | |
90 Recommendation.Filter filter = new Recommendation.Filter(); | |
91 Recommendation.Facet facet = new Recommendation.Facet( | |
92 parts[1], | |
93 parts[2]); | |
94 | |
95 List<Recommendation.Facet> facets = new ArrayList<Recommendation.Facet> | |
96 (); | |
97 facets.add(facet); | |
98 filter.add("longitudinal_section", facets); | |
99 Recommendation r = new Recommendation("bedheight", parts[0], | |
100 this.artifact.getUuid(), filter); | |
101 r.setDisplayName(parts[3]); | |
102 return r; | |
103 } | |
104 | |
105 | |
106 /** | |
107 * Creates the graphical representation and interaction widgets for the data. | |
108 * @param dataList the data. | |
109 * @return graphical representation and interaction widgets for data. | |
110 */ | |
111 @Override | |
112 public Canvas create(DataList dataList) { | |
113 GWT.log("createData()"); | |
114 | |
115 Data data = dataList.get(0); | |
116 DataItem[] items = data.getItems(); | |
117 | |
118 String value = items[0].getStringValue(); | |
119 | |
120 String filter = "minfo-heights"; | |
121 if (value.equals("epoch")) { | |
122 filter = "minfo-heights-epoch"; | |
123 } | |
124 Canvas widget = createWidget(); | |
125 Canvas submit = getNextButton(); | |
126 | |
127 VLayout layout = new VLayout(); | |
128 HLayout helperLayout = new HLayout(); | |
129 helperLayout.addMember(new DatacagePairWidget(this.artifact, | |
130 user, filter, differencesList)); | |
131 | |
132 layout.addMember(widget); | |
133 layout.addMember(submit); | |
134 layout.setMembersMargin(10); | |
135 this.helperContainer.addMember(helperLayout); | |
136 | |
137 this.dataName = "diffids"; | |
138 | |
139 return layout; | |
140 } | |
141 | |
142 | |
143 /** | |
144 * Add record to list of removed records. | |
145 */ | |
146 public void trackRemoved(Record r) { | |
147 RecommendationPairRecord pr = (RecommendationPairRecord) r; | |
148 this.removedPairs.add(pr); | |
149 } | |
150 | |
151 | |
152 /** | |
153 * Validates data, does nothing if invalid, otherwise clones new selected | |
154 * waterlevels and add them to collection, forward the artifact. | |
155 */ | |
156 @Override | |
157 public void onClick(ClickEvent e) { | |
158 GWT.log("DatacageTwinPanel.onClick"); | |
159 | |
160 List<String> errors = validate(); | |
161 if (errors != null && !errors.isEmpty()) { | |
162 showErrors(errors); | |
163 return; | |
164 } | |
165 | |
166 Config config = Config.getInstance(); | |
167 String locale = config.getLocale(); | |
168 | |
169 ListGridRecord[] records = differencesList.getRecords(); | |
170 | |
171 List<Recommendation> ar = new ArrayList<Recommendation>(); | |
172 List<Recommendation> all = new ArrayList<Recommendation>(); | |
173 | |
174 for (ListGridRecord record : records) { | |
175 RecommendationPairRecord r = | |
176 (RecommendationPairRecord) record; | |
177 // Do not add "old" recommendations. | |
178 if (!r.isAlreadyLoaded()) { | |
179 // Check whether one of those is a dike or similar. | |
180 // TODO differentiate and merge: new clones, new, old. | |
181 Recommendation firstR = r.getFirst(); | |
182 if(firstR.getIDs() != null) { | |
183 GWT.log("First IDs: " + firstR.getIDs() + " factory: " | |
184 + firstR.getFactory()); | |
185 } | |
186 firstR.setFactory("bedheight"); | |
187 Recommendation secondR = r.getSecond(); | |
188 if(secondR.getIDs() != null) { | |
189 GWT.log("Second IDs: " + secondR.getIDs() + " factory: " | |
190 + secondR.getFactory()); | |
191 } | |
192 secondR.setFactory("bedheight"); | |
193 | |
194 ar.add(firstR); | |
195 ar.add(secondR); | |
196 } | |
197 else { | |
198 all.add(r.getFirst()); | |
199 all.add(r.getSecond()); | |
200 } | |
201 } | |
202 | |
203 final Recommendation[] toClone = ar.toArray(new Recommendation[ar.size()]); | |
204 final Recommendation[] toUse = all.toArray(new Recommendation[all.size()]); | |
205 | |
206 // Find out whether "old" artifacts have to be removed. | |
207 List<String> artifactIdsToRemove = new ArrayList<String>(); | |
208 for (RecommendationPairRecord rp: this.removedPairs) { | |
209 Recommendation first = rp.getFirst(); | |
210 Recommendation second = rp.getSecond(); | |
211 | |
212 for (Recommendation recommendation: toUse) { | |
213 if (first != null && first.getIDs().equals(recommendation.getIDs())) { | |
214 first = null; | |
215 } | |
216 if (second != null && second.getIDs().equals(recommendation.getIDs())) { | |
217 second = null; | |
218 } | |
219 | |
220 if (first == null && second == null) { | |
221 break; | |
222 } | |
223 } | |
224 if (first != null) { | |
225 artifactIdsToRemove.add(first.getIDs()); | |
226 } | |
227 if (second != null) { | |
228 artifactIdsToRemove.add(second.getIDs()); | |
229 } | |
230 } | |
231 | |
232 // Remove old artifacts, if any. Do this asychronously without much | |
233 // feedback. | |
234 for(final String uuid: artifactIdsToRemove) { | |
235 removeArtifactService.remove(this.collection, | |
236 uuid, | |
237 locale, | |
238 new AsyncCallback<Collection>() { | |
239 public void onFailure(Throwable caught) { | |
240 GWT.log("RemoveArtifact (" + uuid + ") failed."); | |
241 } | |
242 public void onSuccess(Collection collection) { | |
243 GWT.log("RemoveArtifact succeeded"); | |
244 } | |
245 }); | |
246 } | |
247 | |
248 // Clone new ones (and spawn statics), go forward. | |
249 loadArtifactService.loadMany( | |
250 this.collection, | |
251 toClone, | |
252 //"staticwkms" and "waterlevel" | |
253 null, | |
254 locale, | |
255 new AsyncCallback<Artifact[]>() { | |
256 public void onFailure(Throwable caught) { | |
257 GWT.log("Failure of cloning with factories!"); | |
258 } | |
259 public void onSuccess(Artifact[] artifacts) { | |
260 GWT.log("Successfully cloned " + toClone.length + | |
261 " with factories."); | |
262 | |
263 fireStepForwardEvent(new StepForwardEvent( | |
264 getData(toClone, artifacts, toUse))); | |
265 } | |
266 }); | |
267 } | |
268 | |
269 | |
270 /** | |
271 * Creates part of the String that encodes minuend or subtrahend. | |
272 * @param artifact Artifacts UUID. | |
273 * @param recommendation Recommendation to wrap in string. | |
274 */ | |
275 protected String createDataString( | |
276 String artifact, | |
277 Recommendation recommendation) | |
278 { | |
279 Filter filter = recommendation.getFilter(); | |
280 Facet f = null; | |
281 | |
282 if(filter != null) { | |
283 Map<String, List<Facet>> outs = filter.getOuts(); | |
284 Set<Map.Entry<String, List<Facet>>> entries = outs.entrySet(); | |
285 | |
286 for (Map.Entry<String, List<Facet>> entry: entries) { | |
287 List<Facet> fs = entry.getValue(); | |
288 | |
289 f = fs.get(0); | |
290 if (f != null) { | |
291 break; | |
292 } | |
293 } | |
294 | |
295 return "[" + artifact + ";" | |
296 + f.getName() | |
297 + ";" | |
298 + f.getIndex() | |
299 + ";" | |
300 + recommendation.getDisplayName() + "]"; | |
301 } | |
302 else { | |
303 return "[" | |
304 + artifact | |
305 + ";bedheight;0;" | |
306 + recommendation.getDisplayName() + "]"; | |
307 } | |
308 } | |
309 } | |
310 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |