Class GUnit

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class GUnit
    extends java.lang.Object
    implements java.lang.Cloneable
    This is a data holder class for a unit "px" for pixels, "dlg" for dialog, "em" for the size of the current font, "ex" for the size of the "x" character of the current font, "pt" for point size (1/72 of an inch), and "%" for percent of the parent component size.

    Dialog units use font metrics as: 1/4 maximum character width, 1/8 maximum character height (+ external & internal leadings). This is a heritage from the Windows.

    Two different unit types exist: integer values and decimal values. The type "px" only supports integer values. Integer values can range from -536870911 (-0x1FFFFFFF) to 536870911 (0x1FFFFFFF). Decimal values can be from -536870911.ddddddd (-0x1FFFFFFF.ddddddd) to 536870911.ddddddd (0x1FFFFFFF.ddddddd) where "ddddddd" is the decimal part with a maximum of 7 digits. A unit can be a combination of several other units, e.g. "90%-10px+1pt". This type is called FORMULA.

    Author:
    Christopher Mindus
    • Field Summary

      Fields 
      Modifier and TypeFieldDescription
      static GUnitNONE
      An empty "NONE" unit, i.e.
      static intUT_DLG
      The unit type "dialog" or "dlg" for short.
      static intUT_EM
      The unit type size of the current font or "em" for short.
      static intUT_EX
      The unit type size of the "x" character of current font or "ex" for short.
      static intUT_FORMULA
      The unit type for complex values with a formula.
      static intUT_NB
      The unit type size of the notch BOTTOM side safe size in pixels or "nb" for short.
      static intUT_NL
      The unit type size of the notch LEFT side safe size in pixels or "nl" for short.
      static intUT_NR
      The unit type size of the notch RIGHT side safe size in pixels or "nr" for short.
      static intUT_NT
      The unit type size of the notch TOP side safe size in pixels or "nt" for short.
      static intUT_NW
      The unit type size of the maximum notch LEFT/RIGHT side or WIDTH safe size in pixels or "nw" for short.
      static intUT_PC
      The unit type size for percentage of parent or "%" for short.
      static intUT_PCH
      The unit type size for percentage of parent height or "%h" for short.
      static intUT_PCMAX
      The unit type size for maximum size of percentage of parent width and height or "%max" for short.
      static intUT_PCMIN
      The unit type size for minimum size of percentage of parent width and height or "%min" for short.
      static intUT_PCW
      The unit type size for percentage of parent width or "%w" for short.
      static intUT_PT
      The unit type in points, i.e.
      static intUT_PX
      The unit type "pixels" or "px" for short.
      static intUT_VALUE
      The unit type size special value string, set as the formula.
      static intUT_VH
      The unit type size for percentage of viewport height or "vh" for short.
      static intUT_VMAX
      The unit type size for maximum size of percentage of viewport width and height or "vmax" for short.
      static intUT_VMIN
      The unit type size for minimum size of percentage of viewport width and height or "vmin" for short.
      static intUT_VW
      The unit type size for percentage of viewport width or "vw" for short.
    • Constructor Summary

      Constructors 
      ConstructorDescription
      GUnit()
      Creates a unit object to 0 (zero) in pixels.
      GUnit​(double value, int unit)
      Creates a unit object with the specified value in pixels.
      GUnit​(int value)
      Creates a unit object with the specified value in pixels.
      GUnit​(ReadTransaction trans)
      Creates a unit object from a transaction.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      voidappend​(SendTransaction trans)
      Appends the unit object to a transaction.
      GUnitclone()
      Clones this instance.
      static GUnitcreateCSSGUnit​(java.lang.String value, java.lang.String... specialValues)
      Creates a GUnit object from a String.
      static GUnitcreateGUnit​(java.lang.String expr0, java.lang.String... specialValues)
      Creates a GUnit object from a String.
      booleanequals​(GUnit unit)
      Checks if two unit objects are equal.
      booleanequals​(java.lang.Object o)
      Checks if two unit objects are equal.
      java.lang.StringgetCSSValue()
      Gets the unit as a CSS value.
      doublegetPixelValue​(UIComp comp, boolean isHorz)
      Calculates the unit into a pixel value using the font information.
      static javax.script.ScriptEnginegetScriptEngine()
      Returns the script engine manager.
      java.lang.StringgetString()
      Gets the String representation of the bounds as "value type".
      intgetType()
      Gets the type of the unit (0-19), one of the values: UT_PX (pixel), UT_DLG (dialog), UT_EM ("em"), UT_EX ("ex"), UT_PT (point), UT_PC (percent), UT_PCW (percentage of parent width), UT_PCH (percentage of parent height), UT_PCMIN (minimum size of percentage of parent width and height), UT_PCMAX (maximum size of percentage of parent width and height), UT_VW (percentage of viewport width), UT_VH (percentage of viewport height), UT_VMIN (size for minimum size of percentage of viewport width and height), UT_VMAX (size for maximum size of percentage of viewport width and height), UT_NL (size of the notch LEFT side safe size in pixels), UT_NR (size of the notch RIGHT side safe size in pixels), UT_NW (size of the maximum notch LEFT/RIGHT side or WIDTH safe size in pixels), UT_NT (size of the notch TOP side safe size in pixels), UT_NB (size of the notch BOTTOM side safe size in pixels), UT_FORMULA (formula) or UT_VALUE (special value).
      doublegetValue()
      Returns the double value for the unit.
      booleanisContainerDependent()
      Returns if this value is dependent on the container size, i.e.
      booleanisLocked()
      Checks if the unit is "locked" from user change in the GUI design editor.
      booleanisPureCSS()
      Checks if this is a valid CSS pure unit, e.g.
      booleanisZero()
      Checks if the unit is zero.
      protected java.lang.StringparamString()
      Returns the parameter string representing the state of this event.
      voidsetLocked​(boolean on)
      Sets the lock state of the unit.
      java.lang.StringtoString()
      Returns a string representation of this class instance.
      java.lang.Stringvalidate()
      Validates an expression formula.
      static voidvalidateCSSGUnit​(GUnit unit, java.lang.String... specialValues)
      Validates a GUnit object.
      • Methods inherited from class java.lang.Object

        finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • NONE

        public static final GUnit NONE
        An empty "NONE" unit, i.e. "0px".
      • UT_PX

        public static final int UT_PX
        The unit type "pixels" or "px" for short. Only integer values are allowed.
        See Also:
        Constant Field Values
      • UT_PT

        public static final int UT_PT
        The unit type in points, i.e. 1/72 inch or "pt" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_EM

        public static final int UT_EM
        The unit type size of the current font or "em" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_EX

        public static final int UT_EX
        The unit type size of the "x" character of current font or "ex" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_DLG

        public static final int UT_DLG
        The unit type "dialog" or "dlg" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_PC

        public static final int UT_PC
        The unit type size for percentage of parent or "%" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_PCW

        public static final int UT_PCW
        The unit type size for percentage of parent width or "%w" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_PCH

        public static final int UT_PCH
        The unit type size for percentage of parent height or "%h" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_PCMIN

        public static final int UT_PCMIN
        The unit type size for minimum size of percentage of parent width and height or "%min" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_PCMAX

        public static final int UT_PCMAX
        The unit type size for maximum size of percentage of parent width and height or "%max" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_VW

        public static final int UT_VW
        The unit type size for percentage of viewport width or "vw" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_VH

        public static final int UT_VH
        The unit type size for percentage of viewport height or "vh" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_VMIN

        public static final int UT_VMIN
        The unit type size for minimum size of percentage of viewport width and height or "vmin" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_VMAX

        public static final int UT_VMAX
        The unit type size for maximum size of percentage of viewport width and height or "vmax" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_NL

        public static final int UT_NL
        The unit type size of the notch LEFT side safe size in pixels or "nl" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_NR

        public static final int UT_NR
        The unit type size of the notch RIGHT side safe size in pixels or "nr" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_NW

        public static final int UT_NW
        The unit type size of the maximum notch LEFT/RIGHT side or WIDTH safe size in pixels or "nw" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_NT

        public static final int UT_NT
        The unit type size of the notch TOP side safe size in pixels or "nt" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_NB

        public static final int UT_NB
        The unit type size of the notch BOTTOM side safe size in pixels or "nb" for short. Decimals are allowed.
        See Also:
        Constant Field Values
      • UT_FORMULA

        public static final int UT_FORMULA
        The unit type for complex values with a formula.
        See Also:
        Constant Field Values
      • UT_VALUE

        public static final int UT_VALUE
        The unit type size special value string, set as the formula.
        See Also:
        Constant Field Values
    • Constructor Detail

      • GUnit

        public GUnit()
        Creates a unit object to 0 (zero) in pixels.
      • GUnit

        public GUnit​(int value)
        Creates a unit object with the specified value in pixels.
        Parameters:
        value - The value.
        Throws:
        java.lang.IllegalArgumentException - if the value is not in range of -0x1FFFFFFF to 0x1FFFFFFF.
      • GUnit

        public GUnit​(double value,
                     int unit)
        Creates a unit object with the specified value in pixels.
        Parameters:
        value - The value.
        unit - The unit UT_* (excluding UT_FORMULA and UT_VALUE).
        Throws:
        java.lang.IllegalArgumentException - For invalid value range.
      • GUnit

        public GUnit​(ReadTransaction trans)
        Creates a unit object from a transaction.
        Parameters:
        trans - The transaction.
    • Method Detail

      • getScriptEngine

        public static javax.script.ScriptEngine getScriptEngine()
                                                         throws GUnitException
        Returns the script engine manager.
        Throws:
        GUnitException - If engine cannot be instantiated.
      • createGUnit

        public static GUnit createGUnit​(java.lang.String expr0,
                                        java.lang.String... specialValues)
                                 throws GUnitException
        Creates a GUnit object from a String. The String must be in the formats:
          - [+|-] nnn [unit]
          - [+|-] nnn[.ddd] [unit]
          - formula consisting of characters "()+-/* 0123456789." and the unit characters
         
        Parameters:
        expr0 - The expression string.
        specialValues - Accepted special values.
        Returns:
        GUnit the created unit.
        Throws:
        GUnitException - The error message.
      • createCSSGUnit

        public static GUnit createCSSGUnit​(java.lang.String value,
                                           java.lang.String... specialValues)
                                    throws GUnitException
        Creates a GUnit object from a String. The String must be in the formats:
          - [+|-] nnn [unit]
          - [+|-] nnn[.ddd] [unit]
         

        All formats except "pixel" supports decimals ([.ddd] above).

        Formulas are not supported. The following units are supported:

        • px: pixel,
        • pt: point,
        • em: width of "M",
        • ex: width of "x",
        • %: percentage width of parent,
        • vw: viewport width,
        • vh: viewport height.
        Parameters:
        value - The value string.
        specialValues - Accepted special values.
        Returns:
        GUnit the created unit.
        Throws:
        GUnitException - The error message.
      • validateCSSGUnit

        public static void validateCSSGUnit​(GUnit unit,
                                            java.lang.String... specialValues)
                                     throws GUnitException
        Validates a GUnit object.

        Formulas are not supported. The following units are supported:

        • px: pixel,
        • pt: point,
        • em: width of "M",
        • ex: width of "x",
        • %: percentage width of parent,
        • vw: viewport width,
        • vh: viewport height.
        Parameters:
        unit - The unit as CSS value.
        specialValues - Accepted special values.
        Throws:
        GUnitException - The error message.
      • getType

        public int getType()
        Gets the type of the unit (0-19), one of the values:
        • UT_PX (pixel),
        • UT_DLG (dialog),
        • UT_EM ("em"),
        • UT_EX ("ex"),
        • UT_PT (point),
        • UT_PC (percent),
        • UT_PCW (percentage of parent width),
        • UT_PCH (percentage of parent height),
        • UT_PCMIN (minimum size of percentage of parent width and height),
        • UT_PCMAX (maximum size of percentage of parent width and height),
        • UT_VW (percentage of viewport width),
        • UT_VH (percentage of viewport height),
        • UT_VMIN (size for minimum size of percentage of viewport width and height),
        • UT_VMAX (size for maximum size of percentage of viewport width and height),
        • UT_NL (size of the notch LEFT side safe size in pixels),
        • UT_NR (size of the notch RIGHT side safe size in pixels),
        • UT_NW (size of the maximum notch LEFT/RIGHT side or WIDTH safe size in pixels),
        • UT_NT (size of the notch TOP side safe size in pixels),
        • UT_NB (size of the notch BOTTOM side safe size in pixels),
        • UT_FORMULA (formula) or
        • UT_VALUE (special value).
      • isLocked

        public boolean isLocked()
        Checks if the unit is "locked" from user change in the GUI design editor.
        Returns:
        The lock flag.
      • setLocked

        public void setLocked​(boolean on)
        Sets the lock state of the unit.
        Parameters:
        on - The lock flag.
      • getString

        public java.lang.String getString()
        Gets the String representation of the bounds as "value type".
        Returns:
        A string in the form of value type.
      • isZero

        public boolean isZero()
        Checks if the unit is zero.
        Returns:
        true if the value is zero when not a UT_FORMULA or UT_VALUE string such as "fixed", false otherwise.
      • equals

        public boolean equals​(java.lang.Object o)
        Checks if two unit objects are equal.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - another unit object. If null, the units are not equal.
        Returns:
        true if the object has the same type and value.
      • equals

        public boolean equals​(GUnit unit)
        Checks if two unit objects are equal.
        Parameters:
        unit - another size object. If null, the sizes are not equal.
        Returns:
        true if the object has the same type and value.
      • clone

        public GUnit clone()
        Clones this instance.
        Overrides:
        clone in class java.lang.Object
        Returns:
        As cloned instance of GUnit.
      • paramString

        protected java.lang.String paramString()
        Returns the parameter string representing the state of this event. This string is useful for debugging. Super-classes adds extra information to the string by preceding it with a comma followed by the extra information.
        Returns:
        the parameter string of this unit object.
      • getValue

        public double getValue()
        Returns the double value for the unit.
        Returns:
        The value for PT, PX, %, DLG, EX and EM, or Double.NaN otherwise.
      • toString

        public java.lang.String toString()
        Returns a string representation of this class instance.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string representation of this class instance.
      • getCSSValue

        public java.lang.String getCSSValue()
        Gets the unit as a CSS value. Formula and percent is NOT accepted and returns null along with a warning logging.
        Returns:
        The CSS value, or null if it's a UT_FORMULA.
      • append

        public void append​(SendTransaction trans)
        Appends the unit object to a transaction.
        Parameters:
        trans - The transaction.
      • validate

        public java.lang.String validate()
                                  throws GUnitException
        Validates an expression formula.
        Throws:
        GUnitException - For validation errors.
      • getPixelValue

        public double getPixelValue​(UIComp comp,
                                    boolean isHorz)
        Calculates the unit into a pixel value using the font information.
        Parameters:
        comp - The component.
        isHorz - Flag indicating this is a horizontal value (significant only for % or DLG).
        Returns:
        The pixel value, or Double.NaN if the expression is invalid. Note: the Notch values always returns zero.
        Throws:
        java.lang.IllegalStateException - If this is called outside of the Designer.
      • isPureCSS

        public boolean isPureCSS()
        Checks if this is a valid CSS pure unit, e.g. not DLG, Formula.
        Returns:
        true if it's a pure CSS unit, false if it is a calculated value and cannot be used in CSS declarations.
      • isContainerDependent

        public boolean isContainerDependent()
        Returns if this value is dependent on the container size, i.e. value is a percent value or a formula containing percent.
        Returns:
        true if the value is container dependent, i.e. a Percent value, or if it is a formula contains a percent value. false is returned if the value is not dependent of the container.