All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.mmm.ui.tvm.widget.number.TvmNumberInput Maven / Gradle / Ivy

There is a newer version: 0.9.10
Show newest version
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.ui.tvm.widget.number;

import io.github.mmm.base.number.NumberType;
import io.github.mmm.base.range.WritableRange;
import io.github.mmm.ui.api.widget.number.UiNumberInput;
import io.github.mmm.ui.spi.range.NumericRange;
import io.github.mmm.ui.tvm.widget.input.TvmTextualInput;

/**
 * Implementation of {@link UiNumberInput} using TeaVM.
 *
 * @param  type of the {@link #getValue() value}. Typically {@link String}.
 * @since 1.0.0
 */
public abstract class TvmNumberInput> extends TvmTextualInput
    implements UiNumberInput {

  private static final String ATR_STEP = "step";

  private final NumericRange range;

  private V step;

  /**
   * The constructor.
   */
  public TvmNumberInput() {

    super("number");
    this.range = new NumericRange<>(getNumberType());
  }

  /**
   * @return the {@link NumberType} for the underlying {@link Number}.
   */
  protected abstract NumberType getNumberType();

  @Override
  public WritableRange getRange() {

    return this.range;
  }

  @Override
  public V getValueOrThrow() {

    String text = getText().trim();
    if (text.isEmpty()) {
      return null;
    }
    return getNumberType().valueOf(text);
  }

  @Override
  protected void setValueNative(V value) {

    String text = "";
    if (value != null) {
      text = value.toString();
    }
    setText(text);
  }

  @Override
  public V getStep() {

    return this.step;
  }

  @Override
  public void setStep(V step) {

    if (step == null) {
      this.widget.removeAttribute(ATR_STEP);
    } else {
      this.widget.setAttribute(ATR_STEP, step.toString());
    }
    this.step = step;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy