teichmann@5835: package org.dive4elements.river.client.client.ui; raimund@238: sascha@2939: import com.google.gwt.i18n.client.NumberFormat; sascha@2939: sascha@2939: import com.smartgwt.client.data.Record; rrenkert@4221: rrenkert@4221: import com.smartgwt.client.widgets.grid.events.CellClickEvent; sascha@2939: teichmann@5835: import org.dive4elements.river.client.shared.model.Data; sascha@2939: felix@1612: import java.util.ArrayList; felix@1612: import java.util.List; raimund@906: raimund@238: /** felix@1590: * This UIProvider creates a widget to enter a single location (km). raimund@238: */ raimund@238: public class SingleLocationPanel felix@1612: extends MultipleLocationPanel raimund@238: { christian@4184: private static final long serialVersionUID = -300641333561787454L; christian@4184: christian@4184: raimund@238: /** felix@2897: * Creates a new SingleLocationPanel instance. raimund@238: */ raimund@238: public SingleLocationPanel() { felix@1590: picker = new LocationPicker(this); raimund@238: } raimund@238: raimund@238: felix@1612: /** Overridden to restrict to one entered value. */ felix@1586: @Override felix@1612: public List validate() { felix@1612: List errors = new ArrayList(); felix@1612: NumberFormat nf = NumberFormat.getDecimalFormat(); raimund@238: felix@1612: saveLocationValues(locationPanel); raimund@238: felix@1612: if (!locationPanel.validateForm()) { felix@1612: errors.add(MSG.wrongFormat()); felix@1612: return errors; felix@1612: } ingo@519: felix@1612: double[] values = getLocationValues(); felix@1612: double[] good = new double[values.length]; felix@1612: int idx = 0; felix@1590: felix@1612: // We want just one value to be allowed. felix@1612: if (values.length > 1) { felix@1612: errors.add(MSG.too_many_values()); felix@1612: } felix@1590: felix@1612: for (double value: values) { felix@1612: if (value < min || value > max) { felix@1612: String tmp = MSG.error_validate_range(); felix@1612: tmp = tmp.replace("$1", nf.format(value)); felix@1612: tmp = tmp.replace("$2", nf.format(min)); felix@1612: tmp = tmp.replace("$3", nf.format(max)); felix@1612: errors.add(tmp); felix@1612: } felix@1612: else { felix@1612: good[idx++] = value; felix@1590: } ingo@519: } ingo@519: felix@1612: double[] justGood = new double[idx]; felix@1612: for (int i = 0; i < justGood.length; i++) { felix@1612: justGood[i] = good[i]; ingo@563: } ingo@563: felix@1612: if (!errors.isEmpty()) { felix@1612: locationPanel.setValues(justGood); ingo@519: } felix@1591: felix@1612: return errors; felix@1591: } felix@1591: felix@1591: raimund@238: /** raimund@238: * This method returns the selected data. raimund@238: * raimund@238: * @return the selected/inserted data. raimund@238: */ christian@4184: @Override raimund@238: public Data[] getData() { raimund@242: saveLocationValues(locationPanel); felix@1617: double[] values = getLocationValues(); ingo@1387: Data[] data = new Data[values.length+1]; felix@1586: raimund@238: for (int i = 0; i < values.length; i++) { felix@1593: data[i] = createDataArray(getDataItemName(), raimund@238: Double.valueOf(values[i]).toString()); raimund@238: } felix@1612: felix@1586: data[values.length] = createDataArray("ld_mode", "locations"); ingo@1387: raimund@238: return data; raimund@238: } felix@2938: felix@2938: felix@2938: /* This is a copy of super.super.onRecordClick. Straighten out felix@2938: this weird family. */ felix@2938: /** felix@2938: * Callback when an item from the input helper was clicked. felix@2938: * Set the respective km-value in the location value field. felix@2938: * @param e event passed. felix@2938: */ christian@4184: @Override rrenkert@4221: public void onCellClick (CellClickEvent e) { felix@2938: Record record = e.getRecord(); felix@2938: double[] selected = new double[1]; felix@2938: try { felix@2938: selected[0] = felix@2938: Double.parseDouble(record.getAttribute("from")); felix@2938: } felix@2938: catch(NumberFormatException nfe) { felix@2938: // Is there anything else to do here? felix@2938: } felix@2938: setLocationValues(selected); felix@2938: } raimund@3493: raimund@3493: christian@4184: @Override raimund@3493: protected String getLabelString() { raimund@3493: return MSG.single_location(); raimund@3493: } raimund@238: } raimund@238: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :