Mercurial > dive4elements > river
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ManualWSPEditor.java Wed Jun 27 15:31:52 2012 +0000 @@ -0,0 +1,359 @@ +package de.intevation.flys.client.client.ui.chart; + +import java.util.List; + +import com.google.gwt.json.client.JSONArray; +import com.google.gwt.json.client.JSONNumber; +import com.google.gwt.json.client.JSONParser; +import com.google.gwt.json.client.JSONString; +import com.google.gwt.core.client.GWT; +import com.google.gwt.user.client.rpc.AsyncCallback; + +import com.smartgwt.client.util.SC; +import com.smartgwt.client.widgets.Window; +import com.smartgwt.client.widgets.layout.VLayout; +import com.smartgwt.client.widgets.layout.HLayout; +import com.smartgwt.client.widgets.IButton; +import com.smartgwt.client.widgets.Button; +import com.smartgwt.client.widgets.Label; +import com.google.gwt.i18n.client.NumberFormat; + +import com.smartgwt.client.widgets.grid.CellEditValueParser; +import com.smartgwt.client.widgets.grid.CellEditValueFormatter; +import com.smartgwt.client.widgets.events.ClickEvent; +import com.smartgwt.client.widgets.events.ClickHandler; + +import com.smartgwt.client.types.Alignment; + +import de.intevation.flys.client.shared.model.Artifact; + +import de.intevation.flys.client.client.Config; +import de.intevation.flys.client.client.FLYSConstants; +import de.intevation.flys.client.shared.model.Collection; +import de.intevation.flys.client.shared.model.CollectionItem; + +import de.intevation.flys.client.client.services.LoadArtifactService; +import de.intevation.flys.client.client.services.LoadArtifactServiceAsync; +import de.intevation.flys.client.client.services.FeedService; +import de.intevation.flys.client.client.services.FeedServiceAsync; + +import de.intevation.flys.client.shared.model.Data; +import de.intevation.flys.client.shared.model.DefaultArtifact; +import de.intevation.flys.client.shared.model.DefaultData; +import de.intevation.flys.client.shared.model.Recommendation; +import de.intevation.flys.client.shared.model.Settings; +import de.intevation.flys.client.shared.model.Property; +import de.intevation.flys.client.shared.model.PropertyGroup; +import de.intevation.flys.client.shared.model.StringProperty; + +import de.intevation.flys.client.client.event.RedrawRequestHandler; +import de.intevation.flys.client.client.event.RedrawRequestEvent; + +import com.smartgwt.client.widgets.form.DynamicForm; +import com.smartgwt.client.widgets.form.fields.FormItem; +import com.smartgwt.client.widgets.form.fields.TextItem; + + +/** + * UI to enter point data and save it to an PointArtifact. + */ +public class ManualWSPEditor +extends Window +implements ClickHandler +{ + /** The interface that provides i18n messages. */ + protected FLYSConstants MSG = GWT.create(FLYSConstants.class); + + /** Name of the main data item to be fed. */ + public static final String LINE_DATA = "manualpoints.lines"; + + /** When we chaged something, we need a RedrawRequest(Handler). */ + protected RedrawRequestHandler redrawRequestHandler; + + /** The collection */ + protected Collection collection; + + /** Service handle to clone and add artifacts to collection. */ + LoadArtifactServiceAsync loadArtifactService = GWT.create( + de.intevation.flys.client.client.services.LoadArtifactService.class); + + /** Service to feed the artifact with new point-data. */ + FeedServiceAsync feedService = GWT.create( + de.intevation.flys.client.client.services.FeedService.class); + + /** UUID of artifact to feed. */ + protected String uuid; + + /** Name of the outputmode, important when feeding data. */ + protected String outputModeName; + + /** Name of the data item for lines in this context. */ + protected String dataItemName; + + /** Input Field for y-coor of line. */ + protected TextItem valueInputPanel; + + /** Input Field for name of line. */ + protected TextItem nameInputPanel; + + /** Line data that is not added in this session. */ + protected JSONArray oldLines = null; + + + /** + * Setup editor dialog. + * @param collection The collection to use. + */ + public ManualWSPEditor(Collection collection, + RedrawRequestHandler handler, String outputModeName + ) { + this.collection = collection; + this.redrawRequestHandler = handler; + this.outputModeName = outputModeName; + this.dataItemName = outputModeName + "." + LINE_DATA; + init(); + } + + + /** Searches collection for first artifact to serve (manual) line data. */ + public String findManualPointsUUID() { + int size = collection.getItemLength(); + + for (int i = 0; i < size; i++) { + CollectionItem item = collection.getItem(i); + String dataValue = (String) item.getData().get(dataItemName); + if (dataValue != null) { + // Found it. + uuid = item.identifier(); + return uuid; + } + } + + return null; + } + + + /** + * Initialize the editor window and its components. + */ + protected void init() { + setTitle(MSG.addpoints()); + setCanDragReposition(true); + setCanDragResize(true); + + if(findManualPointsUUID() == null) { + addArtifactCreateUI(); + } + else { + createUI(); + } + } + + + /** Create and setup/add the ui. */ + public void createUI() { + Config config = Config.getInstance(); + + Button accept = new Button(MSG.label_ok()); + Button cancel = new Button(MSG.label_cancel()); + cancel.addClickHandler(this); + + accept.addClickHandler(new ClickHandler() { + public void onClick(ClickEvent e) { + okClicked(); + } + }); + + HLayout buttons = new HLayout(); + buttons.addMember(accept); + buttons.addMember(cancel); + buttons.setAlign(Alignment.CENTER); + buttons.setHeight(30); + + DynamicForm form = new DynamicForm(); + valueInputPanel = new TextItem(); + valueInputPanel.setTitle("wsp"); + valueInputPanel.setShowTitle(false); + nameInputPanel = new TextItem(); + nameInputPanel.setTitle("name"); + nameInputPanel.setShowTitle(false); + form.setFields(valueInputPanel, nameInputPanel); + + VLayout layout = new VLayout(); + layout.addMember(form); + + // Use X and Y as default fallback. + String yAxis = "Y"; + + // Get header text from collection settings. + Settings settings = this.collection.getSettings(outputModeName); + List<Property> axes = settings.getSettings("axes"); + if(axes != null) { + for (Property p: axes) { + PropertyGroup pg = (PropertyGroup)p; + StringProperty id = + (StringProperty)pg.getPropertyByName("id"); + if (yAxis.equals("Y")) { + StringProperty name = + (StringProperty)pg.getPropertyByName("label"); + yAxis = name.getValue(); + } + } + } + + + // Find the artifacts uuid. + // TODO this has been called already, why call it again? + findManualPointsUUID(); + CollectionItem item = collection.getItem(uuid); + + // Store the old line data. + if (item != null) { + String jsonData = item.getData().get(dataItemName); + oldLines = (JSONArray) JSONParser.parse(jsonData); + } + else { + GWT.log("No old lines found for " + uuid); + } + + addItem(layout); + + addItem(buttons); + setWidth(380); + setHeight(470); + centerInPage(); + } + + + /** + * Create JSON representation of the points present in the form. + * Add old data, too. + * @return a jsonarray with the old and the new lines. + */ + protected JSONArray jsonArrayFromForm() { + if (oldLines == null) { + oldLines = new JSONArray(); + } + + int idx = 0; + double val; + if (valueInputPanel.getValue() == null) + return oldLines; + try { + NumberFormat nf = NumberFormat.getDecimalFormat(); + double d = nf.parse(valueInputPanel.getValue().toString()); + val = d; + } + catch(NumberFormatException nfe) { + SC.warn("fehler... nfe... TODO"); + return oldLines; + } + + JSONArray data = new JSONArray(); + data.set(0, new JSONNumber(val)); + if (nameInputPanel.getValue() == null) { + data.set(1, new JSONString(valueInputPanel.getValue().toString())); + } + else { + data.set(1, new JSONString(nameInputPanel.getValue().toString())); + } + oldLines.set(oldLines.size(), data); + + return oldLines; + } + + + /** + * Called when OK Button was clicked. Then, if entered values are valid, + * fire a RedrawRequest and destroy. + */ + protected void okClicked() { + // TODO proper validation (might use DynamicForm) + if (valueInputPanel.getValue() == null) { + // not valid... + //TODO + return; + } + GWT.log(valueInputPanel.getValue().toString()); + if(isDialogValid()) { + // Feed JSON-encoded content of form. + JSONArray list = jsonArrayFromForm(); + + Data[] feedData = new Data[] { + DefaultData.createSimpleStringData(dataItemName, + list.toString()) + }; + + feedService.feed( + Config.getInstance().getLocale(), + new DefaultArtifact(uuid, "TODO:hash"), + feedData, + new AsyncCallback<Artifact>() { + public void onFailure(Throwable caught) { + GWT.log("Could not feed artifact with lines."); + SC.warn(MSG.getString(caught.getMessage())); + enable(); + } + public void onSuccess(Artifact fartifact) { + GWT.log("Successfully set lines "); + redrawRequestHandler.onRedrawRequest( + new RedrawRequestEvent()); + destroy(); + } + }); + } + else { + GWT.log("Dialog not valid"); + SC.warn(MSG.error_dialog_not_valid()); + } + } + + + /** Add a ManualPointArtifact to Collection. */ + public void addArtifactCreateUI() { + final Label standByLabel = new Label(MSG.standby()); + addItem(standByLabel); + + setWidth(380); + setHeight(470); + centerInPage(); + + Config config = Config.getInstance(); + String locale = config.getLocale(); + + loadArtifactService.load( + this.collection, + new Recommendation("manualpoints", ""), + "manualpoints", + locale, + new AsyncCallback<Artifact>() { + public void onFailure(Throwable caught) { + GWT.log("Creating manualpoint artifact failed!"); + } + public void onSuccess(Artifact artifact) { + GWT.log("Successfully created artifact."); + removeItem(standByLabel); + uuid = artifact.getUuid(); + createUI(); + } + }); + } + + + /** + * This method is called when the user aborts point editing. + * @param event The event. + */ + public void onClick(ClickEvent event) { + this.destroy(); + } + + + /** Return false if x or y attribute is missing. */ + protected boolean isDialogValid() { + boolean valid = true; + // TODO implement + return valid; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :