Class SimpleTimer

java.lang.Object
com.vaadin.flow.component.Component
com.flowingcode.vaadin.addons.simpletimer.SimpleTimer
All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasElement, HasSize, HasStyle, Serializable

@Tag("simple-timer") @JsModule("./simple-timer/simple-timer.js") public class SimpleTimer extends Component implements HasSize, HasStyle, Serializable
A timer that counts down to zero or counts up to a limit.

The state of the timer is kept on the server, as the time banked from previous runs plus the instant the current run started. The browser only renders it. When the component is detached and attached again, the elapsed time is pushed to the new client element, so the timer keeps counting while it is not in the DOM.

Author:
Leonardo Scardanzan / Flowing Code
See Also:
  • Constructor Details

    • SimpleTimer

      public SimpleTimer()
      Creates a timer.
  • Method Details

    • setCountdown

      public void setCountdown(Duration duration)
      Configures the timer to count down from the given duration to zero, and resets it. A running timer keeps running from the new start.
      Parameters:
      duration - the duration to count down from
      Throws:
      IllegalArgumentException - if duration is negative
    • setCountUp

      public void setCountUp(Duration limit)
      Configures the timer to count up from zero to the given limit, and resets it. A running timer keeps running from zero.
      Parameters:
      limit - the duration at which the timer stops
      Throws:
      IllegalArgumentException - if limit is negative
    • isCountUp

      public boolean isCountUp()
      Returns whether the timer counts up.
      Returns:
      true if the timer counts up to its limit, false if it counts down
    • getLimit

      public Duration getLimit()
      Returns the duration the timer counts down from, or counts up to.
      Returns:
      the limit of the timer
    • start

      public void start()
      Starts the timer. Does nothing if it is already running or has reached its limit.
    • pause

      public void pause()
      Pauses the timer, keeping the elapsed time. Does nothing if it is not running.
    • reset

      public void reset()
      Resets the elapsed time to zero. A running timer keeps running from the start.
    • isRunning

      public boolean isRunning()
      Returns whether the timer is running.
      Returns:
      true if the timer has been started and has not reached its limit
    • getElapsed

      public Duration getElapsed()
      Returns the time the timer has been running, up to its limit. The value is computed on the server and does not need a round trip.
      Returns:
      the elapsed time
    • getRemaining

      public Duration getRemaining()
      Returns the time left until the timer reaches its limit.
      Returns:
      the remaining time
    • setElapsed

      public void setElapsed(Duration elapsed)
      Sets the elapsed time, for instance to restore a timer whose state was saved elsewhere. A running timer keeps running from the given point.
      Parameters:
      elapsed - the elapsed time, clamped between zero and the limit
    • setFractions

      public void setFractions(boolean fractions)
      Enables showing fractions of a second.
      Parameters:
      fractions - whether to show hundredths of a second
    • setMinutes

      public void setMinutes(boolean minutes)
      Enables showing minutes.
      Parameters:
      minutes - whether to show minutes
    • setHours

      public void setHours(boolean hours)
      Enables showing hours and minutes.
      Parameters:
      hours - whether to show hours and minutes
    • setDoubleDigitHours

      public void setDoubleDigitHours(boolean doubleDigitHours)
      Use two digits for hours.
      Parameters:
      doubleDigitHours - true to format hours using two digits.
    • addTimerEndListener

      public Registration addTimerEndListener(ComponentEventListener<SimpleTimer.TimerEndedEvent> listener)
      Adds a listener that is notified when the timer reaches its limit.

      If the timer reaches its limit while it is detached, the listener is notified when the component is attached again, and SimpleTimer.TimerEndedEvent.isRetroactive() returns true.

      Parameters:
      listener - the listener to add
      Returns:
      a handle that can be used for removing the listener
    • addTickListener

      public Registration addTickListener(ComponentEventListener<SimpleTimer.TimerTickEvent> listener, Duration period)
      Adds a listener that is notified while the timer is running, at most once per period.

      Each notification is a round trip from the browser, so use a period that matches what the listener needs. To show the value in another component, use addDisplay(Component) instead, which needs no round trips.

      Parameters:
      listener - the listener to add
      period - the minimum time between notifications
      Returns:
      a handle that can be used for removing the listener
      Throws:
      IllegalArgumentException - if period is zero or negative
    • setDetachPolicy

      public void setDetachPolicy(SimpleTimer.DetachPolicy detachPolicy)
      Sets what the timer does while it is detached. The default is SimpleTimer.DetachPolicy.CONTINUE.
      Parameters:
      detachPolicy - the policy to apply when the timer is detached
    • getDetachPolicy

      public SimpleTimer.DetachPolicy getDetachPolicy()
      Returns what the timer does while it is detached.
      Returns:
      the detach policy
    • addDisplay

      public Registration addDisplay(Component display)
      Shows the value of the timer inside another component as well. The browser updates the value on every frame, without round trips to the server.

      The timer appends a text node to the component and only updates that node, so the content the component already has is kept. To show the value only inside other components, hide the timer with setVisible(boolean): it keeps running while hidden.

      The value is only updated while the timer is attached. To keep a display updated while it is moved in and out of view, for instance inside tabs, attach the timer somewhere it stays attached and add only the display to the tab.

      A component shows the value of one timer at a time: if it is added as a display to two timers, they overwrite each other's value. The timer keeps a reference to the component until the display is removed with the returned handle.

      Parameters:
      display - the component in which to show the value
      Returns:
      a handle that can be used for removing the display
    • onDetach

      protected void onDetach(DetachEvent detachEvent)
      Overrides:
      onDetach in class Component
    • onAttach

      protected void onAttach(AttachEvent attachEvent)
      Overrides:
      onAttach in class Component
    • isVisible

      public boolean isVisible()
      Overrides:
      isVisible in class Component
    • setVisible

      public void setVisible(boolean visible)
      Shows or hides the timer with CSS, so that it keeps running and communicating with the server while hidden. A hidden timer still updates the components added with addDisplay(Component).
      Overrides:
      setVisible in class Component
      Parameters:
      visible - whether to show the timer
    • setStartTime

      @Deprecated public void setStartTime(Number startTime)
      Deprecated.
      Sets the start time, for countdown mode.
      Parameters:
      startTime - value in seconds for the start time
    • setEndTime

      @Deprecated public void setEndTime(Number endTime)
      Deprecated.
      Sets the end time, for countup mode.
      Parameters:
      endTime - value in seconds for the end time
    • getCurrentTime

      @Deprecated public BigDecimal getCurrentTime()
      Deprecated.
      Returns the displayed value of the timer: the remaining time when counting down, or the elapsed time when counting up.
      Returns:
      current value in seconds
    • getCurrentTimeAsync

      @Deprecated public CompletableFuture<BigDecimal> getCurrentTimeAsync()
      Deprecated.
      Returns the displayed value of the timer. The value is computed on the server, so the returned future is already completed.
      Returns:
      a completed future with the value of getCurrentTime()
    • addCurrentTimeChangeListener

      @Deprecated public Registration addCurrentTimeChangeListener(PropertyChangeListener listener, long period, TimeUnit periodUnit)
      Deprecated.
      Use addTickListener(ComponentEventListener, Duration) instead. The value itself is available on the server through getElapsed().
      Adds a property change listener for the currentTime property.
      Parameters:
      listener - the property change listener
      period - the minimum period between listener invocations, or 0 to disable throttling
      periodUnit - time duration of throttling period
      Returns:
      this registration, for chaining
    • addTimerEndEvent

      Adds a timer ended listener that will be triggered when the timer reaches its limit.
      Parameters:
      listener - the listener to add
      Returns:
      a handle that can be used for removing the listener