ingo@42: package de.intevation.flys.client.client.ui; 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; felix@5195: import com.smartgwt.client.widgets.form.fields.events.FocusHandler; ingo@42: ingo@211: import de.intevation.flys.client.client.FLYSConstants; ingo@42: christian@4131: import java.util.Map; christian@4131: ingo@42: public class DoubleArrayPanel ingo@42: extends DynamicForm ingo@42: { felix@1333: /** 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: rrenkert@5104: private String title; rrenkert@5104: felix@1333: /** 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: { felix@5195: this(title, values, handler, null, 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. felix@5195: * @param blurHandler The BlurHandler that is used to valide the input. felix@5195: * @param focusHandler The FocueHandler that is used to valide the input. ingo@42: */ ingo@42: public DoubleArrayPanel( ingo@42: String title, ingo@42: double[] values, felix@5195: BlurHandler blurHandler, felix@5195: FocusHandler focusHandler, ingo@246: TitleOrientation titleOrientation) ingo@42: { rrenkert@5104: this.title = title; 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: felix@5195: ti.addBlurHandler(blurHandler); rrenkert@5217: if (focusHandler != null) { rrenkert@5217: ti.addFocusHandler(focusHandler); rrenkert@5217: } 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: bjoern@3836: if(values == null || values.length == 0) { raimund@1369: ti.clearValue(); raimund@1369: return; raimund@1369: } 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@580: /** ingo@580: * This method appends a double value to the current list of values. ingo@580: * ingo@580: * @param value A new value. ingo@580: */ ingo@580: public void addValue(double value) { ingo@580: NumberFormat f = NumberFormat.getDecimalFormat(); ingo@580: ingo@580: String current = ti.getValueAsString(); ingo@580: ingo@590: if (current == null || current.length() == 0) { ingo@590: current = f.format(value); ingo@590: } ingo@590: else { ingo@590: current += " " + f.format(value); ingo@590: } ingo@580: ingo@580: ti.setValue(current); ingo@580: } ingo@580: ingo@580: 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: */ christian@4131: @SuppressWarnings("unchecked") 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@573: String[] parts = value.split("\\s+"); ingo@42: ingo@42: if (parts == null) { ingo@42: return valid; ingo@42: } ingo@42: christian@4131: NumberFormat nf = NumberFormat.getDecimalFormat(); christian@4131: @SuppressWarnings("rawtypes") christian@4131: Map errors = getErrors(); ingo@42: ingo@42: try { ingo@42: for (String part: parts) { ingo@573: ingo@573: if (part.length() == 0) { ingo@573: continue; ingo@573: } ingo@573: christian@4131: nf.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@573: String[] parts = value.split("\\s+"); 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@573: if (part.length() == 0) { ingo@573: continue; ingo@573: } ingo@573: 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: } rrenkert@5104: rrenkert@5104: public String getItemTitle() { rrenkert@5104: return this.title; rrenkert@5104: } ingo@42: } ingo@42: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :