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.Alignment;
ingo@42: import com.smartgwt.client.widgets.form.DynamicForm;
ingo@42: import com.smartgwt.client.widgets.form.fields.FloatItem;
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.events.BlurHandler;
ingo@42:
ingo@211: import de.intevation.flys.client.client.FLYSConstants;
ingo@42:
christian@4131: import java.util.Map;
ingo@42:
ingo@42:
ingo@42: /**
ingo@42: * This class creates a DynamicForm with three input fields.
ingo@42: *
ingo@42: * @author Ingo Weinzierl
ingo@42: */
ingo@42: public class DoubleRangePanel
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:
ingo@42:
ingo@42: /** The constant name of the input field to enter the start of a distance.*/
ingo@42: public static final String FIELD_FROM = "from";
ingo@42:
ingo@42: /** The constant name of the input field to enter the end of a distance.*/
ingo@42: public static final String FIELD_TO = "to";
ingo@42:
ingo@42: /** The constant name of the input field to enter the step width of a
ingo@42: * distance.*/
ingo@42: public static final String FIELD_WIDTH = "step";
ingo@42:
raimund@233: /** The textboxes */
raimund@233: protected FloatItem fromItem;
raimund@233: protected FloatItem toItem;
raimund@233: protected FloatItem stepItem;
raimund@233:
ingo@42:
ingo@1265: public DoubleRangePanel() {
ingo@1265: }
ingo@1265:
ingo@1265:
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 DoubleRangePanel(
ingo@42: String titleFrom, String titleTo, String titleStep,
ingo@42: double from, double to, double step,
ingo@42: int width,
ingo@42: BlurHandler handler)
ingo@42: {
ingo@787: this(
ingo@787: titleFrom, titleTo, titleStep,
ingo@787: from, to, step,
ingo@787: width,
ingo@787: handler,
ingo@787: "right");
ingo@787: }
ingo@787:
ingo@787:
ingo@787: public DoubleRangePanel(
ingo@787: String titleFrom, String titleTo, String titleStep,
ingo@787: double from, double to, double step,
ingo@787: int width,
ingo@787: BlurHandler handler,
ingo@787: String labelOrientation)
ingo@787: {
raimund@233: fromItem = new FloatItem(FIELD_FROM);
raimund@233: toItem = new FloatItem(FIELD_TO);
raimund@233: stepItem = new FloatItem(FIELD_WIDTH);
ingo@42:
ingo@42: fromItem.addBlurHandler(handler);
ingo@42: toItem.addBlurHandler(handler);
ingo@42: stepItem.addBlurHandler(handler);
ingo@42:
ingo@42: NumberFormat f = NumberFormat.getDecimalFormat();
ingo@42:
ingo@42: fromItem.setValue(f.format(from));
ingo@42: toItem.setValue(f.format(to));
ingo@42: stepItem.setValue(f.format(step));
ingo@42:
ingo@42: StaticTextItem fromText = new StaticTextItem("staticFrom");
ingo@42: fromText.setValue(titleFrom);
ingo@42: fromText.setShowTitle(false);
ingo@42: fromItem.setShowTitle(false);
ingo@42:
ingo@42: StaticTextItem toText = new StaticTextItem("staticTo");
ingo@42: toText.setValue(titleTo);
ingo@42: toText.setShowTitle(false);
ingo@42: toItem.setShowTitle(false);
ingo@42:
ingo@42: StaticTextItem stepText = new StaticTextItem("staticStep");
ingo@42: stepText.setValue(titleStep);
ingo@42: stepText.setShowTitle(false);
ingo@42: stepItem.setShowTitle(false);
ingo@42:
ingo@42: int itemWidth = width / 6;
ingo@42: fromItem.setWidth(itemWidth);
ingo@42: fromText.setWidth(itemWidth);
ingo@42: toItem.setWidth(itemWidth);
ingo@42: toText.setWidth(itemWidth);
ingo@42: stepItem.setWidth(itemWidth);
ingo@42: stepText.setWidth(itemWidth);
ingo@42:
ingo@787: if (labelOrientation.equals("right")) {
ingo@787: setFields(fromItem, fromText, toItem, toText, stepItem, stepText);
ingo@787: }
ingo@787: else {
ingo@787: setFields(fromText, fromItem, toText, toItem, stepText, stepItem);
ingo@787: }
ingo@787:
ingo@42: setFixedColWidths(false);
ingo@42: setNumCols(6);
ingo@42: setWidth(width);
ingo@42: setAlign(Alignment.CENTER);
ingo@42: }
ingo@42:
ingo@42:
ingo@42: /**
raimund@233: * This method takes distances values and sets them to the textboxes
raimund@233: * visualizied by this widget.
raimund@233: *
raimund@233: * @param from The from value.
raimund@233: * @param to The to value.
raimund@233: * @param steps The max steps.
raimund@233: */
raimund@233: public void setValues(double from, double to, double steps) {
raimund@233: NumberFormat f = NumberFormat.getDecimalFormat();
raimund@233:
raimund@233: fromItem.setValue(f.format(from));
raimund@233: toItem.setValue(f.format(to));
raimund@233: stepItem.setValue(f.format(steps));
raimund@233: }
raimund@233:
ingo@562:
ingo@562: public boolean validateForm() {
ingo@1490: try {
ingo@1490: return
ingo@1490: validateForm(fromItem) &&
ingo@1490: validateForm(toItem) &&
ingo@1490: validateForm(stepItem);
ingo@1490: }
ingo@1490: catch (NumberFormatException nfe) {
ingo@1490: return false;
ingo@1490: }
ingo@562: }
ingo@562:
raimund@233: /**
ingo@42: * This method validates the entered text in the input fields. If
ingo@42: * there are values that doesn't represent a valid float, 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:
ingo@42: String v = (String) item.getValue();
ingo@42:
ingo@42: NumberFormat f = NumberFormat.getDecimalFormat();
christian@4131: @SuppressWarnings("rawtypes")
ingo@42: Map errors = getErrors();
ingo@42:
ingo@42: try {
ingo@562: if (v == null) {
ingo@562: throw new NumberFormatException("empty");
ingo@562: }
ingo@562:
christian@4131: f.parse(v);
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: item.focusInItem();
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: * Returns the double value of value.
ingo@42: *
ingo@42: * @return the double value of value.
ingo@42: */
ingo@42: protected double getDouble(String value) {
ingo@42: NumberFormat f = NumberFormat.getDecimalFormat();
ingo@42:
ingo@571: String[] splitted = value.split(" ");
ingo@571:
ingo@571: return f.parse(splitted[0]);
ingo@42: }
ingo@42:
ingo@42:
ingo@42: /**
ingo@42: * Returns the start value.
ingo@42: *
ingo@42: * @return the start value.
ingo@42: */
ingo@1490: public double getFrom() throws NullPointerException {
ingo@42: String v = getValueAsString(FIELD_FROM);
ingo@42:
ingo@42: return getDouble(v);
ingo@42: }
ingo@42:
ingo@42:
ingo@42: /**
ingo@42: * Returns the end value.
ingo@42: *
ingo@42: * @return the end value.
ingo@42: */
ingo@1490: public double getTo() throws NullPointerException {
ingo@42: String v = getValueAsString(FIELD_TO);
ingo@42:
ingo@42: return getDouble(v);
ingo@42: }
ingo@42:
ingo@42:
ingo@42: /**
ingo@42: * Returns the step width.
ingo@42: *
ingo@42: * @return the step width.
ingo@42: */
ingo@1490: public double getStep() throws NullPointerException {
ingo@42: String v = getValueAsString(FIELD_WIDTH);
ingo@42:
ingo@42: return getDouble(v);
ingo@42: }
ingo@580:
ingo@580:
ingo@580: /**
ingo@580: * Sets the value of the field with name fieldname.
ingo@580: *
ingo@580: * @param value The new value.
ingo@580: * @param fieldname The name of the field.
ingo@580: */
ingo@580: public void setDoubleValue(double value, String fieldname) {
ingo@580: NumberFormat f = NumberFormat.getDecimalFormat();
ingo@580: setValue(fieldname, f.format(value));
ingo@580: }
ingo@580:
ingo@580:
ingo@580: /**
ingo@580: * Sets a new start value.
ingo@580: *
ingo@580: * @param value The new start value.
ingo@580: */
ingo@580: public void setFrom(double value) {
ingo@580: setDoubleValue(value, FIELD_FROM);
ingo@580: }
ingo@580:
ingo@580:
ingo@580: /**
ingo@580: * Sets a new end value.
ingo@580: *
ingo@580: * @param value The new end value.
ingo@580: */
ingo@580: public void setTo(double value) {
ingo@580: setDoubleValue(value, FIELD_TO);
ingo@580: }
ingo@780:
ingo@780:
ingo@780: /**
ingo@780: * Sets a new step width.
ingo@780: *
ingo@780: * @param value The new step width.
ingo@780: */
ingo@780: public void setStep(double value) {
ingo@780: setDoubleValue(value, FIELD_WIDTH);
ingo@780: }
ingo@42: }
ingo@42: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :