Class UnitProp

All Implemented Interfaces:
EventListener, IGProp<GUnit>, Cloneable

public class UnitProp extends GProp<GUnit>
The "unit" property class holds the property name, value as a GUnit object and optionally has a parent and/or property listener(s).
Author:
Christopher Mindus
  • Constructor Details

    • UnitProp

      public UnitProp()
      Creates a unit property with no name and with a null value.
    • UnitProp

      public UnitProp(Atom propertyAtom)
      Creates a unit property with the specified name with a null value.
      Parameters:
      propertyAtom - the property atom.
    • UnitProp

      public UnitProp(Atom propertyAtom, GUnit newValue)
      Creates a unit property with the specified name and value.
      Parameters:
      propertyAtom - the property atom.
      newValue - the new unit value for the property.
  • Method Details

    • getValueClasses

      public Class<?>[] getValueClasses()
      Gets the list of possible native Java classes that can be used to set a value using setPropertyValue, in order of importance. Note that loss of precision in data if e.g. an Float property accepts a Double. However, the setPropertyValue throws IllegalArgumentException if the range in invalid, e.g. conversion of a Integer to a Byte, and the value is not -127 to 128.
      Specified by:
      getValueClasses in interface IGProp<GUnit>
      Specified by:
      getValueClasses in class GProp<GUnit>
      Returns:
      The array of Java classes that can be accepted in the setPropertyValue method. The first type is the "native" or most preferred type. The array is at least one item in length and never null.
    • setPropValue

      public boolean setPropValue(GUnit newValue)
      Sets a new value for this property. If any listener is present and the value has changed compare to the last value, the listener is informed.
      Parameters:
      newValue - the new value.
      Returns:
      boolean true for value has changed, false for same value as current value.
    • validatePropType

      public void validatePropType(Object newValue) throws PropTypeException
      Validates that the value to set is of GUnit instance.

      Note: When this method is called, synchronization is done at the property itself, so no synchronization on the tree should be done.

      Specified by:
      validatePropType in interface IGProp<GUnit>
      Specified by:
      validatePropType in class GProp<GUnit>
      Parameters:
      newValue - The new value to set.
      Throws:
      PropTypeException - when the new value isn't of the correct class.
    • getUnit

      public GUnit getUnit()
      Gets the current value set as an GUnit.
      Returns:
      GUnit the unit, never null.
    • setUnit

      public boolean setUnit(GUnit unit)
      Sets new unit for this property. If any listener is present and the value has changed compare to the last value, the listener is informed.
      Parameters:
      unit - the unit.
      Returns:
      boolean true for value has changed, false for same value as current value.
    • appendPropValue

      protected void appendPropValue(PropMgr propertyManager, SendTransaction trans, Object value)
      Appends the unit property value.
      Specified by:
      appendPropValue in class GProp<GUnit>
      Parameters:
      propertyManager - the property manager instance.
      trans - transaction to append the properties to.
      value - the value to append to the transaction, a reference to the current property value.
    • initializeValue

      public boolean initializeValue(PropMgr propertyManager, ReadTransaction trans) throws PropException
      Initializes the property value from a transaction.
      Specified by:
      initializeValue in class GProp<GUnit>
      Parameters:
      propertyManager - the property manager.
      trans - the transaction to initialize the property with.
      Returns:
      true if the value has changed, false otherwise.
      Throws:
      PropException
    • fromElementString

      public Object fromElementString(String string) throws PropException
      Called from the initializeProperty(Element element) method once the element String is retrieved from the "value" attribute of the element. Override this method to provide the appropriate String to Object conversion.

      The default implementation returns the String passed as the parameter.

      Specified by:
      fromElementString in interface IGProp<GUnit>
      Overrides:
      fromElementString in class GProp<GUnit>
      Parameters:
      string - the String value of the object from the String value attribute.
      Returns:
      the Object in "native" property value form of the string value.
      Throws:
      PropException - when the conversion fails.
    • toElementString

      public String toElementString(Object value)
      Called from the initializeElement(Element element) method. Override this method to provide the appropriate Object to String conversion.

      The default implementation returns the Object value.toString() return code.

      Specified by:
      toElementString in interface IGProp<GUnit>
      Overrides:
      toElementString in class GProp<GUnit>
      Parameters:
      value - the value in the object to convert to a string.
      Returns:
      the string corresponding to the object value.
    • getPropValue0

      protected Object getPropValue0(Object value, int index) throws Exception
      Gets the property value in another class form than the "native" one. This method tries conversion of a "native" property value into another class type. This method is used when filling in a POJO variable (Plain Old Java Object Field) from a "native" property value.
      Specified by:
      getPropValue0 in class GProp<GUnit>
      Parameters:
      value - The property value.
      index - The index in the Class array returned by getValueClasses().
      Returns:
      The property value in the requested class, after conversion.
      Throws:
      Exception - Other exceptions that could occur.
    • toNativeValue0

      protected Object toNativeValue0(Object value, int index) throws Exception
      Tries conversion of possibly accepted properties values using the setPropertyValue method into the most preferred property value type. If the input value already is of the preferred type, the same object is returned.
      Specified by:
      toNativeValue0 in class GProp<GUnit>
      Parameters:
      value - The input value to convert to native value.
      index - The index in the array returned by getValueClasses(), or -1 if not found.
      Returns:
      The converted input value in the preferred property value object type.
      Throws:
      Exception - For conversion exceptions.
    • getCSSValue

      public String getCSSValue()
      Gets the unit as a CSS value. Formula and percent is NOT accepted and returns null along with a warning logging.