Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/ManualWSPEditor.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/chart/ManualWSPEditor.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client.ui.chart; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 import com.google.gwt.i18n.client.NumberFormat; | |
5 import com.google.gwt.json.client.JSONArray; | |
6 import com.google.gwt.json.client.JSONNumber; | |
7 import com.google.gwt.json.client.JSONParser; | |
8 import com.google.gwt.json.client.JSONString; | |
9 import com.google.gwt.user.client.rpc.AsyncCallback; | |
10 | |
11 import com.smartgwt.client.types.Alignment; | |
12 import com.smartgwt.client.util.SC; | |
13 import com.smartgwt.client.widgets.Button; | |
14 import com.smartgwt.client.widgets.Label; | |
15 import com.smartgwt.client.widgets.Window; | |
16 import com.smartgwt.client.widgets.events.ClickEvent; | |
17 import com.smartgwt.client.widgets.events.ClickHandler; | |
18 import com.smartgwt.client.widgets.form.DynamicForm; | |
19 import com.smartgwt.client.widgets.form.fields.TextItem; | |
20 import com.smartgwt.client.widgets.form.fields.events.BlurEvent; | |
21 import com.smartgwt.client.widgets.form.fields.events.BlurHandler; | |
22 import com.smartgwt.client.widgets.layout.HLayout; | |
23 import com.smartgwt.client.widgets.layout.VLayout; | |
24 | |
25 import org.dive4elements.river.client.client.Config; | |
26 import org.dive4elements.river.client.client.FLYSConstants; | |
27 import org.dive4elements.river.client.client.event.RedrawRequestEvent; | |
28 import org.dive4elements.river.client.client.event.RedrawRequestHandler; | |
29 import org.dive4elements.river.client.client.services.FeedServiceAsync; | |
30 import org.dive4elements.river.client.client.services.LoadArtifactServiceAsync; | |
31 import org.dive4elements.river.client.client.utils.DoubleValidator; | |
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.CollectionItem; | |
35 import org.dive4elements.river.client.shared.model.Data; | |
36 import org.dive4elements.river.client.shared.model.DefaultArtifact; | |
37 import org.dive4elements.river.client.shared.model.DefaultData; | |
38 import org.dive4elements.river.client.shared.model.Property; | |
39 import org.dive4elements.river.client.shared.model.PropertyGroup; | |
40 import org.dive4elements.river.client.shared.model.Recommendation; | |
41 import org.dive4elements.river.client.shared.model.Settings; | |
42 import org.dive4elements.river.client.shared.model.StringProperty; | |
43 | |
44 import java.util.List; | |
45 import java.util.Map; | |
46 | |
47 /** | |
48 * UI to enter point data and save it to an PointArtifact. | |
49 */ | |
50 public class ManualWSPEditor | |
51 extends Window | |
52 implements ClickHandler | |
53 { | |
54 /** The interface that provides i18n messages. */ | |
55 protected FLYSConstants MSG = GWT.create(FLYSConstants.class); | |
56 | |
57 /** Name of the main data item to be fed. */ | |
58 public static final String LINE_DATA = "manualpoints.lines"; | |
59 | |
60 /** When we change something, we need a RedrawRequest(Handler). */ | |
61 protected RedrawRequestHandler redrawRequestHandler; | |
62 | |
63 /** The collection */ | |
64 protected Collection collection; | |
65 | |
66 /** Service handle to clone and add artifacts to collection. */ | |
67 LoadArtifactServiceAsync loadArtifactService = GWT.create( | |
68 org.dive4elements.river.client.client.services.LoadArtifactService.class); | |
69 | |
70 /** Service to feed the artifact with new point-data. */ | |
71 FeedServiceAsync feedService = GWT.create( | |
72 org.dive4elements.river.client.client.services.FeedService.class); | |
73 | |
74 /** UUID of artifact to feed. */ | |
75 protected String uuid; | |
76 | |
77 /** Name of the outputmode, important when feeding data. */ | |
78 protected String outputModeName; | |
79 | |
80 /** Name of the data item for lines in this context. */ | |
81 protected String dataItemName; | |
82 | |
83 /** Input Field for y-coor of line. */ | |
84 protected TextItem valueInputPanel; | |
85 | |
86 /** Input Field for name of line. */ | |
87 protected TextItem nameInputPanel; | |
88 | |
89 /** Line data that is not added in this session. */ | |
90 protected JSONArray oldLines = null; | |
91 | |
92 | |
93 /** | |
94 * Setup editor dialog. | |
95 * @param collection The collection to use. | |
96 */ | |
97 public ManualWSPEditor(Collection collection, | |
98 RedrawRequestHandler handler, String outputModeName | |
99 ) { | |
100 this.collection = collection; | |
101 this.redrawRequestHandler = handler; | |
102 this.outputModeName = outputModeName; | |
103 this.dataItemName = outputModeName + "." + LINE_DATA; | |
104 init(); | |
105 } | |
106 | |
107 | |
108 /** Searches collection for first artifact to serve (manual) line data. */ | |
109 public String findManualPointsUUID() { | |
110 int size = collection.getItemLength(); | |
111 | |
112 for (int i = 0; i < size; i++) { | |
113 CollectionItem item = collection.getItem(i); | |
114 String dataValue = item.getData().get(dataItemName); | |
115 if (dataValue != null) { | |
116 // Found it. | |
117 uuid = item.identifier(); | |
118 return uuid; | |
119 } | |
120 } | |
121 | |
122 return null; | |
123 } | |
124 | |
125 | |
126 /** | |
127 * Initialize the editor window and its components. | |
128 */ | |
129 protected void init() { | |
130 setTitle(MSG.addWSP()); | |
131 setCanDragReposition(true); | |
132 setCanDragResize(true); | |
133 | |
134 if(findManualPointsUUID() == null) { | |
135 addArtifactCreateUI(); | |
136 } | |
137 else { | |
138 createUI(); | |
139 } | |
140 } | |
141 | |
142 | |
143 /** Create and setup/add the ui. */ | |
144 public void createUI() { | |
145 Button accept = new Button(MSG.label_ok()); | |
146 Button cancel = new Button(MSG.label_cancel()); | |
147 cancel.addClickHandler(this); | |
148 | |
149 accept.addClickHandler(new ClickHandler() { | |
150 @Override | |
151 public void onClick(ClickEvent e) { | |
152 okClicked(); | |
153 } | |
154 }); | |
155 | |
156 HLayout buttons = new HLayout(); | |
157 buttons.addMember(accept); | |
158 buttons.addMember(cancel); | |
159 buttons.setAlign(Alignment.CENTER); | |
160 buttons.setHeight(30); | |
161 | |
162 // Use X and Y as default fallback. | |
163 String yAxis = "Y"; | |
164 | |
165 // Get header text from collection settings. | |
166 Settings settings = this.collection.getSettings(outputModeName); | |
167 List<Property> axes = settings.getSettings("axes"); | |
168 if(axes != null) { | |
169 for (Property p: axes) { | |
170 PropertyGroup pg = (PropertyGroup)p; | |
171 StringProperty id = | |
172 (StringProperty)pg.getPropertyByName("id"); | |
173 if (id.getValue().equals("W")) { | |
174 StringProperty name = | |
175 (StringProperty)pg.getPropertyByName("label"); | |
176 yAxis = name.getValue(); | |
177 } | |
178 } | |
179 } | |
180 | |
181 DynamicForm form = new DynamicForm(); | |
182 valueInputPanel = new TextItem(); | |
183 valueInputPanel.setTitle(yAxis); | |
184 valueInputPanel.setShowTitle(true); | |
185 valueInputPanel.addBlurHandler(new BlurHandler() { | |
186 @Override | |
187 public void onBlur(BlurEvent e) { | |
188 DoubleValidator validator = new DoubleValidator(); | |
189 Map errors = e.getForm().getErrors(); | |
190 validator.validate(e.getItem(), errors); | |
191 e.getForm().setErrors(errors, true); | |
192 } | |
193 }); | |
194 nameInputPanel = new TextItem(); | |
195 nameInputPanel.setTitle(MSG.pointname()); | |
196 nameInputPanel.setShowTitle(true); | |
197 form.setFields(valueInputPanel, nameInputPanel); | |
198 | |
199 VLayout layout = new VLayout(); | |
200 layout.addMember(form); | |
201 | |
202 // Find the artifacts uuid. | |
203 // TODO this has been called already, why call it again? | |
204 findManualPointsUUID(); | |
205 CollectionItem item = collection.getItem(uuid); | |
206 | |
207 // Store the old line data. | |
208 if (item != null) { | |
209 String jsonData = item.getData().get(dataItemName); | |
210 oldLines = (JSONArray) JSONParser.parse(jsonData); | |
211 } | |
212 else { | |
213 GWT.log("No old lines found for " + uuid); | |
214 } | |
215 | |
216 addItem(layout); | |
217 | |
218 addItem(buttons); | |
219 setWidth(360); | |
220 setHeight(120); | |
221 centerInPage(); | |
222 } | |
223 | |
224 | |
225 /** | |
226 * Create JSON representation of the points present in the form. | |
227 * Add old data, too. | |
228 * @return a jsonarray with the old and the new lines. | |
229 */ | |
230 protected JSONArray jsonArrayFromForm() { | |
231 if (oldLines == null) { | |
232 oldLines = new JSONArray(); | |
233 } | |
234 | |
235 double val; | |
236 if (valueInputPanel.getValue() == null) | |
237 return oldLines; | |
238 try { | |
239 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
240 double d = nf.parse(valueInputPanel.getValue().toString()); | |
241 val = d; | |
242 } | |
243 catch(NumberFormatException nfe) { | |
244 GWT.log("fehler... nfe... TODO"); | |
245 return oldLines; | |
246 } | |
247 | |
248 JSONArray data = new JSONArray(); | |
249 data.set(0, new JSONNumber(val)); | |
250 if (nameInputPanel.getValue() == null) { | |
251 data.set(1, new JSONString(valueInputPanel.getValue().toString())); | |
252 } | |
253 else { | |
254 data.set(1, new JSONString(nameInputPanel.getValue().toString())); | |
255 } | |
256 oldLines.set(oldLines.size(), data); | |
257 | |
258 return oldLines; | |
259 } | |
260 | |
261 | |
262 /** | |
263 * Called when OK Button was clicked. Then, if entered values are valid, | |
264 * fire a RedrawRequest and destroy. | |
265 */ | |
266 protected void okClicked() { | |
267 if (valueInputPanel.getValue() == null) { | |
268 return; | |
269 } | |
270 GWT.log(valueInputPanel.getValue().toString()); | |
271 if(isDialogValid()) { | |
272 // Feed JSON-encoded content of form. | |
273 JSONArray list = jsonArrayFromForm(); | |
274 | |
275 Data[] feedData = new Data[] { | |
276 DefaultData.createSimpleStringData(dataItemName, | |
277 list.toString()) | |
278 }; | |
279 | |
280 feedService.feed( | |
281 Config.getInstance().getLocale(), | |
282 new DefaultArtifact(uuid, "TODO:hash"), | |
283 feedData, | |
284 new AsyncCallback<Artifact>() { | |
285 @Override | |
286 public void onFailure(Throwable caught) { | |
287 GWT.log("Could not feed artifact with lines."); | |
288 SC.warn(MSG.getString(caught.getMessage())); | |
289 enable(); | |
290 } | |
291 @Override | |
292 public void onSuccess(Artifact fartifact) { | |
293 GWT.log("Successfully set lines "); | |
294 redrawRequestHandler.onRedrawRequest( | |
295 new RedrawRequestEvent()); | |
296 destroy(); | |
297 } | |
298 }); | |
299 } | |
300 else { | |
301 GWT.log("Dialog not valid"); | |
302 SC.warn(MSG.error_dialog_not_valid()); | |
303 } | |
304 } | |
305 | |
306 | |
307 /** Add a ManualPointArtifact to Collection. */ | |
308 public void addArtifactCreateUI() { | |
309 final Label standByLabel = new Label(MSG.standby()); | |
310 addItem(standByLabel); | |
311 | |
312 setWidth(360); | |
313 setHeight(120); | |
314 centerInPage(); | |
315 | |
316 Config config = Config.getInstance(); | |
317 String locale = config.getLocale(); | |
318 | |
319 loadArtifactService.load( | |
320 this.collection, | |
321 new Recommendation("manualpoints", ""), | |
322 "manualpoints", | |
323 locale, | |
324 new AsyncCallback<Artifact>() { | |
325 @Override | |
326 public void onFailure(Throwable caught) { | |
327 GWT.log("Creating manualpoint artifact failed!"); | |
328 } | |
329 @Override | |
330 public void onSuccess(Artifact artifact) { | |
331 GWT.log("Successfully created artifact."); | |
332 removeItem(standByLabel); | |
333 uuid = artifact.getUuid(); | |
334 createUI(); | |
335 } | |
336 }); | |
337 } | |
338 | |
339 | |
340 /** | |
341 * This method is called when the user aborts point editing. | |
342 * @param event The event. | |
343 */ | |
344 @Override | |
345 public void onClick(ClickEvent event) { | |
346 this.destroy(); | |
347 } | |
348 | |
349 | |
350 /** Return false if x or y attribute is missing. */ | |
351 protected boolean isDialogValid() { | |
352 return (DoubleValidator.isDouble(valueInputPanel.getValue())); | |
353 } | |
354 } | |
355 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |