Package sim.portrayal

Class FieldPortrayal

java.lang.Object
sim.portrayal.FieldPortrayal
Direct Known Subclasses:
FieldPortrayal2D, FieldPortrayal3D

public abstract class FieldPortrayal extends Object
A FieldPortrayal is an object which knows how to portray some kind of Field.

This abstract version of FieldPortrayal provides some basic functionality that many FieldPortrayals may find handy. This functionality allows a FieldPortrayal to store Portrayal objects responsible for drawing various objects within the Field. For example, a SparseGrid2D holds a bunch of objects: the SparseGridPortrayal2D, which is a FieldPortrayal, lets you store Portrayal objects which know how to draw the various objects in the SparseGrid2D.

The default version of the setField(...) method sets the field without checking to see if it's a valid field or not; you'll want to override this to check.

You can associate a Portrayal object with an object stored in the Field in several ways. First, you can specify one Portrayal object to be used for ALL objects stored in the field, using setPortrayalForAll. Second, you can specify a Portrayal object to be used for objects in a Field all belonging to the same class, using setPortrayalForClassOf. Third, you can specify a Portrayal for a specific object, using setPortrayalForObject.

You can get the desired Portrayal for an object by calling getPortrayalForObject. This method looks up the Portrayal for an object by going down the following checklist until a Portrayal is found (earlier checklist items take precedence over later ones):

  1. If there is a portrayalForAll, return it.
  2. If the object is null:
    1. Return the portrayalForNull if there is one
    2. If a portrayal is explicitly registered for null, return that portrayal.
    3. Return the defaultNullPortrayal.
  3. If the object is non-null:
    1. If we are using the "traditional" portrayal ordering, and the object implements the appropriate Portrayal interface, return the object itself as its own Portrayal.
    2. Return the portrayalForNonNull if there is one
    3. If a portrayal is explicitly registered for the object, return that portrayal. Portrayals may be registered for null as well.
    4. If a Portrayal is registered for the object's exact class (superclasses are ignored), return that portrayal.
    5. If we are using the "alternate" portrayal ordering, and the object implements the appropriate Portrayal interface, return the object itself as its own Portrayal.
    6. Return the portrayalForRemainder if there is one
    7. Return the default Portrayal object.

Note that when we decide to use objects themselves (if they are Portrayals) is determined by the portrayal ordering. The "default" (or "classic") version of this ordering uses objects themselves prior to looking up registered portrayals for them or for their classes. The "alternate" ordering instead looks up and uses registered portrayals first.

FieldPortrayals store Portrayal objects in WeakHashMaps. This means that if you register a Portrayal explicitly for an object, and then later the object is eliminated from your model, the FieldPortrayal will not hold onto the object or onto its Portrayal, but will allow them to garbage collect as well. Thus you don't have to worry about de-registering an object.

Some FieldPortrayals benefit (draw faster) if they know that their underlying field is immutable, that is, it never changes. Notably, most FieldPortrayal3Ds benefit, as well as various ValueGrid2DPortrayals.

  • Field Details

    • portrayalForAll

      public Portrayal portrayalForAll
    • portrayalForNull

      public Portrayal portrayalForNull
    • portrayalForNonNull

      public Portrayal portrayalForNonNull
    • portrayalForRemainder

      public Portrayal portrayalForRemainder
    • portrayals

      public WeakHashMap portrayals
    • classPortrayals

      public WeakHashMap classPortrayals
    • field

      protected Object field
    • immutableField

      protected boolean immutableField
  • Constructor Details

    • FieldPortrayal

      public FieldPortrayal()
  • Method Details

    • setAlternatePortrayalOrdering

      public void setAlternatePortrayalOrdering(boolean val)
      Sets whether the alternate portrayal ordering is used. The default is false.
    • getAlternatePortrayalOrdering

      public boolean getAlternatePortrayalOrdering()
      Returns whether the alternate portrayal ordering is used. The default is false.
    • setPortrayalForAll

      public void setPortrayalForAll(Portrayal portrayal)
      Set the portrayal to null to remove it.
    • getPortrayalForAll

      public Portrayal getPortrayalForAll()
    • setPortrayalForNull

      public void setPortrayalForNull(Portrayal portrayal)
      Set the portrayal to null to remove it.
    • getPortrayalForNull

      public Portrayal getPortrayalForNull()
    • setPortrayalForNonNull

      public void setPortrayalForNonNull(Portrayal portrayal)
      Set the portrayal to null to remove it.
    • getPortrayalForNonNull

      public Portrayal getPortrayalForNonNull()
    • setPortrayalForRemainder

      public void setPortrayalForRemainder(Portrayal portrayal)
      Set the portrayal to null to remove it.
    • getPortrayalForRemainder

      public Portrayal getPortrayalForRemainder()
    • setPortrayalForClass

      public void setPortrayalForClass(Class cls, Portrayal portrayal)
      Sets a portrayal for a class -- objects must be of EXACTLY this class (not subclasses) to respond to this. Set the portrayal to null to remove it for a given class.
    • setPortrayalForObject

      public void setPortrayalForObject(Object obj, Portrayal portrayal)
      Sets a portrayal for a class -- objects must be equal(...) to the provided object here to respond to this. Set the portrayal to null to remove it for a given object.
    • getDefaultNullPortrayal

      public Portrayal getDefaultNullPortrayal()
      Returns a default portrayal for null. By default this is set to the same as getDefaultPortrayal(). Override this to provide a more interesting default portrayals for null.
    • getDefaultPortrayal

      public abstract Portrayal getDefaultPortrayal()
      Should return a portrayal which can portray any object regardless of whether it's valid or not
    • getPortrayalForObject

      public Portrayal getPortrayalForObject(Object obj)
      Returns the appropriate Portrayal.
    • setDirtyField

      public void setDirtyField(boolean val)
    • isDirtyField

      public boolean isDirtyField()
    • reset

      public void reset()
      Deprecated.
      Use setDirtyField(true);
    • isImmutableField

      public boolean isImmutableField()
      Returns true if the underlying field is assumed to be unchanging -- thus there's no reason to update once we're created. Not all FieldPortrayals will care about whether or not a field is immutable.
    • setImmutableField

      public void setImmutableField(boolean val)
      Specifies that the underlying field is (or is not) to be assumed unchanging -- thus there's no reason to update once we're created. Not all FieldPortrayals will care about whether or not a field is immutable. Also sets dirtyField to true regardless.
    • getField

      public Object getField()
      Returns the field.
    • setField

      public void setField(Object field)
      Sets the field, and sets the dirtyField flag to true. May throw an exception if the field is inappropriate. The default version just sets the field and sets the dirtyField flag.
    • getInspector

      public Inspector getInspector(LocationWrapper wrapper, GUIState state)
    • getName

      public String getName(LocationWrapper wrapper)
    • getStatus

      public String getStatus(LocationWrapper wrapper)
    • setSelected

      public boolean setSelected(LocationWrapper wrapper, boolean selected)
      Selects or deselects all of the provided objects.
    • setSelected

      public void setSelected(Bag locationWrappers, boolean selected)
    • getObjectLocation

      public Object getObjectLocation(Object object, GUIState gui)
      Returns the first location in the underlying field of the given object, if such a thing is reasonable. Largely used for getObjectPosition(...). If null is returned, then the portrayal is unable to determine the position of the field location. Optionally overridable. The default implementation returns null.
    • setObjectLocation

      public void setObjectLocation(Object obj, Object location, GUIState gui)
      Sets location in the underlying field of the given object, if such a thing is reasonable. Largely used for setObjectPosition(...), and in the Inspector's LocationWrapper via the Stable classes. Optionally overridable. The default implementation does nothing.