comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ManualPointsEditor.java @ 1563:41abc345d2f1

Added dummy handling for new boolean property of each point. flys-client/trunk@3810 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 27 Jan 2012 16:26:25 +0000
parents af29d43cf4da
children 3606f2caf93e
comparison
equal deleted inserted replaced
1562:af29d43cf4da 1563:41abc345d2f1
1 package de.intevation.flys.client.client.ui.chart; 1 package de.intevation.flys.client.client.ui.chart;
2 2
3 import com.google.gwt.json.client.JSONArray; 3 import com.google.gwt.json.client.JSONArray;
4 import com.google.gwt.json.client.JSONBoolean;
4 import com.google.gwt.json.client.JSONNumber; 5 import com.google.gwt.json.client.JSONNumber;
5 import com.google.gwt.json.client.JSONParser; 6 import com.google.gwt.json.client.JSONParser;
6 import com.google.gwt.json.client.JSONString; 7 import com.google.gwt.json.client.JSONString;
7 import com.google.gwt.core.client.GWT; 8 import com.google.gwt.core.client.GWT;
8 import com.google.gwt.user.client.rpc.AsyncCallback; 9 import com.google.gwt.user.client.rpc.AsyncCallback;
149 cancel.addClickHandler(this); 150 cancel.addClickHandler(this);
150 151
151 // TODO refactor. 152 // TODO refactor.
152 accept.addClickHandler(new ClickHandler() { 153 accept.addClickHandler(new ClickHandler() {
153 public void onClick(ClickEvent e) { 154 public void onClick(ClickEvent e) {
154 if(isDialogValid()) { 155 okClicked();
155 // Feed JSON-encoded content of listgrid.
156 String jsonString = "";
157 int idx = 0;
158 JSONArray list = new JSONArray();
159
160 for(ListGridRecord record : listGrid.getRecords()) {
161 if (record instanceof PointRecord) {
162 JSONArray data = new JSONArray();
163
164 PointRecord point = (PointRecord) record;
165 data.set(0, new JSONNumber(point.getX()));
166 data.set(1, new JSONNumber(point.getY()));
167 data.set(2, new JSONString(point.getName()));
168
169 list.set(idx, data);
170 idx++;
171 }
172 else {
173 JSONArray data = new JSONArray();
174
175 String nameString = record.getAttributeAsString("name");
176 // Apply default name if none set.
177 if (nameString == null || nameString.equals("")) {
178 String xString = record.getAttributeAsString(
179 PointRecord.ATTRIBUTE_X);
180 String yString = record.getAttributeAsString(
181 PointRecord.ATTRIBUTE_Y);
182 nameString = xString + "/" + yString;
183 }
184
185 data.set(0, new JSONNumber(record.
186 getAttributeAsDouble(PointRecord.ATTRIBUTE_X)));
187 data.set(1, new JSONNumber(record.
188 getAttributeAsDouble(PointRecord.ATTRIBUTE_Y)));
189 data.set(2, new JSONString(nameString));
190
191 list.set(idx, data);
192 idx++;
193 }
194 }
195
196 // Feed list.toString to respective artifact.
197 Data[] feedData = new Data[] {
198 DefaultData.createSimpleStringData(POINT_DATA,
199 list.toString())
200 };
201 feedService.feed(
202 Config.getInstance().getLocale(),
203 new DefaultArtifact(uuid, "TODO:hash"),
204 feedData,
205 new AsyncCallback<Artifact>() {
206 public void onFailure(Throwable caught) {
207 GWT.log("Could not feed artifact with points.");
208 SC.warn(MSG.getString(caught.getMessage()));
209 enable();
210 }
211 public void onSuccess(Artifact fartifact) {
212 GWT.log("Successfully set points ");
213 redrawRequestHandler.onRedrawRequest(
214 new RedrawRequestEvent());
215 destroy();
216 }
217 });
218 }
219 else {
220 GWT.log("Dialog not valid");
221 SC.warn(MSG.error_dialog_not_valid());
222 }
223 } 156 }
224 }); 157 });
225 158
226 HLayout buttons = new HLayout(); 159 HLayout buttons = new HLayout();
227 buttons.addMember(accept); 160 buttons.addMember(accept);
235 listGrid.setHeight(200); 168 listGrid.setHeight(200);
236 listGrid.setCanSort(false); 169 listGrid.setCanSort(false);
237 listGrid.setCanEdit(true); 170 listGrid.setCanEdit(true);
238 listGrid.setShowHeaderContextMenu(false); 171 listGrid.setShowHeaderContextMenu(false);
239 172
240 // TODO X and Y Header depend on the current chart and axis (e.g. W[nn+m]) 173 CellFormatter doubleFormat = new CellFormatter() {
241 // collection.getSettings(outputName).getSettings -> Propertie -> "axis" -> "label"
242 ListGridField xField = new ListGridField(PointRecord.ATTRIBUTE_X, "X");
243 xField.setType(ListGridFieldType.FLOAT);
244 xField.setCellFormatter(new CellFormatter() {
245 public String format(Object value, ListGridRecord record, int rowNum, int colNum) { 174 public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
246 if(value != null) { 175 if(value != null) {
247 NumberFormat nf = NumberFormat.getFormat("#.##"); 176 NumberFormat nf = NumberFormat.getFormat("#.##");
248 try { 177 try {
249 return nf.format(((Number) value).doubleValue()); 178 return nf.format(((Number) value).doubleValue());
251 return value.toString(); 180 return value.toString();
252 } 181 }
253 } else { 182 } else {
254 return null; 183 return null;
255 } 184 }
256 }}); 185 }};
186
187 // TODO X and Y Header depend on the current chart and axis (e.g. W[nn+m])
188 // collection.getSettings(outputName).getSettings -> Propertie -> "axis" -> "label"
189
190 ListGridField xField = new ListGridField(PointRecord.ATTRIBUTE_X, "X");
191 xField.setType(ListGridFieldType.FLOAT);
192 xField.setCellFormatter(doubleFormat);
193
257 ListGridField yField = new ListGridField(PointRecord.ATTRIBUTE_Y, "Y"); 194 ListGridField yField = new ListGridField(PointRecord.ATTRIBUTE_Y, "Y");
258 yField.setType(ListGridFieldType.FLOAT); 195 yField.setType(ListGridFieldType.FLOAT);
196 yField.setCellFormatter(doubleFormat);
197
259 ListGridField nameField = new ListGridField(PointRecord.ATTRIBUTE_NAME, 198 ListGridField nameField = new ListGridField(PointRecord.ATTRIBUTE_NAME,
260 MSG.pointname()); 199 MSG.pointname());
261 final ListGridField removeField = 200 final ListGridField removeField =
262 new ListGridField("_removeRecord", MSG.removepoint()){{ 201 new ListGridField("_removeRecord", MSG.removepoint()){{
263 setType(ListGridFieldType.ICON); 202 setType(ListGridFieldType.ICON);
268 setCanGroupBy(false); 207 setCanGroupBy(false);
269 setCanFreeze(false); 208 setCanFreeze(false);
270 setWidth(25); 209 setWidth(25);
271 }}; 210 }};
272 211
273 listGrid.setFields(new ListGridField[] {xField, yField, 212 ListGridField activeField = new ListGridField(
213 PointRecord.ATTRIBUTE_ACTIVE, MSG.selection());
214 activeField.setType(ListGridFieldType.BOOLEAN);
215
216 listGrid.setFields(new ListGridField[] {activeField, xField, yField,
274 nameField, removeField}); 217 nameField, removeField});
275 218
276 listGrid.addRecordClickHandler(new RecordClickHandler() { 219 listGrid.addRecordClickHandler(new RecordClickHandler() {
277 public void onRecordClick(final RecordClickEvent event) { 220 public void onRecordClick(final RecordClickEvent event) {
278 // Just handle remove-clicks 221 // Just handle remove-clicks
319 setHeight(470); 262 setHeight(470);
320 centerInPage(); 263 centerInPage();
321 } 264 }
322 265
323 266
267 /** Create JSON representation of the points present in the list grid. */
268 protected JSONArray jsonArrayFromListGrid() {
269 JSONArray list = new JSONArray();
270 int idx = 0;
271
272 for(ListGridRecord record : listGrid.getRecords()) {
273 if (record instanceof PointRecord) {
274 JSONArray data = new JSONArray();
275
276 PointRecord point = (PointRecord) record;
277 data.set(0, new JSONNumber(point.getX()));
278 data.set(1, new JSONNumber(point.getY()));
279 data.set(2, new JSONString(point.getName()));
280 data.set(3, JSONBoolean.getInstance(point.isActive()));
281
282 list.set(idx, data);
283 idx++;
284 }
285 else {
286 JSONArray data = new JSONArray();
287
288 String nameString = record.getAttributeAsString("name");
289 // Apply default name if none set.
290 if (nameString == null || nameString.equals("")) {
291 String xString = record.getAttributeAsString(
292 PointRecord.ATTRIBUTE_X);
293 String yString = record.getAttributeAsString(
294 PointRecord.ATTRIBUTE_Y);
295 nameString = xString + "/" + yString;
296 }
297
298 data.set(0, new JSONNumber(record.
299 getAttributeAsDouble(PointRecord.ATTRIBUTE_X)));
300 data.set(1, new JSONNumber(record.
301 getAttributeAsDouble(PointRecord.ATTRIBUTE_Y)));
302 data.set(2, new JSONString(nameString));
303 data.set(3, JSONBoolean.getInstance(record.getAttributeAsBoolean(
304 PointRecord.ATTRIBUTE_ACTIVE)));
305
306 list.set(idx, data);
307 idx++;
308 }
309 }
310 return list;
311 }
312
313
314 /**
315 * Called when OK Button was clicked. Then, if entered values are valid,
316 * fire a RedrawRequest and destroy.
317 */
318 protected void okClicked() {
319 if(isDialogValid()) {
320 // Feed JSON-encoded content of listgrid.
321 JSONArray list = jsonArrayFromListGrid();
322
323 Data[] feedData = new Data[] {
324 DefaultData.createSimpleStringData(POINT_DATA,
325 list.toString())
326 };
327
328 feedService.feed(
329 Config.getInstance().getLocale(),
330 new DefaultArtifact(uuid, "TODO:hash"),
331 feedData,
332 new AsyncCallback<Artifact>() {
333 public void onFailure(Throwable caught) {
334 GWT.log("Could not feed artifact with points.");
335 SC.warn(MSG.getString(caught.getMessage()));
336 enable();
337 }
338 public void onSuccess(Artifact fartifact) {
339 GWT.log("Successfully set points ");
340 redrawRequestHandler.onRedrawRequest(
341 new RedrawRequestEvent());
342 destroy();
343 }
344 });
345 }
346 else {
347 // TODO i18n?
348 GWT.log("Dialog not valid");
349 SC.warn(MSG.error_dialog_not_valid());
350 }
351 }
352
353
324 /** From a JSON-encoded point, create a PointRecord. */ 354 /** From a JSON-encoded point, create a PointRecord. */
325 public PointRecord pointRecordFromJSON(JSONArray jsonArray) { 355 public PointRecord pointRecordFromJSON(JSONArray jsonArray) {
326 JSONNumber x = (JSONNumber) jsonArray.get(0); 356 JSONNumber x = (JSONNumber) jsonArray.get(0);
327 JSONNumber y = (JSONNumber) jsonArray.get(1); 357 JSONNumber y = (JSONNumber) jsonArray.get(1);
328 JSONString s = (JSONString) jsonArray.get(2); 358 JSONString s = (JSONString) jsonArray.get(2);
329 return new PointRecord(x.doubleValue(), y.doubleValue(), s.stringValue()); 359 JSONBoolean b = (JSONBoolean) jsonArray.get(3);
360
361 return new PointRecord(b.booleanValue(), x.doubleValue(),
362 y.doubleValue(), s.stringValue());
330 } 363 }
331 364
332 365
333 /** Add a ManualPointArtifact to Collection. */ 366 /** Add a ManualPointArtifact to Collection. */
334 public void addArtifactCreateUI() { 367 public void addArtifactCreateUI() {
377 protected static final String ATTRIBUTE_NAME = "name"; 410 protected static final String ATTRIBUTE_NAME = "name";
378 protected static final String ATTRIBUTE_ACTIVE = "active"; 411 protected static final String ATTRIBUTE_ACTIVE = "active";
379 412
380 private PointRecord() {;} 413 private PointRecord() {;}
381 414
382 public PointRecord(double x, double y, String name) { 415 public PointRecord(boolean b, double x, double y, String name) {
416 setActive(b);
383 setName(name); 417 setName(name);
384 setX(x); 418 setX(x);
385 setY(y); 419 setY(y);
386 } 420 }
387 421

http://dive4elements.wald.intevation.org