comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ManualWSPEditor.java @ 2925:6461b8dbe093

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

http://dive4elements.wald.intevation.org