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