teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
teichmann@5861: * Software engineering by Intevation GmbH
teichmann@5861: *
teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3)
teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the
teichmann@5993: * documentation coming with Dive4Elements River for details.
teichmann@5861: */
teichmann@5861:
teichmann@5835: package org.dive4elements.river.client.client.ui.fixation;
raimund@2505:
raimund@2505: import java.util.List;
raimund@2505: import java.util.ArrayList;
raimund@2505:
raimund@2505: import com.google.gwt.core.client.GWT;
raimund@2505:
raimund@2505: import com.smartgwt.client.widgets.Canvas;
raimund@2505: import com.smartgwt.client.widgets.Label;
raimund@2505:
raimund@2505: import com.smartgwt.client.widgets.layout.HLayout;
raimund@2505: import com.smartgwt.client.widgets.layout.VLayout;
raimund@2505:
raimund@2505: import com.smartgwt.client.widgets.form.fields.FormItem;
raimund@2505: import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
raimund@2505: import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
raimund@2505:
teichmann@5835: import org.dive4elements.river.client.client.FLYSConstants;
teichmann@5835: import org.dive4elements.river.client.client.ui.DoubleRangePanel;
raimund@2505:
teichmann@5835: import org.dive4elements.river.client.shared.model.Data;
teichmann@5835: import org.dive4elements.river.client.shared.model.DataItem;
teichmann@5835: import org.dive4elements.river.client.shared.model.DataList;
teichmann@5835: import org.dive4elements.river.client.shared.model.DefaultData;
teichmann@5835: import org.dive4elements.river.client.shared.model.DefaultDataItem;
raimund@2505: /**
raimund@2505: * This UIProvider creates a panel for location or distance input.
raimund@2505: *
raimund@2505: * @author Raimund Renkert
raimund@2505: */
raimund@2505: public class FixLocationPanel
raimund@2505: extends FixationPanel
raimund@2505: implements BlurHandler
raimund@2505: {
raimund@2505: /** The message class that provides i18n strings. */
raimund@2505: protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
raimund@2505:
raimund@2505: /** The constant name of the input field to enter locations.*/
raimund@2505: public static final String FIELD_VALUE_LOCATION = "location";
raimund@2505:
raimund@2505: /** The constant name of the input field to enter distance.*/
raimund@2505: public static final String FIELD_VALUE_DISTANCE = "distance";
raimund@2505:
raimund@2505: DoubleRangePanel inputPanel;
raimund@2505:
raimund@2505: double from;
raimund@2505: double to;
raimund@2505: double step;
raimund@2505:
raimund@2505: public FixLocationPanel() {
raimund@2505: htmlOverview = "";
raimund@2505: }
raimund@2505:
raimund@2505: public Canvas createWidget(DataList data) {
raimund@2505: instances.put(this.artifact.getUuid(), this);
raimund@2505:
raimund@2505: VLayout layout = new VLayout();
raimund@2505:
raimund@2505: Canvas title = new Label(MESSAGES.distance());
raimund@2505: title.setHeight("25px");
raimund@2505:
raimund@2505: inputPanel = new DoubleRangePanel(
raimund@2505: MESSAGES.unitFrom(),
raimund@2505: MESSAGES.unitTo(),
raimund@2505: MESSAGES.unitWidth(),
raimund@2537: 0d,
raimund@2537: 0d,
raimund@2537: 0d,
raimund@2505: 240,
raimund@2505: this);
raimund@2505:
raimund@2505: layout.addMember(title);
raimund@2505: layout.addMember(inputPanel);
raimund@2505: return layout;
raimund@2505: }
raimund@2505:
raimund@2505: @Override
raimund@2505: public Canvas createOld(DataList dataList) {
raimund@2505: List items = dataList.getAll();
raimund@2505:
rrenkert@4866: Data f = getData(items, "ld_from");
rrenkert@4866: Data t = getData(items, "ld_to");
rrenkert@4866: Data s = getData(items, "ld_step");
raimund@2505: DataItem[] fItems = f.getItems();
raimund@2505: DataItem[] tItems = t.getItems();
raimund@2505: DataItem[] sItems = s.getItems();
raimund@2505:
raimund@2505: StringBuilder sb = new StringBuilder();
raimund@2505: sb.append(fItems[0].getLabel());
raimund@2505: sb.append(" " + MESSAGES.unitFrom() + " ");
raimund@2505: sb.append(tItems[0].getLabel());
raimund@2505: sb.append(" " + MESSAGES.unitTo() + " ");
raimund@2505: sb.append(sItems[0].getLabel());
raimund@2505: sb.append(" " + MESSAGES.unitWidth());
raimund@2505:
raimund@2505: Label old = new Label(sb.toString());
raimund@2505: old.setWidth(130);
raimund@2505:
raimund@2505: HLayout layout = new HLayout();
raimund@2505: layout.setWidth("400px");
raimund@2505:
raimund@2505: Label label = new Label(dataList.getLabel());
raimund@2505: label.setWidth("200px");
raimund@2505:
raimund@2505: Canvas back = getBackButton(dataList.getState());
raimund@2505:
raimund@2505: layout.addMember(label);
raimund@2505: layout.addMember(old);
raimund@2505: layout.addMember(back);
raimund@2505:
raimund@2505: return layout;
raimund@2505: }
raimund@2505:
raimund@2505:
raimund@2505: /**
raimund@2505: * This method returns the selected data.
raimund@2505: *
raimund@2505: * @return the selected/inserted data.
raimund@2505: */
raimund@2505: public Data[] getData() {
raimund@2505: List data = new ArrayList();
raimund@2505:
raimund@2505: boolean valid = saveRangeValues(inputPanel);
raimund@2505: if (valid) {
raimund@2505: String f = Double.valueOf(this.from).toString();
raimund@2505: String t = Double.valueOf(this.to).toString();
raimund@2505: String s = Double.valueOf(this.step).toString();
rrenkert@4866: DataItem fi = new DefaultDataItem("ld_from", "ld_from", f);
rrenkert@4866: DataItem ti = new DefaultDataItem("ld_to", "ld_to", t);
rrenkert@4866: DataItem si = new DefaultDataItem("ld_step", "ld_step", s);
tom@8856: data.add(new DefaultData(
tom@8856: "ld_from", null, null, new DataItem[]{ fi }));
tom@8856: data.add(new DefaultData(
tom@8856: "ld_to", null, null, new DataItem[]{ ti }));
tom@8856: data.add(new DefaultData(
tom@8856: "ld_step", null, null, new DataItem[]{ si }));
raimund@2505: }
raimund@2505: // what else?
sascha@3379: return data.toArray(new Data[data.size()]);
raimund@2505: }
raimund@2505:
raimund@2505:
raimund@2505: protected boolean saveRangeValues(DoubleRangePanel p) {
raimund@2505: FormItem[] items = p.getFields();
raimund@2505: boolean valid = p.validateForm();
raimund@2505:
raimund@2505: if(valid) {
raimund@2505: this.from = p.getFrom();
raimund@2505: this.to = p.getTo();
raimund@2505: this.step = p.getStep();
raimund@2505: }
raimund@2505: return valid;
raimund@2505: }
raimund@2505:
raimund@2505:
raimund@2505: @Override
raimund@2505: public void setValues(String cid, boolean checked) {
raimund@2505: // No user interaction, do nothing.
raimund@2505: }
raimund@2505:
raimund@2505:
raimund@2505: @Override
raimund@2505: public boolean renderCheckboxes() {
raimund@2505: // No selection, return false.
raimund@2505: return false;
raimund@2505: }
raimund@2505:
raimund@2505:
raimund@2537: public void success() {
raimund@2537: inputPanel.setValues(fixInfo.getFrom(), fixInfo.getTo(), 100d);
raimund@2537: }
raimund@2517:
raimund@2505: /**
raimund@2505: * This method is used to validate the inserted data in the form fields.
raimund@2505: *
raimund@2505: * @param event The BlurEvent that gives information about the FormItem that
raimund@2505: * has been modified and its value.
raimund@2505: */
raimund@2505: public void onBlur(BlurEvent event) {
raimund@2505: FormItem item = event.getItem();
raimund@2505: String field = item.getFieldName();
raimund@2505:
raimund@2505: if (field == null) {
raimund@2505: return;
raimund@2505: }
raimund@2505: DoubleRangePanel p = (DoubleRangePanel) event.getForm();
raimund@2505: }
raimund@2505:
raimund@2505:
raimund@2505: public void dumpGWT(String cid) {
raimund@2505: GWT.log("Setting values for cId: " + cid);
raimund@2505: GWT.log("River: " + fixInfo.getRiver());
raimund@2505: GWT.log("Date: " + fixInfo.getEventByCId(cid).getDate());
raimund@2537: GWT.log("Name: " + fixInfo.getEventByCId(cid).getDescription());
raimund@2505: }
raimund@2505: }