Class ClosureModel

java.lang.Object
groovy.swing.model.ClosureModel
All Implemented Interfaces:
NestedValueModel, ValueModel

public class ClosureModel extends Object implements ValueModel, NestedValueModel
Represents a value model using a closure to extract the value from some source model and an optional write closure for updating the value.
  • Constructor Details

    • ClosureModel

      public ClosureModel(ValueModel sourceModel, Closure readClosure)
      Creates a read-only closure-backed model.
      Parameters:
      sourceModel - the model that supplies the source object
      readClosure - the closure used to read the derived value
    • ClosureModel

      public ClosureModel(ValueModel sourceModel, Closure readClosure, Closure writeClosure)
      Creates a closure-backed model with explicit read and write closures.
      Parameters:
      sourceModel - the model that supplies the source object
      readClosure - the closure used to read the derived value
      writeClosure - the closure used to write back a new value, or null for read-only access
    • ClosureModel

      public ClosureModel(ValueModel sourceModel, Closure readClosure, Closure writeClosure, Class type)
      Creates a closure-backed model with an explicit declared type.
      Parameters:
      sourceModel - the model that supplies the source object
      readClosure - the closure used to read the derived value
      writeClosure - the closure used to write back a new value, or null for read-only access
      type - the declared type of the derived value
  • Method Details

    • getSourceModel

      public ValueModel getSourceModel()
      Returns the model that supplies the source object consumed by the closures.
      Specified by:
      getSourceModel in interface NestedValueModel
      Returns:
      the nested source model
    • getValue

      public Object getValue()
      Evaluates the current source object through the read closure.
      Specified by:
      getValue in interface ValueModel
      Returns:
      the derived value, or null if the source model currently holds null
    • setValue

      public void setValue(Object value)
      Applies the write closure to the current source object when the model is editable.
      Specified by:
      setValue in interface ValueModel
      Parameters:
      value - the value to write
    • getType

      public Class getType()
      Returns the declared value type for this derived model.
      Specified by:
      getType in interface ValueModel
      Returns:
      the model type
    • isEditable

      public boolean isEditable()
      Indicates whether this model has a write closure.
      Specified by:
      isEditable in interface ValueModel
      Returns:
      true when a write closure is available