RangeLimitedDoubleProperty.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * MapView: a JavaFX map renderer for tile-based servers
  5.  * http://tidalwave.it/projects/mapview
  6.  *
  7.  * Copyright (C) 2024 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/mapview-src
  22.  * git clone https://github.com/tidalwave-it/mapview-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.mapviewer.impl;

  27. import jakarta.annotation.Nonnull;
  28. import jakarta.annotation.Nullable;
  29. import java.util.Objects;
  30. import javafx.beans.property.SimpleDoubleProperty;

  31. /***************************************************************************************************************************************************************
  32.  *
  33.  * A specialisation of {@link javafx.beans.property.DoubleProperty} that supports limits.
  34.  *
  35.  * @author  Fabrizio Giudici
  36.  *
  37.  **************************************************************************************************************************************************************/
  38. public class RangeLimitedDoubleProperty extends SimpleDoubleProperty
  39.   {
  40.     private double min;

  41.     private double max;

  42.     /***********************************************************************************************************************************************************
  43.      * Creates a new instance.
  44.      * @param   bean            the owner bean
  45.      * @param   name            the property name
  46.      * @param   initialValue    the initial value
  47.      * @param   min             the minimum value
  48.      * @param   max             the maximum value
  49.      * @see                     #setLimits(double, double)
  50.      **********************************************************************************************************************************************************/
  51.     public RangeLimitedDoubleProperty (@Nonnull final Object bean, @Nonnull final String name, final double initialValue, final double min, final double max)
  52.       {
  53.         super(bean, name, Math.clamp(initialValue, min, max));
  54.         this.min = min;
  55.         this.max = max;
  56.       }

  57.     /***********************************************************************************************************************************************************
  58.      * {@inheritDoc}
  59.      **********************************************************************************************************************************************************/
  60.     @Override
  61.     public void set (final double value)
  62.       {
  63.         super.set(Math.clamp(value, min, max));
  64.       }

  65.     /***********************************************************************************************************************************************************
  66.      * {@inheritDoc}
  67.      **********************************************************************************************************************************************************/
  68.     @Override
  69.     public void setValue (@Nullable final Number value)
  70.       {
  71.         Objects.requireNonNull(value, "value");
  72.         super.setValue(Math.clamp(value.doubleValue(), min, max));
  73.       }

  74.     /***********************************************************************************************************************************************************
  75.      * Changes the limit. The current property value will be eventually changed to fit the limits, and a change event fired.
  76.      * @param   min             the minimum value
  77.      * @param   max             the maximum value
  78.      **********************************************************************************************************************************************************/
  79.     public void setLimits (final double min, final double max)
  80.       {
  81.         this.min = min;
  82.         this.max = max;
  83.         set(get());
  84.       }
  85.   }