java.lang.Object
ch.ladestation.connectncharge.util.mvcbase.ControllerBase<M>
Direct Known Subclasses:
ApplicationController

public abstract class ControllerBase<M> extends Object
Base class for all Controllers.

The whole application logic is located in controller classes.

Controller classes work on and manage the Model. Models encapsulate the whole application state.

Controllers provide the whole core functionality of the application, so called 'Actions'

Execution of Actions is asynchronous. The sequence is kept stable, such that for all actions A and B: if B is submitted after A, B will only be executed after A is finished.

  • Field Details

    • log

      protected final org.slf4j.Logger log
    • model

      protected final M model
  • Constructor Details

    • ControllerBase

      protected ControllerBase(M model)
      Controller needs a Model.
      Parameters:
      model - Model managed by this Controller
  • Method Details

    • arrayRemove

      protected static <V> V[] arrayRemove(V[] theArray, V toRemove)
    • arrayAdd

      protected static <V> V[] arrayAdd(V[] theArray, V toAdd)
    • get

      protected static <V> V get(ObservableValue<V> observableValue)
    • get

      protected static <V> V[] get(ObservableArray<V> observableArray)
    • get

      protected static <V> V get(ObservableArray<V> observableArray, int position)
    • syncSet

      protected static <V> void syncSet(ObservableValue<V> val, V newVal)
    • syncSet

      protected static <V> void syncSet(ObservableArray<V> arr, V[] newVal)
    • syncRemove

      protected static <V> void syncRemove(ObservableArray<V> arr, V elem)
    • syncAdd

      protected static <V> void syncAdd(ObservableArray<V> arr, V elem)
    • syncAddUnique

      protected static <V> void syncAddUnique(ObservableArray<V> arr, V elem)
    • shutdown

      public void shutdown()
    • startUp

      public void startUp()
      If anything needs to be run once at startup from the controller
    • async

      protected void async(Supplier<M> action, Consumer<M> onDone)
      Schedule the given action for execution in strict order in external thread, asynchronously.

      onDone is called as soon as action is finished

    • async

      protected void async(Runnable todo)
      Schedule the given action for execution in strict order in external thread, asynchronously.
    • runLater

      public void runLater(Consumer<M> action)
      Parameters:
      action - Schedule the given action after all the actions already scheduled have finished.
    • awaitCompletion

      public void awaitCompletion()
      Intermediate solution for TestCase support.

      Best solution would be that 'action' of 'runLater' is executed on calling thread.

      Waits until all current actions in actionQueue are completed.

      In most cases it's wrong to call this method from within an application.

    • getModel

      public M getModel()
      Only the other base classes 'ViewMixin' and 'PUI_Base' need access, therefore it's 'package private' ...except when trying to mock this. so nvm.
      Returns:
      the model of this controller
    • setValue

      protected <V> void setValue(ObservableValue<V> observableValue, V newValue)
      Even for setting a value the controller is responsible.

      No application specific class can access ObservableValue.setValue

      Value is set asynchronously.

    • setValues

      protected <V> void setValues(ObservableArray<V> observableArray, V[] newValues)
      Even for setting values in the array the controller is responsible.

      No application specific class can access ObservableValue.setValues

      Values are set asynchronously.

    • setValue

      protected <V> void setValue(ObservableArray<V> observableArray, int position, V newValue)
      Even for setting a value in the array the controller is responsible.

      No application specific class can access ObservableValue.setValue

      Value is set asynchronously.

    • toggleValue

      protected void toggleValue(ObservableValue<Boolean> observableValue)
      Convenience method to toggle a ObservableValue<Boolean>
    • toggle

      protected void toggle(ObservableArray<Boolean> observableArray, int position)
      Convenience method to toggle a ObservableArray<Boolean> at position x
    • increaseValue

      protected void increaseValue(ObservableValue<Integer> observableValue)
      Convenience method to increase a ObservableValue<Integer> by 1
    • increase

      protected void increase(ObservableArray<Integer> observableArray, int position)
      Convenience method to increase a ObservableArray<Integer> by 1 at position x
    • decreaseValue

      protected void decreaseValue(ObservableValue<Integer> observableValue)
      Convenience method to decrease a ObservableValue<Integer> by 1
    • decrease

      protected void decrease(ObservableArray<Integer> observableArray, int position)
      Convenience method to decrease a ObservableArray<Integer> by 1 at position x
    • remove

      protected <V> void remove(ObservableArray<V> observableArray, V elementToRemove)
      Convenience method to remove an element from an ObservableArray<V>
      Type Parameters:
      V - the type of the array and element
      Parameters:
      observableArray - the array that will be affected
      elementToRemove - the element to remove
    • add

      protected <V> void add(ObservableArray<V> observableArray, V elementToAdd)
      Convenience method to add an element to an ObservableArray<V> asynchronously.
      Type Parameters:
      V - the type of the array and element
      Parameters:
      observableArray - the array that will be affected
      elementToAdd - the element to add
    • addUnique

      protected <V> void addUnique(ObservableArray<V> observableArray, V elementToAdd)
      Convenience method to add an element to an ObservableArray<V> asynchronously but only if the array doesn't already contain that element.
      Type Parameters:
      V - the type of the array and element
      Parameters:
      observableArray - the array that will be affected
      elementToAdd - the element to add
    • pauseExecution

      protected void pauseExecution(Duration duration)
      Utility function to pause execution of actions for the specified amount of time.

      An InterruptedException will be catched and ignored while setting the interrupt flag again.

      Parameters:
      duration - time to sleep
    • updateModel

      protected void updateModel(ControllerBase.SetterInterface... setters)
      Use this if you need to update several ObservableValues in one async call.

      Use 'set', 'increase', 'decrease' or 'toggle' to get an appropriate Setter

    • set

      protected <V> ControllerBase.Setter<V> set(ObservableValue<V> observableValue, V value)
    • increase

      protected ControllerBase.Setter<Integer> increase(ObservableValue<Integer> observableValue)
    • decrease

      protected ControllerBase.Setter<Integer> decrease(ObservableValue<Integer> observableValue)
    • toggle

      protected ControllerBase.Setter<Boolean> toggle(ObservableValue<Boolean> observableValue)
    • set

      protected <V> ControllerBase.ArraySetter<V> set(ObservableArray<V> observableArray, V[] values)