felix@1591: package de.intevation.flys.client.client.ui;
felix@1591:
christian@4131: import com.google.gwt.core.client.GWT;
felix@1591: import com.google.gwt.i18n.client.NumberFormat;
felix@1591:
christian@4131: import com.smartgwt.client.data.Record;
felix@1591: import com.smartgwt.client.util.SC;
felix@1591: import com.smartgwt.client.widgets.Canvas;
felix@1591: import com.smartgwt.client.widgets.Label;
christian@4131: import com.smartgwt.client.widgets.form.fields.FormItem;
felix@1591: import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
christian@4131: import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
christian@4131: import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
felix@1591: import com.smartgwt.client.widgets.layout.HLayout;
felix@1591: import com.smartgwt.client.widgets.layout.VLayout;
felix@1591:
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:
christian@4131: import java.util.ArrayList;
christian@4131: import java.util.List;
christian@4131:
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: {
christian@4131: private static final long serialVersionUID = -5306604428440015046L;
christian@4131:
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@1617: values = new double[0];
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@1615: // Subclass uses translated data items name as label.
felix@1615: 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@1615: /** Store label of first data item in list. */
felix@1593: public void findDataItemName(DataList list) {
felix@1593: this.dataItemName = list.getAll().get(0).getLabel();
felix@1593: }
felix@1593:
felix@1593:
felix@1615: /** Get label of first data item that this uiprovider has seen. */
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: */
christian@4131: @Override
felix@1591: public Canvas createOld(DataList dataList) {
felix@1593: findDataItemName(dataList);
felix@1593:
felix@1591: List items = dataList.getAll();
felix@1618: Data dLocation = getData(items, getDataItemName());
felix@1618: 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@1615: // TODO evaluate: isn't this what findDataItemName is doing?
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:
christian@4131: DataItem[] items = data.getItems();
christian@4131: DataItem iMin = getDataItem(items, "min");
christian@4131: DataItem iMax = getDataItem(items, "max");
felix@1591:
christian@4131: try {
christian@4131: min = Double.parseDouble(iMin.getStringValue());
christian@4131: max = Double.parseDouble(iMax.getStringValue());
felix@1591: }
christian@4131: catch (NumberFormatException nfe) {
christian@4131: SC.warn(MSG.error_read_minmax_values());
christian@4131: min = -Double.MAX_VALUE;
christian@4131: max = Double.MAX_VALUE;
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) {
christian@4131: // could not parse, don't know what else to do
christian@4131: GWT.log("LocationPanel", nfe);
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@1615: 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(),
christian@4131: new BlurHandler(){@Override
christian@4131: 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@1615: /*
felix@1591: //picker.prepareFilter();
felix@1591: helperContainer.addMember(picker.getLocationTable());
felix@1591: helperContainer.addMember(picker.getFilterLayout());
felix@1591: helperContainer.addMember(picker.getResultCountForm());
felix@1591: */
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: 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: * 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) {
christian@4131: GWT.log("onRecordClick", nfe);
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 :