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

us.ihmc.javafx.parameter.JavaFXPropertyHolder Maven / Gradle / Ivy

The newest version!
package us.ihmc.javafx.parameter;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import java.util.ArrayList;
import java.util.Objects;

public abstract class JavaFXPropertyHolder
{
   private final ArrayList changedListeners = new ArrayList<>();
   protected ChangeListener changeListener = this::changed;
   protected ChangeListener changingListener = this::changing;
   private volatile boolean skipNextChange = false;
   private volatile boolean isChanging = false;

   public JavaFXPropertyHolder()
   {

   }

   protected void changed(ObservableValue observable, T oldValue, T newValue)
   {
      if (!skipNextChange && !isChanging && !Objects.equals(oldValue, newValue))
      {
         for (Runnable changedListener : changedListeners)
         {
            changedListener.run();
         }
      }

      skipNextChange = false;
   }

   protected void changing(ObservableValue observable, Boolean wasChanging, Boolean isChanging)
   {
      this.isChanging = isChanging;
      if (wasChanging) // necessary for both click and drag to work for sliders
      {
         for (Runnable changedListener : changedListeners)
         {
            changedListener.run();
         }
      }
   }

   public void setValue(T value, boolean notifyListeners)
   {
      if (!notifyListeners)
         skipNextChange = true;
      setValueInternal(value);
   }

   public void addValueChangedListener(Runnable onChanged)
   {
      changedListeners.add(onChanged);
   }

   public void setValue(T value)
   {
      setValue(value, true);
   }

   public abstract T getValue();

   protected abstract void setValueInternal(T value);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy