ingo@42: package de.intevation.flys.client.client.ui; ingo@42: ingo@42: import java.util.Map; ingo@42: ingo@42: import com.google.gwt.core.client.GWT; ingo@42: import com.google.gwt.i18n.client.NumberFormat; ingo@42: ingo@42: import com.smartgwt.client.types.TitleOrientation; ingo@42: import com.smartgwt.client.widgets.form.DynamicForm; ingo@42: import com.smartgwt.client.widgets.form.fields.FormItem; ingo@42: import com.smartgwt.client.widgets.form.fields.StaticTextItem; ingo@42: import com.smartgwt.client.widgets.form.fields.TextItem; ingo@42: import com.smartgwt.client.widgets.form.fields.events.BlurHandler; ingo@42: ingo@211: import de.intevation.flys.client.client.FLYSConstants; ingo@42: ingo@42: public class DoubleArrayPanel ingo@42: extends DynamicForm ingo@42: { ingo@42: /** The message class that provides i18n strings.*/ ingo@211: protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); ingo@42: raimund@233: protected TextItem ti; ingo@42: ingo@42: /** The constant input field name.*/ ingo@42: public static final String FIELD_NAME = "doublearray"; ingo@42: ingo@42: ingo@246: public DoubleArrayPanel( ingo@246: String title, ingo@246: double[] values, ingo@246: BlurHandler handler) ingo@246: { ingo@246: this(title, values, handler, TitleOrientation.RIGHT); ingo@246: } ingo@246: ingo@246: ingo@42: /** ingo@42: * Creates a new form with a single input field that displays an array of ingo@42: * double values. ingo@42: * ingo@42: * @param name The name of the TextItem. ingo@42: * @param title The title of the TextItem. ingo@42: * @param values The double values that should be displayed initially. ingo@42: * @param handler The BlurHandler that is used to valide the input. ingo@42: */ ingo@42: public DoubleArrayPanel( ingo@42: String title, ingo@42: double[] values, ingo@246: BlurHandler handler, ingo@246: TitleOrientation titleOrientation) ingo@42: { raimund@233: ti = new TextItem(FIELD_NAME); ingo@42: StaticTextItem sti = new StaticTextItem("staticarray"); ingo@42: ingo@42: ti.setShowTitle(false); ingo@42: sti.setShowTitle(false); ingo@42: sti.setValue(title); ingo@42: ingo@42: ti.addBlurHandler(handler); ingo@42: ingo@246: if (titleOrientation == TitleOrientation.RIGHT) { ingo@246: setFields(ti, sti); ingo@246: } ingo@246: else { ingo@246: setFields(sti, ti); ingo@246: } ingo@246: ingo@246: setTitleOrientation(titleOrientation); ingo@42: setNumCols(2); ingo@42: ingo@42: if (values == null) { ingo@42: return; ingo@42: } ingo@42: ingo@42: NumberFormat f = NumberFormat.getDecimalFormat(); ingo@42: ingo@42: StringBuilder text = new StringBuilder(); ingo@42: boolean firstItem = true; ingo@42: ingo@42: for (double val: values) { ingo@42: if (!firstItem) { ingo@42: text.append(" "); ingo@42: } ingo@42: ingo@42: text.append(f.format(val)); ingo@42: ingo@42: firstItem = false; ingo@42: } ingo@42: ingo@42: ti.setValue(text.toString()); ingo@42: } ingo@42: ingo@42: ingo@42: /** raimund@233: * This method takes the double array to set the values to the textbox. raimund@233: * raimund@233: * @param values The double values. raimund@233: */ raimund@233: public void setValues(double[] values) { raimund@233: NumberFormat f = NumberFormat.getDecimalFormat(); raimund@233: raimund@233: StringBuilder text = new StringBuilder(); raimund@233: boolean firstItem = true; raimund@238: if (values != null) { raimund@238: for (double val: values) { raimund@238: if (!firstItem) { raimund@238: text.append(" "); raimund@238: } raimund@233: raimund@238: text.append(f.format(val)); raimund@238: raimund@238: firstItem = false; raimund@233: } raimund@233: } raimund@233: raimund@233: ti.clearValue(); raimund@233: ti.setValue(text.toString()); raimund@233: } raimund@233: raimund@233: ingo@563: protected boolean validateForm() { ingo@563: return validateForm(ti); ingo@563: } ingo@563: ingo@563: raimund@233: /** ingo@42: * This method validates the entered text in the location input field. If ingo@42: * there are values that doesn't represent a valid location, an error is ingo@42: * displayed. ingo@42: * ingo@42: * @param item The FormItem. ingo@42: */ ingo@42: protected boolean validateForm(FormItem item) { ingo@59: if (item instanceof StaticTextItem) { ingo@59: return true; ingo@59: } ingo@59: ingo@42: boolean valid = true; ingo@42: String value = (String) item.getValue(); ingo@42: ingo@42: if (value == null) { ingo@42: return valid; ingo@42: } ingo@42: ingo@42: String[] parts = value.split(" "); ingo@42: ingo@42: if (parts == null) { ingo@42: return valid; ingo@42: } ingo@42: ingo@42: NumberFormat f = NumberFormat.getDecimalFormat(); ingo@42: Map errors = getErrors(); ingo@42: ingo@42: try { ingo@42: for (String part: parts) { ingo@42: double location = f.parse(part); ingo@42: } ingo@42: ingo@42: errors.remove(item.getFieldName()); ingo@42: } ingo@42: catch (NumberFormatException nfe) { ingo@42: errors.put(item.getFieldName(), MESSAGES.wrongFormat()); ingo@42: ingo@42: valid = false; ingo@42: } ingo@42: ingo@42: setErrors(errors, true); ingo@42: ingo@42: return valid; ingo@42: } ingo@42: ingo@42: ingo@42: /** ingo@42: * This method returns the double array that has been entered in ingo@42: * item. ingo@42: * ingo@42: * @param item The item that contains the desired values. ingo@42: * ingo@42: * @return the values as double array. ingo@42: */ ingo@42: public double[] getInputValues(FormItem item) { ingo@42: String value = (String) item.getValue(); ingo@42: ingo@42: if (value == null) { ingo@42: return null; ingo@42: } ingo@42: ingo@42: String[] parts = value.split(" "); ingo@42: ingo@42: if (parts == null) { ingo@42: return null; ingo@42: } ingo@42: ingo@42: NumberFormat f = NumberFormat.getDecimalFormat(); ingo@42: ingo@571: double[] values = new double[parts.length]; ingo@571: ingo@42: int i = 0; ingo@571: OUTER: for (String part: parts) { ingo@42: try { ingo@571: double x = f.parse(part); ingo@571: for (int j = 0; j < i; ++j) { ingo@571: if (values[j] == x) { ingo@571: continue OUTER; ingo@571: } ingo@571: } ingo@571: values[i++] = x; ingo@42: } ingo@42: catch (NumberFormatException nfe) { ingo@42: // do nothing ingo@42: } ingo@42: } ingo@42: ingo@571: double [] out = new double[i]; ingo@571: System.arraycopy(values, 0, out, 0, i); ingo@571: ingo@571: return out; ingo@42: } ingo@246: ingo@246: ingo@246: /** ingo@246: * Returns the double values of this panel. ingo@246: * ingo@246: * @return the double values of this panel. ingo@246: */ ingo@246: public double[] getInputValues() { ingo@246: return getInputValues(ti); ingo@246: } ingo@42: } ingo@42: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :