felix@1591: package de.intevation.flys.client.client.ui;
felix@1591:
felix@1591: import java.util.ArrayList;
felix@1591: import java.util.List;
felix@1591:
felix@1591: import com.google.gwt.core.client.GWT;
felix@1591: import com.google.gwt.i18n.client.NumberFormat;
felix@1591:
felix@1591: import com.smartgwt.client.util.SC;
felix@1591: import com.smartgwt.client.widgets.Canvas;
felix@1591: import com.smartgwt.client.widgets.Label;
felix@1591: import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
felix@1591: import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
felix@1591: import com.smartgwt.client.widgets.form.fields.FormItem;
felix@1591:
felix@1591: import com.smartgwt.client.widgets.layout.HLayout;
felix@1591: import com.smartgwt.client.widgets.layout.VLayout;
felix@1591:
felix@1591: import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
felix@1591: import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
felix@1591:
felix@1591: import com.smartgwt.client.data.Record;
felix@1591:
felix@1591: import de.intevation.flys.client.shared.model.ArtifactDescription;
felix@1591: import de.intevation.flys.client.shared.model.Data;
felix@1591: import de.intevation.flys.client.shared.model.DataItem;
felix@1591: import de.intevation.flys.client.shared.model.DataList;
felix@1591: import de.intevation.flys.client.shared.model.DefaultData;
felix@1591: import de.intevation.flys.client.shared.model.DefaultDataItem;
felix@1591: import de.intevation.flys.client.shared.model.DistanceInfoObject;
felix@1591: import de.intevation.flys.client.shared.model.RangeData;
felix@1591:
felix@1591: import de.intevation.flys.client.client.services.DistanceInfoService;
felix@1591: import de.intevation.flys.client.client.services.DistanceInfoServiceAsync;
felix@1591: import de.intevation.flys.client.client.Config;
felix@1591: import de.intevation.flys.client.client.ui.range.DistanceInfoDataSource;
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * This UIProvider serves as base for UI Providers to enter a single location (km).
felix@1591: */
felix@1591: public abstract class LocationPanel
felix@1591: extends AbstractUIProvider
felix@1591: {
felix@1591: /** A container that will contain the location or the distance panel. */
felix@1591: protected HLayout inputLayout;
felix@1591:
felix@1591: /** The minimal value that the user is allowed to enter. */
felix@1591: protected double min;
felix@1591:
felix@1591: /** The maximal value that the user is allowed to enter. */
felix@1591: protected double max;
felix@1591:
felix@1591: /** The values entered in the location mode. */
felix@1591: protected double[] values;
felix@1591:
felix@1593: /** Name of the data item that keeps this location(s). */
felix@1593: protected String dataItemName;
felix@1593:
felix@1591: /** The input panel for locations. */
felix@1591: protected DoubleArrayPanel locationPanel;
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * Creates a new LocationDistancePanel instance.
felix@1591: */
felix@1591: public LocationPanel() {
felix@1591: }
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * This method creates a widget that contains a label, a panel with
felix@1591: * checkboxes to switch the input mode between location and distance input,
felix@1591: * and a mode specific panel.
felix@1591: *
felix@1591: * @param data The data that might be inserted.
felix@1591: *
felix@1591: * @return a panel.
felix@1591: */
felix@1591: @Override
felix@1591: public Canvas create(DataList data) {
felix@1593: findDataItemName(data);
felix@1593:
felix@1591: VLayout layout = new VLayout();
felix@1591: layout.setMembersMargin(10);
felix@1591:
felix@1591: Label label = new Label(MSG.location ());
felix@1591: Canvas widget = createWidget(data);
felix@1591: Canvas submit = getNextButton();
felix@1591:
felix@1591: initDefaults(data);
felix@1591:
felix@1591: widget.setHeight(50);
felix@1591: label.setHeight(25);
felix@1591:
felix@1591: layout.addMember(label);
felix@1591: layout.addMember(widget);
felix@1591: layout.addMember(submit);
felix@1591:
felix@1591: return layout;
felix@1591: }
felix@1591:
felix@1591:
felix@1593: public void findDataItemName(DataList list) {
felix@1593: this.dataItemName = list.getAll().get(0).getLabel();
felix@1593: }
felix@1593:
felix@1593:
felix@1593: public String getDataItemName() {
felix@1593: return this.dataItemName;
felix@1593: }
felix@1593:
felix@1591:
felix@1591: /**
felix@1591: * This method creates a Canvas element showing the old Data objects in the
felix@1591: * DataList data.
felix@1591: */
felix@1591: public Canvas createOld(DataList dataList) {
felix@1593: findDataItemName(dataList);
felix@1593:
felix@1591: List items = dataList.getAll();
felix@1593: Data dLocation = getData(items, getDataItemName());
felix@1591: DataItem[] loc = dLocation.getItems();
felix@1591:
felix@1591: HLayout layout = new HLayout();
felix@1591: layout.setWidth("400px");
felix@1591:
felix@1591: Label label = new Label(dataList.getLabel());
felix@1591: label.setWidth("200px");
felix@1591:
felix@1591: Canvas back = getBackButton(dataList.getState());
felix@1591:
felix@1591: Label selected = new Label(loc[0].getLabel());
felix@1591: selected.setWidth("130px");
felix@1591:
felix@1591: layout.addMember(label);
felix@1591: layout.addMember(selected);
felix@1591: layout.addMember(back);
felix@1591:
felix@1591: return layout;
felix@1591: }
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * This method reads the default values defined in the DataItems of the Data
felix@1591: * objects in list.
felix@1591: *
felix@1591: * @param list The DataList container that stores the Data objects.
felix@1591: */
felix@1591: protected void initDefaults(DataList list) {
felix@1591: Data data = list.get(0);
felix@1591:
felix@1591: /*
felix@1591: // Compatibility with MinMax- DataItems:
felix@1591: RangeData rangeData = null;
felix@1591:
felix@1591: for (int i = 0, n = list.size(); i < n; i++) {
felix@1591: Data tmp = list.get(i);
felix@1591:
felix@1591: if (tmp instanceof RangeData) {
felix@1591: rangeData = (RangeData) tmp;
felix@1591: }
felix@1591: }
felix@1591:
felix@1591: if (rangeData != null) {
felix@1591: min = Double.parseDouble(rangeData.getDefaultLower().toString());
felix@1591: max = Double.parseDouble(rangeData.getDefaultUpper().toString());
felix@1591: // catch ..?
felix@1591: }
felix@1591: */
felix@1591:
felix@1591: if (false) {}
felix@1591: else {
felix@1591: DataItem[] items = data.getItems();
felix@1591: DataItem iMin = getDataItem(items, "min");
felix@1591: DataItem iMax = getDataItem(items, "max");
felix@1591:
felix@1591: try {
felix@1591: min = Double.parseDouble(iMin.getStringValue());
felix@1591: max = Double.parseDouble(iMax.getStringValue());
felix@1591: }
felix@1591: catch (NumberFormatException nfe) {
felix@1591: SC.warn(MSG.error_read_minmax_values());
felix@1591: min = -Double.MAX_VALUE;
felix@1591: max = Double.MAX_VALUE;
felix@1591: }
felix@1591: }
felix@1591:
felix@1591: DataItem def = data.getDefault();
felix@1591: String value = def.getStringValue();
felix@1591:
felix@1591: try {
felix@1591: double d = Double.parseDouble(value);
felix@1591: setLocationValues(new double[] { d } );
felix@1591: }
felix@1591: catch (NumberFormatException nfe) {
felix@1591: // could not parse, dont know what to do else
felix@1591: }
felix@1591: }
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * This method grabs the Data with name name from the list and
felix@1591: * returns it.
felix@1591: *
felix@1591: * @param items A list of Data.
felix@1591: * @param name The name of the Data that we are searching for.
felix@1591: *
felix@1591: * @return the Data with the name name.
felix@1591: */
felix@1591: @Override
felix@1591: protected Data getData(List data, String name) {
felix@1591: for (Data d: data) {
felix@1591: if (name.equals(d.getLabel())) {
felix@1591: return d;
felix@1591: }
felix@1591: }
felix@1591:
felix@1591: return null;
felix@1591: }
felix@1591:
felix@1591:
felix@1591: protected Canvas createWidget(DataList data) {
felix@1591: VLayout layout = new VLayout();
felix@1591: inputLayout = new HLayout();
felix@1591:
felix@1591: // The initial view will display the location input mode.
felix@1591: locationPanel = new DoubleArrayPanel(
felix@1591: MSG.unitLocation(),
felix@1591: getLocationValues(),
felix@1591: new BlurHandler(){public void onBlur(BlurEvent be) {}});
felix@1591:
felix@1591: // TODO Remove picker references, refactor such that subclasses can
felix@1591: // easily use their picker if they want.
felix@1591: //picker.getLocationTable().setAutoFetchData(true);
felix@1591:
felix@1591: inputLayout.addMember(locationPanel);
felix@1591:
felix@1591: layout.addMember(inputLayout);
felix@1591:
felix@1591: inputLayout.setMembersMargin(30);
felix@1591:
felix@1591: //picker.prepareFilter();
felix@1591:
felix@1591: /*
felix@1591: helperContainer.addMember(picker.getLocationTable());
felix@1591: helperContainer.addMember(picker.getFilterLayout());
felix@1591: helperContainer.addMember(picker.getResultCountForm());
felix@1591: */
felix@1591: //createInputPanel();
felix@1591: return layout;
felix@1591: }
felix@1591:
felix@1591:
felix@1591: @Override
felix@1591: public List validate() {
felix@1591: List errors = new ArrayList();
felix@1591: NumberFormat nf = NumberFormat.getDecimalFormat();
felix@1591:
felix@1591: saveLocationValues(locationPanel);
felix@1591:
felix@1591: if (!locationPanel.validateForm()) {
felix@1591: errors.add(MSG.wrongFormat());
felix@1591: return errors;
felix@1591: }
felix@1591:
felix@1591: double[] values = getLocationValues();
felix@1591: double[] good = new double[values.length];
felix@1591: int idx = 0;
felix@1591:
felix@1591: if (values.length > 1) {
felix@1591: errors.add(MSG.too_many_values());
felix@1591: }
felix@1591:
felix@1591: for (double value: values) {
felix@1591: if (value < min || value > max) {
felix@1591: String tmp = MSG.error_validate_range();
felix@1591: tmp = tmp.replace("$1", nf.format(value));
felix@1591: tmp = tmp.replace("$2", nf.format(min));
felix@1591: tmp = tmp.replace("$3", nf.format(max));
felix@1591: errors.add(tmp);
felix@1591: }
felix@1591: else {
felix@1591: good[idx++] = value;
felix@1591: }
felix@1591: }
felix@1591:
felix@1591: double[] justGood = new double[idx];
felix@1591: for (int i = 0; i < justGood.length; i++) {
felix@1591: justGood[i] = good[i];
felix@1591: }
felix@1591:
felix@1591: if (!errors.isEmpty()) {
felix@1591: locationPanel.setValues(justGood);
felix@1591: }
felix@1591:
felix@1591: return errors;
felix@1591: }
felix@1591:
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * This method returns the selected data.
felix@1591: *
felix@1591: * @return the selected/inserted data.
felix@1591: // TODO we are abstract because of this. Refactor to use DATA_NAME and
felix@1591: // similar fields for ld_mode .
felix@1591: public Data[] getData() {
felix@1591: saveLocationValues(locationPanel);
felix@1591: double[] values = getLocationValues();
felix@1591: Data[] data = new Data[values.length+1];
felix@1591:
felix@1591: for (int i = 0; i < values.length; i++) {
felix@1591: data[i] = createDataArray(DATA_ITEM_NAME,
felix@1591: Double.valueOf(values[i]).toString());
felix@1591: }
felix@1591: data[values.length] = createDataArray("ld_mode", "locations");
felix@1591:
felix@1591: return data;
felix@1591: }
felix@1591: */
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * Validates and stores all values entered in the location mode.
felix@1591: *
felix@1591: * @param p The DoubleArrayPanel.
felix@1591: */
felix@1591: protected void saveLocationValues(DoubleArrayPanel p) {
felix@1591: FormItem[] formItems = p.getFields();
felix@1591:
felix@1591: for (FormItem item: formItems) {
felix@1591: if (item.getFieldName().equals(DoubleArrayPanel.FIELD_NAME)) {
felix@1591: saveLocationValue(p, item);
felix@1591: }
felix@1591: }
felix@1591: }
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * Validates and stores a value entered in the location mode.
felix@1591: *
felix@1591: * @param p The DoubleArrayPanel.
felix@1591: * @param item The item that needs to be validated.
felix@1591: */
felix@1591: protected void saveLocationValue(DoubleArrayPanel p, FormItem item) {
felix@1591: if (p.validateForm(item)) {
felix@1591: setLocationValues(p.getInputValues(item));
felix@1591: }
felix@1591: }
felix@1591:
felix@1591:
felix@1591: /** Get the location values. */
felix@1591: protected double[] getLocationValues() {
felix@1591: return values;
felix@1591: }
felix@1591:
felix@1591:
felix@1591: /** Sets Location values and updates the panel. */
felix@1591: protected void setLocationValues(double[] values) {
felix@1591: this.values = values;
felix@1591: locationPanel.setValues(values);
felix@1591: }
felix@1591:
felix@1591:
felix@1591: /**
felix@1591: * Callback when an item from the input helper was clicked.
felix@1591: * Set the respective km-value in the location value field.
felix@1591: * @param e event passed.
felix@1591: */
felix@1591: public void onRecordClick (RecordClickEvent e) {
felix@1591: Record record = e.getRecord();
felix@1591: double[] selected = new double[1];
felix@1591: try {
felix@1591: selected[0] =
felix@1591: Double.parseDouble(record.getAttribute("from"));
felix@1591: }
felix@1591: catch(NumberFormatException nfe) {
felix@1591: // Is there anything else to do here?
felix@1591: }
felix@1591: setLocationValues(selected);
felix@1591: }
felix@1591: }
felix@1591: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :